Clone attachment to other piece

This commit is contained in:
2025-10-15 15:06:41 +02:00
parent 8c05c0e825
commit 589994d62f
7 changed files with 275 additions and 181 deletions

View File

@@ -316,14 +316,26 @@ export const handle = implement(api, {
const { userId } = yield* requireOneOf(WRITE_ACCESS);
const db = yield* Database.Database;
const sha256 = Sha256.make(new Uint8Array(Bun.SHA256.byteLength));
Bun.SHA256.hash(attachment.data, sha256);
let sha256: Sha256;
if ("sha256" in attachment) {
sha256 = attachment.sha256;
yield* db
.insertInto("File")
.values({ sha256, data: attachment.data })
.onConflict((cb) => cb.column("sha256").doNothing())
.$call(Database.execute);
yield* db
.selectFrom("File")
.select("sha256")
.where("sha256", "=", sha256)
.$call(Database.executeTakeFirst)
.pipe(Effect.mapError(() => NotFound.make()));
} else {
sha256 = Sha256.make(new Uint8Array(Bun.SHA256.byteLength));
Bun.SHA256.hash(attachment.data, sha256);
yield* db
.insertInto("File")
.values({ sha256, data: attachment.data })
.onConflict((cb) => cb.column("sha256").doNothing())
.$call(Database.execute);
}
const res = yield* db
.insertInto("Attachment")