mirror of
https://github.com/haiwen/seahub.git
synced 2025-09-20 19:08:21 +00:00
[seahub] Added the ability to modify the format of the text preview suffix
This commit is contained in:
@@ -317,6 +317,11 @@ USE_PDFJS = True
|
|||||||
FILE_ENCODING_LIST = ['auto', 'utf-8', 'gbk', 'ISO-8859-1', 'ISO-8859-5']
|
FILE_ENCODING_LIST = ['auto', 'utf-8', 'gbk', 'ISO-8859-1', 'ISO-8859-5']
|
||||||
FILE_ENCODING_TRY_LIST = ['utf-8', 'gbk']
|
FILE_ENCODING_TRY_LIST = ['utf-8', 'gbk']
|
||||||
HIGHLIGHT_KEYWORD = False # If True, highlight the keywords in the file when the visit is via clicking a link in 'search result' page.
|
HIGHLIGHT_KEYWORD = False # If True, highlight the keywords in the file when the visit is via clicking a link in 'search result' page.
|
||||||
|
# Can preview the text file type suffix
|
||||||
|
TEXT_PREVIEW_EXT = """ac, am, bat, c, cc, cmake, cpp, cs, css, diff, el, h, html,
|
||||||
|
htm, java, js, json, less, make, org, php, pl, properties, py, rb,
|
||||||
|
scala, script, sh, sql, txt, text, tex, vi, vim, xhtml, xml, log, csv,
|
||||||
|
groovy, rst, patch, go"""
|
||||||
|
|
||||||
# Common settings(file extension, storage) for avatar and group avatar.
|
# Common settings(file extension, storage) for avatar and group avatar.
|
||||||
AVATAR_FILE_STORAGE = '' # Replace with 'seahub.base.database_storage.DatabaseStorage' if save avatar files to database
|
AVATAR_FILE_STORAGE = '' # Replace with 'seahub.base.database_storage.DatabaseStorage' if save avatar files to database
|
||||||
@@ -685,4 +690,6 @@ CONSTANCE_CONFIG = {
|
|||||||
'SHARE_LINK_TOKEN_LENGTH': (SHARE_LINK_TOKEN_LENGTH, ''),
|
'SHARE_LINK_TOKEN_LENGTH': (SHARE_LINK_TOKEN_LENGTH, ''),
|
||||||
'SHARE_LINK_PASSWORD_MIN_LENGTH': (SHARE_LINK_PASSWORD_MIN_LENGTH,''),
|
'SHARE_LINK_PASSWORD_MIN_LENGTH': (SHARE_LINK_PASSWORD_MIN_LENGTH,''),
|
||||||
'ENABLE_TWO_FACTOR_AUTH': (ENABLE_TWO_FACTOR_AUTH,''),
|
'ENABLE_TWO_FACTOR_AUTH': (ENABLE_TWO_FACTOR_AUTH,''),
|
||||||
|
|
||||||
|
'TEXT_PREVIEW_EXT': (TEXT_PREVIEW_EXT, ''),
|
||||||
}
|
}
|
||||||
|
@@ -143,6 +143,10 @@
|
|||||||
{% include "snippets/web_settings_form.html" %}
|
{% include "snippets/web_settings_form.html" %}
|
||||||
{% endwith %}
|
{% endwith %}
|
||||||
|
|
||||||
|
{% with type="input" setting_display_name="Preview Text ext" setting_name="TEXT_PREVIEW_EXT" setting_val=config_dict.TEXT_PREVIEW_EXT %}
|
||||||
|
{% trans "Each suffix is separated by a comma" as help_tip %}
|
||||||
|
{% include "snippets/web_settings_form.html" %}
|
||||||
|
{% endwith %}
|
||||||
</div>
|
</div>
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
||||||
|
@@ -94,7 +94,6 @@ EMPTY_SHA1 = '0000000000000000000000000000000000000000'
|
|||||||
MAX_INT = 2147483647
|
MAX_INT = 2147483647
|
||||||
|
|
||||||
PREVIEW_FILEEXT = {
|
PREVIEW_FILEEXT = {
|
||||||
TEXT: ('ac', 'am', 'bat', 'c', 'cc', 'cmake', 'cpp', 'cs', 'css', 'diff', 'el', 'h', 'html', 'htm', 'java', 'js', 'json', 'less', 'make', 'org', 'php', 'pl', 'properties', 'py', 'rb', 'scala', 'script', 'sh', 'sql', 'txt', 'text', 'tex', 'vi', 'vim', 'xhtml', 'xml', 'log', 'csv', 'groovy', 'rst', 'patch', 'go'),
|
|
||||||
IMAGE: ('gif', 'jpeg', 'jpg', 'png', 'ico', 'bmp'),
|
IMAGE: ('gif', 'jpeg', 'jpg', 'png', 'ico', 'bmp'),
|
||||||
DOCUMENT: ('doc', 'docx', 'ppt', 'pptx', 'odt', 'fodt', 'odp', 'fodp'),
|
DOCUMENT: ('doc', 'docx', 'ppt', 'pptx', 'odt', 'fodt', 'odp', 'fodp'),
|
||||||
SPREADSHEET: ('xls', 'xlsx', 'ods', 'fods'),
|
SPREADSHEET: ('xls', 'xlsx', 'ods', 'fods'),
|
||||||
@@ -334,12 +333,24 @@ def get_user_repos(username, org_id=None):
|
|||||||
|
|
||||||
return (owned_repos, shared_repos, groups_repos, public_repos)
|
return (owned_repos, shared_repos, groups_repos, public_repos)
|
||||||
|
|
||||||
|
def get_conf_text_ext():
|
||||||
|
"""
|
||||||
|
Get the conf of text ext in constance settings, and remove space.
|
||||||
|
"""
|
||||||
|
if hasattr(config, 'TEXT_PREVIEW_EXT'):
|
||||||
|
text_ext = getattr(config, 'TEXT_PREVIEW_EXT').split(',')
|
||||||
|
return [x.strip() for x in text_ext]
|
||||||
|
return []
|
||||||
|
|
||||||
def get_file_type_and_ext(filename):
|
def get_file_type_and_ext(filename):
|
||||||
"""
|
"""
|
||||||
Return file type and extension if the file can be previewd online,
|
Return file type and extension if the file can be previewd online,
|
||||||
otherwise, return unknown type.
|
otherwise, return unknown type.
|
||||||
"""
|
"""
|
||||||
fileExt = os.path.splitext(filename)[1][1:].lower()
|
fileExt = os.path.splitext(filename)[1][1:].lower()
|
||||||
|
if fileExt in get_conf_text_ext():
|
||||||
|
return (TEXT, fileExt)
|
||||||
|
|
||||||
filetype = FILEEXT_TYPE_MAP.get(fileExt)
|
filetype = FILEEXT_TYPE_MAP.get(fileExt)
|
||||||
if filetype:
|
if filetype:
|
||||||
return (filetype, fileExt)
|
return (filetype, fileExt)
|
||||||
|
@@ -34,6 +34,7 @@ from django.utils.translation import ugettext as _
|
|||||||
from django.views.decorators.http import require_POST
|
from django.views.decorators.http import require_POST
|
||||||
from django.template.defaultfilters import filesizeformat
|
from django.template.defaultfilters import filesizeformat
|
||||||
from django.views.decorators.csrf import csrf_exempt
|
from django.views.decorators.csrf import csrf_exempt
|
||||||
|
from constance import config
|
||||||
|
|
||||||
from seaserv import seafile_api
|
from seaserv import seafile_api
|
||||||
from seaserv import get_repo, send_message, get_commits, \
|
from seaserv import get_repo, send_message, get_commits, \
|
||||||
@@ -55,7 +56,7 @@ from seahub.utils import render_error, is_org_context, \
|
|||||||
mkstemp, EMPTY_SHA1, HtmlDiff, gen_inner_file_get_url, \
|
mkstemp, EMPTY_SHA1, HtmlDiff, gen_inner_file_get_url, \
|
||||||
user_traffic_over_limit, get_file_audit_events_by_path, \
|
user_traffic_over_limit, get_file_audit_events_by_path, \
|
||||||
generate_file_audit_event_type, FILE_AUDIT_ENABLED, gen_token, \
|
generate_file_audit_event_type, FILE_AUDIT_ENABLED, gen_token, \
|
||||||
get_site_scheme_and_netloc
|
get_site_scheme_and_netloc,get_conf_text_ext
|
||||||
from seahub.utils.ip import get_remote_ip
|
from seahub.utils.ip import get_remote_ip
|
||||||
from seahub.utils.timeutils import utc_to_local
|
from seahub.utils.timeutils import utc_to_local
|
||||||
from seahub.utils.file_types import (IMAGE, PDF, DOCUMENT, SPREADSHEET, AUDIO,
|
from seahub.utils.file_types import (IMAGE, PDF, DOCUMENT, SPREADSHEET, AUDIO,
|
||||||
@@ -328,7 +329,7 @@ def can_preview_file(file_name, file_size, repo=None):
|
|||||||
if repo and repo.encrypted and (file_type in (DOCUMENT, SPREADSHEET, PDF)):
|
if repo and repo.encrypted and (file_type in (DOCUMENT, SPREADSHEET, PDF)):
|
||||||
return (False, _(u'The library is encrypted, can not open file online.'))
|
return (False, _(u'The library is encrypted, can not open file online.'))
|
||||||
|
|
||||||
if file_ext in FILEEXT_TYPE_MAP: # check file extension
|
if file_ext in FILEEXT_TYPE_MAP or file_ext in get_conf_text_ext(): # check file extension
|
||||||
exceeds_limit, err_msg = file_size_exceeds_preview_limit(file_size,
|
exceeds_limit, err_msg = file_size_exceeds_preview_limit(file_size,
|
||||||
file_type)
|
file_type)
|
||||||
if exceeds_limit:
|
if exceeds_limit:
|
||||||
|
@@ -1945,7 +1945,7 @@ def sys_settings(request):
|
|||||||
if HAS_TWO_FACTOR_AUTH:
|
if HAS_TWO_FACTOR_AUTH:
|
||||||
DIGIT_WEB_SETTINGS.append('ENABLE_TWO_FACTOR_AUTH')
|
DIGIT_WEB_SETTINGS.append('ENABLE_TWO_FACTOR_AUTH')
|
||||||
|
|
||||||
STRING_WEB_SETTINGS = ('SERVICE_URL', 'FILE_SERVER_ROOT',)
|
STRING_WEB_SETTINGS = ('SERVICE_URL', 'FILE_SERVER_ROOT', 'TEXT_PREVIEW_EXT')
|
||||||
|
|
||||||
if request.is_ajax() and request.method == "POST":
|
if request.is_ajax() and request.method == "POST":
|
||||||
content_type = 'application/json; charset=utf-8'
|
content_type = 'application/json; charset=utf-8'
|
||||||
|
32
tests/seahub/views/file/test_constance_text_conf.py
Normal file
32
tests/seahub/views/file/test_constance_text_conf.py
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
from mock import patch
|
||||||
|
from django.core.urlresolvers import reverse
|
||||||
|
import requests
|
||||||
|
|
||||||
|
from constance import config
|
||||||
|
|
||||||
|
from seahub.test_utils import BaseTestCase
|
||||||
|
|
||||||
|
import datetime
|
||||||
|
|
||||||
|
class DefaultText(BaseTestCase):
|
||||||
|
def setUp(self):
|
||||||
|
self.login_as(self.user)
|
||||||
|
self.text = self.create_file(repo_id=self.repo.id,
|
||||||
|
parent_dir='/',
|
||||||
|
filename='test.az',
|
||||||
|
username=self.user.username)
|
||||||
|
|
||||||
|
def test_can_useful(self):
|
||||||
|
self.clear_cache()
|
||||||
|
setattr(config, 'TEXT_PREVIEW_EXT', '')
|
||||||
|
resp = self.client.get(reverse('view_lib_file', args=[
|
||||||
|
self.repo.id,
|
||||||
|
self.text
|
||||||
|
]))
|
||||||
|
assert resp.context['err'] == 'invalid extension'
|
||||||
|
setattr(config, 'TEXT_PREVIEW_EXT', 'az')
|
||||||
|
resp = self.client.get(reverse('view_lib_file', args=[
|
||||||
|
self.repo.id,
|
||||||
|
self.text
|
||||||
|
]))
|
||||||
|
assert resp.context['err'] == ''
|
Reference in New Issue
Block a user