diff --git a/seahub/settings.py b/seahub/settings.py index ba97fa5f2a..b73c5ee4f6 100644 --- a/seahub/settings.py +++ b/seahub/settings.py @@ -317,6 +317,11 @@ USE_PDFJS = True FILE_ENCODING_LIST = ['auto', 'utf-8', 'gbk', 'ISO-8859-1', 'ISO-8859-5'] 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. +# 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. 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_PASSWORD_MIN_LENGTH': (SHARE_LINK_PASSWORD_MIN_LENGTH,''), 'ENABLE_TWO_FACTOR_AUTH': (ENABLE_TWO_FACTOR_AUTH,''), + + 'TEXT_PREVIEW_EXT': (TEXT_PREVIEW_EXT, ''), } diff --git a/seahub/templates/sysadmin/settings.html b/seahub/templates/sysadmin/settings.html index d5a20f4751..0877aca83c 100644 --- a/seahub/templates/sysadmin/settings.html +++ b/seahub/templates/sysadmin/settings.html @@ -143,6 +143,10 @@ {% include "snippets/web_settings_form.html" %} {% 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 %} {% endblock %} diff --git a/seahub/utils/__init__.py b/seahub/utils/__init__.py index 0beaa468a4..82aed1bf21 100644 --- a/seahub/utils/__init__.py +++ b/seahub/utils/__init__.py @@ -94,7 +94,6 @@ EMPTY_SHA1 = '0000000000000000000000000000000000000000' MAX_INT = 2147483647 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'), DOCUMENT: ('doc', 'docx', 'ppt', 'pptx', 'odt', 'fodt', 'odp', 'fodp'), 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) +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): """ Return file type and extension if the file can be previewd online, otherwise, return unknown type. """ fileExt = os.path.splitext(filename)[1][1:].lower() + if fileExt in get_conf_text_ext(): + return (TEXT, fileExt) + filetype = FILEEXT_TYPE_MAP.get(fileExt) if filetype: return (filetype, fileExt) diff --git a/seahub/views/file.py b/seahub/views/file.py index ddc394a14b..703b51d717 100644 --- a/seahub/views/file.py +++ b/seahub/views/file.py @@ -34,6 +34,7 @@ from django.utils.translation import ugettext as _ from django.views.decorators.http import require_POST from django.template.defaultfilters import filesizeformat from django.views.decorators.csrf import csrf_exempt +from constance import config from seaserv import seafile_api 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, \ user_traffic_over_limit, get_file_audit_events_by_path, \ 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.timeutils import utc_to_local 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)): 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, file_type) if exceeds_limit: diff --git a/seahub/views/sysadmin.py b/seahub/views/sysadmin.py index fd36711d1b..437c2403ab 100644 --- a/seahub/views/sysadmin.py +++ b/seahub/views/sysadmin.py @@ -1945,7 +1945,7 @@ def sys_settings(request): if HAS_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": content_type = 'application/json; charset=utf-8' diff --git a/tests/seahub/views/file/test_constance_text_conf.py b/tests/seahub/views/file/test_constance_text_conf.py new file mode 100644 index 0000000000..2daec87252 --- /dev/null +++ b/tests/seahub/views/file/test_constance_text_conf.py @@ -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'] == ''