Only use ajax for file upload. Prepare for handling of duplicate error.

This commit is contained in:
Bart Kamphorst
2018-11-02 14:38:12 +01:00
parent eade94dcfd
commit 6857995442
3 changed files with 26 additions and 10 deletions
+3 -7
View File
@@ -180,14 +180,11 @@ module Precious
end
end
# AJAX calls only
post '/upload_file' do
wiki = wiki_new
unless wiki.allow_uploads
@message = "File uploads are disabled"
mustache :error
return
end
halt 405 unless wiki.allow_uploads
if params[:file]
fullname = params[:file][:filename]
@@ -224,8 +221,7 @@ module Precious
committer.commit
redirect to(request.referer)
rescue Gollum::DuplicatePageError => e
@message = "Duplicate page: #{e.message}"
mustache :error
halt 409 # Signal conflict
end
end
@@ -228,8 +228,8 @@
}
window.ace_editor.insert(text);
},
error: function(r, textStatus) {
alert('Error uploading file: ' + textStatus);
error: function(r, textStatus, errorThrown) {
alert('Error uploading file: ' + textStatus + ' ' + errorThrown);
$editorBody.removeClass('uploading');
}
});
@@ -158,6 +158,7 @@ $(document).ready(function() {
}
};
// ua detection
if ($.browser.mozilla) {
$('body').addClass('ff');
@@ -187,7 +188,26 @@ $(document).ready(function() {
}
],
OK: function( res ) {
$('#upload').submit();
var formData = new FormData($('#upload').get(0));
var endpoint = $('#upload').attr("action");
$.ajax({
url: endpoint,
type: 'POST',
data: formData,
processData: false,
contentType: false,
success: function(data) {
// File successfully uploaded
},
error: function(data, textStatus, errorThrown) {
if (data.status == 409) {
alert('This file already exists.');
} else {
alert('Error uploading file: ' + textStatus + ' ' + errorThrown);
}
}
});
}
});
});