Open History to see the last commit
1export const COMPOSER_ATTACHMENT_DIRECTORY = "t3-composer-attachments";
2
3const UUID_PATTERN = "[a-f\\d]{8}-[a-f\\d]{4}-[a-f\\d]{4}-[a-f\\d]{4}-[a-f\\d]{12}";
4const GENERATED_FILE_NAME = new RegExp(`^${UUID_PATTERN}-`, "i");
5const IOS_DOCUMENTS_PATH = new RegExp(
6 `^(.*/Containers/Data/Application/)${UUID_PATTERN}/Documents$`,
7 "i",
8);
9
10function fileUriPath(uri: string): string | null {
11 try {
12 const url = new URL(uri);
13 if (url.protocol !== "file:" || url.hostname || url.search || url.hash) {
14 return null;
15 }
16 const path = decodeURIComponent(url.pathname);
17 if (path.includes("\\") || path.includes("\0") || path.split("/").includes("..")) {
18 return null;
19 }
20 return path.startsWith("/private/var/") ? path.slice("/private".length) : path;
21 } catch {
22 return null;
23 }
24}
25
26function ownedFileLocation(uri: string) {
27 const path = fileUriPath(uri);
28 if (path === null) {
29 return null;
30 }
31 const separator = `/${COMPOSER_ATTACHMENT_DIRECTORY}/`;
32 const index = path.lastIndexOf(separator);
33 const name = index < 0 ? "" : path.slice(index + separator.length);
34 if (!name || name === "." || name.includes("/")) {
35 return null;
36 }
37 return { documentPath: path.slice(0, index), name };
38}
39
40/** Compares references across iOS data-container moves without rewriting saved drafts. */
41export function composerAttachmentFileReferenceKey(uri: string): string {
42 const location = ownedFileLocation(uri);
43 if (!location) {
44 return uri;
45 }
46 const containerPrefix = GENERATED_FILE_NAME.test(location.name)
47 ? IOS_DOCUMENTS_PATH.exec(location.documentPath)?.[1]
48 : undefined;
49 const documentPath = containerPrefix
50 ? `${containerPrefix}<app>/Documents`
51 : location.documentPath;
52 return `file://${documentPath}/${COMPOSER_ATTACHMENT_DIRECTORY}/${encodeURIComponent(location.name)}`;
53}
54
55/**
56 * Resolves only our saved attachment copies. iOS preserves Documents on updates
57 * but can change its container UUID. Picker and open-in-place source URIs must
58 * bypass this resolver so another app's document keeps its original location.
59 */
60export function resolveOwnedComposerAttachmentFileUri(
61 uri: string,
62 documentDirectoryUri: string,
63): string | null {
64 const location = ownedFileLocation(uri);
65 const documentPath = fileUriPath(documentDirectoryUri)?.replace(/\/+$/, "");
66 if (!location || !documentPath) {
67 return null;
68 }
69 if (location.documentPath !== documentPath) {
70 const currentContainerPrefix = IOS_DOCUMENTS_PATH.exec(documentPath)?.[1];
71 if (
72 !currentContainerPrefix ||
73 currentContainerPrefix !== IOS_DOCUMENTS_PATH.exec(location.documentPath)?.[1] ||
74 !GENERATED_FILE_NAME.test(location.name)
75 ) {
76 return null;
77 }
78 }
79 const resolved = new URL(documentDirectoryUri);
80 resolved.pathname = `${resolved.pathname.replace(/\/+$/, "")}/${COMPOSER_ATTACHMENT_DIRECTORY}/${encodeURIComponent(location.name)}`;
81 return resolved.href;
82}