Attach a file that is already in Files
Attaching meant uploading, even when the file was sitting in the account already -- picking it off disk again to send the server a copy of what it was holding. The composer can now attach from Files. A blob the account can already see needs no upload at all: an attachment carrying a `blobId` is what a forward produces, so the send path has always known what to do with one. Attaching a large file the server is already storing now costs nothing and takes no time. A file in an account somebody *shared* is different, because blobs belong to the account they were uploaded to and a draft in yours cannot reference one in theirs. Those are fetched and uploaded to your account, and the picker says so before you attach rather than leaving someone wondering why one file was instant and another was not. The picker borrows the Files store, so it browses what Files browses, shared accounts included, and puts the file manager back where it was on the way out -- a detour through somebody's shared folder to find an attachment should not leave Files somewhere else afterwards. Verified against the mock, and worth recording how, because the first attempt measured nothing: `client.upload` uses XMLHttpRequest, since it reports progress, so a counter wrapped around `fetch` sees no uploads whether or not any happen and agrees with you either way. Counted at XHR instead: attaching one's own file issues no upload, and attaching a shared one issues exactly one, to the reader's own account.
This commit is contained in:
@@ -25,6 +25,15 @@ export interface ComposeAttachment {
|
||||
abort?: AbortController;
|
||||
}
|
||||
|
||||
/** A file in Files, enough of it to attach. */
|
||||
export interface AttachableFile {
|
||||
accountId: Id;
|
||||
name: string;
|
||||
type: string | null;
|
||||
size: number | null;
|
||||
blobId: Id;
|
||||
}
|
||||
|
||||
export type Priority = "high" | "normal" | "low";
|
||||
|
||||
export interface Draft {
|
||||
@@ -76,6 +85,8 @@ interface ComposeState {
|
||||
close(key: string, opts?: { discard?: boolean }): Promise<void>;
|
||||
focus(key: string): void;
|
||||
addFiles(key: string, files: File[]): void;
|
||||
/** Attach files already in Files, by reference where the account allows it. */
|
||||
addFromFiles(key: string, nodes: AttachableFile[]): Promise<void>;
|
||||
removeAttachment(key: string, attId: string): void;
|
||||
saveDraft(key: string, opts?: { silent?: boolean }): Promise<Id | null>;
|
||||
send(key: string): Promise<void>;
|
||||
@@ -361,6 +372,47 @@ export const useCompose = create<ComposeState>((set, get) => ({
|
||||
}
|
||||
},
|
||||
|
||||
/*
|
||||
* Attach something already in Files.
|
||||
*
|
||||
* A blob the account can already see needs no upload: an attachment carrying
|
||||
* a `blobId` is exactly what a forward produces, so the send path already
|
||||
* knows what to do with one. Attaching a 20 MB file the server is holding
|
||||
* anyway then costs nothing and takes no time.
|
||||
*
|
||||
* A file in an account somebody *shared* is a different matter. Blobs belong
|
||||
* to the account they were uploaded to, so a draft in your account cannot
|
||||
* reference one in theirs; it is fetched and uploaded to yours. Slower, and
|
||||
* unavoidable, but it happens without the reader having to know any of this.
|
||||
*/
|
||||
async addFromFiles(key, nodes) {
|
||||
const accountId = useMail.getState().accountId;
|
||||
if (!accountId || !nodes.length) return;
|
||||
const max = client.maxSizeUpload;
|
||||
const atts: ComposeAttachment[] = nodes.map((n) => ({
|
||||
id: uid("a"),
|
||||
name: n.name,
|
||||
type: n.type || "application/octet-stream",
|
||||
size: n.size ?? 0,
|
||||
blobId: n.accountId === accountId ? n.blobId : null,
|
||||
progress: n.accountId === accountId ? 100 : 0,
|
||||
error: (n.size ?? 0) > max ? `Larger than ${Math.round(max / 1048576)} MB limit` : null,
|
||||
}));
|
||||
get().update(key, { attachments: [...(get().drafts.find((d) => d.key === key)?.attachments ?? []), ...atts] });
|
||||
|
||||
for (const [i, a] of atts.entries()) {
|
||||
if (a.error || a.blobId) continue;
|
||||
const node = nodes[i]!;
|
||||
try {
|
||||
const blob = await client.fetchBlob(node.accountId, node.blobId, a.type);
|
||||
const up = await client.upload(accountId, blob, { type: a.type });
|
||||
patchAtt(key, a.id, { blobId: up.blobId, progress: 100, size: up.size || a.size }, set);
|
||||
} catch (err) {
|
||||
patchAtt(key, a.id, { error: (err as Error).message || "Could not attach" }, set);
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
removeAttachment(key, attId) {
|
||||
const d = get().drafts.find((x) => x.key === key);
|
||||
const a = d?.attachments.find((x) => x.id === attId);
|
||||
|
||||
Reference in New Issue
Block a user