Skip to content

Commit

Permalink
add utils
Browse files Browse the repository at this point in the history
  • Loading branch information
SkywalkerSpace committed Sep 23, 2020
1 parent 8c6ba5c commit 77d9267
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 73 deletions.
6 changes: 3 additions & 3 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
FROM phusion/baseimage:0.11
# docker.seafile.top/seafile-dev/seatable-thumbnail-server:1.3.0
# docker.seafile.top/seafile-dev/seatable-thumbnail-server:1.x.x

# Aliyun ubuntu source
RUN rm -rf /etc/apt/sources.list
Expand All @@ -15,7 +15,7 @@ RUN apt-get install -y nginx
RUN apt-get install -y mysql-client

# set python3.6 global
RUN apt-get install -y python3 python3-pip python3-setuptools python3-ldap
RUN apt-get install -y python3 python3-pip
RUN python3.6 -m pip install --upgrade pip -i https://mirrors.aliyun.com/pypi/simple && rm -r /root/.cache/pip
RUN rm -f /usr/bin/python && \
rm -f /usr/bin/python3 && \
Expand All @@ -26,7 +26,7 @@ RUN rm -f /usr/bin/python && \
ln -s /usr/local/bin/pip3.6 /usr/bin/pip && \
ln -s /usr/local/bin/pip3.6 /usr/bin/pip3

RUN pip3 install uvicorn pillow pymysql sqlalchemy future \
RUN pip3 install uvicorn pillow pymysql sqlalchemy future requests \
-i https://mirrors.aliyun.com/pypi/simple && rm -r /root/.cache/pip


Expand Down
2 changes: 2 additions & 0 deletions docker/scripts/init_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@
MYSQL_HOST = 'host'
MYSQL_PORT = '3306'
DATABASE_NAME = 'db_name'
PLUGINS_REPO_ID = ''
"""

if not os.path.exists(seatable_thumbnail_config_path):
Expand Down
23 changes: 3 additions & 20 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ pillow
pymysql
sqlalchemy
future # seaf-server
requests

# psd_tools
# moviepy
17 changes: 3 additions & 14 deletions seatable_thumbnail/plugin.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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
24 changes: 3 additions & 21 deletions seatable_thumbnail/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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]
Expand All @@ -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')
Expand Down
21 changes: 6 additions & 15 deletions seatable_thumbnail/thumbnail.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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())

Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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')
Expand All @@ -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
Expand Down
40 changes: 40 additions & 0 deletions seatable_thumbnail/utils.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 77d9267

Please sign in to comment.