2017-11-23 15:00:05 +01:00
|
|
|
var upload = (function () {
|
|
|
|
|
|
|
|
var exports = {};
|
|
|
|
|
|
|
|
function make_upload_absolute(uri) {
|
|
|
|
if (uri.indexOf(compose.uploads_path) === 0) {
|
|
|
|
// Rewrite the URI to a usable link
|
|
|
|
return compose.uploads_domain + uri;
|
|
|
|
}
|
|
|
|
return uri;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This function resets an input type="file". Pass in the
|
|
|
|
// jquery object.
|
|
|
|
function clear_out_file_list(jq_file_list) {
|
|
|
|
if (compose.clone_file_input !== undefined) {
|
|
|
|
jq_file_list.replaceWith(compose.clone_file_input.clone(true));
|
|
|
|
}
|
|
|
|
// Hack explanation:
|
|
|
|
// IE won't let you do this (untested, but so says StackOverflow):
|
|
|
|
// $("#file_input").val("");
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.uploadStarted = function () {
|
|
|
|
$("#compose-send-button").attr("disabled", "");
|
2017-11-26 19:58:36 +01:00
|
|
|
$("#compose-send-status").addClass("alert-info").show();
|
2017-11-26 20:01:37 +01:00
|
|
|
$(".compose-send-status-close").one('click', compose.abort_xhr);
|
2017-11-26 20:03:46 +01:00
|
|
|
$("#compose-error-msg").html($("<p>").text(i18n.t("Uploading…"))
|
2017-11-23 15:00:05 +01:00
|
|
|
.after('<div class="progress progress-striped active">' +
|
2017-11-26 20:05:18 +01:00
|
|
|
'<div class="bar" id="compose-upload-bar" style="width: 00%;"></div>' +
|
2017-11-23 15:00:05 +01:00
|
|
|
'</div>'));
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.progressUpdated = function (i, file, progress) {
|
2017-11-26 20:05:18 +01:00
|
|
|
$("#compose-upload-bar").width(progress + "%");
|
2017-11-23 15:00:05 +01:00
|
|
|
};
|
|
|
|
|
2018-01-27 03:50:37 +01:00
|
|
|
exports.uploadError = function (error_code, server_response, file) {
|
2017-11-23 15:00:05 +01:00
|
|
|
var msg;
|
2017-11-26 19:58:36 +01:00
|
|
|
$("#compose-send-status").addClass("alert-error")
|
2017-11-23 15:00:05 +01:00
|
|
|
.removeClass("alert-info");
|
|
|
|
$("#compose-send-button").prop("disabled", false);
|
2018-01-27 03:50:37 +01:00
|
|
|
switch (error_code) {
|
2017-11-23 15:00:05 +01:00
|
|
|
case 'BrowserNotSupported':
|
|
|
|
msg = i18n.t("File upload is not yet available for your browser.");
|
|
|
|
break;
|
|
|
|
case 'TooManyFiles':
|
|
|
|
msg = i18n.t("Unable to upload that many files at once.");
|
|
|
|
break;
|
|
|
|
case 'FileTooLarge':
|
|
|
|
// sanitization not needed as the file name is not potentially parsed as HTML, etc.
|
|
|
|
var context = {
|
|
|
|
file_name: file.name,
|
|
|
|
};
|
|
|
|
msg = i18n.t('"__file_name__" was too large; the maximum file size is 25MiB.', context);
|
|
|
|
break;
|
2018-01-27 03:50:37 +01:00
|
|
|
case 413: // HTTP status "Request Entity Too Large"
|
2017-11-23 15:00:05 +01:00
|
|
|
msg = i18n.t("Sorry, the file was too large.");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
msg = i18n.t("An unknown error occurred.");
|
|
|
|
break;
|
|
|
|
}
|
2017-11-26 20:03:46 +01:00
|
|
|
$("#compose-error-msg").text(msg);
|
2017-11-23 15:00:05 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
exports.uploadFinished = function (i, file, response) {
|
|
|
|
if (response.uri === undefined) {
|
|
|
|
return;
|
|
|
|
}
|
2017-11-26 20:37:44 +01:00
|
|
|
var textbox = $("#compose-textarea");
|
2017-11-23 15:00:05 +01:00
|
|
|
var split_uri = response.uri.split("/");
|
|
|
|
var filename = split_uri[split_uri.length - 1];
|
|
|
|
// Urgh, yet another hack to make sure we're "composing"
|
|
|
|
// when text gets added into the composebox.
|
|
|
|
if (!compose_state.composing()) {
|
|
|
|
compose_actions.start('stream');
|
|
|
|
}
|
|
|
|
|
|
|
|
var uri = make_upload_absolute(response.uri);
|
|
|
|
|
|
|
|
if (i === -1) {
|
|
|
|
// This is a paste, so there's no filename. Show the image directly
|
|
|
|
textbox.val(textbox.val() + "[pasted image](" + uri + ") ");
|
|
|
|
} else {
|
|
|
|
// This is a dropped file, so make the filename a link to the image
|
|
|
|
textbox.val(textbox.val() + "[" + filename + "](" + uri + ")" + " ");
|
|
|
|
}
|
|
|
|
compose_ui.autosize_textarea();
|
|
|
|
$("#compose-send-button").prop("disabled", false);
|
2017-11-26 19:58:36 +01:00
|
|
|
$("#compose-send-status").removeClass("alert-info")
|
2017-11-23 15:00:05 +01:00
|
|
|
.hide();
|
|
|
|
|
|
|
|
// In order to upload the same file twice in a row, we need to clear out
|
|
|
|
// the #file_input element, so that the next time we use the file dialog,
|
|
|
|
// an actual change event is fired. This is extracted to a function
|
|
|
|
// to abstract away some IE hacks.
|
|
|
|
clear_out_file_list($("#file_input"));
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.initialize = function () {
|
|
|
|
$("#compose").filedrop({
|
|
|
|
url: "/json/user_uploads",
|
|
|
|
fallback_id: "file_input",
|
|
|
|
paramname: "file",
|
|
|
|
maxfilesize: page_params.maxfilesize,
|
|
|
|
data: {
|
|
|
|
// the token isn't automatically included in filedrop's post
|
|
|
|
csrfmiddlewaretoken: csrf_token,
|
|
|
|
},
|
|
|
|
raw_droppable: ['text/uri-list', 'text/plain'],
|
|
|
|
drop: exports.uploadStarted,
|
|
|
|
progressUpdated: exports.progressUpdated,
|
|
|
|
error: exports.uploadError,
|
|
|
|
uploadFinished: exports.uploadFinished,
|
|
|
|
rawDrop: function (contents) {
|
2017-11-26 20:37:44 +01:00
|
|
|
var textbox = $("#compose-textarea");
|
2017-11-23 15:00:05 +01:00
|
|
|
if (!compose_state.composing()) {
|
|
|
|
compose_actions.start('stream');
|
|
|
|
}
|
|
|
|
textbox.val(textbox.val() + contents);
|
|
|
|
compose_ui.autosize_textarea();
|
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
return exports;
|
|
|
|
}());
|
|
|
|
|
|
|
|
if (typeof module !== 'undefined') {
|
|
|
|
module.exports = upload;
|
|
|
|
}
|