From c19df2eece21f1a4c28fcc4c962fb2af3d8394dc Mon Sep 17 00:00:00 2001 From: llj Date: Thu, 9 Aug 2012 15:28:22 +0800 Subject: [PATCH] [view_file,view_shared_file,edit_file]:directly get file_content instead of ajax get --- templates/repo_edit_file.html | 48 +++++++--------- templates/repo_view_file.html | 7 --- templates/snippets/repo_file_get.html | 82 +++++++++++++-------------- templates/view_shared_file.html | 1 - urls.py | 3 +- views.py | 75 ++++++++++-------------- 6 files changed, 93 insertions(+), 123 deletions(-) diff --git a/templates/repo_edit_file.html b/templates/repo_edit_file.html index fdfcb44b2b..4bf6bf12ec 100644 --- a/templates/repo_edit_file.html +++ b/templates/repo_edit_file.html @@ -21,10 +21,10 @@
-

文件内容读取中...

+
-
+
@@ -38,34 +38,26 @@ {% endif %} {% endblock %} diff --git a/templates/repo_view_file.html b/templates/repo_view_file.html index 0f6eea7fe7..c5fefee4a6 100644 --- a/templates/repo_view_file.html +++ b/templates/repo_view_file.html @@ -89,14 +89,7 @@ {% block extra_script %} {% include "snippets/file_view_js.html" %} {% endblock %} diff --git a/urls.py b/urls.py index 810a2c2885..9ed55ba0c0 100644 --- a/urls.py +++ b/urls.py @@ -5,7 +5,7 @@ from django.views.generic.simple import direct_to_template from seahub.views import root, myhome, \ repo, repo_history, modify_token, remove_repo, sys_seafadmin, sys_useradmin, \ activate_user, user_add, user_remove, sys_group_admin, sys_org_admin, \ - ownerhome, repo_history_revert, repo_file_get, repo_file_edit, org_remove, \ + ownerhome, repo_history_revert, repo_file_edit, org_remove, \ user_info, repo_set_access_property, repo_access_file, \ repo_remove_share, repo_download, org_info, repo_view_file, pdf_full_view, \ seafile_access_check, repo_history_changes, \ @@ -67,7 +67,6 @@ urlpatterns = patterns('', # (r'^repo/removefetched/(?P[^/]+)/(?P[^/]+)/$', remove_fetched_repo), # (r'^repo/setap/(?P[^/]+)/$', repo_set_access_property), url(r'^repo/(?P[^/]+)/files/$', repo_view_file, name="repo_view_file"), - (r'^repo/(?P[^/]+)/file/get/$', repo_file_get), (r'^repo/(?P[^/]+)/file/edit/$', repo_file_edit), (r'^pdf_full_view/$', pdf_full_view), url(r'^repo/(?P[^/]+)/(?P[^/]+)/$', repo_access_file, name='repo_access_file'), diff --git a/views.py b/views.py index 3f2a2f1bd4..64fa2b100f 100644 --- a/views.py +++ b/views.py @@ -798,6 +798,12 @@ def repo_view_file(request, repo_id): # raw path raw_path = gen_file_get_url(token, filename) + + # get file content + err = '' + file_content = '' + if filetype == 'Text' or filetype == 'Markdown': + err, file_content = repo_file_get(raw_path) # file share link l = FileShare.objects.filter(repo_id=repo_id).filter(\ @@ -834,63 +840,31 @@ def repo_view_file(request, repo_id): 'domain': domain, 'file_shared_link': file_shared_link, 'contacts': contacts, + 'err': err, + 'file_content': file_content, }, context_instance=RequestContext(request)) -def repo_file_get(request, repo_id): - """ - Handle ajax request to get file content from httpserver. - If get current worktree file, need access_token, path and username from - url params. - If get history file, need access_token, path username and obj_id from - url params. - """ - if not request.is_ajax(): - return Http404 - - # http_server_root = get_httpserver_root() - content_type = 'application/json; charset=utf-8' - access_token = request.GET.get('t') - path = request.GET.get('p', '/') - if path[-1] == '/': - path = path[:-1] - - filename = urllib2.quote(os.path.basename(path).encode('utf-8')) - obj_id = request.GET.get('obj_id', '') - if not obj_id: - try: - obj_id = seafserv_threaded_rpc.get_file_by_path(repo_id, path) - except: - obj_id = None - if not obj_id: - data = json.dumps({'error': '获取文件数据失败'}) - return HttpResponse(data, status=400, content_type=content_type) - - # username = request.GET.get('u', '') - redirect_url = gen_file_get_url(access_token, filename) +def repo_file_get(raw_path): + err = '' + file_content = '' try: - proxied_request = urllib2.urlopen(redirect_url) - if long(proxied_request.headers['Content-Length']) > FILE_PREVIEW_MAX_SIZE: - data = json.dumps({'error': '文件超过10M,无法在线查看。'}) - return HttpResponse(data, status=400, content_type=content_type) + file_response = urllib2.urlopen(raw_path) + if long(file_response.headers['Content-Length']) > FILE_PREVIEW_MAX_SIZE: + err = '文件超过10M,无法在线查看。' else: - content = proxied_request.read() + content = file_response.read() except urllib2.HTTPError, e: err = 'HTTPError: 无法在线打开该文件' - data = json.dumps({'error': err}) - return HttpResponse(data, status=400, content_type=content_type) except urllib2.URLError as e: err = 'URLError: 无法在线打开该文件' - data = json.dumps({'error': err}) - return HttpResponse(data, status=400, content_type=content_type) else: try: u_content = content.decode('utf-8') except: # XXX: file in windows is encoded in gbk u_content = content.decode('gbk') - from django.utils.html import escape - data = json.dumps({'content': u_content}) - return HttpResponse(data, status=200, content_type=content_type) + file_content = u_content + return err, file_content def pdf_full_view(request): @@ -942,13 +916,18 @@ def repo_file_edit(request, repo_id): filetype, fileext = valid_previewed_file(filename) + # get file content + raw_path = gen_file_get_url(token, filename) + err, file_content = repo_file_get(raw_path) + return render_to_response('repo_edit_file.html', { 'repo':repo, 'u_filename':u_filename, 'path':path, 'zipped':zipped, - 'token':token, 'fileext':fileext, + 'err':err, + 'file_content':file_content, }, context_instance=RequestContext(request)) @@ -1751,6 +1730,12 @@ def view_shared_file(request, token): # Raw path raw_path = gen_file_get_url(access_token, quote_filename) + + # get file content + err = '' + file_content = '' + if filetype == 'Text' or filetype == 'Markdown': + err, file_content = repo_file_get(raw_path) # Increase file shared link view_cnt, this operation should be atomic fileshare = FileShare.objects.get(token=token) @@ -1768,6 +1753,8 @@ def view_shared_file(request, token): 'fileext': fileext, 'raw_path': raw_path, 'username': username, + 'err': err, + 'file_content': file_content, }, context_instance=RequestContext(request)) @login_required