feat(attachments): support local files and video attachments (#6321)
* feat(attachments): support local files and video attachments Share gateway inspection and send validation for borrowed local paths while preserving binary uploads, original-file ownership, and existing limits. Related to NAN-284. Native provider video input remains outside this change. * test: make cross-platform verification fixtures self-contained * test(webui): align typed fixtures with runtime contracts * fix(webui): preserve uploaded video playback during history sync * test(tui): preserve local attachment drafts across resizes * perf(attachments): preserve upload caches and release video previews * docs(webui): keep attachment guidance user-focused * chore: remove redundant dependency comment * test(providers): make stream idle renewal timing deterministic * fix(attachments): keep upload byte limits out of local references Validate common format/count rules separately from upload byte budgets. Keep local inspection and send-time checks metadata-only, and exclude local references from composer and pending-upload totals. Explain upload size rejections with actual size and maximum in all ten locales. Related to NAN-284. * fix(attachments): retain image references and draft order * fix(attachments): close local reference UX and authorization gaps * fix(attachments): preserve focus during asynchronous path confirmation
This commit is contained in:
1 parent
7dfeff9ddf
commit
254f94b068
65 files changed
+1816
-292
No files matched your search
@@ -1,6 +1,79 @@
|
||||
/** Local preview data is never included in a WebSocket message. */
|
||||
export interface DraftAttachment { data_url: string; name?: string }
|
||||
export interface AttachmentReference { reference: string; name?: string }
|
||||
/** Preview bodies upload over HTTP; borrowed paths never upload file bytes. */
|
||||
export interface LocalAttachment {
|
||||
path: string;
|
||||
name: string;
|
||||
size: number;
|
||||
mime: string;
|
||||
kind: "image" | "video" | "file";
|
||||
url: string;
|
||||
}
|
||||
export function isLocalAttachment(value: unknown): value is LocalAttachment {
|
||||
if (!value || typeof value !== "object") return false;
|
||||
const row = value as Record<string, unknown>;
|
||||
return typeof row.path === "string" && row.path.length > 0 && row.path.length <= 4096
|
||||
&& typeof row.name === "string" && typeof row.mime === "string"
|
||||
&& typeof row.size === "number" && Number.isSafeInteger(row.size) && row.size > 0
|
||||
&& typeof row.kind === "string" && ["image", "video", "file"].includes(row.kind)
|
||||
&& typeof row.url === "string" && (row.url === "" || row.url.startsWith("/api/media/"));
|
||||
}
|
||||
export type DraftAttachment =
|
||||
| { data_url: string; name?: string; path?: never }
|
||||
| { path: string; name?: string; data_url?: never };
|
||||
export type AttachmentReference =
|
||||
| { reference: string; name?: string; path?: never }
|
||||
| { path: string; name?: string; reference?: never };
|
||||
|
||||
/** Recognize only a standalone pasted path, never search prose/code for one. */
|
||||
export function pastedFilePath(text: string): string | null {
|
||||
let value = text.trim();
|
||||
if (!value || /[\r\n\0]/.test(value)) return null;
|
||||
const quoted = (value.startsWith('"') && value.endsWith('"'))
|
||||
|| (value.startsWith("'") && value.endsWith("'"));
|
||||
if (quoted) value = value.slice(1, -1);
|
||||
if (!value || /["`<>|]/.test(value) || /^[a-z][a-z\d+.-]*:\/\//i.test(value)
|
||||
|| /^[a-z]:[\\/]fakepath[\\/]/i.test(value)) return null;
|
||||
if (/^(?:[a-z]:[\\/]|\/|\\\\|\.{1,2}[\\/]|~[\\/])/i.test(value)) return value;
|
||||
// Bare relative names and relative directory paths; whitespace needs quotes.
|
||||
return (quoted || !/\s/.test(value)) && /(?:[\\/]|\.[\w-]+$)/u.test(value)
|
||||
? value : null;
|
||||
}
|
||||
/** One in-flight paste owns only its original text range, not the whole composer. */
|
||||
export class PastedPathRange {
|
||||
private valid = true;
|
||||
|
||||
constructor(private value: string, private start: number, private end: number) {}
|
||||
|
||||
update(next: string): boolean {
|
||||
if (!this.valid || next === this.value) return this.valid;
|
||||
let start = 0;
|
||||
while (start < this.value.length && start < next.length && this.value[start] === next[start]) start += 1;
|
||||
let oldEnd = this.value.length;
|
||||
let newEnd = next.length;
|
||||
while (oldEnd > start && newEnd > start && this.value[oldEnd - 1] === next[newEnd - 1]) {
|
||||
oldEnd -= 1;
|
||||
newEnd -= 1;
|
||||
}
|
||||
if (oldEnd <= this.start) {
|
||||
const shift = newEnd - oldEnd;
|
||||
this.start += shift;
|
||||
this.end += shift;
|
||||
} else if (start < this.end) {
|
||||
this.valid = false;
|
||||
}
|
||||
this.value = next;
|
||||
return this.valid;
|
||||
}
|
||||
|
||||
replace(text: string, cursor: number): { value: string; cursor: number } | null {
|
||||
if (!this.valid) return null;
|
||||
const shift = text.length - (this.end - this.start);
|
||||
return {
|
||||
value: this.value.slice(0, this.start) + text + this.value.slice(this.end),
|
||||
cursor: cursor <= this.start ? cursor : cursor >= this.end ? cursor + shift : this.start + text.length,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
export interface UploadCapability { path: string; token: string }
|
||||
|
||||
// Allow the gateway's five-minute body budget plus request/response overhead.
|
||||
@@ -17,16 +90,20 @@ export async function uploadAttachments(
|
||||
drafts: readonly DraftAttachment[], capability: UploadCapability | null,
|
||||
baseUrl: string, stillConnected: () => boolean,
|
||||
): Promise<AttachmentReference[]> {
|
||||
if (!capability || !stillConnected()) throw new Error("Attachment upload connection is not ready");
|
||||
const url = new URL(capability.path, baseUrl.replace(/^ws:/, "http:").replace(/^wss:/, "https:"));
|
||||
if (!["http:", "https:"].includes(url.protocol)) throw new Error("HTTP upload is unavailable");
|
||||
// Send same-origin edge-auth cookies without following redirects with the capability.
|
||||
url.search = "";
|
||||
url.username = "";
|
||||
url.password = "";
|
||||
const references: AttachmentReference[] = [];
|
||||
for (const draft of drafts) {
|
||||
if (!stillConnected()) throw new Error("Connection changed during attachment upload");
|
||||
if (draft.path !== undefined) {
|
||||
references.push({ path: draft.path, ...(draft.name ? { name: draft.name } : {}) });
|
||||
continue;
|
||||
}
|
||||
if (!capability) throw new Error("Attachment upload connection is not ready");
|
||||
const url = new URL(capability.path, baseUrl.replace(/^ws:/, "http:").replace(/^wss:/, "https:"));
|
||||
if (!["http:", "https:"].includes(url.protocol)) throw new Error("HTTP upload is unavailable");
|
||||
// Never send capabilities across redirects or forward URL credentials.
|
||||
url.search = "";
|
||||
url.username = "";
|
||||
url.password = "";
|
||||
const match = /^data:([^;,]+);base64,([\s\S]*)$/.exec(draft.data_url);
|
||||
if (!match?.[1] || !match[2]) throw new Error("Invalid attachment preview");
|
||||
const raw = atob(match[2]);
|
||||
|
||||
Reference in new issue
Block a user