Skip to content

Commit

Permalink
Delete the entire temporary directory instead of just the temp file, …
Browse files Browse the repository at this point in the history
…also use `cacheDirectory` over `documentDirectory` (#3985)

* lint

* remove extra arg
  • Loading branch information
haileyok authored May 13, 2024
1 parent 00a57df commit 73d094c
Showing 1 changed file with 18 additions and 13 deletions.
31 changes: 18 additions & 13 deletions src/lib/media/manip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
cacheDirectory,
copyAsync,
deleteAsync,
documentDirectory,
EncodingType,
makeDirectoryAsync,
StorageAccessFramework,
Expand Down Expand Up @@ -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 =
Expand All @@ -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)
}
}

0 comments on commit 73d094c

Please sign in to comment.