Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

Attach archives pasted from Finder or Explorer, and take the common archive types phones send (#5825)

Milind Soniccbe58b7a6a7
diff --git a/server/attachments.test.ts b/server/attachments.test.ts
index 3096a58dac..2e3144cf39 100644
--- a/server/attachments.test.ts
+++ b/server/attachments.test.ts
@@ -541,6 +541,9 @@ describe("shared files", () => {
expect(extensionForFileMime("application/vnd.openxmlformats-officedocument.wordprocessingml.document")).toBe(".docx");
expect(extensionForFileMime("application/zip")).toBe(".zip");
expect(extensionForFileMime("application/x-compressed-tar")).toBe(".tgz");
+ expect(extensionForFileMime("application/x-zip-compressed")).toBe(".zip");
+ expect(extensionForFileMime("Application/X-RAR-Compressed")).toBe(".rar");
+ expect(extensionForFileMime("application/x-gzip")).toBe(".gz");
expect(extensionForFileMime("application/x-msdownload")).toBeNull();
expect(extensionForFileMime("application/octet-stream")).toBeNull();
expect(extensionForFileMime("text/html")).toBeNull();
@@ -593,6 +596,18 @@ describe("shared files", () => {
expect(readdirSync(ATTACHMENTS_DIR)).toEqual([`${UPLOAD_A}${extension}`]);
});
+ it.each([
+ ["Project.ZIP", "application/x-zip-compressed", ".zip", "Project.zip"],
+ ["logs.tar.gz", "application/x-gzip", ".gz", "logs.tar.gz"],
+ ["dump.rar", "application/x-rar-compressed", ".rar", "dump.rar"],
+ ])("stores %s sent as %s unchanged, under its archive extension", async (name, mime, extension, shown) => {
+ const bytes = Buffer.from([0x50, 0x4b, 0x03, 0x04, 0, 1, 2]);
+ const saved = await saveFile((async function* () { yield bytes; })(), name, mime, { uploadId: UPLOAD_A });
+ expect(saved.path).toBe(join(ATTACHMENTS_DIR, `${UPLOAD_A}${extension}`));
+ expect(saved.name).toBe(shown);
+ expect(readFileSync(saved.path)).toEqual(bytes);
+ });
+
it("rejects empty and oversized streams without leaving partial files", async () => {
await expect(saveFile((async function* () {})(), "empty.txt", "text/plain")).rejects.toThrow(/empty file/);
expect(readdirSync(ATTACHMENTS_DIR)).toEqual([]);
diff --git a/server/attachments.ts b/server/attachments.ts
index cfc7bc7ece..0d597666db 100644
--- a/server/attachments.ts
+++ b/server/attachments.ts
@@ -104,10 +104,13 @@ const FILE_MIMES: Readonly<Record<string, string>> = {
"application/vnd.oasis.opendocument.spreadsheet": ".ods",
"application/vnd.oasis.opendocument.presentation": ".odp",
"application/zip": ".zip",
+ "application/x-zip-compressed": ".zip",
"application/vnd.rar": ".rar",
+ "application/x-rar-compressed": ".rar",
"application/x-7z-compressed": ".7z",
"application/x-tar": ".tar",
"application/gzip": ".gz",
+ "application/x-gzip": ".gz",
"application/x-compressed-tar": ".tgz",
"application/x-bzip2": ".bz2",
"application/x-xz": ".xz",
diff --git a/src/components/Composer.tsx b/src/components/Composer.tsx
index 62a13e004b..988e6b31cd 100644
--- a/src/components/Composer.tsx
+++ b/src/components/Composer.tsx
@@ -48,6 +48,7 @@ import { useOnCall } from "@/lib/call";
import { isLiveCallRunning, useLiveMedia } from "@/lib/live-call-media";
import {
appendPastedText,
+ clipboardFilesToAttach,
clipboardHasImages,
clipboardImageFiles,
composeMessage,
@@ -482,7 +483,7 @@ export function Composer({
const modeBot = group ? undefined : bot;
const { choice: approvalChoice, dialogs: approvalDialogs } = useThreadApproval(modeBot);
const uploadImage = useCallback((file: File) => uploadDraftImage(draftId, file), [draftId]);
- const pickFiles = async (picked: FileList | null) => {
+ const pickFiles = async (picked: FileList | readonly File[] | null) => {
if (!picked?.length) return;
changeDraftAttachmentPending(draftId, true);
try {
@@ -603,6 +604,12 @@ export function Composer({
};
const handlePaste = (e: React.ClipboardEvent<HTMLTextAreaElement>) => {
+ const pastedFiles = clipboardFilesToAttach(e.clipboardData);
+ if (pastedFiles.length > 0) {
+ e.preventDefault();
+ void pickFiles(pastedFiles);
+ return;
+ }
const imageFiles = clipboardImageFiles(e.clipboardData);
if (imageFiles.length > 0 || clipboardHasImages(e.clipboardData)) {
e.preventDefault();
diff --git a/src/lib/composer-attachments.test.ts b/src/lib/composer-attachments.test.ts
index 808fd5ea41..6180adee18 100644
--- a/src/lib/composer-attachments.test.ts
+++ b/src/lib/composer-attachments.test.ts
@@ -5,6 +5,7 @@ import {
attachmentAudioUrl,
attachmentBasename,
attachmentImageUrl,
+ clipboardFilesToAttach,
clipboardHasImages,
clipboardImageFiles,
composeMessage,
@@ -467,6 +468,23 @@ describe("clipboardImageFiles", () => {
});
});
+describe("clipboardFilesToAttach", () => {
+ it("attaches a copied archive or document, with any pictures copied beside it", () => {
+ const zip = new File([new Uint8Array([0x50, 0x4b])], "Project.ZIP", { type: "application/x-zip-compressed" });
+ const tarball = new File([new Uint8Array([0x1f, 0x8b])], "logs.tar.gz", { type: "" });
+ const png = new File(["image"], "shot.png", { type: "image/png" });
+ expect(clipboardFilesToAttach({ files: [zip] })).toEqual([zip]);
+ expect(clipboardFilesToAttach({ files: [tarball, png] })).toEqual([tarball, png]);
+ });
+
+ it("leaves pictures alone and plain text to the text paste", () => {
+ const png = new File(["image"], "shot.png", { type: "image/png" });
+ expect(clipboardFilesToAttach({ files: [png] })).toEqual([]);
+ expect(clipboardFilesToAttach({ files: [] })).toEqual([]);
+ expect(clipboardFilesToAttach(null)).toEqual([]);
+ });
+});
+
describe("clipboardHasImages", () => {
it("detects images in items", () => {
expect(clipboardHasImages({
diff --git a/src/lib/composer-attachments.ts b/src/lib/composer-attachments.ts
index 5cad7066dc..0cba12411f 100644
--- a/src/lib/composer-attachments.ts
+++ b/src/lib/composer-attachments.ts
@@ -247,6 +247,15 @@ export function clipboardImageFiles(
return [];
}
+/** A file copied in Finder or Explorer reaches a paste in clipboardData.files (Chromium), with only its name as text.
+ * Anything there but pictures is attached the way a picked file is; pictures alone keep the image paste. */
+export function clipboardFilesToAttach<T extends { type: string }>(
+ clipboardData: { files?: Iterable<T> | null } | null | undefined,
+): T[] {
+ const files = Array.from(clipboardData?.files ?? []);
+ return files.some((file) => !isImageFile(file)) ? files : [];
+}
+
export function clipboardHasImages(
clipboardData: {
files?: Iterable<{ type: string; size?: number }> | null;