DEV: Refactor uppy s3 mulipart mixin to not use self = this (#14960)
We no longer use this pattern, so we can replace with function binding instead.
This commit is contained in:
parent
aa31fbe29a
commit
7e39910de6
|
@ -1,13 +1,13 @@
|
||||||
import Mixin from "@ember/object/mixin";
|
import Mixin from "@ember/object/mixin";
|
||||||
|
import { bind } from "discourse-common/utils/decorators";
|
||||||
import { Promise } from "rsvp";
|
import { Promise } from "rsvp";
|
||||||
import { ajax } from "discourse/lib/ajax";
|
import { ajax } from "discourse/lib/ajax";
|
||||||
import AwsS3Multipart from "@uppy/aws-s3-multipart";
|
import AwsS3Multipart from "@uppy/aws-s3-multipart";
|
||||||
|
const RETRY_DELAYS = [0, 1000, 3000, 5000];
|
||||||
|
|
||||||
export default Mixin.create({
|
export default Mixin.create({
|
||||||
_useS3MultipartUploads() {
|
_useS3MultipartUploads() {
|
||||||
this.set("usingS3MultipartUploads", true);
|
this.set("usingS3MultipartUploads", true);
|
||||||
const self = this;
|
|
||||||
const retryDelays = [0, 1000, 3000, 5000];
|
|
||||||
|
|
||||||
this._uppyInstance.use(AwsS3Multipart, {
|
this._uppyInstance.use(AwsS3Multipart, {
|
||||||
// controls how many simultaneous _chunks_ are uploaded, not files,
|
// controls how many simultaneous _chunks_ are uploaded, not files,
|
||||||
|
@ -18,10 +18,22 @@ export default Mixin.create({
|
||||||
// chunk size via getChunkSize(file), so we may want to increase
|
// chunk size via getChunkSize(file), so we may want to increase
|
||||||
// the chunk size for larger files
|
// the chunk size for larger files
|
||||||
limit: 10,
|
limit: 10,
|
||||||
retryDelays,
|
retryDelays: RETRY_DELAYS,
|
||||||
|
|
||||||
createMultipartUpload(file) {
|
createMultipartUpload: this._createMultipartUpload,
|
||||||
self._uppyInstance.emit("create-multipart", file.id);
|
prepareUploadParts: this._prepareUploadParts,
|
||||||
|
completeMultipartUpload: this._completeMultipartUpload,
|
||||||
|
abortMultipartUpload: this._abortMultipartUpload,
|
||||||
|
|
||||||
|
// we will need a listParts function at some point when we want to
|
||||||
|
// resume multipart uploads; this is used by uppy to figure out
|
||||||
|
// what parts are uploaded and which still need to be
|
||||||
|
});
|
||||||
|
},
|
||||||
|
|
||||||
|
@bind
|
||||||
|
_createMultipartUpload(file) {
|
||||||
|
this._uppyInstance.emit("create-multipart", file.id);
|
||||||
|
|
||||||
const data = {
|
const data = {
|
||||||
file_name: file.name,
|
file_name: file.name,
|
||||||
|
@ -43,7 +55,7 @@ export default Mixin.create({
|
||||||
data,
|
data,
|
||||||
// uppy is inconsistent, an error here fires the upload-error event
|
// uppy is inconsistent, an error here fires the upload-error event
|
||||||
}).then((responseData) => {
|
}).then((responseData) => {
|
||||||
self._uppyInstance.emit("create-multipart-success", file.id);
|
this._uppyInstance.emit("create-multipart-success", file.id);
|
||||||
|
|
||||||
file.meta.unique_identifier = responseData.unique_identifier;
|
file.meta.unique_identifier = responseData.unique_identifier;
|
||||||
return {
|
return {
|
||||||
|
@ -53,7 +65,8 @@ export default Mixin.create({
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
prepareUploadParts(file, partData) {
|
@bind
|
||||||
|
_prepareUploadParts(file, partData) {
|
||||||
if (file.preparePartsRetryAttempts === undefined) {
|
if (file.preparePartsRetryAttempts === undefined) {
|
||||||
file.preparePartsRetryAttempts = 0;
|
file.preparePartsRetryAttempts = 0;
|
||||||
}
|
}
|
||||||
|
@ -67,7 +80,7 @@ export default Mixin.create({
|
||||||
.then((data) => {
|
.then((data) => {
|
||||||
if (file.preparePartsRetryAttempts) {
|
if (file.preparePartsRetryAttempts) {
|
||||||
delete file.preparePartsRetryAttempts;
|
delete file.preparePartsRetryAttempts;
|
||||||
self._consoleDebug(
|
this._consoleDebug(
|
||||||
`[uppy] Retrying batch fetch for ${file.id} was successful, continuing.`
|
`[uppy] Retrying batch fetch for ${file.id} was successful, continuing.`
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -85,26 +98,27 @@ export default Mixin.create({
|
||||||
// if we exceed the attempts then there is no way that uppy will
|
// if we exceed the attempts then there is no way that uppy will
|
||||||
// retry the upload once again, so in that case the alert can
|
// retry the upload once again, so in that case the alert can
|
||||||
// be safely shown to the user that their upload has failed.
|
// be safely shown to the user that their upload has failed.
|
||||||
if (file.preparePartsRetryAttempts < retryDelays.length) {
|
if (file.preparePartsRetryAttempts < RETRY_DELAYS.length) {
|
||||||
file.preparePartsRetryAttempts += 1;
|
file.preparePartsRetryAttempts += 1;
|
||||||
const attemptsLeft =
|
const attemptsLeft =
|
||||||
retryDelays.length - file.preparePartsRetryAttempts + 1;
|
RETRY_DELAYS.length - file.preparePartsRetryAttempts + 1;
|
||||||
self._consoleDebug(
|
this._consoleDebug(
|
||||||
`[uppy] Fetching a batch of upload part URLs for ${file.id} failed with status ${status}, retrying ${attemptsLeft} more times...`
|
`[uppy] Fetching a batch of upload part URLs for ${file.id} failed with status ${status}, retrying ${attemptsLeft} more times...`
|
||||||
);
|
);
|
||||||
return Promise.reject({ source: { status } });
|
return Promise.reject({ source: { status } });
|
||||||
} else {
|
} else {
|
||||||
self._consoleDebug(
|
this._consoleDebug(
|
||||||
`[uppy] Fetching a batch of upload part URLs for ${file.id} failed too many times, throwing error.`
|
`[uppy] Fetching a batch of upload part URLs for ${file.id} failed too many times, throwing error.`
|
||||||
);
|
);
|
||||||
// uppy is inconsistent, an error here does not fire the upload-error event
|
// uppy is inconsistent, an error here does not fire the upload-error event
|
||||||
self._handleUploadError(file, err);
|
this._handleUploadError(file, err);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
completeMultipartUpload(file, data) {
|
@bind
|
||||||
self._uppyInstance.emit("complete-multipart", file.id);
|
_completeMultipartUpload(file, data) {
|
||||||
|
this._uppyInstance.emit("complete-multipart", file.id);
|
||||||
const parts = data.parts.map((part) => {
|
const parts = data.parts.map((part) => {
|
||||||
return { part_number: part.PartNumber, etag: part.ETag };
|
return { part_number: part.PartNumber, etag: part.ETag };
|
||||||
});
|
});
|
||||||
|
@ -119,12 +133,13 @@ export default Mixin.create({
|
||||||
}),
|
}),
|
||||||
// uppy is inconsistent, an error here fires the upload-error event
|
// uppy is inconsistent, an error here fires the upload-error event
|
||||||
}).then((responseData) => {
|
}).then((responseData) => {
|
||||||
self._uppyInstance.emit("complete-multipart-success", file.id);
|
this._uppyInstance.emit("complete-multipart-success", file.id);
|
||||||
return responseData;
|
return responseData;
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
abortMultipartUpload(file, { key, uploadId }) {
|
@bind
|
||||||
|
_abortMultipartUpload(file, { key, uploadId }) {
|
||||||
// if the user cancels the upload before the key and uploadId
|
// if the user cancels the upload before the key and uploadId
|
||||||
// are stored from the createMultipartUpload response then they
|
// are stored from the createMultipartUpload response then they
|
||||||
// will not be set, and we don't have to abort the upload because
|
// will not be set, and we don't have to abort the upload because
|
||||||
|
@ -136,7 +151,7 @@ export default Mixin.create({
|
||||||
// this gives us a chance to inspect the upload stub before
|
// this gives us a chance to inspect the upload stub before
|
||||||
// it is deleted from external storage by aborting the multipart
|
// it is deleted from external storage by aborting the multipart
|
||||||
// upload; see also ExternalUploadManager
|
// upload; see also ExternalUploadManager
|
||||||
if (file.meta.error && self.siteSettings.enable_upload_debug_mode) {
|
if (file.meta.error && this.siteSettings.enable_upload_debug_mode) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -147,13 +162,7 @@ export default Mixin.create({
|
||||||
},
|
},
|
||||||
// uppy is inconsistent, an error here does not fire the upload-error event
|
// uppy is inconsistent, an error here does not fire the upload-error event
|
||||||
}).catch((err) => {
|
}).catch((err) => {
|
||||||
self._handleUploadError(file, err);
|
this._handleUploadError(file, err);
|
||||||
});
|
|
||||||
},
|
|
||||||
|
|
||||||
// we will need a listParts function at some point when we want to
|
|
||||||
// resume multipart uploads; this is used by uppy to figure out
|
|
||||||
// what parts are uploaded and which still need to be
|
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue