Skip to content

Commit 579613d

Browse files
committed
Fix image upload to use uploadBytesResumable with proper cancel support and content type metadata
1 parent 3b52313 commit 579613d

2 files changed

Lines changed: 14 additions & 13 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "misinformation-game",
3-
"version": "2.4.2",
3+
"version": "2.4.3",
44
"author": "Padraig X. Lamont",
55
"private": true,
66
"dependencies": {

src/database/postToDB.js

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {compressJson} from "./compressJson";
88
import {STUDY_UNCOMPRESSED_KEYS} from "../model/study";
99
import {collection, setDoc} from "@firebase/firestore";
1010
import {doc} from "firebase/firestore";
11-
import {uploadBytes} from "@firebase/storage";
11+
import {uploadBytesResumable} from "@firebase/storage";
1212
import {ref} from "firebase/storage";
1313

1414
/**
@@ -22,10 +22,11 @@ export function uploadStudyConfiguration(study) {
2222

2323
/**
2424
* Uploads the image {@param image} to storage at the path {@param path}.
25-
* @return {firebase.storage.UploadTask} a task object for tracking the upload.
25+
* @return {UploadTask} a task object for tracking the upload.
2626
*/
2727
export function uploadImageToStorage(path, image) {
28-
return uploadBytes(ref(storage, path), image.buffer);
28+
const metadata = { contentType: "image/" + image.type };
29+
return uploadBytesResumable(ref(storage, path), image.buffer, metadata);
2930
}
3031

3132
/**
@@ -62,16 +63,16 @@ export function uploadImagesToStorage(imageDict, progressFn) {
6263
}
6364
}).catch((error) => {
6465
console.log("Error uploading " + path);
65-
try {
66-
console.error(error);
67-
progress.errored = true;
68-
// If any upload errors, cancel all other uploads.
69-
for (let index = 0; index < tasks.length; ++index) {
70-
tasks[index].cancel();
71-
}
72-
} finally {
73-
reject(error);
66+
console.error(error);
67+
if (progress.errored)
68+
return;
69+
70+
progress.errored = true;
71+
// If any upload errors, cancel all other uploads.
72+
for (let index = 0; index < tasks.length; ++index) {
73+
tasks[index].cancel();
7474
}
75+
reject(error);
7576
});
7677
tasks.push(task);
7778
progress.started += 1;

0 commit comments

Comments
 (0)