Skip to content

Commit

Permalink
Merge pull request #7 from seatable/update_last_modified
Browse files Browse the repository at this point in the history
update last_modified
  • Loading branch information
freeplant authored Mar 31, 2021
2 parents 44628ca + 3b8f0e6 commit 438e027
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 22 deletions.
2 changes: 1 addition & 1 deletion seatable_thumbnail/permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ def has_collection_table_permission(self):

token = self.collection_table['token']
obj = self.db_session.query(
DTableCollectionTables).filter_by(token=token).first()
DTableCollectionTables).filter_by(token=token).first()
if not obj:
return False

Expand Down
27 changes: 11 additions & 16 deletions seatable_thumbnail/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
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
from seatable_thumbnail.utils import get_file_id, get_file_obj


class ThumbnailSerializer(object):
Expand All @@ -33,8 +33,8 @@ def gen_thumbnail_info(self):
self.thumbnail_info = thumbnail_info

def parse_django_session(self, session_data):
# only for django 1.11.x
encoded_data = base64.b64decode(session_data)
# django/contrib/sessions/backends/base.py
encoded_data = base64.b64decode(session_data.encode('ascii'))
hash_key, serialized = encoded_data.split(b':', 1)
return json.loads(serialized.decode('latin-1'))

Expand Down Expand Up @@ -64,7 +64,7 @@ def get_enable_file_type(self):
self.enable_file_type = enable_file_type

def params_check(self):
size_str = self.request.query_dict['size'][0]
size_str = self.request.query_dict.get('size', ['256'])[0]
size = int(size_str)

file_path = '/' + self.request.url.split('/', 3)[-1]
Expand Down Expand Up @@ -107,12 +107,16 @@ def resource_check(self):
Workspaces).filter_by(id=workspace_id).first()
repo_id = workspace.repo_id
workspace_owner = workspace.owner
file_id = get_file_id(repo_id, file_path)
file_obj = get_file_obj(repo_id, file_path)
file_id = file_obj.obj_id

thumbnail_dir = os.path.join(settings.THUMBNAIL_DIR, str(size))
thumbnail_path = os.path.join(thumbnail_dir, file_id)
os.makedirs(thumbnail_dir, exist_ok=True)
exist, last_modified = self.exist_check(thumbnail_path)

last_modified_time = file_obj.mtime
last_modified = formatdate(int(last_modified_time), usegmt=True)

etag = '"' + file_id + '"'

self.resource = {
Expand All @@ -121,19 +125,10 @@ def resource_check(self):
'workspace_owner': workspace_owner,
'thumbnail_dir': thumbnail_dir,
'thumbnail_path': thumbnail_path,
'exist': exist,
'last_modified': last_modified,
'etag': etag,
}

def exist_check(self, thumbnail_path):
if os.path.exists(thumbnail_path):
last_modified_time = os.path.getmtime(thumbnail_path)
last_modified = formatdate(int(last_modified_time), usegmt=True)
return True, last_modified
else:
return False, ''


class PluginSerializer(object):
def __init__(self, db_session, request):
Expand Down Expand Up @@ -190,4 +185,4 @@ def resource_check(self):
'file_info': file_info,
'last_modified': last_modified,
'etag': etag,
}
}
6 changes: 1 addition & 5 deletions seatable_thumbnail/thumbnail.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import os
import time
import urllib.request
import urllib.parse
import zipfile
import tempfile
from io import BytesIO
from PIL import Image
from email.utils import formatdate

from seaserv import get_repo, get_file_size
import seatable_thumbnail.settings as settings
Expand All @@ -22,7 +20,7 @@ def __init__(self, **info):
self.get()

def get(self):
if self.exist:
if os.path.exists(self.thumbnail_path):
with open(self.thumbnail_path, 'rb') as f:
self.body = f.read()
else:
Expand Down Expand Up @@ -101,8 +99,6 @@ def create_image_thumbnail(self, image):
image = self.get_rotated_image(image)
image.thumbnail((self.size, self.size), Image.ANTIALIAS)
image.save(self.thumbnail_path, THUMBNAIL_EXTENSION)
last_modified_time = time.time()
self.last_modified = formatdate(int(last_modified_time), usegmt=True)

# PIL to bytes
image_bytes = BytesIO()
Expand Down
8 changes: 8 additions & 0 deletions seatable_thumbnail/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ def get_file_id(repo_id, file_path):
return file_id


def get_file_obj(repo_id, file_path):
file_obj = seafile_api.get_dirent_by_path(repo_id, file_path)
if not file_obj:
raise ValueError(404, 'file_obj not found.')

return file_obj


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)
Expand Down

0 comments on commit 438e027

Please sign in to comment.