Merge pull request #288 from glutanimate/mediasrv-addon-support
Extend mediasrv to also serve media files in addons directory
This commit is contained in:
commit
7d65594944
@ -410,6 +410,19 @@ Are you sure you want to continue?"""
|
|||||||
if not os.path.exists(bp):
|
if not os.path.exists(bp):
|
||||||
return
|
return
|
||||||
os.rename(bp, p)
|
os.rename(bp, p)
|
||||||
|
|
||||||
|
# Web Exports
|
||||||
|
######################################################################
|
||||||
|
|
||||||
|
_webExports = {}
|
||||||
|
|
||||||
|
def setWebExports(self, module, pattern):
|
||||||
|
addon = self.addonFromModule(module)
|
||||||
|
self._webExports[addon] = pattern
|
||||||
|
|
||||||
|
def getWebExports(self, addon):
|
||||||
|
return self._webExports.get(addon)
|
||||||
|
|
||||||
|
|
||||||
# Add-ons Dialog
|
# Add-ons Dialog
|
||||||
######################################################################
|
######################################################################
|
||||||
|
@ -1349,7 +1349,7 @@ Please ensure a profile is open and Anki is not busy, then try again."""),
|
|||||||
##########################################################################
|
##########################################################################
|
||||||
|
|
||||||
def setupMediaServer(self):
|
def setupMediaServer(self):
|
||||||
self.mediaServer = aqt.mediasrv.MediaServer()
|
self.mediaServer = aqt.mediasrv.MediaServer(self)
|
||||||
self.mediaServer.start()
|
self.mediaServer.start()
|
||||||
|
|
||||||
def baseHTML(self):
|
def baseHTML(self):
|
||||||
|
@ -9,6 +9,7 @@ import socketserver
|
|||||||
import socket
|
import socket
|
||||||
from anki.utils import devMode
|
from anki.utils import devMode
|
||||||
import threading
|
import threading
|
||||||
|
import re
|
||||||
|
|
||||||
# locate web folder in source/binary distribution
|
# locate web folder in source/binary distribution
|
||||||
def _getExportFolder():
|
def _getExportFolder():
|
||||||
@ -47,7 +48,12 @@ class MediaServer(threading.Thread):
|
|||||||
_port = None
|
_port = None
|
||||||
_ready = threading.Event()
|
_ready = threading.Event()
|
||||||
|
|
||||||
|
def __init__(self, mw, *args, **kwargs):
|
||||||
|
super().__init__(*args, **kwargs)
|
||||||
|
self.mw = mw
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
|
RequestHandler.mw = self.mw
|
||||||
self.server = ThreadedHTTPServer(("127.0.0.1", 0), RequestHandler)
|
self.server = ThreadedHTTPServer(("127.0.0.1", 0), RequestHandler)
|
||||||
self._ready.set()
|
self._ready.set()
|
||||||
self.server.serve_forever()
|
self.server.serve_forever()
|
||||||
@ -62,6 +68,7 @@ class MediaServer(threading.Thread):
|
|||||||
class RequestHandler(http.server.SimpleHTTPRequestHandler):
|
class RequestHandler(http.server.SimpleHTTPRequestHandler):
|
||||||
|
|
||||||
timeout = 1
|
timeout = 1
|
||||||
|
mw = None
|
||||||
|
|
||||||
def do_GET(self):
|
def do_GET(self):
|
||||||
f = self.send_head()
|
f = self.send_head()
|
||||||
@ -120,12 +127,41 @@ class RequestHandler(http.server.SimpleHTTPRequestHandler):
|
|||||||
self.log_date_time_string(),
|
self.log_date_time_string(),
|
||||||
format%args))
|
format%args))
|
||||||
|
|
||||||
# catch /_anki references and rewrite them to web export folder
|
|
||||||
def _redirectWebExports(self, path):
|
def _redirectWebExports(self, path):
|
||||||
|
# catch /_anki references and rewrite them to web export folder
|
||||||
targetPath = os.path.join(os.getcwd(), "_anki", "")
|
targetPath = os.path.join(os.getcwd(), "_anki", "")
|
||||||
if path.startswith(targetPath):
|
if path.startswith(targetPath):
|
||||||
newPath = os.path.join(_exportFolder, path[len(targetPath):])
|
newPath = os.path.join(_exportFolder, path[len(targetPath):])
|
||||||
return newPath
|
return newPath
|
||||||
|
|
||||||
|
# catch /_addons references and rewrite them to addons folder
|
||||||
|
targetPath = os.path.join(os.getcwd(), "_addons", "")
|
||||||
|
if path.startswith(targetPath):
|
||||||
|
try:
|
||||||
|
addMgr = self.mw.addonManager
|
||||||
|
except AttributeError:
|
||||||
|
return path
|
||||||
|
|
||||||
|
addonPath = path[len(targetPath):]
|
||||||
|
|
||||||
|
try:
|
||||||
|
addon, subPath = addonPath.split(os.path.sep, 1)
|
||||||
|
except ValueError:
|
||||||
|
return path
|
||||||
|
if not addon:
|
||||||
|
return path
|
||||||
|
|
||||||
|
pattern = addMgr.getWebExports(addon)
|
||||||
|
if not pattern:
|
||||||
|
return path
|
||||||
|
|
||||||
|
if not re.fullmatch(pattern, subPath):
|
||||||
|
return path
|
||||||
|
|
||||||
|
newPath = os.path.join(addMgr.addonsFolder(), addonPath)
|
||||||
|
return newPath
|
||||||
|
|
||||||
return path
|
return path
|
||||||
|
|
||||||
# work around Windows machines with incorrect mime type
|
# work around Windows machines with incorrect mime type
|
||||||
|
Loading…
Reference in New Issue
Block a user