From 186592119ec37ad41f903f34a27703236569189c Mon Sep 17 00:00:00 2001 From: sniper-py Date: Wed, 23 Sep 2020 15:05:02 +0800 Subject: [PATCH] add utils --- main.py | 23 +++--------------- seatable_thumbnail/plugin.py | 17 +++---------- seatable_thumbnail/serializers.py | 24 +++---------------- seatable_thumbnail/thumbnail.py | 21 +++++----------- seatable_thumbnail/utils.py | 40 +++++++++++++++++++++++++++++++ 5 files changed, 55 insertions(+), 70 deletions(-) create mode 100644 seatable_thumbnail/utils.py diff --git a/main.py b/main.py index d0d7bb5..e4b541f 100644 --- a/main.py +++ b/main.py @@ -8,6 +8,7 @@ from seatable_thumbnail.http_request import HTTPRequest from seatable_thumbnail.http_response import gen_error_response, gen_plugin_response, \ gen_text_response, gen_thumbnail_response, gen_cache_response +from seatable_thumbnail.utils import cache_check logger = logging.getLogger(__name__) @@ -74,16 +75,7 @@ async def __call__(self, scope, receive, send): # cache try: - etag = thumbnail_info.get('etag') - if_none_match_headers = request.headers.get('if-none-match') - if_none_match = if_none_match_headers[0] if if_none_match_headers else '' - - last_modified = thumbnail_info.get('last_modified') - if_modified_since_headers = request.headers.get('if-modified-since') - if_modified_since = if_modified_since_headers[0] if if_modified_since_headers else '' - - if (if_none_match and if_none_match == etag) \ - or (if_modified_since and if_modified_since == last_modified): + if cache_check(request, thumbnail_info): response_start, response_body = gen_cache_response() await send(response_start) await send(response_body) @@ -132,16 +124,7 @@ async def __call__(self, scope, receive, send): # cache try: - etag = plugin_info.get('etag') - if_none_match_headers = request.headers.get('if-none-match') - if_none_match = if_none_match_headers[0] if if_none_match_headers else '' - - last_modified = plugin_info.get('last_modified') - if_modified_since_headers = request.headers.get('if-modified-since') - if_modified_since = if_modified_since_headers[0] if if_modified_since_headers else '' - - if (if_none_match and if_none_match == etag) \ - or (if_modified_since and if_modified_since == last_modified): + if cache_check(request, plugin_info): response_start, response_body = gen_cache_response() await send(response_start) await send(response_body) diff --git a/seatable_thumbnail/plugin.py b/seatable_thumbnail/plugin.py index 5a32400..b063a2c 100644 --- a/seatable_thumbnail/plugin.py +++ b/seatable_thumbnail/plugin.py @@ -1,10 +1,8 @@ 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 +from seatable_thumbnail.utils import get_inner_path class Plugin(object): @@ -14,16 +12,7 @@ def __init__(self, **info): self.get() def get(self): - inner_path = self.get_inner_path() + inner_path = get_inner_path( + settings.PLUGINS_REPO_ID, self.file_id, self.file_name) response = requests.get(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_name)) - - return self.inner_path diff --git a/seatable_thumbnail/serializers.py b/seatable_thumbnail/serializers.py index 00a955c..f5e8a66 100644 --- a/seatable_thumbnail/serializers.py +++ b/seatable_thumbnail/serializers.py @@ -6,11 +6,11 @@ from datetime import datetime from email.utils import formatdate -from seaserv import seafile_api import seatable_thumbnail.settings as settings from seatable_thumbnail.constants import TEXT_CONTENT_TYPE, FILE_EXT_TYPE_MAP, \ IMAGE, PSD, VIDEO, XMIND from seatable_thumbnail.models import Workspaces, DjangoSession, DTableSystemPlugins +from seatable_thumbnail.utils import get_file_id class ThumbnailSerializer(object): @@ -32,13 +32,6 @@ 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) @@ -113,9 +106,7 @@ def resource_check(self): Workspaces).filter_by(id=workspace_id).first() repo_id = workspace.repo_id workspace_owner = workspace.owner - self.repo_id = repo_id - self.file_path = file_path - file_id = self.get_file_id() + file_id = get_file_id(repo_id, file_path) thumbnail_dir = os.path.join(settings.THUMBNAIL_DIR, str(size)) thumbnail_path = os.path.join(thumbnail_dir, file_id) @@ -160,13 +151,6 @@ 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): plugin_name = self.request.url.split('/')[1] path = self.request.query_dict['path'][0] @@ -191,9 +175,7 @@ def resource_check(self): file_path ='/' + plugin.name + path file_name = os.path.basename(file_path) - self.repo_id = settings.PLUGINS_REPO_ID - self.file_path = file_path - file_id = self.get_file_id() + file_id = get_file_id(settings.PLUGINS_REPO_ID, file_path) 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 002f067..d37584a 100644 --- a/seatable_thumbnail/thumbnail.py +++ b/seatable_thumbnail/thumbnail.py @@ -8,10 +8,11 @@ from PIL import Image from email.utils import formatdate -from seaserv import get_repo, get_file_size, seafile_api +from seaserv import get_repo, get_file_size import seatable_thumbnail.settings as settings from seatable_thumbnail.constants import IMAGE_MODES, EMPTY_BYTES,\ THUMBNAIL_EXTENSION, IMAGE, PSD, VIDEO, XMIND +from seatable_thumbnail.utils import get_inner_path class Thumbnail(object): @@ -36,7 +37,7 @@ def generate(self): if file_size > settings.THUMBNAIL_IMAGE_SIZE_LIMIT * 1024 * 1024: raise AssertionError(400, 'file_size invalid.') - inner_path = self.get_inner_path() + inner_path = get_inner_path(self.repo_id, self.file_id, self.file_name) image_file = urllib.request.urlopen(inner_path) f = BytesIO(image_file.read()) @@ -48,7 +49,7 @@ def generate(self): from psd_tools import PSDImage tmp_psd = os.path.join(tempfile.gettempdir(), self.file_id) - inner_path = self.get_inner_path() + inner_path = get_inner_path(self.repo_id, self.file_id, self.file_name) urllib.request.urlretrieve(inner_path, tmp_psd) psd = PSDImage.open(tmp_psd) @@ -63,7 +64,7 @@ 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) - inner_path = self.get_inner_path() + inner_path = get_inner_path(self.repo_id, self.file_id, self.file_name) urllib.request.urlretrieve(inner_path, tmp_video) clip = VideoFileClip(tmp_video) @@ -77,7 +78,7 @@ def generate(self): # ===== xmind ===== elif self.file_type == XMIND: - inner_path = self.get_inner_path() + inner_path = get_inner_path(self.repo_id, self.file_id, self.file_name) xmind_file = urllib.request.urlopen(inner_path) f = BytesIO(xmind_file.read()) xmind_zip_file = zipfile.ZipFile(f, 'r') @@ -87,16 +88,6 @@ def generate(self): image = Image.open(f) self.create_image_thumbnail(image) - def get_inner_path(self): - token = seafile_api.get_fileserver_access_token( - self.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_name)) - - return self.inner_path - def create_image_thumbnail(self, image): width, height = image.size image_memory_cost = width * height * 4 / 1024 / 1024 diff --git a/seatable_thumbnail/utils.py b/seatable_thumbnail/utils.py new file mode 100644 index 0000000..799ba1e --- /dev/null +++ b/seatable_thumbnail/utils.py @@ -0,0 +1,40 @@ +import urllib.request +import urllib.parse + +from seaserv import seafile_api +import seatable_thumbnail.settings as settings + + +def get_file_id(repo_id, file_path): + file_id = seafile_api.get_file_id_by_path(repo_id, file_path) + if not file_id: + raise ValueError(404, 'file_id not found.') + + return file_id + + +def get_inner_path(repo_id, file_id, file_name): + token = seafile_api.get_fileserver_access_token( + repo_id, file_id, 'view', '', use_onetime=True) + if not token: + raise ValueError(404, 'token not found.') + inner_path = '%s/files/%s/%s' % ( + settings.INNER_FILE_SERVER_ROOT.rstrip('/'), token, urllib.parse.quote(file_name)) + + return inner_path + + +def cache_check(request, info): + etag = info.get('etag') + if_none_match_headers = request.headers.get('if-none-match') + if_none_match = if_none_match_headers[0] if if_none_match_headers else '' + + last_modified = info.get('last_modified') + if_modified_since_headers = request.headers.get('if-modified-since') + if_modified_since = if_modified_since_headers[0] if if_modified_since_headers else '' + + if (if_none_match and if_none_match == etag) \ + or (if_modified_since and if_modified_since == last_modified): + return True + else: + return False