UiFileManagerPlugin.py 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import io
  2. import os
  3. import re
  4. import urllib
  5. from Plugin import PluginManager
  6. from Config import config
  7. from Translate import Translate
  8. plugin_dir = os.path.dirname(__file__)
  9. if "_" not in locals():
  10. _ = Translate(plugin_dir + "/languages/")
  11. @PluginManager.registerTo("UiRequest")
  12. class UiFileManagerPlugin(object):
  13. def actionWrapper(self, path, extra_headers=None):
  14. match = re.match("/list/(.*?)(/.*|)$", path)
  15. if not match:
  16. return super().actionWrapper(path, extra_headers)
  17. if not extra_headers:
  18. extra_headers = {}
  19. request_address, inner_path = match.groups()
  20. script_nonce = self.getScriptNonce()
  21. self.sendHeader(extra_headers=extra_headers, script_nonce=script_nonce)
  22. site = self.server.site_manager.need(request_address)
  23. if not site:
  24. return super().actionWrapper(path, extra_headers)
  25. request_params = urllib.parse.urlencode(
  26. {"address": site.address, "site": request_address, "inner_path": inner_path.strip("/")}
  27. )
  28. is_content_loaded = "content.json" in site.content_manager.contents
  29. return iter([super().renderWrapper(
  30. site, path, "uimedia/plugins/uifilemanager/list.html?%s" % request_params,
  31. "List", extra_headers, show_loadingscreen=not is_content_loaded, script_nonce=script_nonce
  32. )])
  33. def actionUiMedia(self, path, *args, **kwargs):
  34. if path.startswith("/uimedia/plugins/uifilemanager/"):
  35. file_path = path.replace("/uimedia/plugins/uifilemanager/", plugin_dir + "/media/")
  36. if config.debug and (file_path.endswith("all.js") or file_path.endswith("all.css")):
  37. # If debugging merge *.css to all.css and *.js to all.js
  38. from Debug import DebugMedia
  39. DebugMedia.merge(file_path)
  40. if file_path.endswith("js"):
  41. data = _.translateData(open(file_path).read(), mode="js").encode("utf8")
  42. elif file_path.endswith("html"):
  43. if self.get.get("address"):
  44. site = self.server.site_manager.need(self.get.get("address"))
  45. if "content.json" not in site.content_manager.contents:
  46. site.needFile("content.json")
  47. data = _.translateData(open(file_path).read(), mode="html").encode("utf8")
  48. else:
  49. data = open(file_path, "rb").read()
  50. return self.actionFile(file_path, file_obj=io.BytesIO(data), file_size=len(data))
  51. else:
  52. return super().actionUiMedia(path)
  53. def error404(self, path=""):
  54. if not path.endswith("index.html") and not path.endswith("/"):
  55. return super().error404(path)
  56. path_parts = self.parsePath(path)
  57. if not path_parts:
  58. return super().error404(path)
  59. site = self.server.site_manager.get(path_parts["request_address"])
  60. if not site or not site.content_manager.contents.get("content.json"):
  61. return super().error404(path)
  62. if path_parts["inner_path"] in site.content_manager.contents.get("content.json").get("files", {}):
  63. return super().error404(path)
  64. self.sendHeader(200)
  65. path_redirect = "/list" + re.sub("^/media/", "/", path)
  66. self.log.debug("Index.html not found: %s, redirecting to: %s" % (path, path_redirect))
  67. return self.formatRedirect(path_redirect)