Skip to content

Commit

Permalink
get_file_id
Browse files Browse the repository at this point in the history
  • Loading branch information
SkywalkerSpace committed Sep 23, 2020
1 parent c913a23 commit 8c6ba5c
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 17 deletions.
6 changes: 4 additions & 2 deletions seatable_thumbnail/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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
28 changes: 21 additions & 7 deletions seatable_thumbnail/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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')
Expand All @@ -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')
Expand Down
18 changes: 10 additions & 8 deletions seatable_thumbnail/thumbnail.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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()
Expand All @@ -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(
Expand All @@ -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')
Expand All @@ -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
Expand Down

0 comments on commit 8c6ba5c

Please sign in to comment.