Skip to content
This repository has been archived by the owner on Nov 28, 2022. It is now read-only.

Commit

Permalink
Limited simultaneous upload requests (#890)
Browse files Browse the repository at this point in the history
closes TryGhost/Ghost#9120

- use `ember-concurrency` to enqueue uploads in `{{gh-uploader}}
- set simultaneous upload limit to 2
  • Loading branch information
kevinansfield authored and aileen committed Oct 9, 2017
1 parent 2c786a5 commit 0ef0af5
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions app/components/gh-uploader.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import {run} from '@ember/runloop';
// "allowMultiple" attribute so that single-image uploads don't allow multiple
// simultaneous uploads

const MAX_SIMULTANEOUS_UPLOADS = 2;

/**
* Result from a file upload
* @typedef {Object} UploadResult
Expand Down Expand Up @@ -166,7 +168,11 @@ export default Component.extend({
// NOTE: for...of loop results in a transpilation that errors in Edge,
// once we drop IE11 support we should be able to use native for...of
for (let i = 0; i < files.length; i++) {
uploads.push(this.get('_uploadFile').perform(files[i], i));
let file = files[i];
let tracker = new UploadTracker({file});

this.get('_uploadTrackers').pushObject(tracker);
uploads.push(this.get('_uploadFile').perform(tracker, file, i));
}

// populates this.errors and this.uploadUrls
Expand All @@ -179,14 +185,11 @@ export default Component.extend({
this.onComplete(this.get('uploadUrls'));
}).drop(),

_uploadFile: task(function* (file, index) {
_uploadFile: task(function* (tracker, file, index) {
let ajax = this.get('ajax');
let formData = this._getFormData(file);
let url = `${ghostPaths().apiRoot}${this.get('uploadUrl')}`;

let tracker = new UploadTracker({file});
this.get('_uploadTrackers').pushObject(tracker);

try {
let response = yield ajax.post(url, {
data: formData,
Expand Down Expand Up @@ -242,7 +245,7 @@ export default Component.extend({
this.get('errors').pushObject(result);
this.onUploadFailure(result);
}
}),
}).maxConcurrency(MAX_SIMULTANEOUS_UPLOADS).enqueue(),

// NOTE: this is necessary because the API doesn't accept direct file uploads
_getFormData(file) {
Expand Down

0 comments on commit 0ef0af5

Please sign in to comment.