Browse Source

Merge pull request #1430 from tar672/s3-compatable-assetmanager

S3 compatible asset manager
pull/1446/head
Artur Arseniev 8 years ago
committed by GitHub
parent
commit
bcb3fc959e
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 7
      docs/modules/Assets.md
  2. 7
      src/asset_manager/config/config.js
  3. 26
      src/asset_manager/view/FileUploader.js
  4. 12
      test/specs/asset_manager/view/FileUploader.js

7
docs/modules/Assets.md

@ -63,6 +63,13 @@ Below the list of currently available options
// Custom parameters to pass with the upload request, eg. csrf token // Custom parameters to pass with the upload request, eg. csrf token
params: {}, params: {},
// The credentials setting for the upload request, eg. 'include', 'omit'
credentials: 'include',
// Allow uploading multiple files per request.
// If disabled filename will not have '[]' appended
multiUpload: true,
// If true, tries to add automatically uploaded assets. // If true, tries to add automatically uploaded assets.
// To make it work the server should respond with a JSON containing assets // To make it work the server should respond with a JSON containing assets
// in a data key, eg: // in a data key, eg:

7
src/asset_manager/config/config.js

@ -29,6 +29,13 @@ module.exports = {
// Custom parameters to pass with the upload request, eg. csrf token // Custom parameters to pass with the upload request, eg. csrf token
params: {}, params: {},
// The credentials setting for the upload request, eg. 'include', 'omit'
credentials: 'include',
// Allow uploading multiple files per request.
// If disabled filename will not have '[]' appended
multiUpload: true,
// If true, tries to add automatically uploaded assets. // If true, tries to add automatically uploaded assets.
// To make it work the server should respond with a JSON containing assets // To make it work the server should respond with a JSON containing assets
// in a data key, eg: // in a data key, eg:

26
src/asset_manager/view/FileUploader.js

@ -7,7 +7,7 @@ module.exports = Backbone.View.extend(
template: _.template(` template: _.template(`
<form> <form>
<div id="<%= pfx %>title"><%= title %></div> <div id="<%= pfx %>title"><%= title %></div>
<input type="file" id="<%= uploadId %>" name="file" accept="*/*" <%= disabled ? 'disabled' : '' %> multiple/> <input type="file" id="<%= uploadId %>" name="file" accept="*/*" <%= disabled ? 'disabled' : '' %> <%= multiUpload ? 'multiple' : '' %>/>
<div style="clear:both;"></div> <div style="clear:both;"></div>
</form> </form>
`), `),
@ -26,6 +26,7 @@ module.exports = Backbone.View.extend(
c.disableUpload !== undefined c.disableUpload !== undefined
? c.disableUpload ? c.disableUpload
: !c.upload && !c.embedAsBase64; : !c.upload && !c.embedAsBase64;
this.multiUpload = c.multiUpload !== undefined ? c.multiUpload : true;
this.events['change #' + this.uploadId] = 'uploadFile'; this.events['change #' + this.uploadId] = 'uploadFile';
let uploadFile = c.uploadFile; let uploadFile = c.uploadFile;
@ -78,7 +79,13 @@ module.exports = Backbone.View.extend(
const em = this.config.em; const em = this.config.em;
const config = this.config; const config = this.config;
const target = this.target; const target = this.target;
const json = typeof text === 'string' ? JSON.parse(text) : text; let json;
try {
json = typeof text === 'string' ? JSON.parse(text) : text;
} catch (e) {
json = text;
}
em && em.trigger('asset:upload:response', json); em && em.trigger('asset:upload:response', json);
if (config.autoAdd && target) { if (config.autoAdd && target) {
@ -101,14 +108,18 @@ module.exports = Backbone.View.extend(
const config = this.config; const config = this.config;
const params = config.params; const params = config.params;
for (let i = 0; i < files.length; i++) {
body.append(`${config.uploadName}[]`, files[i]);
}
for (let param in params) { for (let param in params) {
body.append(param, params[param]); body.append(param, params[param]);
} }
if (this.multiUpload) {
for (let i = 0; i < files.length; i++) {
body.append(`${config.uploadName}[]`, files[i]);
}
} else if (files.length) {
body.append(config.uploadName, files[0]);
}
var target = this.target; var target = this.target;
const url = config.upload; const url = config.upload;
const headers = config.headers; const headers = config.headers;
@ -122,7 +133,7 @@ module.exports = Backbone.View.extend(
this.onUploadStart(); this.onUploadStart();
return fetch(url, { return fetch(url, {
method: 'post', method: 'post',
credentials: 'include', credentials: config.credentials || 'include',
headers, headers,
body body
}) })
@ -229,6 +240,7 @@ module.exports = Backbone.View.extend(
title: this.config.uploadText, title: this.config.uploadText,
uploadId: this.uploadId, uploadId: this.uploadId,
disabled: this.disabled, disabled: this.disabled,
multiUpload: this.multiUpload,
pfx: this.pfx pfx: this.pfx
}) })
); );

12
test/specs/asset_manager/view/FileUploader.js

@ -67,6 +67,18 @@ module.exports = {
); );
}); });
test('Handles multiUpload false', () => {
var view = new FileUploader({
config: {
multiUpload: false
}
});
view.render();
expect(
view.$el.find('input[type=file]').prop('multiple')
).toBeFalsy();
});
test('Handles embedAsBase64 parameter', () => { test('Handles embedAsBase64 parameter', () => {
var view = new FileUploader({ var view = new FileUploader({
config: { config: {

Loading…
Cancel
Save