Skip to content

Commit b12ab30

Browse files
fix(mobile): files with brackets in their names attach on Android (pingdotgg#28983)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 23075af commit b12ab30

2 files changed

Lines changed: 33 additions & 5 deletions

File tree

‎apps/mobile/src/lib/composerImages.test.ts‎

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,19 @@ vi.mock("expo-file-system", () => ({
5959
return files.get(this.uri)?.mimeType ?? "";
6060
}
6161

62+
get size(): number | null {
63+
const entry = files.get(this.uri);
64+
return entry && !entry.deleted
65+
? Math.floor((entry.base64.replace(/=+$/, "").length * 3) / 4)
66+
: null;
67+
}
68+
69+
async copy(destination: { readonly uri: string }): Promise<void> {
70+
const entry = files.get(this.uri);
71+
if (!entry) throw new Error("missing file");
72+
files.set(destination.uri, { ...entry });
73+
}
74+
6275
async base64(): Promise<string> {
6376
const entry = files.get(this.uri);
6477
if (!entry || entry.deleted) {
@@ -98,14 +111,15 @@ vi.mock("expo-file-system", () => ({
98111

99112
create(): void {}
100113
},
114+
FileMode: { ReadOnly: "r", WriteOnly: "w" },
101115
Paths: { document: "file:///documents" },
102116
}));
103117

104118
vi.mock("./uuid", () => ({
105119
uuidv4: () => "attachment-id",
106120
}));
107121

108-
import { convertPastedImagesToAttachments } from "./composerImages";
122+
import { convertPastedImagesToAttachments, persistComposerAttachmentFile } from "./composerImages";
109123

110124
describe("native pasted image cleanup", () => {
111125
beforeEach(() => {
@@ -204,6 +218,22 @@ describe("native pasted image cleanup", () => {
204218
});
205219
});
206220

221+
describe("persistComposerAttachmentFile", () => {
222+
beforeEach(() => {
223+
files.clear();
224+
});
225+
226+
it("stores picked files under a name Android's java.net.URI accepts", async () => {
227+
const source = "file:///cache/DocumentPicker/Report [draft].pdf";
228+
files.set(source, { base64: "JVBERg==", deleted: false });
229+
230+
const fileUri = await persistComposerAttachmentFile(source, "Report [draft].pdf");
231+
232+
// expo-file-system encodes spaces and `%` but leaves `[`, `]`, `|` and `^` raw.
233+
expect(fileUri.split("/").at(-1)).toMatch(/^[\w\-.!~*'();:@&=+$, %]+\.pdf$/);
234+
});
235+
});
236+
207237
describe("composerStripAttachments", () => {
208238
const image = {
209239
id: "img-1",

‎apps/mobile/src/lib/composerImages.ts‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,8 @@ export async function persistComposerAttachmentFile(
127127
const { Directory, File, FileMode, Paths } = await import("expo-file-system");
128128
const directory = new Directory(Paths.document, COMPOSER_ATTACHMENT_DIRECTORY);
129129
directory.create({ idempotent: true, intermediates: true });
130-
const safeName =
131-
Array.from(name, (character) =>
132-
character === "/" || character === "\\" || character.charCodeAt(0) < 32 ? "-" : character,
133-
).join("") || "file";
130+
// Android's java.net.URI rejects characters expo-file-system leaves unencoded, such as `[` and `]`.
131+
const safeName = name.replace(/[^\w. -]/g, "-") || "file";
134132
const destination = new File(directory, `${uuidv4()}-${safeName}`);
135133
const source = new File(uri);
136134
const sourceSize = source.size;

0 commit comments

Comments
 (0)