mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
Merge pull request #1199 from ZogStriP/uploads
adds the `max_attachment_size_kb` setting
This commit is contained in:
@@ -88,9 +88,8 @@ Discourse.Utilities = {
|
||||
},
|
||||
|
||||
emailValid: function(email) {
|
||||
// see: http://stackoverflow.com/questions/46155/validate-email-address-in-javascript
|
||||
var re;
|
||||
re = /^[a-zA-Z0-9!#$%&'*+\/=?\^_`{|}~\-]+(?:\.[a-zA-Z0-9!#$%&'\*+\/=?\^_`{|}~\-]+)*@(?:[a-zA-Z0-9](?:[a-zA-Z0-9\-]*[a-zA-Z0-9])?\.)+[a-zA-Z0-9](?:[a-zA-Z0-9\-]*[a-zA-Z0-9])?$/;
|
||||
// see: http://stackoverflow.com/questions/46155/validate-email-address-in-javascript
|
||||
var re = /^[a-zA-Z0-9!#$%&'*+\/=?\^_`{|}~\-]+(?:\.[a-zA-Z0-9!#$%&'\*+\/=?\^_`{|}~\-]+)*@(?:[a-zA-Z0-9](?:[a-zA-Z0-9\-]*[a-zA-Z0-9])?\.)+[a-zA-Z0-9](?:[a-zA-Z0-9\-]*[a-zA-Z0-9])?$/;
|
||||
return re.test(email);
|
||||
},
|
||||
|
||||
@@ -165,40 +164,35 @@ Discourse.Utilities = {
|
||||
@param {Array} files The list of files we want to upload
|
||||
**/
|
||||
validateFilesForUpload: function(files) {
|
||||
if (files) {
|
||||
// can only upload one file at a time
|
||||
if (files.length > 1) {
|
||||
bootbox.alert(I18n.t('post.errors.too_many_uploads'));
|
||||
return false;
|
||||
} else if (files.length > 0) {
|
||||
var upload = files[0];
|
||||
// ensures that new users can upload image
|
||||
if (Discourse.User.current('trust_level') === 0 && Discourse.SiteSettings.newuser_max_images === 0) {
|
||||
bootbox.alert(I18n.t('post.errors.upload_not_allowed_for_new_user'));
|
||||
return false;
|
||||
}
|
||||
// if the image was pasted, sets its name to a default one
|
||||
if (upload instanceof Blob && !(upload instanceof File) && upload.type === "image/png") { upload.name = "blob.png"; }
|
||||
// check that the uploaded file is authorized
|
||||
if (!Discourse.Utilities.isAuthorizedUpload(upload)) {
|
||||
var extensions = Discourse.SiteSettings.authorized_extensions.replace(/\|/g, ", ");
|
||||
bootbox.alert(I18n.t('post.errors.upload_not_authorized', { authorized_extensions: extensions }));
|
||||
return false;
|
||||
}
|
||||
// check file size
|
||||
if (upload.size && upload.size > 0) {
|
||||
var fileSizeInKB = upload.size / 1024;
|
||||
if (fileSizeInKB > Discourse.SiteSettings.max_upload_size_kb) {
|
||||
bootbox.alert(I18n.t('post.errors.upload_too_large', { max_size_kb: Discourse.SiteSettings.max_upload_size_kb }));
|
||||
return false;
|
||||
}
|
||||
// everything is fine
|
||||
return true;
|
||||
}
|
||||
}
|
||||
if (!files || files.length === 0) { return false; }
|
||||
// can only upload one file at a time
|
||||
if (files.length > 1) {
|
||||
bootbox.alert(I18n.t('post.errors.too_many_uploads'));
|
||||
return false;
|
||||
}
|
||||
// there has been an error
|
||||
return false;
|
||||
var upload = files[0];
|
||||
// ensures that new users can upload image
|
||||
if (Discourse.User.current('trust_level') === 0 && Discourse.SiteSettings.newuser_max_images === 0) {
|
||||
bootbox.alert(I18n.t('post.errors.upload_not_allowed_for_new_user'));
|
||||
return false;
|
||||
}
|
||||
// if the image was pasted, sets its name to a default one
|
||||
if (upload instanceof Blob && !(upload instanceof File) && upload.type === "image/png") { upload.name = "blob.png"; }
|
||||
// check that the uploaded file is authorized
|
||||
if (!Discourse.Utilities.isAuthorizedUpload(upload)) {
|
||||
var extensions = Discourse.SiteSettings.authorized_extensions.replace(/\|/g, ", ");
|
||||
bootbox.alert(I18n.t('post.errors.upload_not_authorized', { authorized_extensions: extensions }));
|
||||
return false;
|
||||
}
|
||||
// check file size
|
||||
var fileSizeKB = upload.size / 1024;
|
||||
var maxSizeKB = Discourse.Utilities.maxUploadSizeInKB(upload.name);
|
||||
if (fileSizeKB > maxSizeKB) {
|
||||
bootbox.alert(I18n.t('post.errors.upload_too_large', { max_size_kb: maxSizeKB }));
|
||||
return false;
|
||||
}
|
||||
// everything went fine
|
||||
return true;
|
||||
},
|
||||
|
||||
/**
|
||||
@@ -209,8 +203,7 @@ Discourse.Utilities = {
|
||||
**/
|
||||
isAuthorizedUpload: function(file) {
|
||||
var extensions = Discourse.SiteSettings.authorized_extensions;
|
||||
if (!extensions) return false;
|
||||
var regexp = new RegExp("\\.(" + extensions.replace(/\./g, "") + ")$", "i");
|
||||
var regexp = new RegExp("(" + extensions + ")$", "i");
|
||||
return file && file.name ? file.name.match(regexp) : false;
|
||||
},
|
||||
|
||||
@@ -221,7 +214,7 @@ Discourse.Utilities = {
|
||||
@param {Upload} upload The upload we want the markdown from
|
||||
**/
|
||||
getUploadMarkdown: function(upload) {
|
||||
if (this.isAnImage(upload.original_filename)) {
|
||||
if (Discourse.Utilities.isAnImage(upload.original_filename)) {
|
||||
return '<img src="' + upload.url + '" width="' + upload.width + '" height="' + upload.height + '">';
|
||||
} else {
|
||||
return '<a class="attachment" href="' + upload.url + '">' + upload.original_filename + '</a><span class="size">(' + I18n.toHumanSize(upload.filesize) + ')</span>';
|
||||
@@ -235,7 +228,17 @@ Discourse.Utilities = {
|
||||
@param {String} path The path
|
||||
**/
|
||||
isAnImage: function(path) {
|
||||
return path && path.match(/\.(png|jpg|jpeg|gif|bmp|tif)$/i);
|
||||
return path && path.match(/\.(png|jpg|jpeg|gif|bmp|tif|tiff)$/i);
|
||||
},
|
||||
|
||||
/**
|
||||
Retrieve max upload size in KB depending on the file is an image or not
|
||||
|
||||
@method maxUploadSizeInKB
|
||||
@param {String} path The path
|
||||
**/
|
||||
maxUploadSizeInKB: function(path) {
|
||||
return Discourse.Utilities.isAnImage(path) ? Discourse.SiteSettings.max_image_size_kb : Discourse.SiteSettings.max_attachment_size_kb;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
@@ -302,13 +302,11 @@ Discourse.ComposerView = Discourse.View.extend({
|
||||
case 0: return;
|
||||
// 413 == entity too large, returned usually from nginx
|
||||
case 413:
|
||||
bootbox.alert(I18n.t('post.errors.upload_too_large', {max_size_kb: Discourse.SiteSettings.max_upload_size_kb}));
|
||||
var maxSizeKB = Discourse.Utilities.maxUploadSizeInKB(data.files[0].name);
|
||||
bootbox.alert(I18n.t('post.errors.upload_too_large', { max_size_kb: maxSizeKB }));
|
||||
return;
|
||||
// 415 == media type not authorized
|
||||
case 415:
|
||||
var extensions = Discourse.SiteSettings.authorized_extensions.replace(/\|/g, ", ");
|
||||
bootbox.alert(I18n.t('post.errors.upload_not_authorized', { authorized_extensions: extensions }));
|
||||
return;
|
||||
// 422 == there has been an error on the server (mostly due to FastImage)
|
||||
case 422:
|
||||
bootbox.alert(data.jqXHR.responseText);
|
||||
|
||||
Reference in New Issue
Block a user