mirror of
https://github.com/haiwen/seahub.git
synced 2025-09-02 15:38:15 +00:00
Add previewd file extention and type to cache. And clean code.
This commit is contained in:
5
urls.py
5
urls.py
@@ -17,6 +17,7 @@ from seahub.views import root, peers, myhome, \
|
||||
from seahub.notifications.views import notification_list
|
||||
from seahub.share.views import share_admin
|
||||
from seahub.group.views import group_list
|
||||
from seahub.utils import set_file_ext_cache
|
||||
|
||||
# Uncomment the next two lines to enable the admin:
|
||||
#from django.contrib import admin
|
||||
@@ -108,3 +109,7 @@ if settings.DEBUG:
|
||||
urlpatterns += patterns('',
|
||||
(r'^%s/(?P<path>.*)$' % (media_url), 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT}),
|
||||
)
|
||||
|
||||
# Set file extension cache at django startup, this function should be
|
||||
# called only once.
|
||||
set_file_ext_cache()
|
||||
|
26
utils.py
26
utils.py
@@ -19,12 +19,12 @@ import settings
|
||||
EMPTY_SHA1 = '0000000000000000000000000000000000000000'
|
||||
|
||||
PREVIEW_FILEEXT = {
|
||||
'Document': ('ac', 'am', 'bat', 'c', 'cc', 'cmake', 'cpp', 'css', 'diff', 'h', 'htm', 'html', 'xhtml', 'java', 'js', 'json', 'less', 'make', 'php', 'properties', 'py', 'rb', 'scala', 'script', 'sh', 'sql', 'txt','text', 'vi', 'vim', 'xml'),
|
||||
'Document': ('ac', 'am', 'bat', 'c', 'cc', 'cmake', 'cpp', 'css', 'diff', 'h', 'html', 'java', 'js', 'json', 'less', 'make', 'markdown', 'org', 'php', 'properties', 'py', 'rb', 'scala', 'script', 'sh', 'sql', 'txt','text', 'vi', 'vim'),
|
||||
'Image': ('gif', 'jpeg', 'jpg', 'png'),
|
||||
'SVG': ('svg',),
|
||||
}
|
||||
|
||||
|
||||
FILEEXT_PREFIX = 'FILEEXT_'
|
||||
def go_permission_error(request, msg=None):
|
||||
"""
|
||||
Return permisson error page.
|
||||
@@ -239,10 +239,15 @@ def valid_previewed_file(filename):
|
||||
|
||||
"""
|
||||
fileExt = os.path.splitext(filename)[1][1:]
|
||||
for filetype in PREVIEW_FILEEXT.keys():
|
||||
if fileExt in PREVIEW_FILEEXT.get(filetype):
|
||||
return (True, filetype)
|
||||
return (False, '')
|
||||
filetype = cache.get(FILEEXT_PREFIX + fileExt)
|
||||
if filetype:
|
||||
return filetype
|
||||
else:
|
||||
for k in PREVIEW_FILEEXT.keys():
|
||||
if fileExt in PREVIEW_FILEEXT.get(k):
|
||||
cache.set(FILEEXT_PREFIX + fileExt, k)
|
||||
return k
|
||||
return None
|
||||
|
||||
def get_file_revision_id_size (commit_id, path):
|
||||
"""Given a commit and a file path in that commit, return the seafile id
|
||||
@@ -259,4 +264,13 @@ def get_file_revision_id_size (commit_id, path):
|
||||
|
||||
return None, None
|
||||
|
||||
def set_file_ext_cache():
|
||||
"""
|
||||
Put previewed file extension and file type to cache.
|
||||
|
||||
"""
|
||||
for filetype in PREVIEW_FILEEXT.keys():
|
||||
for fileext in PREVIEW_FILEEXT.get(filetype):
|
||||
key = FILEEXT_PREFIX + fileext
|
||||
value = filetype
|
||||
cache.set(key, value)
|
||||
|
6
views.py
6
views.py
@@ -845,7 +845,7 @@ def repo_view_file(request, repo_id):
|
||||
zipped = gen_path_link(path, repo.name)
|
||||
|
||||
# determin whether file can preview on web
|
||||
can_preview, filetype = valid_previewed_file(filename)
|
||||
filetype = valid_previewed_file(filename)
|
||||
|
||||
# raw path
|
||||
tmp_str = '%s/access?repo_id=%s&id=%s&filename=%s&op=%s&t=%s&u=%s'
|
||||
@@ -880,7 +880,6 @@ def repo_view_file(request, repo_id):
|
||||
'view_history': view_history,
|
||||
'current_commit': current_commit,
|
||||
'token': token,
|
||||
'can_preview': can_preview,
|
||||
'filetype': filetype,
|
||||
'raw_path': raw_path,
|
||||
'fileshare': fileshare,
|
||||
@@ -1895,7 +1894,7 @@ def view_shared_file(request, token):
|
||||
access_token = gen_token()
|
||||
seafserv_rpc.web_save_access_token(access_token, obj_id)
|
||||
|
||||
can_preview, filetype = valid_previewed_file(filename)
|
||||
filetype = valid_previewed_file(filename)
|
||||
|
||||
# Raw path
|
||||
tmp_str = '%s/access?repo_id=%s&id=%s&filename=%s&op=%s&t=%s&u=%s'
|
||||
@@ -1917,7 +1916,6 @@ def view_shared_file(request, token):
|
||||
'file_name': filename,
|
||||
'shared_token': token,
|
||||
'access_token': access_token,
|
||||
'can_preview': can_preview,
|
||||
'filetype': filetype,
|
||||
'raw_path': raw_path,
|
||||
'username': username,
|
||||
|
Reference in New Issue
Block a user