Loading...
;
+ }
+
+ return (
+
+
Download a shared file
+
+ It's decrypted in this browser, the server never sees what's inside.
+
+
+
+
+
+ {error.value &&
{error.value}
}
+
+ );
+}
diff --git a/routes/api/files/[id].ts b/routes/api/files/[id].ts
index 911dc6c..d89b275 100644
--- a/routes/api/files/[id].ts
+++ b/routes/api/files/[id].ts
@@ -1,7 +1,7 @@
import { define } from "@/utils.ts";
import { authenticate, unauthorized } from "@/src/auth.ts";
-import { db, type FileRow } from "@/src/db.ts";
-import { delete_upload, open_upload } from "@/src/storage.ts";
+import { db, type FileRow, type ShareRow } from "@/src/db.ts";
+import { delete_upload, upload_response } from "@/src/storage.ts";
const not_found = () => Response.json({ error: "File not found" }, { status: 404 });
@@ -17,17 +17,7 @@ export const handler = define.handlers({
if (!file || file.user_id !== user.id) {
return not_found();
}
-
- const stored = await open_upload(file.storage_id).catch(() => undefined);
- if (!stored) {
- return not_found();
- }
- return new Response(stored.readable, {
- headers: {
- "Content-Type": "application/octet-stream",
- "Cache-Control": "no-store",
- },
- });
+ return await upload_response(file.storage_id) ?? not_found();
},
async DELETE(ctx) {
@@ -41,6 +31,10 @@ export const handler = define.handlers({
return not_found();
}
+ const shares = await db.get_entries_by_index("shares", "file_id", file.id) as ShareRow[];
+ for (const share of shares) {
+ await db.delete_entry("shares", share);
+ }
await db.delete_entry("files", file);
await delete_upload(file.storage_id);
return new Response(null, { status: 204 });
diff --git a/routes/api/files/[id]/share.ts b/routes/api/files/[id]/share.ts
new file mode 100644
index 0000000..17ff1fd
--- /dev/null
+++ b/routes/api/files/[id]/share.ts
@@ -0,0 +1,62 @@
+import { z } from "@zod/zod";
+
+import { define } from "@/utils.ts";
+import { authenticate, unauthorized } from "@/src/auth.ts";
+import { db, type FileRow, type ShareRow } from "@/src/db.ts";
+import type { ShareEntry } from "@/src/files.ts";
+
+const ShareBody = z.object({
+ metadata: z.base64().min(1).max(4096),
+ secret: z.base64().min(1).max(200),
+});
+
+const not_found = () => Response.json({ error: "File not found" }, { status: 404 });
+
+export const handler = define.handlers({
+ // The body only has encrypted data, the link secret itself never reaches the server
+ async POST(ctx) {
+ const user = await authenticate(ctx.req);
+ if (!user) {
+ return unauthorized();
+ }
+
+ const body = ShareBody.safeParse(await ctx.req.json().catch(() => null));
+ if (!body.success) {
+ return Response.json({ error: "Invalid request" }, { status: 400 });
+ }
+
+ const file = await db.get_entry("files", ctx.params.id) as FileRow | undefined;
+ if (!file || file.user_id !== user.id) {
+ return not_found();
+ }
+ if (await db.get_entry_by_index("shares", "file_id", file.id)) {
+ return Response.json({ error: "This file is already shared" }, { status: 409 });
+ }
+
+ const share = await db.create_entry("shares", {
+ user_id: user.id,
+ file_id: file.id,
+ metadata: body.data.metadata,
+ secret: body.data.secret,
+ });
+ return Response.json({ id: share.id, secret: share.secret } satisfies ShareEntry, { status: 201 });
+ },
+
+ async DELETE(ctx) {
+ const user = await authenticate(ctx.req);
+ if (!user) {
+ return unauthorized();
+ }
+
+ const file = await db.get_entry("files", ctx.params.id) as FileRow | undefined;
+ if (!file || file.user_id !== user.id) {
+ return not_found();
+ }
+
+ const shares = await db.get_entries_by_index("shares", "file_id", file.id) as ShareRow[];
+ for (const share of shares) {
+ await db.delete_entry("shares", share);
+ }
+ return new Response(null, { status: 204 });
+ },
+});
diff --git a/routes/api/files/index.ts b/routes/api/files/index.ts
index 8dd0038..2aac4fc 100644
--- a/routes/api/files/index.ts
+++ b/routes/api/files/index.ts
@@ -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 },
);
diff --git a/routes/api/shares/[id].ts b/routes/api/shares/[id].ts
new file mode 100644
index 0000000..72360f2
--- /dev/null
+++ b/routes/api/shares/[id].ts
@@ -0,0 +1,15 @@
+import { define } from "@/utils.ts";
+import type { SharedFileEntry } from "@/src/files.ts";
+import { get_shared_file, share_not_found } from "@/src/shares.ts";
+
+export const handler = define.handlers({
+ async GET(ctx) {
+ const shared = await get_shared_file(ctx.params.id);
+ if (!shared) {
+ return share_not_found();
+ }
+ return Response.json({ size: shared.file.size, metadata: shared.share.metadata } satisfies SharedFileEntry, {
+ headers: { "Cache-Control": "no-store" },
+ });
+ },
+});
diff --git a/routes/api/shares/[id]/file.ts b/routes/api/shares/[id]/file.ts
new file mode 100644
index 0000000..cb89227
--- /dev/null
+++ b/routes/api/shares/[id]/file.ts
@@ -0,0 +1,14 @@
+import { define } from "@/utils.ts";
+import { get_shared_file, share_not_found } from "@/src/shares.ts";
+import { upload_response } from "@/src/storage.ts";
+
+export const handler = define.handlers({
+ // Streams the encrypted file, the recipient decrypts it with the secret in the link
+ async GET(ctx) {
+ const shared = await get_shared_file(ctx.params.id);
+ if (!shared) {
+ return share_not_found();
+ }
+ return await upload_response(shared.file.storage_id) ?? share_not_found();
+ },
+});
diff --git a/routes/download/[id].tsx b/routes/download/[id].tsx
new file mode 100644
index 0000000..17ee6d3
--- /dev/null
+++ b/routes/download/[id].tsx
@@ -0,0 +1,13 @@
+import type { RouteConfig } from "fresh";
+
+import { define } from "@/utils.ts";
+import SharedDownload from "@/islands/SharedDownload.tsx";
+
+// Share links look like /download/