about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorHailey <me@haileyok.com>2024-05-13 02:10:29 -0700
committerGitHub <noreply@github.com>2024-05-13 02:10:29 -0700
commit73d094c67e53506fd3c4ab2c29b37ab481cd9331 (patch)
tree48125930be8217d96f28b38d9d97894ae771b194 /src
parent00a57df5b16bc946c50079914962cc2819011e80 (diff)
downloadvoidsky-73d094c67e53506fd3c4ab2c29b37ab481cd9331.tar.zst
Delete the entire temporary directory instead of just the temp file, also use `cacheDirectory` over `documentDirectory` (#3985)
* lint

* remove extra arg
Diffstat (limited to 'src')
-rw-r--r--src/lib/media/manip.ts31
1 files changed, 18 insertions, 13 deletions
diff --git a/src/lib/media/manip.ts b/src/lib/media/manip.ts
index 71d5c701f..3e647004b 100644
--- a/src/lib/media/manip.ts
+++ b/src/lib/media/manip.ts
@@ -5,7 +5,6 @@ import {
   cacheDirectory,
   copyAsync,
   deleteAsync,
-  documentDirectory,
   EncodingType,
   makeDirectoryAsync,
   StorageAccessFramework,
@@ -268,9 +267,9 @@ export async function saveToDevice(
 ) {
   try {
     if (isIOS) {
-      const tmpFileUrl = await withTempFile(filename, encoded)
-      await Sharing.shareAsync(tmpFileUrl, {UTI: type})
-      safeDeleteAsync(tmpFileUrl)
+      await withTempFile(filename, encoded, async tmpFileUrl => {
+        await Sharing.shareAsync(tmpFileUrl, {UTI: type})
+      })
       return true
     } else {
       const permissions =
@@ -297,18 +296,24 @@ export async function saveToDevice(
   }
 }
 
-async function withTempFile(
+async function withTempFile<T>(
   filename: string,
   encoded: string,
-): Promise<string> {
+  cb: (url: string) => T | Promise<T>,
+): Promise<T> {
+  // cacheDirectory will not ever be null so we assert as a string.
   // Using a directory so that the file name is not a random string
-  // documentDirectory will always be available on native, so we assert as a string.
-  const tmpDirUri = joinPath(documentDirectory as string, String(uuid.v4()))
+  const tmpDirUri = joinPath(cacheDirectory as string, String(uuid.v4()))
   await makeDirectoryAsync(tmpDirUri, {intermediates: true})
 
-  const tmpFileUrl = joinPath(tmpDirUri, filename)
-  await writeAsStringAsync(tmpFileUrl, encoded, {
-    encoding: EncodingType.Base64,
-  })
-  return tmpFileUrl
+  try {
+    const tmpFileUrl = joinPath(tmpDirUri, filename)
+    await writeAsStringAsync(tmpFileUrl, encoded, {
+      encoding: EncodingType.Base64,
+    })
+
+    return await cb(tmpFileUrl)
+  } finally {
+    safeDeleteAsync(tmpDirUri)
+  }
 }