Merge branch 'add-list-of-authorized-extensions' of git://github.com/ZogStriP/discourse into ZogStriP-add-list-of-authorized-extensions

Conflicts:
	app/models/site_setting.rb
This commit is contained in:
Robin Ward
2013-07-03 11:18:42 -04:00
21 changed files with 69 additions and 53 deletions
@@ -158,7 +158,6 @@ Discourse.Utilities = {
}
},
/**
Validate a list of files to be uploaded
@@ -169,18 +168,19 @@ Discourse.Utilities = {
if (files) {
// can only upload one file at a time
if (files.length > 1) {
bootbox.alert(Em.String.i18n('post.errors.upload_too_many_images'));
bootbox.alert(Em.String.i18n('post.errors.too_many_uploads'));
return false;
} else if (files.length > 0) {
// check that the uploaded file is an image
// TODO: we should provide support for other types of file
if (files[0].type && files[0].type.indexOf('image/') !== 0) {
bootbox.alert(Em.String.i18n('post.errors.only_images_are_supported'));
var upload = files[0];
// check that the uploaded file is authorized
if (!Discourse.Utilities.isAuthorizedUpload(upload)) {
var extensions = Discourse.SiteSettings.authorized_extensions.replace(/\|/g, ", ");
bootbox.alert(Em.String.i18n('post.errors.upload_not_authorized', { authorized_extensions: extensions }));
return false;
}
// check file size
if (files[0].size && files[0].size > 0) {
var fileSizeInKB = files[0].size / 1024;
if (upload.size && upload.size > 0) {
var fileSizeInKB = upload.size / 1024;
if (fileSizeInKB > Discourse.SiteSettings.max_upload_size_kb) {
bootbox.alert(Em.String.i18n('post.errors.upload_too_large', { max_size_kb: Discourse.SiteSettings.max_upload_size_kb }));
return false;
@@ -192,6 +192,19 @@ Discourse.Utilities = {
}
// there has been an error
return false;
},
/**
Check the extension of the file against the list of authorized extensions
@method isAuthorizedUpload
@param {File} files The file we want to upload
**/
isAuthorizedUpload: function(file) {
var extensions = Discourse.SiteSettings.authorized_extensions;
if (!extensions) return false;
var regexp = new RegExp("\\.(" + extensions.replace(/\./g, "") + ")$", "i");
return file && file.name ? file.name.match(regexp) : false;
}
};
@@ -75,9 +75,9 @@
{{#if currentUser}}
<a href="#" {{action togglePreview}} class='toggle-preview'>{{{content.toggleText}}}</a>
<div id='draft-status'></div>
{{#if view.loadingImage}}
<div id="image-uploading">
{{i18n image_selector.uploading_image}} {{view.uploadProgress}}% <a id="cancel-image-upload">{{i18n cancel}}</a>
{{#if view.isUploading}}
<div id="file-uploading">
{{i18n image_selector.uploading_image}} {{view.uploadProgress}}% <a id="cancel-file-upload">{{i18n cancel}}</a>
</div>
{{/if}}
{{/if}}
@@ -249,20 +249,20 @@ Discourse.ComposerView = Discourse.View.extend({
$uploadTarget.on('fileuploadsubmit', function (e, data) {
var result = Discourse.Utilities.validateFilesForUpload(data.files);
// reset upload status when everything is ok
if (result) composerView.setProperties({ uploadProgress: 0, loadingImage: true });
if (result) composerView.setProperties({ uploadProgress: 0, isUploading: true });
return result;
});
// send - this event is triggered when the upload request is about to start
$uploadTarget.on('fileuploadsend', function (e, data) {
// hide the "image selector" modal
// hide the "file selector" modal
composerView.get('controller').send('closeModal');
// cf. https://github.com/blueimp/jQuery-File-Upload/wiki/API#how-to-cancel-an-upload
var jqXHR = data.xhr();
// need to wait for the link to show up in the DOM
Em.run.schedule('afterRender', function() {
// bind on the click event on the cancel link
$('#cancel-image-upload').on('click', function() {
$('#cancel-file-upload').on('click', function() {
// cancel the upload
// NOTE: this will trigger a 'fileuploadfail' event with status = 0
if (jqXHR) jqXHR.abort();
@@ -283,13 +283,13 @@ Discourse.ComposerView = Discourse.View.extend({
var upload = data.result;
var html = "<img src=\"" + upload.url + "\" width=\"" + upload.width + "\" height=\"" + upload.height + "\">";
composerView.addMarkdown(html);
composerView.set('loadingImage', false);
composerView.set('isUploading', false);
});
// fail
$uploadTarget.on('fileuploadfail', function (e, data) {
// hide upload status
composerView.set('loadingImage', false);
composerView.set('isUploading', false);
// deal with meaningful errors first
if (data.jqXHR) {
switch (data.jqXHR.status) {
@@ -299,9 +299,10 @@ Discourse.ComposerView = Discourse.View.extend({
case 413:
bootbox.alert(Em.String.i18n('post.errors.upload_too_large', {max_size_kb: Discourse.SiteSettings.max_upload_size_kb}));
return;
// 415 == media type not recognized (ie. not an image)
// 415 == media type not authorized
case 415:
bootbox.alert(Em.String.i18n('post.errors.only_images_are_supported'));
var extensions = Discourse.SiteSettings.authorized_extensions.replace(/\|/g, ", ");
bootbox.alert(Em.String.i18n('post.errors.upload_not_authorized', { authorized_extensions: extensions }));
return;
// 422 == there has been an error on the server (mostly due to FastImage)
case 422: