Skip to content

A platform for community discussion. Free, open, simple.

DEV: Gracefully handle video thumbnail errors (#36087)

Blake Erickson37ea14baafc2
diff --git a/frontend/discourse/app/lib/composer-video-thumbnail-uppy.js b/frontend/discourse/app/lib/composer-video-thumbnail-uppy.js
index 183598fe40f..a7b751402c4 100644
--- a/frontend/discourse/app/lib/composer-video-thumbnail-uppy.js
+++ b/frontend/discourse/app/lib/composer-video-thumbnail-uppy.js
@@ -36,7 +36,13 @@ export default class ComposerVideoThumbnailUppy {
}
const video = document.createElement("video");
- video.src = URL.createObjectURL(videoFile.data);
+ const objectUrl = URL.createObjectURL(videoFile.data);
+ video.src = objectUrl;
+
+ // Clean up object URL when done
+ const cleanup = () => {
+ URL.revokeObjectURL(objectUrl);
+ };
// These attributes are needed for thumbnail generation on mobile.
// This video tag is not visible, so this is all happening in the background.
@@ -58,6 +64,7 @@ export default class ComposerVideoThumbnailUppy {
const stalledEventsTimer = setTimeout(() => {
// in some cases, no video events are hit (for example, when browser disables autoplay)
// we need to give up in those cases, otherwise the upload will hang
+ cleanup();
return callback();
}, 3000);
@@ -71,6 +78,7 @@ export default class ComposerVideoThumbnailUppy {
setTimeout(() => {
// If dimensions can't be read, abort.
if (video.videoWidth === 0) {
+ cleanup();
return callback();
}
@@ -98,43 +106,68 @@ export default class ComposerVideoThumbnailUppy {
if (!isEmpty) {
// upload video thumbnail
- canvas.toBlob((blob) => {
- this._uppyUpload = new UppyUpload(getOwner(this), {
- id: "video-thumbnail",
- type: "thumbnail",
- additionalParams: {
- videoSha1,
- },
- uploadDone() {
- callback();
- },
- });
- this._uppyUpload.setup();
-
- this._uppyUpload.uppyWrapper.uppyInstance.on(
- "upload-error",
- (file, error, response) => {
- let message = i18n("wizard.upload_error");
- if (response.body.errors) {
- message = response.body.errors.join("\n");
- }
-
- // eslint-disable-next-line no-console
- console.error(message);
+ try {
+ canvas.toBlob((blob) => {
+ if (!blob) {
+ // toBlob can return null if blob creation fails
+ warn("Failed to create thumbnail blob", {
+ id: "discourse.upload.video-thumbnail-blob-error",
+ });
+ cleanup();
+ return callback();
+ }
+
+ try {
+ this._uppyUpload = new UppyUpload(getOwner(this), {
+ id: "video-thumbnail",
+ type: "thumbnail",
+ additionalParams: {
+ videoSha1,
+ },
+ uploadDone() {
+ cleanup();
+ callback();
+ },
+ });
+ this._uppyUpload.setup();
+
+ this._uppyUpload.uppyWrapper.uppyInstance.on(
+ "upload-error",
+ (file, error, response) => {
+ let message = i18n("wizard.upload_error");
+ if (response?.body?.errors) {
+ message = response.body.errors.join("\n");
+ }
+
+ // eslint-disable-next-line no-console
+ console.warn(
+ "Video thumbnail upload failed, continuing without thumbnail:",
+ message
+ );
+ cleanup();
+ callback();
+ }
+ );
+
+ blob.name = `${videoSha1}.png`;
+ this._uppyUpload.addFiles(blob);
+ } catch (err) {
+ warn(`error setting up video thumbnail upload: ${err}`, {
+ id: "discourse.upload.video-thumbnail-setup-error",
+ });
+ cleanup();
callback();
}
- );
-
- try {
- blob.name = `${videoSha1}.png`;
- this._uppyUpload.addFiles(blob);
- } catch (err) {
- warn(`error adding files to uppy: ${err}`, {
- id: "discourse.upload.uppy-add-files-error",
- });
- }
- });
+ });
+ } catch (err) {
+ warn(`error creating video thumbnail blob: ${err}`, {
+ id: "discourse.upload.video-thumbnail-to-blob-error",
+ });
+ cleanup();
+ callback();
+ }
} else {
+ cleanup();
callback();
}
}, 100);
@@ -145,6 +178,7 @@ export default class ComposerVideoThumbnailUppy {
console.warn(
"Video could not be loaded or decoded for thumbnail generation"
);
+ cleanup();
callback();
};
}
diff --git a/spec/system/composer_uploads_spec.rb b/spec/system/composer_uploads_spec.rb
index 34c9fb2bd38..5be9cbc3dcb 100644
--- a/spec/system/composer_uploads_spec.rb
+++ b/spec/system/composer_uploads_spec.rb
@@ -150,6 +150,31 @@ describe "Uploading files in the composer", type: :system do
expect(composer).to have_no_in_progress_uploads
expect(composer.preview).to have_css(".onebox-placeholder-container", count: 2)
end
+
+ it "handles thumbnail upload errors gracefully without blocking video upload" do
+ visit "/new-topic"
+ expect(composer).to be_opened
+ topic.fill_in_composer_title("Thumbnail Error Test")
+
+ # Mock canvas.toBlob to fail, simulating a thumbnail generation error
+ page.execute_script <<-JS
+ HTMLCanvasElement.prototype.toBlob = function(callback, type, quality) {
+ // Simulate a failure in blob creation
+ setTimeout(() => callback(null), 0);
+ };
+ JS
+
+ file_path_1 = file_from_fixtures("small.webm", "media").path
+ attach_file("file-uploader", file_path_1, make_visible: true)
+
+ # Video upload should complete successfully even if thumbnail generation fails
+ expect(composer).to have_no_in_progress_uploads
+ expect(composer.preview).to have_css(".onebox-placeholder-container")
+
+ # Video should still be uploadable/submittable
+ composer.submit
+ expect(find("#topic-title")).to have_content("Thumbnail Error Test")
+ end
end
context "when multiple images are uploaded" do