File sharing

This commit is contained in:
2026-10-29 20:44:26 -03:00
parent b65d8efad8
commit de1ad70068
18 changed files with 582 additions and 59 deletions
+14 -7
View File
@@ -2,7 +2,7 @@ import { z } from "@zod/zod";
import { define } from "@/utils.ts";
import { authenticate, unauthorized } from "@/src/auth.ts";
import { db, type FileRow } from "@/src/db.ts";
import { db, type FileRow, type ShareRow } from "@/src/db.ts";
import type { FileEntry } from "@/src/files.ts";
import { MAX_UPLOAD_SIZE } from "@/src/server_constants.ts";
import { delete_upload, save_upload, UploadTooLargeError } from "@/src/storage.ts";
@@ -19,13 +19,19 @@ export const handler = define.handlers({
}
const files = await db.get_entries_by_index("files", "user_id", user.id) as FileRow[];
const shares = await db.get_entries_by_index("shares", "user_id", user.id) as ShareRow[];
const share_by_file = new Map(shares.map((share) => [share.file_id, share]));
return Response.json(
files.map((file): FileEntry => ({
id: file.id,
date_created: file.date_created,
size: file.size,
metadata: file.metadata,
})).reverse(),
files.map((file): FileEntry => {
const share = share_by_file.get(file.id);
return {
id: file.id,
date_created: file.date_created,
size: file.size,
metadata: file.metadata,
share: share ? { id: share.id, secret: share.secret } : null,
};
}).reverse(),
);
},
@@ -67,6 +73,7 @@ export const handler = define.handlers({
date_created: file.date_created,
size: file.size,
metadata: file.metadata,
share: null,
} satisfies FileEntry,
{ status: 201 },
);