mirror of
https://github.com/haiwen/seahub.git
synced 2025-10-21 19:00:12 +00:00
[file encoding] added encoding option & used chardet for Text/Markdown/Sf file
This commit is contained in:
@@ -7,7 +7,7 @@ These are referenced from the setting TEMPLATE_CONTEXT_PROCESSORS and used by
|
|||||||
RequestContext.
|
RequestContext.
|
||||||
"""
|
"""
|
||||||
from settings import SEAFILE_VERSION, SITE_TITLE, SITE_NAME, SITE_BASE, \
|
from settings import SEAFILE_VERSION, SITE_TITLE, SITE_NAME, SITE_BASE, \
|
||||||
ENABLE_SIGNUP, MAX_FILE_NAME, USE_PDFJS
|
ENABLE_SIGNUP, MAX_FILE_NAME, USE_PDFJS, FILE_ENCODING_LIST
|
||||||
try:
|
try:
|
||||||
from settings import BUSINESS_MODE
|
from settings import BUSINESS_MODE
|
||||||
except ImportError:
|
except ImportError:
|
||||||
@@ -38,5 +38,6 @@ def base(request):
|
|||||||
'enable_signup': ENABLE_SIGNUP,
|
'enable_signup': ENABLE_SIGNUP,
|
||||||
'max_file_name': MAX_FILE_NAME,
|
'max_file_name': MAX_FILE_NAME,
|
||||||
'use_pdfjs': USE_PDFJS,
|
'use_pdfjs': USE_PDFJS,
|
||||||
|
'file_encoding_list': FILE_ENCODING_LIST,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -1507,6 +1507,16 @@ textarea:-moz-placeholder {/* for FF */
|
|||||||
#file-op button {
|
#file-op button {
|
||||||
padding:2px 8px;
|
padding:2px 8px;
|
||||||
}
|
}
|
||||||
|
#file-enc-cont {
|
||||||
|
width:950px;
|
||||||
|
margin:-20px auto 6px;
|
||||||
|
text-align:right;
|
||||||
|
}
|
||||||
|
#file-enc {
|
||||||
|
border:1px solid #ddd;
|
||||||
|
border-radius:2px;
|
||||||
|
background:#efefef;
|
||||||
|
}
|
||||||
#file-view-tip {
|
#file-view-tip {
|
||||||
height: 150px;
|
height: 150px;
|
||||||
padding:10px;
|
padding:10px;
|
||||||
|
@@ -146,6 +146,7 @@ ACCOUNT_ACTIVATION_DAYS = 7
|
|||||||
# File preview
|
# File preview
|
||||||
FILE_PREVIEW_MAX_SIZE = 10 * 1024 * 1024
|
FILE_PREVIEW_MAX_SIZE = 10 * 1024 * 1024
|
||||||
USE_PDFJS = True
|
USE_PDFJS = True
|
||||||
|
FILE_ENCODING_LIST = ['auto', 'utf-8', 'gbk', 'ISO-8859-1', 'ISO-8859-5']
|
||||||
|
|
||||||
# Avatar
|
# Avatar
|
||||||
AVATAR_STORAGE_DIR = 'avatars'
|
AVATAR_STORAGE_DIR = 'avatars'
|
||||||
|
@@ -73,7 +73,7 @@
|
|||||||
|
|
||||||
{% if not read_only %}
|
{% if not read_only %}
|
||||||
{% if filetype == 'Text' or filetype == 'Markdown' or filetype == 'Sf' %}
|
{% if filetype == 'Text' or filetype == 'Markdown' or filetype == 'Sf' %}
|
||||||
<button data="{{ SITE_ROOT }}repo/{{ repo.id }}/file/edit/?p={{ path }}" id="edit">{% trans "Edit"%}</button>
|
<button data="{{ SITE_ROOT }}repo/{{ repo.id }}/file/edit/?p={{ path }}&file_enc={{file_enc}}" id="edit">{% trans "Edit"%}</button>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
{% endif %}
|
{% endif %}
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
@@ -5,14 +5,31 @@ content of files that can be viewed online shows here.
|
|||||||
For details please refer to 'snippets/file_content_js.html'.
|
For details please refer to 'snippets/file_content_js.html'.
|
||||||
{% endcomment %}
|
{% endcomment %}
|
||||||
<div id="file-view">
|
<div id="file-view">
|
||||||
|
{% if filetype == 'Text' or filetype == 'Sf' or filetype == 'Markdown' %}
|
||||||
|
<div id="file-enc-cont">
|
||||||
|
<label for="file-enc">{% trans "Encoding:" %}</label>
|
||||||
|
<select id="file-enc">
|
||||||
|
{% for enc in file_encoding_list %}
|
||||||
|
<option value="{{ enc }}" {% if file_enc == enc %} selected="selected" {% endif %}>{% if enc == 'auto'%}{% trans "auto detect" %}{% else %}{{ enc }}{% endif %}</option>
|
||||||
|
{% endfor %}
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
|
{% endif %}
|
||||||
|
|
||||||
{% if not err %}
|
{% if not err %}
|
||||||
{% if filetype == 'Text' or filetype == 'Sf' %}
|
{% if filetype == 'Text' or filetype == 'Sf' or filetype == 'Markdown' %}
|
||||||
{% ifnotequal file_content None %}
|
{% ifnotequal file_content None %}
|
||||||
{% if filetype == 'Text' %}
|
{% if filetype == 'Text' %}
|
||||||
<textarea id="docu-view" class="vh">{{ file_content|escape }}</textarea>
|
<textarea id="docu-view" class="vh">{{ file_content|escape }}</textarea>
|
||||||
{% else %}
|
{% endif %}
|
||||||
|
|
||||||
|
{% if filetype == 'Sf' %}
|
||||||
<div id="sf" class="article">{{ file_content|safe }}</div>
|
<div id="sf" class="article">{{ file_content|safe }}</div>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
||||||
|
{% if filetype == 'Markdown' %}
|
||||||
|
<div id="md-view" class="article"></div>
|
||||||
|
{% endif %}
|
||||||
{% endifnotequal %}
|
{% endifnotequal %}
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
||||||
|
@@ -40,8 +40,7 @@
|
|||||||
{% if filetype == 'Markdown' %}
|
{% if filetype == 'Markdown' %}
|
||||||
{% ifnotequal file_content None %}
|
{% ifnotequal file_content None %}
|
||||||
var converter = new Showdown.converter();
|
var converter = new Showdown.converter();
|
||||||
$('#file-view').html('<div id="md-view" class="article">' + converter.makeHtml('{{ file_content|escapejs }}') + '</div>');
|
$('#md-view').html(converter.makeHtml('{{ file_content|escapejs }}')).children(':first').css('margin-top', '0');
|
||||||
$('#md-view').children(':first').css('margin-top', '0');
|
|
||||||
{% endifnotequal %}
|
{% endifnotequal %}
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
||||||
@@ -168,3 +167,29 @@ $('#file-view').html('<div id="file-view-tip"><p>{% trans "This type of file can
|
|||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
||||||
{% endif %}{# 'if not err' ends here. #}
|
{% endif %}{# 'if not err' ends here. #}
|
||||||
|
|
||||||
|
{% if filetype == 'Text' or filetype == 'Sf' or filetype == 'Markdown' %}
|
||||||
|
$('#file-enc').change(function() {
|
||||||
|
var file_enc = $(this).val();
|
||||||
|
var s = location.search;
|
||||||
|
if (s.indexOf('?') == -1) {
|
||||||
|
location.search = '?file_enc=' + file_enc;
|
||||||
|
} else {
|
||||||
|
if (s.indexOf('file_enc') == -1) {
|
||||||
|
location.search += '&file_enc=' + file_enc;
|
||||||
|
} else {
|
||||||
|
var params = s.substr(1).split('&');
|
||||||
|
var param;
|
||||||
|
for (var i = 0, len = params.length; i < len; i++) {
|
||||||
|
param = params[i].split('=');
|
||||||
|
if (param[0] == 'file_enc') {
|
||||||
|
param[1] = file_enc;
|
||||||
|
params[i] = param.join('=');
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
location.search = '?' + params.join('&');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
{% endif %}
|
||||||
|
58
views.py
58
views.py
@@ -9,6 +9,7 @@ import sys
|
|||||||
import urllib
|
import urllib
|
||||||
import urllib2
|
import urllib2
|
||||||
import logging
|
import logging
|
||||||
|
import chardet
|
||||||
from urllib import quote
|
from urllib import quote
|
||||||
from django.core.cache import cache
|
from django.core.cache import cache
|
||||||
from django.core.urlresolvers import reverse
|
from django.core.urlresolvers import reverse
|
||||||
@@ -78,7 +79,7 @@ try:
|
|||||||
DOCUMENT_CONVERTOR_ROOT += '/'
|
DOCUMENT_CONVERTOR_ROOT += '/'
|
||||||
except ImportError:
|
except ImportError:
|
||||||
DOCUMENT_CONVERTOR_ROOT = None
|
DOCUMENT_CONVERTOR_ROOT = None
|
||||||
from settings import FILE_PREVIEW_MAX_SIZE, INIT_PASSWD, USE_PDFJS,\
|
from settings import FILE_PREVIEW_MAX_SIZE, INIT_PASSWD, USE_PDFJS, FILE_ENCODING_LIST, \
|
||||||
SEND_EMAIL_ON_ADDING_SYSTEM_MEMBER, SEND_EMAIL_ON_RESETTING_USER_PASSWD
|
SEND_EMAIL_ON_ADDING_SYSTEM_MEMBER, SEND_EMAIL_ON_RESETTING_USER_PASSWD
|
||||||
|
|
||||||
try:
|
try:
|
||||||
@@ -1247,6 +1248,7 @@ def repo_view_file(request, repo_id):
|
|||||||
filename = urllib2.quote(u_filename.encode('utf-8'))
|
filename = urllib2.quote(u_filename.encode('utf-8'))
|
||||||
comment_open = request.GET.get('comment_open', '')
|
comment_open = request.GET.get('comment_open', '')
|
||||||
page_from = request.GET.get('from', '')
|
page_from = request.GET.get('from', '')
|
||||||
|
file_enc = request.GET.get('file_enc', 'auto')
|
||||||
|
|
||||||
commit_id = request.GET.get('commit_id', '')
|
commit_id = request.GET.get('commit_id', '')
|
||||||
view_history = True if commit_id else False
|
view_history = True if commit_id else False
|
||||||
@@ -1304,7 +1306,7 @@ def repo_view_file(request, repo_id):
|
|||||||
raw_path = gen_file_get_url(token, filename)
|
raw_path = gen_file_get_url(token, filename)
|
||||||
|
|
||||||
# get file content
|
# get file content
|
||||||
err, file_content, swf_exists, filetype = get_file_content(filetype, raw_path, obj_id, fileext)
|
err, file_content, swf_exists, filetype = get_file_content(filetype, raw_path, obj_id, fileext, file_enc)
|
||||||
|
|
||||||
img_prev = None
|
img_prev = None
|
||||||
img_next = None
|
img_next = None
|
||||||
@@ -1346,6 +1348,7 @@ def repo_view_file(request, repo_id):
|
|||||||
'raw_path': raw_path,
|
'raw_path': raw_path,
|
||||||
'err': err,
|
'err': err,
|
||||||
'file_content': file_content,
|
'file_content': file_content,
|
||||||
|
'file_enc': file_enc,
|
||||||
'swf_exists': swf_exists,
|
'swf_exists': swf_exists,
|
||||||
'DOCUMENT_CONVERTOR_ROOT': DOCUMENT_CONVERTOR_ROOT,
|
'DOCUMENT_CONVERTOR_ROOT': DOCUMENT_CONVERTOR_ROOT,
|
||||||
'page_from': page_from,
|
'page_from': page_from,
|
||||||
@@ -1423,6 +1426,7 @@ def repo_view_file(request, repo_id):
|
|||||||
'contacts': contacts,
|
'contacts': contacts,
|
||||||
'err': err,
|
'err': err,
|
||||||
'file_content': file_content,
|
'file_content': file_content,
|
||||||
|
'file_enc': file_enc,
|
||||||
"applet_root": get_ccnetapplet_root(),
|
"applet_root": get_ccnetapplet_root(),
|
||||||
'groups': groups,
|
'groups': groups,
|
||||||
'comments': comments,
|
'comments': comments,
|
||||||
@@ -1492,10 +1496,13 @@ def file_comment(request):
|
|||||||
content_type=content_type)
|
content_type=content_type)
|
||||||
|
|
||||||
|
|
||||||
def repo_file_get(raw_path):
|
def repo_file_get(raw_path, file_enc):
|
||||||
err = ''
|
err = ''
|
||||||
file_content = ''
|
file_content = ''
|
||||||
encoding = ''
|
encoding = ''
|
||||||
|
if file_enc in FILE_ENCODING_LIST and file_enc != 'auto':
|
||||||
|
encoding = file_enc
|
||||||
|
|
||||||
try:
|
try:
|
||||||
file_response = urllib2.urlopen(raw_path)
|
file_response = urllib2.urlopen(raw_path)
|
||||||
if long(file_response.headers['Content-Length']) > FILE_PREVIEW_MAX_SIZE:
|
if long(file_response.headers['Content-Length']) > FILE_PREVIEW_MAX_SIZE:
|
||||||
@@ -1510,23 +1517,37 @@ def repo_file_get(raw_path):
|
|||||||
err = _(u'URLError: failed to open file online')
|
err = _(u'URLError: failed to open file online')
|
||||||
return err, '', ''
|
return err, '', ''
|
||||||
else:
|
else:
|
||||||
try:
|
if encoding:
|
||||||
u_content = content.decode('utf-8')
|
|
||||||
encoding = 'utf-8'
|
|
||||||
except UnicodeDecodeError:
|
|
||||||
# XXX: file in windows is encoded in gbk
|
|
||||||
try:
|
try:
|
||||||
u_content = content.decode('gbk')
|
u_content = content.decode(encoding)
|
||||||
encoding = 'gbk'
|
|
||||||
except UnicodeDecodeError:
|
except UnicodeDecodeError:
|
||||||
err = _(u'Unknown file encoding')
|
err = _(u'The encoding you chose is not proper.')
|
||||||
return err, '', ''
|
return err, '', ''
|
||||||
|
else:
|
||||||
|
try:
|
||||||
|
u_content = content.decode('utf-8')
|
||||||
|
encoding = 'utf-8'
|
||||||
|
except UnicodeDecodeError:
|
||||||
|
try:
|
||||||
|
u_content = content.decode('gbk')
|
||||||
|
encoding = 'gbk'
|
||||||
|
except UnicodeDecodeError:
|
||||||
|
encoding = chardet.detect(content)['encoding']
|
||||||
|
if encoding != None:
|
||||||
|
try:
|
||||||
|
u_content = content.decode(encoding)
|
||||||
|
except UnicodeDecodeError:
|
||||||
|
err = _(u'Unknown file encoding')
|
||||||
|
return err, '', ''
|
||||||
|
else:
|
||||||
|
err = _(u'Unknown file encoding')
|
||||||
|
return err, '', ''
|
||||||
|
|
||||||
file_content = u_content
|
file_content = u_content
|
||||||
|
|
||||||
return err, file_content, encoding
|
return err, file_content, encoding
|
||||||
|
|
||||||
def get_file_content(filetype, raw_path, obj_id, fileext):
|
def get_file_content(filetype, raw_path, obj_id, fileext, file_enc):
|
||||||
err = ''
|
err = ''
|
||||||
file_content = ''
|
file_content = ''
|
||||||
swf_exists = False
|
swf_exists = False
|
||||||
@@ -1537,7 +1558,7 @@ def get_file_content(filetype, raw_path, obj_id, fileext):
|
|||||||
file_content['img_w'], file_content['img_h'] = img.size
|
file_content['img_w'], file_content['img_h'] = img.size
|
||||||
|
|
||||||
if filetype == 'Text' or filetype == 'Markdown' or filetype == 'Sf':
|
if filetype == 'Text' or filetype == 'Markdown' or filetype == 'Sf':
|
||||||
err, file_content, encoding = repo_file_get(raw_path)
|
err, file_content, encoding = repo_file_get(raw_path, file_enc)
|
||||||
elif filetype == 'Document':
|
elif filetype == 'Document':
|
||||||
if DOCUMENT_CONVERTOR_ROOT:
|
if DOCUMENT_CONVERTOR_ROOT:
|
||||||
err, swf_exists = flash_prepare(raw_path, obj_id, fileext)
|
err, swf_exists = flash_prepare(raw_path, obj_id, fileext)
|
||||||
@@ -1664,7 +1685,8 @@ def file_edit(request, repo_id):
|
|||||||
op = 'decrypt'
|
op = 'decrypt'
|
||||||
if not op:
|
if not op:
|
||||||
raw_path = gen_file_get_url(token, filename)
|
raw_path = gen_file_get_url(token, filename)
|
||||||
err, file_content, encoding = repo_file_get(raw_path)
|
file_enc = request.GET.get('file_enc', 'auto')
|
||||||
|
err, file_content, encoding = repo_file_get(raw_path, file_enc)
|
||||||
else:
|
else:
|
||||||
err = _(u'Edit online is not offered for this type of file.')
|
err = _(u'Edit online is not offered for this type of file.')
|
||||||
|
|
||||||
@@ -2581,7 +2603,8 @@ def view_shared_file(request, token):
|
|||||||
raw_path = gen_file_get_url(access_token, quote_filename)
|
raw_path = gen_file_get_url(access_token, quote_filename)
|
||||||
|
|
||||||
# get file content
|
# get file content
|
||||||
err, file_content, swf_exists, filetype = get_file_content(filetype, raw_path, obj_id, fileext)
|
file_enc = request.GET.get('file_enc', 'auto')
|
||||||
|
err, file_content, swf_exists, filetype = get_file_content(filetype, raw_path, obj_id, fileext, file_enc)
|
||||||
|
|
||||||
# Increase file shared link view_cnt, this operation should be atomic
|
# Increase file shared link view_cnt, this operation should be atomic
|
||||||
fileshare = FileShare.objects.get(token=token)
|
fileshare = FileShare.objects.get(token=token)
|
||||||
@@ -2601,6 +2624,7 @@ def view_shared_file(request, token):
|
|||||||
'username': username,
|
'username': username,
|
||||||
'err': err,
|
'err': err,
|
||||||
'file_content': file_content,
|
'file_content': file_content,
|
||||||
|
'file_enc': file_enc,
|
||||||
'swf_exists': swf_exists,
|
'swf_exists': swf_exists,
|
||||||
'DOCUMENT_CONVERTOR_ROOT': DOCUMENT_CONVERTOR_ROOT,
|
'DOCUMENT_CONVERTOR_ROOT': DOCUMENT_CONVERTOR_ROOT,
|
||||||
}, context_instance=RequestContext(request))
|
}, context_instance=RequestContext(request))
|
||||||
@@ -2683,7 +2707,8 @@ def view_file_via_shared_dir(request, token):
|
|||||||
# Raw path
|
# Raw path
|
||||||
raw_path = gen_file_get_url(access_token, quote_filename)
|
raw_path = gen_file_get_url(access_token, quote_filename)
|
||||||
# get file content
|
# get file content
|
||||||
err, file_content, swf_exists, filetype = get_file_content(filetype, raw_path, obj_id, fileext)
|
file_enc = request.GET.get('file_enc', 'auto')
|
||||||
|
err, file_content, swf_exists, filetype, encoding = get_file_content(filetype, raw_path, obj_id, fileext, file_enc)
|
||||||
|
|
||||||
zipped = gen_path_link(path, '')
|
zipped = gen_path_link(path, '')
|
||||||
|
|
||||||
@@ -2700,6 +2725,7 @@ def view_file_via_shared_dir(request, token):
|
|||||||
'username': username,
|
'username': username,
|
||||||
'err': err,
|
'err': err,
|
||||||
'file_content': file_content,
|
'file_content': file_content,
|
||||||
|
'file_enc': file_enc,
|
||||||
'swf_exists': swf_exists,
|
'swf_exists': swf_exists,
|
||||||
'DOCUMENT_CONVERTOR_ROOT': DOCUMENT_CONVERTOR_ROOT,
|
'DOCUMENT_CONVERTOR_ROOT': DOCUMENT_CONVERTOR_ROOT,
|
||||||
'zipped': zipped,
|
'zipped': zipped,
|
||||||
|
Reference in New Issue
Block a user