38 lines
1.1 KiB
Python
38 lines
1.1 KiB
Python
|
|
||
|
import os
|
||
|
from gi.repository import Gio
|
||
|
from .mimetypes import get_mimetype
|
||
|
|
||
|
def file_path(filepath):
|
||
|
root = os.path.dirname(os.path.realpath(__file__))
|
||
|
return os.path.join(root, filepath)
|
||
|
|
||
|
def resource_request_callback(req):
|
||
|
|
||
|
schema = req.get_scheme()
|
||
|
pathname = req.get_path()
|
||
|
ext = pathname.split('.')[-1]
|
||
|
|
||
|
print('----------------------------------------')
|
||
|
print(req.get_uri(),schema, pathname, ext, get_mimetype(ext))
|
||
|
print('----------------------------------------')
|
||
|
|
||
|
if schema == 'app':
|
||
|
data = open(file_path('./webview' + pathname)).read()
|
||
|
data = Gio.MemoryInputStream.new_from_data(data.encode())
|
||
|
|
||
|
# ------- 更多功能的reponse ----------------
|
||
|
# res = WebKit2.URISchemeResponse.new(data, -1)
|
||
|
# res.set_content_type(get_mimetype(ext))
|
||
|
# res.set_http_headers('text/html')
|
||
|
# res.set_status(200)
|
||
|
# req.finish_with_response(res)
|
||
|
# ----------------------------------------
|
||
|
|
||
|
# 简单的response
|
||
|
req.finish(data, -1, get_mimetype(ext))
|
||
|
|
||
|
|
||
|
def register_schenes(webview):
|
||
|
context = webview.get_context()
|
||
|
context.register_uri_scheme('app', resource_request_callback)
|