Revert "DEV: Improve uppy plugin base and large file handling (#14383)" (#14387)

This reverts commit 36bd6e8c3b.
This commit is contained in:
Martin Brennan 2021-09-20 16:59:23 +10:00 committed by GitHub
parent 4fb7d045a0
commit 5fb45e712f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 28 additions and 108 deletions

View File

@ -1,6 +1,5 @@
import { UploadPreProcessorPlugin } from "discourse/lib/uppy-plugin-base"; import { UploadPreProcessorPlugin } from "discourse/lib/uppy-plugin-base";
import { Promise } from "rsvp"; import { Promise } from "rsvp";
import { HUGE_FILE_THRESHOLD_BYTES } from "discourse/mixins/uppy-upload";
export default class UppyChecksum extends UploadPreProcessorPlugin { export default class UppyChecksum extends UploadPreProcessorPlugin {
static pluginId = "uppy-checksum"; static pluginId = "uppy-checksum";
@ -35,19 +34,13 @@ export default class UppyChecksum extends UploadPreProcessorPlugin {
_generateChecksum(fileIds) { _generateChecksum(fileIds) {
if (!this._canUseSubtleCrypto()) { if (!this._canUseSubtleCrypto()) {
return this._skipAll(fileIds, true); return Promise.resolve();
} }
let promises = fileIds.map((fileId) => { let promises = fileIds.map((fileId) => {
let file = this._getFile(fileId); let file = this._getFile(fileId);
this._emitProgress(file);
if (file.size > HUGE_FILE_THRESHOLD_BYTES) { this._emitProgress(file);
this._consoleWarn(
"The file provided is too large to checksum, skipping."
);
return this._skip(file);
}
return file.data.arrayBuffer().then((arrayBuffer) => { return file.data.arrayBuffer().then((arrayBuffer) => {
return window.crypto.subtle return window.crypto.subtle

View File

@ -22,19 +22,17 @@ export default class UppyMediaOptimization extends UploadPreProcessorPlugin {
return this.optimizeFn(file, { stopWorkerOnError: !this.runParallel }) return this.optimizeFn(file, { stopWorkerOnError: !this.runParallel })
.then((optimizedFile) => { .then((optimizedFile) => {
let skipped = false;
if (!optimizedFile) { if (!optimizedFile) {
this._consoleWarn( this._consoleWarn(
"Nothing happened, possible error or other restriction, or the file format is not a valid one for compression." "Nothing happened, possible error or other restriction."
); );
skipped = true;
} else { } else {
this._setFileState(fileId, { this._setFileState(fileId, {
data: optimizedFile, data: optimizedFile,
size: optimizedFile.size, size: optimizedFile.size,
}); });
} }
this._emitComplete(file, skipped); this._emitComplete(file);
}) })
.catch((err) => { .catch((err) => {
this._consoleWarn(err); this._consoleWarn(err);

View File

@ -1,6 +1,5 @@
import { BasePlugin } from "@uppy/core"; import { BasePlugin } from "@uppy/core";
import { Promise } from "rsvp"; import { warn } from "@ember/debug";
import { debug, warn } from "@ember/debug";
export class UppyPluginBase extends BasePlugin { export class UppyPluginBase extends BasePlugin {
constructor(uppy, opts) { constructor(uppy, opts) {
@ -9,13 +8,7 @@ export class UppyPluginBase extends BasePlugin {
} }
_consoleWarn(msg) { _consoleWarn(msg) {
warn(`[${this.id}] ${msg}`, { id: `discourse.${this.id}` }); warn(msg, { id: `discourse.${this.id}` });
}
_consoleDebug(msg) {
if (this.siteSettings?.enable_upload_debug_mode) {
debug(`[${this.id}] ${msg}`, { id: `discourse.${this.id}` });
}
} }
_getFile(fileId) { _getFile(fileId) {
@ -48,36 +41,10 @@ export class UploadPreProcessorPlugin extends UppyPluginBase {
} }
_emitProgress(file) { _emitProgress(file) {
this.uppy.emit("preprocess-progress", file, null, this.id); this.uppy.emit("preprocess-progress", this.id, file);
} }
_emitComplete(file, skipped = false) { _emitComplete(file) {
this.uppy.emit("preprocess-complete", file, skipped, this.id); this.uppy.emit("preprocess-complete", this.id, file);
return Promise.resolve();
}
_emitAllComplete(fileIds, skipped = false) {
fileIds.forEach((fileId) => {
let file = this._getFile(fileId);
this._emitComplete(file, skipped);
});
return Promise.resolve();
}
_emitError(file, errorMessage) {
// the error message is stored twice; once to show in a displayErrorForUpload
// modal, and on the .message property to show in the uppy logs
this.uppy.emit("upload-error", file, {
errors: [errorMessage],
message: `[${this.id}] ${errorMessage}`,
});
}
_skip(file) {
return this._emitComplete(file, true);
}
_skipAll(file) {
return this._emitAllComplete(file, true);
} }
} }

View File

@ -382,8 +382,6 @@ export default Mixin.create(ExtendableUploader, {
limit: 10, limit: 10,
createMultipartUpload(file) { createMultipartUpload(file) {
self._uppyInstance.emit("create-multipart", file.id);
const data = { const data = {
file_name: file.name, file_name: file.name,
file_size: file.size, file_size: file.size,
@ -404,8 +402,6 @@ export default Mixin.create(ExtendableUploader, {
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);
file.meta.unique_identifier = responseData.unique_identifier; file.meta.unique_identifier = responseData.unique_identifier;
return { return {
uploadId: responseData.external_upload_identifier, uploadId: responseData.external_upload_identifier,
@ -434,7 +430,6 @@ export default Mixin.create(ExtendableUploader, {
}, },
completeMultipartUpload(file, data) { completeMultipartUpload(file, data) {
self._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 };
}); });
@ -447,7 +442,6 @@ export default Mixin.create(ExtendableUploader, {
}), }),
// 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);
return responseData; return responseData;
}); });
}, },
@ -562,4 +556,11 @@ export default Mixin.create(ExtendableUploader, {
showUploadSelector(toolbarEvent) { showUploadSelector(toolbarEvent) {
this.send("showUploadSelector", toolbarEvent); this.send("showUploadSelector", toolbarEvent);
}, },
_debugLog(message) {
if (this.siteSettings.enable_upload_debug_mode) {
// eslint-disable-next-line no-console
console.log(message);
}
},
}); });

View File

@ -1,5 +1,4 @@
import Mixin from "@ember/object/mixin"; import Mixin from "@ember/object/mixin";
import { debug } from "@ember/debug";
/** /**
* Use this mixin with any component that needs to upload files or images * Use this mixin with any component that needs to upload files or images
@ -81,10 +80,8 @@ export default Mixin.create({
// //
// See: https://uppy.io/docs/writing-plugins/#Progress-events // See: https://uppy.io/docs/writing-plugins/#Progress-events
_onPreProcessProgress(callback) { _onPreProcessProgress(callback) {
this._uppyInstance.on("preprocess-progress", (file, progress, pluginId) => { this._uppyInstance.on("preprocess-progress", (pluginId, file) => {
this._consoleDebug( this._debugLog(`[${pluginId}] processing file ${file.name} (${file.id})`);
`[${pluginId}] processing file ${file.name} (${file.id})`
);
this._preProcessorStatus[pluginId].activeProcessing++; this._preProcessorStatus[pluginId].activeProcessing++;
@ -93,18 +90,16 @@ export default Mixin.create({
}, },
_onPreProcessComplete(callback, allCompleteCallback) { _onPreProcessComplete(callback, allCompleteCallback) {
this._uppyInstance.on("preprocess-complete", (file, skipped, pluginId) => { this._uppyInstance.on("preprocess-complete", (pluginId, file) => {
this._consoleDebug( this._debugLog(
`[${pluginId}] ${skipped ? "skipped" : "completed"} processing file ${ `[${pluginId}] completed processing file ${file.name} (${file.id})`
file.name
} (${file.id})`
); );
callback(file); callback(file);
this._completePreProcessing(pluginId, (allComplete) => { this._completePreProcessing(pluginId, (allComplete) => {
if (allComplete) { if (allComplete) {
this._consoleDebug("[uppy] All upload preprocessors complete!"); this._debugLog("All upload preprocessors complete.");
allCompleteCallback(); allCompleteCallback();
} }
}); });
@ -173,10 +168,4 @@ export default Mixin.create({
} }
} }
}, },
_consoleDebug(msg) {
if (this.siteSettings.enable_upload_debug_mode) {
debug(msg, { id: "discourse.extendable-uploader" });
}
},
}); });

View File

@ -16,8 +16,6 @@ import UppyChecksum from "discourse/lib/uppy-checksum-plugin";
import { on } from "discourse-common/utils/decorators"; import { on } from "discourse-common/utils/decorators";
import { warn } from "@ember/debug"; import { warn } from "@ember/debug";
export const HUGE_FILE_THRESHOLD_BYTES = 104_857_600; // 100MB
export default Mixin.create({ export default Mixin.create({
uploading: false, uploading: false,
uploadProgress: 0, uploadProgress: 0,

View File

@ -10,17 +10,10 @@ class FakeUppy {
"uppy-test/file/vv2/xvejg5w/blah/png-1d-1d-2v-1d-1e-image/jpeg-9043429-1624921727764": { "uppy-test/file/vv2/xvejg5w/blah/png-1d-1d-2v-1d-1e-image/jpeg-9043429-1624921727764": {
meta: {}, meta: {},
data: createFile("test1.png"), data: createFile("test1.png"),
size: 1024,
}, },
"uppy-test/file/blah1/ads37x2/blah1/png-1d-1d-2v-1d-1e-image/jpeg-99999-1837921727764": { "uppy-test/file/blah1/ads37x2/blah1/png-1d-1d-2v-1d-1e-image/jpeg-99999-1837921727764": {
meta: {}, meta: {},
data: createFile("test2.png"), data: createFile("test2.png"),
size: 2048,
},
"uppy-test/file/mnb3/jfhrg43x/blah3/png-1d-1d-2v-1d-1e-image/jpeg-111111-1837921727764": {
meta: {},
data: createFile("test2.png"),
size: 209715200,
}, },
}; };
} }
@ -69,8 +62,8 @@ module("Unit | Utility | UppyChecksum Plugin", function () {
plugin.uppy.preprocessors[0]([fileId]).then(() => { plugin.uppy.preprocessors[0]([fileId]).then(() => {
assert.equal( assert.equal(
plugin.uppy.emitted.length, plugin.uppy.emitted.length,
1, 0,
"only the complete event was fired by the checksum plugin because it skipped the file" "no events were fired by the checksum plugin because it returned early"
); );
done(); done();
}); });
@ -92,8 +85,8 @@ module("Unit | Utility | UppyChecksum Plugin", function () {
plugin.uppy.preprocessors[0]([fileId]).then(() => { plugin.uppy.preprocessors[0]([fileId]).then(() => {
assert.equal( assert.equal(
plugin.uppy.emitted.length, plugin.uppy.emitted.length,
1, 0,
"only the complete event was fired by the checksum plugin because it skipped the file" "no events were fired by the checksum plugin because it returned early"
); );
done(); done();
}); });
@ -113,32 +106,13 @@ module("Unit | Utility | UppyChecksum Plugin", function () {
plugin.uppy.preprocessors[0]([fileId]).then(() => { plugin.uppy.preprocessors[0]([fileId]).then(() => {
assert.equal( assert.equal(
plugin.uppy.emitted.length, plugin.uppy.emitted.length,
1, 0,
"only the complete event was fired by the checksum plugin because it skipped the file" "no events were fired by the checksum plugin because it returned early"
); );
done(); done();
}); });
}); });
test("it does nothing if the file is > 100MB", function (assert) {
const capabilities = {};
const fakeUppy = new FakeUppy();
const plugin = new UppyChecksum(fakeUppy, {
capabilities,
});
plugin.install();
const done = assert.async();
const fileId =
"uppy-test/file/mnb3/jfhrg43x/blah3/png-1d-1d-2v-1d-1e-image/jpeg-111111-1837921727764";
plugin.uppy.preprocessors[0]([fileId]).then(() => {
assert.equal(plugin.uppy.emitted[0].event, "preprocess-progress");
assert.equal(plugin.uppy.emitted[1].event, "preprocess-complete");
assert.equal(plugin.uppy.getFile(fileId).meta.sha1_checksum, null);
done();
});
});
test("it gets a sha1 hash of each file and adds it to the file meta", function (assert) { test("it gets a sha1 hash of each file and adds it to the file meta", function (assert) {
const capabilities = {}; const capabilities = {};
const fakeUppy = new FakeUppy(); const fakeUppy = new FakeUppy();