mirror of
https://github.com/haiwen/seahub.git
synced 2025-08-19 15:38:38 +00:00
[repo] use ajax to fetch url for file upload/update
This commit is contained in:
parent
53c32a65a5
commit
28b8884524
@ -56,7 +56,7 @@
|
|||||||
{% if no_quota %}
|
{% if no_quota %}
|
||||||
<p class="error">{% trans "The owner of this library has run out of space." %}</p>
|
<p class="error">{% trans "The owner of this library has run out of space." %}</p>
|
||||||
{% else %}
|
{% else %}
|
||||||
<form id="upload-file-form" enctype="multipart/form-data" method="post" action="{{ajax_upload_url}}">{% csrf_token %}
|
<form id="upload-file-form" enctype="multipart/form-data" method="post" action="">{% csrf_token %}
|
||||||
{% if repo.enc_version == 2 and not server_crypto and repo.encrypted %}
|
{% if repo.enc_version == 2 and not server_crypto and repo.encrypted %}
|
||||||
<input type="file" name="file" /><br />
|
<input type="file" name="file" /><br />
|
||||||
<p class="error hide">{% trans "Please select a file at first." %}</p>
|
<p class="error hide">{% trans "Please select a file at first." %}</p>
|
||||||
@ -792,8 +792,15 @@ $('#upload-file').click(function () {
|
|||||||
fd.append('file_size', file.size);
|
fd.append('file_size', file.size);
|
||||||
|
|
||||||
$.modal.close();
|
$.modal.close();
|
||||||
var sb_url = form.attr('action');
|
$.ajax({
|
||||||
encAndSubmitFile(file, fd, sb_url);
|
url: '{% url 'get_file_op_url' repo.id %}?op_type=' + e('upload-blks'),
|
||||||
|
cache: false,
|
||||||
|
dataType: 'json',
|
||||||
|
success: function(data) {
|
||||||
|
encAndSubmitFile(file, fd, data['url']);
|
||||||
|
},
|
||||||
|
error: ajaxErrorHandler
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
{% else %}
|
{% else %}
|
||||||
@ -816,7 +823,14 @@ $('#upload-file').click(function () {
|
|||||||
|
|
||||||
parent_dir.val(cur_path);
|
parent_dir.val(cur_path);
|
||||||
|
|
||||||
// Initialize the jQuery File Upload widget:
|
$.ajax({
|
||||||
|
url: '{% url 'get_file_op_url' repo.id %}?op_type=' + e('upload'),
|
||||||
|
cache: false,
|
||||||
|
dataType: 'json',
|
||||||
|
success: function(data) {
|
||||||
|
form.attr('action', data['url']);
|
||||||
|
|
||||||
|
// Initialize the jQuery File Upload widget, which needs to use form action
|
||||||
form.fileupload({
|
form.fileupload({
|
||||||
//singleFileUploads: false // using 1 request to upload all files of a selection
|
//singleFileUploads: false // using 1 request to upload all files of a selection
|
||||||
sequentialUploads: true
|
sequentialUploads: true
|
||||||
@ -833,6 +847,9 @@ $('#upload-file').click(function () {
|
|||||||
'redirect',
|
'redirect',
|
||||||
window.location.href.replace(/\/repo\/[-a-z0-9]{36}\/.*/, '/media/cors/result.html?%s')
|
window.location.href.replace(/\/repo\/[-a-z0-9]{36}\/.*/, '/media/cors/result.html?%s')
|
||||||
);
|
);
|
||||||
|
},
|
||||||
|
error: ajaxErrorHandler
|
||||||
|
});
|
||||||
});
|
});
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
||||||
@ -1302,8 +1319,16 @@ $('.file-update', context).click(function() {
|
|||||||
|
|
||||||
$.modal.close();
|
$.modal.close();
|
||||||
|
|
||||||
var sb_url = '{{ ajax_update_url }}?head=' + $('#repo-latest-commit .commit-msg').data('cmtid');
|
$.ajax({
|
||||||
|
url: '{% url 'get_file_op_url' repo.id %}?op_type=' + e('update-blks'),
|
||||||
|
cache: false,
|
||||||
|
dataType: 'json',
|
||||||
|
success: function(data) {
|
||||||
|
var sb_url = data['url'] + '?head=' + $('#repo-latest-commit .commit-msg').data('cmtid');
|
||||||
encAndSubmitFile(file, fd, sb_url);
|
encAndSubmitFile(file, fd, sb_url);
|
||||||
|
},
|
||||||
|
error: ajaxErrorHandler
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
@ -1329,9 +1354,14 @@ $('.file-update', context).click(function() {
|
|||||||
var hd = $('#update-file-dialog .hd');
|
var hd = $('#update-file-dialog .hd');
|
||||||
hd.html(hd.html().replace('%(file_name)s', '<span class="op-target">' + file_name + '</span>'));
|
hd.html(hd.html().replace('%(file_name)s', '<span class="op-target">' + file_name + '</span>'));
|
||||||
|
|
||||||
|
$.ajax({
|
||||||
|
url: '{% url 'get_file_op_url' repo.id %}?op_type=' + e('update'),
|
||||||
|
cache: false,
|
||||||
|
dataType: 'json',
|
||||||
|
success: function(data) {
|
||||||
// Initialize the jQuery File Upload widget:
|
// Initialize the jQuery File Upload widget:
|
||||||
form.fileupload({
|
form.fileupload({
|
||||||
url: '{{ ajax_update_url }}?head=' + $('#repo-latest-commit .commit-msg').data('cmtid'),
|
url: data['url'] + '?head=' + $('#repo-latest-commit .commit-msg').data('cmtid'),
|
||||||
{% if max_upload_file_size %}
|
{% if max_upload_file_size %}
|
||||||
maxFileSize: {{max_upload_file_size}}, // in bytes
|
maxFileSize: {{max_upload_file_size}}, // in bytes
|
||||||
{% endif %}
|
{% endif %}
|
||||||
@ -1343,19 +1373,15 @@ $('.file-update', context).click(function() {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
{% if max_upload_file_size %}
|
|
||||||
form.fileupload('option', {
|
|
||||||
maxFileSize: {{max_upload_file_size}}
|
|
||||||
});
|
|
||||||
{% endif %}
|
|
||||||
|
|
||||||
// Enable iframe cross-domain access via redirect option:
|
// Enable iframe cross-domain access via redirect option:
|
||||||
form.fileupload(
|
form.fileupload(
|
||||||
'option',
|
'option',
|
||||||
'redirect',
|
'redirect',
|
||||||
window.location.href.replace(/\/repo\/[-a-z0-9]{36}\/.*/, '/media/cors/result.html?%s')
|
window.location.href.replace(/\/repo\/[-a-z0-9]{36}\/.*/, '/media/cors/result.html?%s')
|
||||||
);
|
);
|
||||||
|
},
|
||||||
|
error: ajaxErrorHandler
|
||||||
|
});
|
||||||
return false;
|
return false;
|
||||||
});
|
});
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
@ -142,6 +142,9 @@ urlpatterns = patterns('',
|
|||||||
url(r'^ajax/repo/(?P<repo_id>[-0-9a-f]{36})/current_commit/$', get_current_commit, name='get_current_commit'),
|
url(r'^ajax/repo/(?P<repo_id>[-0-9a-f]{36})/current_commit/$', get_current_commit, name='get_current_commit'),
|
||||||
|
|
||||||
url(r'^ajax/repo/(?P<repo_id>[-0-9a-f]{36})/encrypted_file/(?P<file_id>[0-9a-f]{40})/download/$', download_enc_file, name='download_enc_file'),
|
url(r'^ajax/repo/(?P<repo_id>[-0-9a-f]{36})/encrypted_file/(?P<file_id>[0-9a-f]{40})/download/$', download_enc_file, name='download_enc_file'),
|
||||||
|
|
||||||
|
url(r'^ajax/repo/(?P<repo_id>[-0-9a-f]{36})/file_op_url/$', get_file_op_url, name='get_file_op_url'),
|
||||||
|
|
||||||
url(r'^ajax/group/(?P<group_id>\d+)/repos/$', get_unenc_group_repos, name='get_group_repos'),
|
url(r'^ajax/group/(?P<group_id>\d+)/repos/$', get_unenc_group_repos, name='get_group_repos'),
|
||||||
url(r'^ajax/my-unenc-repos/$', get_my_unenc_repos, name='get_my_unenc_repos'),
|
url(r'^ajax/my-unenc-repos/$', get_my_unenc_repos, name='get_my_unenc_repos'),
|
||||||
url(r'^ajax/unenc-rw-repos/$', unenc_rw_repos, name='unenc_rw_repos'),
|
url(r'^ajax/unenc-rw-repos/$', unenc_rw_repos, name='unenc_rw_repos'),
|
||||||
|
@ -34,7 +34,7 @@ from seahub.signals import repo_deleted
|
|||||||
from seahub.utils import check_filename_with_rename, EMPTY_SHA1, gen_block_get_url, \
|
from seahub.utils import check_filename_with_rename, EMPTY_SHA1, gen_block_get_url, \
|
||||||
check_and_get_org_by_repo, TRAFFIC_STATS_ENABLED, get_user_traffic_stat,\
|
check_and_get_org_by_repo, TRAFFIC_STATS_ENABLED, get_user_traffic_stat,\
|
||||||
new_merge_with_no_conflict, get_commit_before_new_merge, \
|
new_merge_with_no_conflict, get_commit_before_new_merge, \
|
||||||
get_repo_last_modify
|
get_repo_last_modify, gen_file_upload_url
|
||||||
from seahub.utils.star import star_file, unstar_file
|
from seahub.utils.star import star_file, unstar_file
|
||||||
|
|
||||||
# Get an instance of a logger
|
# Get an instance of a logger
|
||||||
@ -1283,3 +1283,27 @@ def my_shared_and_group_repos(request):
|
|||||||
|
|
||||||
return HttpResponse(json.dumps({"shared": shared_repos_html, "group": group_repos_html}),
|
return HttpResponse(json.dumps({"shared": shared_repos_html, "group": group_repos_html}),
|
||||||
content_type=content_type)
|
content_type=content_type)
|
||||||
|
|
||||||
|
@login_required
|
||||||
|
def get_file_op_url(request, repo_id):
|
||||||
|
"""Get file upload/update url for AJAX.
|
||||||
|
"""
|
||||||
|
if not request.is_ajax():
|
||||||
|
raise Http404
|
||||||
|
|
||||||
|
content_type = 'application/json; charset=utf-8'
|
||||||
|
|
||||||
|
op_type = request.GET.get('op_type') # value can be 'upload', 'update', 'upload-blks', 'update-blks'
|
||||||
|
if not op_type:
|
||||||
|
err_msg = _(u'Argument missing')
|
||||||
|
return HttpResponse(json.dumps({"error": err_msg}), status=400,
|
||||||
|
content_type=content_type)
|
||||||
|
|
||||||
|
username = request.user.username
|
||||||
|
url = ''
|
||||||
|
if check_repo_access_permission(repo_id, request.user) == 'rw':
|
||||||
|
token = seafile_api.get_httpserver_access_token(repo_id, 'dummy',
|
||||||
|
op_type, username)
|
||||||
|
url = gen_file_upload_url(token, op_type + '-aj')
|
||||||
|
|
||||||
|
return HttpResponse(json.dumps({"url": url}), content_type=content_type)
|
||||||
|
@ -114,52 +114,6 @@ def get_upload_url(request, repo_id):
|
|||||||
# else:
|
# else:
|
||||||
# return ''
|
# return ''
|
||||||
|
|
||||||
def get_ajax_upload_url(request, repo_id):
|
|
||||||
"""Get file upload url for AJAX.
|
|
||||||
"""
|
|
||||||
username = request.user.username
|
|
||||||
if check_repo_access_permission(repo_id, request.user) == 'rw':
|
|
||||||
token = seafile_api.get_httpserver_access_token(repo_id, 'dummy',
|
|
||||||
'upload', username)
|
|
||||||
return gen_file_upload_url(token, 'upload-aj')
|
|
||||||
else:
|
|
||||||
return ''
|
|
||||||
|
|
||||||
def get_ajax_update_url(request, repo_id):
|
|
||||||
"""Get file upload url for AJAX.
|
|
||||||
"""
|
|
||||||
username = request.user.username
|
|
||||||
if check_repo_access_permission(repo_id, request.user) == 'rw':
|
|
||||||
token = seafile_api.get_httpserver_access_token(repo_id, 'dummy',
|
|
||||||
'update', username)
|
|
||||||
return gen_file_upload_url(token, 'update-aj')
|
|
||||||
else:
|
|
||||||
return ''
|
|
||||||
|
|
||||||
def get_blks_upload_url(request, repo_id):
|
|
||||||
'''
|
|
||||||
Get upload url for encrypted file (uploaded in blocks)
|
|
||||||
'''
|
|
||||||
username = request.user.username
|
|
||||||
if check_repo_access_permission(repo_id, request.user) == 'rw':
|
|
||||||
token = seafile_api.get_httpserver_access_token(repo_id, 'dummy',
|
|
||||||
'upload-blks', username)
|
|
||||||
return gen_file_upload_url(token, 'upload-blks-aj')
|
|
||||||
else:
|
|
||||||
return ''
|
|
||||||
|
|
||||||
def get_blks_update_url(request, repo_id):
|
|
||||||
'''
|
|
||||||
Get update url for encrypted file (uploaded in blocks)
|
|
||||||
'''
|
|
||||||
username = request.user.username
|
|
||||||
if check_repo_access_permission(repo_id, request.user) == 'rw':
|
|
||||||
token = seafile_api.get_httpserver_access_token(repo_id, 'dummy',
|
|
||||||
'update-blks', username)
|
|
||||||
return gen_file_upload_url(token, 'update-blks-aj')
|
|
||||||
else:
|
|
||||||
return ''
|
|
||||||
|
|
||||||
def get_fileshare(repo_id, username, path):
|
def get_fileshare(repo_id, username, path):
|
||||||
if path == '/': # no shared link for root dir
|
if path == '/': # no shared link for root dir
|
||||||
return None
|
return None
|
||||||
@ -264,12 +218,6 @@ def render_repo(request, repo):
|
|||||||
repo_group_str = ''
|
repo_group_str = ''
|
||||||
upload_url = get_upload_url(request, repo.id)
|
upload_url = get_upload_url(request, repo.id)
|
||||||
|
|
||||||
if repo.encrypted and repo.enc_version == 2 and not server_crypto:
|
|
||||||
ajax_upload_url = get_blks_upload_url(request, repo.id)
|
|
||||||
ajax_update_url = get_blks_update_url(request, repo.id)
|
|
||||||
else:
|
|
||||||
ajax_upload_url = get_ajax_upload_url(request, repo.id)
|
|
||||||
ajax_update_url = get_ajax_update_url(request, repo.id)
|
|
||||||
fileshare = get_fileshare(repo.id, username, path)
|
fileshare = get_fileshare(repo.id, username, path)
|
||||||
dir_shared_link = get_dir_share_link(fileshare)
|
dir_shared_link = get_dir_share_link(fileshare)
|
||||||
uploadlink = get_uploadlink(repo.id, username, path)
|
uploadlink = get_uploadlink(repo.id, username, path)
|
||||||
@ -295,8 +243,6 @@ def render_repo(request, repo):
|
|||||||
'no_quota': no_quota,
|
'no_quota': no_quota,
|
||||||
'max_upload_file_size': max_upload_file_size,
|
'max_upload_file_size': max_upload_file_size,
|
||||||
'upload_url': upload_url,
|
'upload_url': upload_url,
|
||||||
'ajax_upload_url': ajax_upload_url,
|
|
||||||
'ajax_update_url': ajax_update_url,
|
|
||||||
'httpserver_root': httpserver_root,
|
'httpserver_root': httpserver_root,
|
||||||
'protocol': protocol,
|
'protocol': protocol,
|
||||||
'domain': domain,
|
'domain': domain,
|
||||||
|
Loading…
Reference in New Issue
Block a user