Skip to content
TypeScript

fix(mobile): files with brackets in their names attach on Android (#28983)

Julius Marmingeb12ab30950b7
diff --git a/apps/mobile/src/lib/composerImages.test.ts b/apps/mobile/src/lib/composerImages.test.ts
index cac785d299d..ac01bb09b42 100644
--- a/apps/mobile/src/lib/composerImages.test.ts
+++ b/apps/mobile/src/lib/composerImages.test.ts
@@ -59,6 +59,19 @@ vi.mock("expo-file-system", () => ({
return files.get(this.uri)?.mimeType ?? "";
}
+ get size(): number | null {
+ const entry = files.get(this.uri);
+ return entry && !entry.deleted
+ ? Math.floor((entry.base64.replace(/=+$/, "").length * 3) / 4)
+ : null;
+ }
+
+ async copy(destination: { readonly uri: string }): Promise<void> {
+ const entry = files.get(this.uri);
+ if (!entry) throw new Error("missing file");
+ files.set(destination.uri, { ...entry });
+ }
+
async base64(): Promise<string> {
const entry = files.get(this.uri);
if (!entry || entry.deleted) {
@@ -98,6 +111,7 @@ vi.mock("expo-file-system", () => ({
create(): void {}
},
+ FileMode: { ReadOnly: "r", WriteOnly: "w" },
Paths: { document: "file:///documents" },
}));
@@ -105,7 +119,7 @@ vi.mock("./uuid", () => ({
uuidv4: () => "attachment-id",
}));
-import { convertPastedImagesToAttachments } from "./composerImages";
+import { convertPastedImagesToAttachments, persistComposerAttachmentFile } from "./composerImages";
describe("native pasted image cleanup", () => {
beforeEach(() => {
@@ -204,6 +218,22 @@ describe("native pasted image cleanup", () => {
});
});
+describe("persistComposerAttachmentFile", () => {
+ beforeEach(() => {
+ files.clear();
+ });
+
+ it("stores picked files under a name Android's java.net.URI accepts", async () => {
+ const source = "file:///cache/DocumentPicker/Report [draft].pdf";
+ files.set(source, { base64: "JVBERg==", deleted: false });
+
+ const fileUri = await persistComposerAttachmentFile(source, "Report [draft].pdf");
+
+ // expo-file-system encodes spaces and `%` but leaves `[`, `]`, `|` and `^` raw.
+ expect(fileUri.split("/").at(-1)).toMatch(/^[\w\-.!~*'();:@&=+$, %]+\.pdf$/);
+ });
+});
+
describe("composerStripAttachments", () => {
const image = {
id: "img-1",
diff --git a/apps/mobile/src/lib/composerImages.ts b/apps/mobile/src/lib/composerImages.ts
index a32298b74be..5b8b0b803d3 100644
--- a/apps/mobile/src/lib/composerImages.ts
+++ b/apps/mobile/src/lib/composerImages.ts
@@ -127,10 +127,8 @@ export async function persistComposerAttachmentFile(
const { Directory, File, FileMode, Paths } = await import("expo-file-system");
const directory = new Directory(Paths.document, COMPOSER_ATTACHMENT_DIRECTORY);
directory.create({ idempotent: true, intermediates: true });
- const safeName =
- Array.from(name, (character) =>
- character === "/" || character === "\\" || character.charCodeAt(0) < 32 ? "-" : character,
- ).join("") || "file";
+ // Android's java.net.URI rejects characters expo-file-system leaves unencoded, such as `[` and `]`.
+ const safeName = name.replace(/[^\w. -]/g, "-") || "file";
const destination = new File(directory, `${uuidv4()}-${safeName}`);
const source = new File(uri);
const sourceSize = source.size;