sailfork / t3code-github / apps/ mobile/ src/ lib/ composerAttachmentFiles.ts

export const COMPOSER_ATTACHMENT_DIRECTORY = "t3-composer-attachments";

const UUID_PATTERN = "[a-f\\d]{8}-[a-f\\d]{4}-[a-f\\d]{4}-[a-f\\d]{4}-[a-f\\d]{12}";
const GENERATED_FILE_NAME = new RegExp(`^${UUID_PATTERN}-`, "i");
const IOS_DOCUMENTS_PATH = new RegExp(
  `^(.*/Containers/Data/Application/)${UUID_PATTERN}/Documents

	
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
	apps/mobile/src/lib/composerAttachmentFiles.ts at main · sailfork/t3code-github · Sailfork
	
		
, "i", ); const retainedFiles = new Map<string, number>(); function fileUriPath(uri: string): string | null { try { const url = new URL(uri); if (url.protocol !== "file:" || url.hostname || url.search || url.hash) { return null; } const path = decodeURIComponent(url.pathname); if (path.includes("\\") || path.includes("\0") || path.split("/").includes("..")) { return null; } return path.startsWith("/private/var/") ? path.slice("/private".length) : path; } catch { return null; } } function ownedFileLocation(uri: string) { const path = fileUriPath(uri); if (path === null) { return null; } const separator = `/${COMPOSER_ATTACHMENT_DIRECTORY}/`; const index = path.lastIndexOf(separator); const name = index < 0 ? "" : path.slice(index + separator.length); if (!name || name === "." || name.includes("/")) { return null; } return { documentPath: path.slice(0, index), name }; } export function composerAttachmentFileReferenceKey(uri: string): string { const location = ownedFileLocation(uri); if (!location) { return uri; } const containerPrefix = GENERATED_FILE_NAME.test(location.name) ? IOS_DOCUMENTS_PATH.exec(location.documentPath)?.[1] : undefined; const documentPath = containerPrefix ? `${containerPrefix}<app>/Documents` : location.documentPath; return `file://${documentPath}/${COMPOSER_ATTACHMENT_DIRECTORY}/${encodeURIComponent(location.name)}`; } export function retainComposerAttachmentFile(uri: string, onLastRelease: () => void): () => void { const key = composerAttachmentFileReferenceKey(uri); retainedFiles.set(key, (retainedFiles.get(key) ?? 0) + 1); let released = false; return () => { if (released) { return; } released = true; const remaining = (retainedFiles.get(key) ?? 1) - 1; if (remaining > 0) { retainedFiles.set(key, remaining); return; } retainedFiles.delete(key); onLastRelease(); }; } export function isComposerAttachmentFileRetained(uri: string): boolean { return retainedFiles.has(composerAttachmentFileReferenceKey(uri)); } /** iOS updates can change the Documents container UUID. Bypass source URIs from pickers or other apps. */ export function resolveOwnedComposerAttachmentFileUri( uri: string, documentDirectoryUri: string, ): string | null { const location = ownedFileLocation(uri); const documentPath = fileUriPath(documentDirectoryUri)?.replace(/\/+$/, ""); if (!location || !documentPath) { return null; } if (location.documentPath !== documentPath) { const currentContainerPrefix = IOS_DOCUMENTS_PATH.exec(documentPath)?.[1]; if ( !currentContainerPrefix || currentContainerPrefix !== IOS_DOCUMENTS_PATH.exec(location.documentPath)?.[1] || !GENERATED_FILE_NAME.test(location.name) ) { return null; } } const resolved = new URL(documentDirectoryUri); resolved.pathname = `${resolved.pathname.replace(/\/+$/, "")}/${COMPOSER_ATTACHMENT_DIRECTORY}/${encodeURIComponent(location.name)}`; return resolved.href; }
apps/mobile/src/lib/composerAttachmentFiles.ts at main · sailfork/t3code-github · Sailfork
,\n \"i\",\n);\nconst retainedFiles = new Map\u003cstring, number>();\n\nfunction fileUriPath(uri: string): string | null {\n try {\n const url = new URL(uri);\n if (url.protocol !== \"file:\" || url.hostname || url.search || url.hash) {\n return null;\n }\n const path = decodeURIComponent(url.pathname);\n if (path.includes(\"\\\\\") || path.includes(\"\\0\") || path.split(\"/\").includes(\"..\")) {\n return null;\n }\n return path.startsWith(\"/private/var/\") ? path.slice(\"/private\".length) : path;\n } catch {\n return null;\n }\n}\n\nfunction ownedFileLocation(uri: string) {\n const path = fileUriPath(uri);\n if (path === null) {\n return null;\n }\n const separator = `/${COMPOSER_ATTACHMENT_DIRECTORY}/`;\n const index = path.lastIndexOf(separator);\n const name = index \u003c 0 ? \"\" : path.slice(index + separator.length);\n if (!name || name === \".\" || name.includes(\"/\")) {\n return null;\n }\n return { documentPath: path.slice(0, index), name };\n}\n\nexport function composerAttachmentFileReferenceKey(uri: string): string {\n const location = ownedFileLocation(uri);\n if (!location) {\n return uri;\n }\n const containerPrefix = GENERATED_FILE_NAME.test(location.name)\n ? IOS_DOCUMENTS_PATH.exec(location.documentPath)?.[1]\n : undefined;\n const documentPath = containerPrefix\n ? `${containerPrefix}\u003capp>/Documents`\n : location.documentPath;\n return `file://${documentPath}/${COMPOSER_ATTACHMENT_DIRECTORY}/${encodeURIComponent(location.name)}`;\n}\n\nexport function retainComposerAttachmentFile(uri: string, onLastRelease: () => void): () => void {\n const key = composerAttachmentFileReferenceKey(uri);\n retainedFiles.set(key, (retainedFiles.get(key) ?? 0) + 1);\n let released = false;\n return () => {\n if (released) {\n return;\n }\n released = true;\n const remaining = (retainedFiles.get(key) ?? 1) - 1;\n if (remaining > 0) {\n retainedFiles.set(key, remaining);\n return;\n }\n retainedFiles.delete(key);\n onLastRelease();\n };\n}\n\nexport function isComposerAttachmentFileRetained(uri: string): boolean {\n return retainedFiles.has(composerAttachmentFileReferenceKey(uri));\n}\n\n/** iOS updates can change the Documents container UUID. Bypass source URIs from pickers or other apps. */\nexport function resolveOwnedComposerAttachmentFileUri(\n uri: string,\n documentDirectoryUri: string,\n): string | null {\n const location = ownedFileLocation(uri);\n const documentPath = fileUriPath(documentDirectoryUri)?.replace(/\\/+$/, \"\");\n if (!location || !documentPath) {\n return null;\n }\n if (location.documentPath !== documentPath) {\n const currentContainerPrefix = IOS_DOCUMENTS_PATH.exec(documentPath)?.[1];\n if (\n !currentContainerPrefix ||\n currentContainerPrefix !== IOS_DOCUMENTS_PATH.exec(location.documentPath)?.[1] ||\n !GENERATED_FILE_NAME.test(location.name)\n ) {\n return null;\n }\n }\n const resolved = new URL(documentDirectoryUri);\n resolved.pathname = `${resolved.pathname.replace(/\\/+$/, \"\")}/${COMPOSER_ATTACHMENT_DIRECTORY}/${encodeURIComponent(location.name)}`;\n return resolved.href;\n}\n","preview":null,"history":null,"version":"c645b35ebc494870543bea1ae05d676e03aee000"},"/_session?shell=1":null}