File size: 794 Bytes
0e5c445 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import type { Conversation } from "$lib/types/Conversation";
import { sha256 } from "$lib/utils/sha256";
import { collections } from "../database";
export async function uploadFile(file: Blob, conv: Conversation): Promise<string> {
const sha = await sha256(await file.text());
const upload = collections.bucket.openUploadStream(`${conv._id}-${sha}`, {
metadata: { conversation: conv._id.toString(), mime: "image/jpeg" },
});
upload.write((await file.arrayBuffer()) as unknown as Buffer);
upload.end();
// only return the filename when upload throws a finish event or a 10s time out occurs
return new Promise((resolve, reject) => {
upload.once("finish", () => resolve(sha));
upload.once("error", reject);
setTimeout(() => reject(new Error("Upload timed out")), 10000);
});
}
|