Skip to content

Commit

Permalink
Merge branch 'Test'
Browse files Browse the repository at this point in the history
  • Loading branch information
Pooya-Oladazimi committed Jan 13, 2023
2 parents 42e9cff + 83831f6 commit 13f8463
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 11 deletions.
17 changes: 16 additions & 1 deletion ckanext/multiuploader/controllers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# encoding: utf-8

from flask import request, url_for
from flask import request
import ckan.lib.helpers as h
from ckanext.multiuploader.lib import Helper
import ckan.plugins.toolkit as toolkit
Expand Down Expand Up @@ -39,11 +39,26 @@ def upload_resources():
return toolkit.abort(403, "You need to authenticate before accessing this function" )




def delete_uploaded_resources():
package_name = request.form.get('pck_id')
if toolkit.g.user:
package = toolkit.get_action('package_show')({}, {'name_or_id': package_name})
package['resources'] = []
toolkit.get_action('package_update')({},package)
return "True"
else:
return toolkit.abort(403, "You need to authenticate before accessing this function" )



def cancel_dataset_plugin_is_enabled():
if Helper.check_plugin_enabled('cancel_dataset_creation'):
return True
return False


def get_upload_limit():
max_size = toolkit.config.get("ckan.max_resource_size")
if not max_size:
Expand Down
7 changes: 7 additions & 0 deletions ckanext/multiuploader/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ def get_blueprint(self):
UploadController.upload_resources,
methods=['POST']
)

blueprint.add_url_rule(
u'/multiuploader/delete_uploaded_resources',
u'delete_uploaded_resources',
UploadController.delete_uploaded_resources,
methods=['POST']
)

return blueprint

Expand Down
8 changes: 8 additions & 0 deletions ckanext/multiuploader/public/statics/multi_uploader.css
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,11 @@
display: none;
}

#cancel_waiting{
display: none;
}

#upload-cancel-sign{
font-size: 20px;

}
66 changes: 56 additions & 10 deletions ckanext/multiuploader/public/statics/multi_uploader.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* Author: p.oladazimi
*/

var uploadReqs = [];
var fileList = [];
var dest_url = $('#dest_url').val();
var test_test = "";
Expand Down Expand Up @@ -144,6 +145,11 @@ $(document).ready(function(){
return 0;
}
if(fileValidity()){
$('#cancel_waiting').hide();
$('.modal-title').show();
$('#upload-cancel').show();
$('#progress-bar-container').show();
$('#upload-error-container').hide();
$('#file-danger-size').hide();
$('#progress-modal').modal({
backdrop: 'static',
Expand Down Expand Up @@ -180,6 +186,14 @@ $(document).ready(function(){
});


/**
* Cancel an ongoing upload
*/
$('#upload-cancel').click(function(){
cancelAlreadyUploaded();
});


});


Expand Down Expand Up @@ -215,37 +229,38 @@ function checkFileSizes(){
*/
function uploadFiles(file, action, Max){
var formdata = new FormData();
let reqUpload = new XMLHttpRequest();
uploadReqs.push(reqUpload);
formdata.set('files', file);
formdata.set('isLink', 0);
formdata.set('pck_id', $('#pck_id').val());
formdata.set('save', action);
formdata.set('id', $('#id').val());
formdata.set('description', $('#field-description').val());
var req = new XMLHttpRequest();
formdata.set('description', $('#field-description').val());
var oldProgress = 0;
req.upload.addEventListener('progress', function(e){
reqUpload.upload.addEventListener('progress', function(e){
let progress = (Math.ceil(e.loaded/(e.total * 1.1) * 100) / Max);
uploadPercent += (progress - oldProgress)
updateProgressBar(uploadPercent);
oldProgress = progress
}, false);
req.onreadystatechange = function() {
if (req.readyState == XMLHttpRequest.DONE && req.status === 200) {
already_uploaded_count += 1;
reqUpload.onreadystatechange = function() {
if (reqUpload.readyState == XMLHttpRequest.DONE && reqUpload.status === 200) {
already_uploaded_count += 1;
if (already_uploaded_count === Max){
updateProgressBar(100);
window.location.replace(this.responseText);
window.location.replace(this.responseText);
}

}
else if (req.readyState == XMLHttpRequest.DONE && req.status !== 200){
else if (reqUpload.readyState == XMLHttpRequest.DONE && reqUpload.status !== 200){
$('#progress-bar-container').hide();
$('#upload-error-container').show();
$('#upload-progress-modal-close').show();
}
}
req.open("POST", dest_url)
req.send(formdata)
reqUpload.open("POST", dest_url)
reqUpload.send(formdata)
return 0;
}

Expand Down Expand Up @@ -273,6 +288,37 @@ function uploadLink(action){
return 0;
}

/**
* Cancel uploaded files
*/
function cancelAlreadyUploaded(){
$('#cancel_waiting').show();
$('#progress-bar-container').hide();
$('.modal-title').hide();
for(let i=0; i<uploadReqs.length; i++){
uploadReqs[i].abort();
$('#upload-error-container').hide();
$('#upload-progress-modal-close').hide();
$('#upload-cancel').hide();
}
already_uploaded_count = 0;
uploadPercent = 0;
var formdata = new FormData();
let dest_url = $('#cancel_upload_url').val();
formdata.set('pck_id', $('#pck_id').val());
var req = new XMLHttpRequest();
req.onreadystatechange = function() {
if (req.readyState == XMLHttpRequest.DONE && req.status === 200) {
$('#progress-modal').modal('hide');
}
}
req.open("POST", dest_url)
req.send(formdata)
return 1;
}



/**
*
* when click the previous button (deprecated)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
{% set data = data or {} %}
{% set errors = errors or {} %}
{% set action = h.url_for('multiuploader.upload_resources', _external=True) %}
{% set cancelUrl = h.url_for('multiuploader.delete_uploaded_resources', _external=True) %}
<input type="hidden" value="{{action}}" id="dest_url" >
<input type="hidden" value="{{cancelUrl}}" id="cancel_upload_url" >

<form id="resource-edit" class="dataset-form dataset-resource-form" method="post" action="" data-module="basic-form resource-form" enctype="multipart/form-data">

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,14 @@ <h5 class="modal-title">{{_('Upload in Progress')}}</h5>
{{_("Upload failed! Please try later.")}}
</p>
</div>
<div class="row cancel-waiting" id="cancel_waiting">
<div class="col-sm-12 text-center" id="upload-cancel-sign">
<i class="fa fa-spinner fa-spin"></i>{{_(' Canceling. Please Wait.')}}
</div>
</div>
</div>
<div class="modal-footer">
<button id="upload-cancel" type="button" class="btn btn-danger">Cancel This Upload</button>
<button id="upload-progress-modal-close" type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
</div>
</div>
Expand Down

0 comments on commit 13f8463

Please sign in to comment.