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
+17 -4
View File
@@ -1,21 +1,34 @@
import { Signal, useSignal, useSignalEffect } from "@preact/signals";
import HubFileList, { type HubFile } from "@/islands/HubFileList.tsx";
import HubFileList, { type HubFile, type HubShare } from "@/islands/HubFileList.tsx";
import HubUpload, { type UploadState } from "@/islands/HubUpload.tsx";
import { post_json } from "@/src/client.ts";
import { decrypt_metadata, encrypt_file } from "@/src/file_crypto.ts";
import type { FileEntry } from "@/src/files.ts";
import type { FileEntry, ShareEntry } from "@/src/files.ts";
import { list_files, upload_file } from "@/src/files_api.ts";
import { load_master_key } from "@/src/key_store.ts";
import { type GetKeyResponse, verify_key_check } from "@/src/keys.ts";
import { open_share_secret, share_link } from "@/src/share_crypto.ts";
function has_files(event: DragEvent) {
return event.dataTransfer?.types.includes("Files") ?? false;
}
async function to_hub_share(master_key: CryptoKey, share: ShareEntry): Promise<HubShare | null> {
const link_secret = await open_share_secret(master_key, share.secret).catch(() => null);
return link_secret ? { link: share_link(share.id, link_secret) } : null;
}
async function to_hub_file(master_key: CryptoKey, entry: FileEntry): Promise<HubFile> {
const metadata = await decrypt_metadata(master_key, entry.metadata).catch(() => null);
return { id: entry.id, uploaded_at: entry.date_created, stored_size: entry.size, metadata };
return {
id: entry.id,
uploaded_at: entry.date_created,
stored_size: entry.size,
encrypted_metadata: entry.metadata,
metadata,
share: entry.share && await to_hub_share(master_key, entry.share),
};
}
interface HubProps {
@@ -161,7 +174,7 @@ export default function Hub(props: HubProps) {
/>
</div>
<div class="flex min-h-0 flex-col md:w-1/2">
<HubFileList files={files} />
<HubFileList files={files} master_key={master_key.value} />
</div>
</div>
);
+137 -15
View File
@@ -2,31 +2,75 @@ import { Signal, useSignal } from "@preact/signals";
import Button from "@/components/Button.tsx";
import FileInfo from "@/components/FileInfo.tsx";
import { CloseIcon, DownloadIcon } from "@/components/Icons.tsx";
import { CloseIcon, CopyIcon, DownloadIcon, ShareIcon } from "@/components/Icons.tsx";
import InputField from "@/components/InputField.tsx";
import type { FileMetadata } from "@/src/file_crypto.ts";
import { download_blob } from "@/src/files.ts";
import { delete_file, download_file } from "@/src/files_api.ts";
import { delete_file, download_file, share_file, unshare_file } from "@/src/files_api.ts";
import { create_share, share_link } from "@/src/share_crypto.ts";
export interface HubShare {
// Has the link secret, so it only exists in this browser
link: string;
}
export interface HubFile {
id: string;
uploaded_at: string;
// Size of the encrypted file, shown when the metadata can't be read
stored_size: number;
// As stored on the server, needed to encrypt it again for a share link
encrypted_metadata: string;
// null when it can't be decrypted with this key
metadata: FileMetadata | null;
share: HubShare | null;
}
type FileAction = "download" | "delete" | "share" | "unshare";
const DATE_FORMAT = new Intl.DateTimeFormat(undefined, { dateStyle: "medium" });
interface ShareLinkProps {
name: string;
link: string;
copied: boolean;
on_copy: (link: string) => void;
}
function ShareLink(props: ShareLinkProps) {
return (
<div class="mt-4 flex flex-row gap-2">
<InputField
type="text"
readOnly
value={props.link}
aria-label={`Share link for ${props.name}`}
class="h-10 min-w-0 text-sm"
onFocus={(event) => event.currentTarget.select()}
/>
<Button
variant="primary"
class="flex h-10 flex-none items-center gap-2 px-4 py-0 text-sm"
onClick={() => props.on_copy(props.link)}
>
<CopyIcon class="size-4" />
{props.copied ? "Copied" : "Copy"}
</Button>
</div>
);
}
interface HubFileListProps {
files: Signal<HubFile[]>;
master_key: CryptoKey;
}
export default function HubFileList(props: HubFileListProps) {
const busy = useSignal<{ id: string; action: "download" | "delete" } | null>(null);
const busy = useSignal<{ id: string; action: FileAction } | null>(null);
const copied = useSignal<string | null>(null);
const error = useSignal("");
async function run(file: HubFile, action: "download" | "delete", fn: () => Promise<void>) {
async function run(file: HubFile, action: FileAction, fn: () => Promise<void>) {
if (busy.value) {
return;
}
@@ -41,6 +85,29 @@ export default function HubFileList(props: HubFileListProps) {
}
}
function is_busy(file: HubFile, action: FileAction) {
return busy.value?.id === file.id && busy.value.action === action;
}
function set_share(file: HubFile, share: HubShare | null) {
props.files.value = props.files.value.map((other) => other.id === file.id ? { ...other, share } : other);
}
async function copy(file: HubFile, link: string) {
try {
await navigator.clipboard.writeText(link);
} catch {
error.value = "Couldn't copy the link, select it and copy it by hand";
return;
}
copied.value = file.id;
setTimeout(() => {
if (copied.value === file.id) {
copied.value = null;
}
}, 2000);
}
function download(file: HubFile) {
const metadata = file.metadata;
if (metadata) {
@@ -48,6 +115,32 @@ export default function HubFileList(props: HubFileListProps) {
}
}
function share(file: HubFile) {
run(file, "share", async () => {
const created = await create_share(props.master_key, file.encrypted_metadata);
const result = await share_file(file.id, created);
if (!result.ok) {
throw new Error(result.error);
}
const link = share_link(result.data.id, created.link_secret);
set_share(file, { link });
await copy(file, link);
});
}
function unshare(file: HubFile) {
if (!confirm(`Stop sharing "${file.metadata?.name ?? "this file"}"? The link will stop working.`)) {
return;
}
run(file, "unshare", async () => {
const result = await unshare_file(file.id);
if (!result.ok && result.status !== 404) {
throw new Error(result.error);
}
set_share(file, null);
});
}
function remove(file: HubFile) {
if (!confirm(`Delete "${file.metadata?.name ?? "this file"}"? This can't be undone.`)) {
return;
@@ -107,17 +200,46 @@ export default function HubFileList(props: HubFileListProps) {
{file.metadata && (
<>
<hr class="my-4 w-full border-gray-200 dark:border-gray-700" />
<Button
variant="link"
class="flex items-center gap-2 self-start"
disabled={busy.value !== null}
onClick={() => download(file)}
>
<DownloadIcon class="h-4 w-3" />
{busy.value?.id === file.id && busy.value.action === "download"
? "Downloading..."
: "Download"}
</Button>
<div class="flex flex-row flex-wrap items-center gap-x-6 gap-y-2">
<Button
variant="link"
class="flex items-center gap-2"
disabled={busy.value !== null}
onClick={() => download(file)}
>
<DownloadIcon class="h-4 w-3" />
{is_busy(file, "download") ? "Downloading..." : "Download"}
</Button>
{file.share
? (
<Button
variant="link"
disabled={busy.value !== null}
onClick={() => unshare(file)}
>
{is_busy(file, "unshare") ? "Removing link..." : "Stop sharing"}
</Button>
)
: (
<Button
variant="link"
class="flex items-center gap-2"
disabled={busy.value !== null}
onClick={() => share(file)}
>
<ShareIcon class="size-4" />
{is_busy(file, "share") ? "Creating link..." : "Share"}
</Button>
)}
</div>
{file.share && (
<ShareLink
name={file.metadata.name}
link={file.share.link}
copied={copied.value === file.id}
on_copy={(link) => copy(file, link)}
/>
)}
</>
)}
</li>
+88
View File
@@ -0,0 +1,88 @@
import { useSignal, useSignalEffect } from "@preact/signals";
import Button from "@/components/Button.tsx";
import FileInfo from "@/components/FileInfo.tsx";
import { DownloadIcon } from "@/components/Icons.tsx";
import type { FileMetadata } from "@/src/file_crypto.ts";
import { download_blob } from "@/src/files.ts";
import { download_shared_file, get_shared_file } from "@/src/files_api.ts";
import { decrypt_share_metadata } from "@/src/share_crypto.ts";
interface SharedDownloadProps {
share_id: string;
}
export default function SharedDownload(props: SharedDownloadProps) {
const metadata = useSignal<FileMetadata | null>(null);
const load_error = useSignal("");
const downloading = useSignal(false);
const error = useSignal("");
async function load() {
// The secret is after the #, so the browser never sends it to the server
const link_secret = location.hash.slice(1);
if (!link_secret) {
load_error.value = "This link is missing its key, make sure you copied all of it";
return;
}
const result = await get_shared_file(props.share_id);
if (!result.ok) {
load_error.value = result.error;
return;
}
try {
metadata.value = await decrypt_share_metadata(link_secret, result.data.metadata);
} catch (err) {
load_error.value = err instanceof Error ? err.message : String(err);
}
}
async function download() {
const file = metadata.value;
if (!file || downloading.value) {
return;
}
downloading.value = true;
error.value = "";
try {
download_blob(await download_shared_file(props.share_id, file), file.name);
} catch (err) {
error.value = err instanceof Error ? err.message : String(err);
} finally {
downloading.value = false;
}
}
useSignalEffect(() => {
load();
});
if (!metadata.value) {
return load_error.value
? <p role="alert" class="py-8 text-center text-error-600 dark:text-error-400">{load_error.value}</p>
: <p class="py-8 text-center text-gray-800 dark:text-gray-400">Loading...</p>;
}
return (
<div class="mx-auto flex w-full max-w-md flex-col py-8">
<h1 class="text-3xl font-bold">Download a shared file</h1>
<p class="mt-2 leading-loose text-gray-800 dark:text-gray-400">
It's decrypted in this browser, the server never sees what's inside.
</p>
<div class="mt-8 rounded bg-white p-4 shadow-light dark:border dark:border-gray-700 dark:bg-gray-900">
<FileInfo name={metadata.value.name} size={metadata.value.size} />
</div>
<Button
variant="primary"
class="mt-6 flex items-center justify-center gap-2"
disabled={downloading.value}
onClick={download}
>
<DownloadIcon class="h-4 w-3" />
{downloading.value ? "Downloading..." : "Download"}
</Button>
{error.value && <p role="alert" class="mt-4 text-error-600 dark:text-error-400">{error.value}</p>}
</div>
);
}