Delete the entire temporary directory instead of just the temp file, also use `cacheDirectory` over `documentDirectory` (#3985)

* lint

* remove extra arg
zio/stable
Hailey 2024-05-13 02:10:29 -07:00 committed by GitHub
parent 00a57df5b1
commit 73d094c67e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 18 additions and 13 deletions

View File

@ -5,7 +5,6 @@ import {
cacheDirectory, cacheDirectory,
copyAsync, copyAsync,
deleteAsync, deleteAsync,
documentDirectory,
EncodingType, EncodingType,
makeDirectoryAsync, makeDirectoryAsync,
StorageAccessFramework, StorageAccessFramework,
@ -268,9 +267,9 @@ export async function saveToDevice(
) { ) {
try { try {
if (isIOS) { if (isIOS) {
const tmpFileUrl = await withTempFile(filename, encoded) await withTempFile(filename, encoded, async tmpFileUrl => {
await Sharing.shareAsync(tmpFileUrl, {UTI: type}) await Sharing.shareAsync(tmpFileUrl, {UTI: type})
safeDeleteAsync(tmpFileUrl) })
return true return true
} else { } else {
const permissions = const permissions =
@ -297,18 +296,24 @@ export async function saveToDevice(
} }
} }
async function withTempFile( async function withTempFile<T>(
filename: string, filename: string,
encoded: 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 // 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(cacheDirectory as string, String(uuid.v4()))
const tmpDirUri = joinPath(documentDirectory as string, String(uuid.v4()))
await makeDirectoryAsync(tmpDirUri, {intermediates: true}) await makeDirectoryAsync(tmpDirUri, {intermediates: true})
try {
const tmpFileUrl = joinPath(tmpDirUri, filename) const tmpFileUrl = joinPath(tmpDirUri, filename)
await writeAsStringAsync(tmpFileUrl, encoded, { await writeAsStringAsync(tmpFileUrl, encoded, {
encoding: EncodingType.Base64, encoding: EncodingType.Base64,
}) })
return tmpFileUrl
return await cb(tmpFileUrl)
} finally {
safeDeleteAsync(tmpDirUri)
}
} }