diff --git a/docker/scripts/init_config.py b/docker/scripts/init_config.py index 904caf9..6d7ee97 100644 --- a/docker/scripts/init_config.py +++ b/docker/scripts/init_config.py @@ -95,6 +95,10 @@ # location /thumbnail/ { # proxy_pass https://thumbnail.seatable.cn/thumbnail/; # } + # + # location /dtable-plugins/ { + # proxy_pass https://thumbnail.seatable.cn/dtable-plugins/; + # } } """ diff --git a/seatable_thumbnail/http_response.py b/seatable_thumbnail/http_response.py index c026015..d59ed76 100644 --- a/seatable_thumbnail/http_response.py +++ b/seatable_thumbnail/http_response.py @@ -54,7 +54,7 @@ def gen_thumbnail_response(thumbnail, etag, last_modified): def gen_plugin_response(plugin, content_type, etag, last_modified): - response_start = gen_response_start(200, content_type) + response_start = gen_response_start(200, content_type.encode('utf-8')) response_body = gen_response_body(plugin) # cache diff --git a/seatable_thumbnail/plugin.py b/seatable_thumbnail/plugin.py new file mode 100644 index 0000000..ef6e2a5 --- /dev/null +++ b/seatable_thumbnail/plugin.py @@ -0,0 +1,27 @@ +import requests +import urllib.request +import urllib.parse + +from seaserv import seafile_api +import seatable_thumbnail.settings as settings +from seatable_thumbnail.constants import EMPTY_BYTES + + +class Plugin(object): + def __init__(self, **info): + self.__dict__.update(info) + self.body = EMPTY_BYTES + self.get() + + def get(self): + self.get_inner_path() + response = requests.get(self.inner_path) + self.body = response.content + + def get_inner_path(self): + token = seafile_api.get_fileserver_access_token( + settings.PLUGINS_REPO_ID, self.file_id, 'view', '', use_onetime=True) + if not token: + raise ValueError(404, 'token not found.') + self.inner_path = '%s/files/%s/%s' % ( + settings.INNER_FILE_SERVER_ROOT.rstrip('/'), token, urllib.parse.quote(self.file_path.lstrip('/'))) diff --git a/seatable_thumbnail/serializers.py b/seatable_thumbnail/serializers.py index 0e5304a..df64725 100644 --- a/seatable_thumbnail/serializers.py +++ b/seatable_thumbnail/serializers.py @@ -7,7 +7,7 @@ from seaserv import seafile_api import seatable_thumbnail.settings as settings -from seatable_thumbnail.constants import FILE_EXT_TYPE_MAP, \ +from seatable_thumbnail.constants import TEXT_CONTENT_TYPE, FILE_EXT_TYPE_MAP, \ IMAGE, PSD, VIDEO, XMIND from seatable_thumbnail.models import Workspaces, DjangoSession, DTableSystemPlugins @@ -157,7 +157,7 @@ def params_check(self): plugin_name = self.request.url.split('/')[1] timestamp = self.request.query_dict['t'][0] if self.request.query_dict.get('t') else '' version = self.request.query_dict['version'][0] if self.request.query_dict.get('version') else '' - content_type = mimetypes.guess_type(path) + content_type = mimetypes.guess_type(path)[0] if mimetypes.guess_type(path) else TEXT_CONTENT_TYPE.decode('utf-8') self.params = { 'path': path, @@ -175,18 +175,18 @@ def resource_check(self): DTableSystemPlugins).filter_by(name=plugin_name).first() file_path ='/' + plugin.name + path - repo_id = settings.PLUGINS_REPO_ID - file_id = seafile_api.get_file_id_by_path(repo_id, file_path) + file_id = seafile_api.get_file_id_by_path(settings.PLUGINS_REPO_ID, file_path) if not file_id: raise ValueError(404, 'file_id not found.') - info = json.loads(plugin.info) - last_modified = info['last_modified'] + file_info = json.loads(plugin.info) + last_modified = file_info['last_modified'] etag = '"' + file_id + '"' self.resource = { + 'file_path': file_path, 'file_id': file_id, - 'info': info, + 'file_info': file_info, 'last_modified': last_modified, 'etag': etag, } \ No newline at end of file