Skip to content

Commit

Permalink
fix single_file allowed types option
Browse files Browse the repository at this point in the history
  • Loading branch information
wellingguzman committed Oct 27, 2016
1 parent dbb8eaa commit fc90a7b
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 20 deletions.
2 changes: 1 addition & 1 deletion app/core/uis/multiple_files.js
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ define([
_.each(e.dataTransfer.files, function(file) {
// force a fileModel object
var fileModel = new FilesModel({}, {collection:{}});
fileModel.setFile(file, function(item) {
fileModel.setFile(file, null, function(item) {
item[app.statusMapping.status_name] = app.statusMapping.active_num;
// Unset the model ID so that a new file record is created
// (and the old file record isn't replaced w/ this data)
Expand Down
19 changes: 10 additions & 9 deletions app/core/uis/single_file.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,13 +175,13 @@ define([
'click #retriveUrlBtn': function(e) {
var url = this.$el.find('#urlInput').val();
var model = this.fileModel;
model.setLink(url);
model.setLink(url, this.options.settings.get('allowed_filetypes'));
},
'change input[type=file]': function(event) {
var target = $(event.target);
var file = target[0].files[0];
var model = this.fileModel;
model.setFile(file);
model.setFile(file, this.options.settings.get('allowed_filetypes'));
},
'click button[data-action="computer"], .ui-thumbnail-dropzone, .single-image-thumbnail img': function(e) {
this.$el.find('#fileAddInput').click();
Expand All @@ -200,7 +200,7 @@ define([
}

var model = this.fileModel;
model.setLink(url);
model.setLink(url, this.options.settings.get('allowed_filetypes'));
},

removeFile: function(e) {
Expand All @@ -218,16 +218,17 @@ define([
collection.fetch();

//please proxy this instead
var me = this;
var self = this;

view.itemClicked = function(e) {
var id = $(e.target).closest('tr').attr('data-id');
model = collection.get(id);
if (!app.settings.isFileAllowed(model)) {
return false;

if (model.isFileAllowed(self.options.settings.get('allowed_filetypes'))) {
fileModel.clear({silent: true});
fileModel.set(_.clone(model.attributes));
}
fileModel.clear({silent: true});
fileModel.set(_.clone(model.attributes));

app.router.removeOverlayPage(this);
};
},
Expand Down Expand Up @@ -293,7 +294,7 @@ define([
}

var file = e.dataTransfer.files[0];
model.setFile(file);
model.setFile(file, this.options.settings.get('allowed_filetypes'));
$dropzone.removeClass('dragover');
}, this);

Expand Down
58 changes: 48 additions & 10 deletions app/modules/files/FilesModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ define([
'core/entries/EntriesModel',
'core/notification',
'core/t',
'utils',
'helpers/file'
],

function(app, _, Backbone, EntriesModel, Notification, __t, File) {
function(app, _, Backbone, EntriesModel, Notification, __t, Utils, File) {
var FilesModel = EntriesModel.extend({

initialize: function() {
Expand Down Expand Up @@ -45,10 +46,8 @@ function(app, _, Backbone, EntriesModel, Notification, __t, File) {
return name;
},

setFile: function(file, fn) {
var model = this;

if (!app.settings.isFileAllowed(file)) {
setFile: function(file, allowedMimeTypes, fn) {
if (!this.isFileAllowed(file.type, allowedMimeTypes)) {
return false;
}

Expand All @@ -61,6 +60,7 @@ function(app, _, Backbone, EntriesModel, Notification, __t, File) {
return false;
}

var model = this;
File.getDataFromInput(file, function(fileData, details, file) {
File.isImage(fileData, function(isImage) {
var modelData = {
Expand Down Expand Up @@ -100,10 +100,10 @@ function(app, _, Backbone, EntriesModel, Notification, __t, File) {

// setData will try to get thumbnail from a base64
// this is used by retrieve links
setData: function(item) {
setData: function(item, allowedMimeTypes) {
var model = this;

if (!app.settings.isFileAllowed(item)) {
if (!this.isMimeTypeAllowed(item.type, allowedMimeTypes)) {
return false;
}

Expand All @@ -114,20 +114,58 @@ function(app, _, Backbone, EntriesModel, Notification, __t, File) {
});
},

setLink: function(url) {
setLink: function(url, allowedMimeTypes) {
var model = this;
app.sendLink(url, function(data) {
var item = data[0];
item[app.statusMapping.status_name] = app.statusMapping.active_num;
// Unset the model ID so that a new file record is created
// (and the old file record isn't replaced w/ this data)
item.id = undefined;
item.user = self.userId;
item.user = model.userId;

model.setData(item);
model.setData(item, allowedMimeTypes);
});
},

isFileAllowed: function(allowedMimeTypes) {
return this.isMimeTypeAllowed(this.get('type'), allowedMimeTypes);
},

isMimeTypeAllowed: function(fileType, allowedMimeTypes) {
// if there's not fileType but allowedMimeTypes provided
// by default the file is not allowed
var allowed = (!fileType && allowedMimeTypes) ? false : true;

if (fileType && allowedMimeTypes) {
var self = this;
allowed = allowedMimeTypes.split(',').some(function (allowedType) {
return self.isMimeType(fileType, allowedType);
});
}

// this should not be here
// but, we will let it slide for now.
if (!allowed) {
app.router.openModal({type: 'alert', text: 'This type of file is not allowed'});
}

return allowed;
},

isMimeType: function(mimeType, allowedMimeType) {
mimeType = mimeType || this.type;
if (!_.isString(mimeType)) {
return false;
}

if (allowedMimeType.endsWith('/*')) {
return allowedMimeType.split('/')[0] === mimeType.split('/')[0];
}

return allowedMimeType === mimeType;
},

constructor: function FilesModel(data, options) {
FilesModel.__super__.constructor.call(this, data, options);
}
Expand Down

0 comments on commit fc90a7b

Please sign in to comment.