From 8c6ba5cfc4e83ec9c737e3c8987b59fe80f8b9fc Mon Sep 17 00:00:00 2001 From: sniper-py Date: Wed, 23 Sep 2020 11:55:39 +0800 Subject: [PATCH] get_file_id --- seatable_thumbnail/plugin.py | 6 ++++-- seatable_thumbnail/serializers.py | 28 +++++++++++++++++++++------- seatable_thumbnail/thumbnail.py | 18 ++++++++++-------- 3 files changed, 35 insertions(+), 17 deletions(-) diff --git a/seatable_thumbnail/plugin.py b/seatable_thumbnail/plugin.py index de74a03..5a32400 100644 --- a/seatable_thumbnail/plugin.py +++ b/seatable_thumbnail/plugin.py @@ -14,8 +14,8 @@ def __init__(self, **info): self.get() def get(self): - self.get_inner_path() - response = requests.get(self.inner_path) + inner_path = self.get_inner_path() + response = requests.get(inner_path) self.body = response.content def get_inner_path(self): @@ -25,3 +25,5 @@ def get_inner_path(self): 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_name)) + + return self.inner_path diff --git a/seatable_thumbnail/serializers.py b/seatable_thumbnail/serializers.py index 26cdc59..00a955c 100644 --- a/seatable_thumbnail/serializers.py +++ b/seatable_thumbnail/serializers.py @@ -32,6 +32,13 @@ def gen_thumbnail_info(self): thumbnail_info.update(self.resource) self.thumbnail_info = thumbnail_info + def get_file_id(self): + self.file_id = seafile_api.get_file_id_by_path(self.repo_id, self.file_path) + if not self.file_id: + raise ValueError(404, 'file_id not found.') + + return self.file_id + def parse_django_session(self, session_data): # only for django 1.11.x encoded_data = base64.b64decode(session_data) @@ -106,9 +113,9 @@ def resource_check(self): Workspaces).filter_by(id=workspace_id).first() repo_id = workspace.repo_id workspace_owner = workspace.owner - file_id = seafile_api.get_file_id_by_path(repo_id, file_path) - if not file_id: - raise ValueError(404, 'file_id not found.') + self.repo_id = repo_id + self.file_path = file_path + file_id = self.get_file_id() thumbnail_dir = os.path.join(settings.THUMBNAIL_DIR, str(size)) thumbnail_path = os.path.join(thumbnail_dir, file_id) @@ -153,9 +160,16 @@ def gen_plugin_info(self): plugin_info.update(self.resource) self.plugin_info = plugin_info + def get_file_id(self): + self.file_id = seafile_api.get_file_id_by_path(self.repo_id, self.file_path) + if not self.file_id: + raise ValueError(404, 'file_id not found.') + + return self.file_id + def params_check(self): - path = self.request.query_dict['path'][0] plugin_name = self.request.url.split('/')[1] + path = self.request.query_dict['path'][0] 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)[0] if mimetypes.guess_type(path) else TEXT_CONTENT_TYPE.decode('utf-8') @@ -177,9 +191,9 @@ def resource_check(self): file_path ='/' + plugin.name + path file_name = os.path.basename(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.') + self.repo_id = settings.PLUGINS_REPO_ID + self.file_path = file_path + file_id = self.get_file_id() file_info = json.loads(plugin.info) last_modified_time = datetime.strptime(file_info['last_modified'][:-6], '%Y-%m-%dT%H:%M:%S') diff --git a/seatable_thumbnail/thumbnail.py b/seatable_thumbnail/thumbnail.py index 6889e42..002f067 100644 --- a/seatable_thumbnail/thumbnail.py +++ b/seatable_thumbnail/thumbnail.py @@ -36,8 +36,8 @@ def generate(self): if file_size > settings.THUMBNAIL_IMAGE_SIZE_LIMIT * 1024 * 1024: raise AssertionError(400, 'file_size invalid.') - self.get_inner_path() - image_file = urllib.request.urlopen(self.inner_path) + inner_path = self.get_inner_path() + image_file = urllib.request.urlopen(inner_path) f = BytesIO(image_file.read()) image = Image.open(f) @@ -48,8 +48,8 @@ def generate(self): from psd_tools import PSDImage tmp_psd = os.path.join(tempfile.gettempdir(), self.file_id) - self.get_inner_path() - urllib.request.urlretrieve(self.inner_path, tmp_psd) + inner_path = self.get_inner_path() + urllib.request.urlretrieve(inner_path, tmp_psd) psd = PSDImage.open(tmp_psd) image = psd.topil() @@ -63,8 +63,8 @@ def generate(self): tmp_image_path = os.path.join( tempfile.gettempdir(), self.file_id + '.png') tmp_video = os.path.join(tempfile.gettempdir(), self.file_id) - self.get_inner_path() - urllib.request.urlretrieve(self.inner_path, tmp_video) + inner_path = self.get_inner_path() + urllib.request.urlretrieve(inner_path, tmp_video) clip = VideoFileClip(tmp_video) clip.save_frame( @@ -77,8 +77,8 @@ def generate(self): # ===== xmind ===== elif self.file_type == XMIND: - self.get_inner_path() - xmind_file = urllib.request.urlopen(self.inner_path) + inner_path = self.get_inner_path() + xmind_file = urllib.request.urlopen(inner_path) f = BytesIO(xmind_file.read()) xmind_zip_file = zipfile.ZipFile(f, 'r') xmind_thumbnail = xmind_zip_file.read('Thumbnails/thumbnail.png') @@ -95,6 +95,8 @@ def get_inner_path(self): self.inner_path = '%s/files/%s/%s' % ( settings.INNER_FILE_SERVER_ROOT.rstrip('/'), token, urllib.parse.quote(self.file_name)) + return self.inner_path + def create_image_thumbnail(self, image): width, height = image.size image_memory_cost = width * height * 4 / 1024 / 1024