Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make csrfName configurable; aware of response filePath #279

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions src/js/easymde.js
Original file line number Diff line number Diff line change
Expand Up @@ -811,16 +811,9 @@ function afterImageUploaded(editor, url) {
var stat = getState(cm);
var options = editor.options;
var imageName = url.substr(url.lastIndexOf('/') + 1);
var ext = imageName.substring(imageName.lastIndexOf('.') + 1);
// var ext = imageName.substring(imageName.lastIndexOf('.') + 1);

// Check if media is an image
if (['png', 'jpg', 'jpeg', 'gif', 'svg'].includes(ext)) {
_replaceSelection(cm, stat.image, options.insertTexts.uploadedImage, url);
} else {
var text_link = options.insertTexts.link;
text_link[0] = '[' + imageName;
_replaceSelection(cm, stat.link, text_link, url);
}
_replaceSelection(cm, stat.image, options.insertTexts.uploadedImage, url);

// show uploaded image filename for 1000ms
editor.updateStatusBar('upload-image', editor.options.imageTexts.sbOnUploaded.replace('#image_name#', imageName));
Expand Down Expand Up @@ -2328,7 +2321,8 @@ EasyMDE.prototype.uploadImage = function (file, onSuccess, onError) {

// insert CSRF token if provided in config.
if (self.options.imageCSRFToken) {
formData.append('csrfmiddlewaretoken', self.options.imageCSRFToken);
var csrfName = self.options.imageCSRFName || 'csrfmiddlewaretoken';
formData.append(csrfName, self.options.imageCSRFToken);
}
var request = new XMLHttpRequest();
request.upload.onprogress = function (event) {
Expand All @@ -2347,8 +2341,14 @@ EasyMDE.prototype.uploadImage = function (file, onSuccess, onError) {
onErrorSup(fillErrorMessage(self.options.errorMessages.importError));
return;
}
if (this.status === 200 && response && !response.error && response.data && response.data.filePath) {
onSuccess(window.location.origin + '/' + response.data.filePath);
if ((this.status === 200 || this.status === 201) && response && !response.error && response.data && response.data.filePath) {
var filePath = response.data.filePath;
if (/https?:\/\//i.test(filePath)) {
onSuccess(filePath);
} else {
onSuccess(window.location.origin + '/' + filePath);
}

} else {
if (response.error && response.error in self.options.errorMessages) { // preformatted error message
onErrorSup(fillErrorMessage(self.options.errorMessages[response.error]));
Expand Down