An ATproto social media client -- with an independent Appview.

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

* lint

* remove extra arg

authored by hailey.at and committed by

GitHub 73d094c6 00a57df5

+18 -13
+18 -13
src/lib/media/manip.ts
··· 5 5 cacheDirectory, 6 6 copyAsync, 7 7 deleteAsync, 8 - documentDirectory, 9 8 EncodingType, 10 9 makeDirectoryAsync, 11 10 StorageAccessFramework, ··· 268 267 ) { 269 268 try { 270 269 if (isIOS) { 271 - const tmpFileUrl = await withTempFile(filename, encoded) 272 - await Sharing.shareAsync(tmpFileUrl, {UTI: type}) 273 - safeDeleteAsync(tmpFileUrl) 270 + await withTempFile(filename, encoded, async tmpFileUrl => { 271 + await Sharing.shareAsync(tmpFileUrl, {UTI: type}) 272 + }) 274 273 return true 275 274 } else { 276 275 const permissions = ··· 297 296 } 298 297 } 299 298 300 - async function withTempFile( 299 + async function withTempFile<T>( 301 300 filename: string, 302 301 encoded: string, 303 - ): Promise<string> { 302 + cb: (url: string) => T | Promise<T>, 303 + ): Promise<T> { 304 + // cacheDirectory will not ever be null so we assert as a string. 304 305 // Using a directory so that the file name is not a random string 305 - // documentDirectory will always be available on native, so we assert as a string. 306 - const tmpDirUri = joinPath(documentDirectory as string, String(uuid.v4())) 306 + const tmpDirUri = joinPath(cacheDirectory as string, String(uuid.v4())) 307 307 await makeDirectoryAsync(tmpDirUri, {intermediates: true}) 308 308 309 - const tmpFileUrl = joinPath(tmpDirUri, filename) 310 - await writeAsStringAsync(tmpFileUrl, encoded, { 311 - encoding: EncodingType.Base64, 312 - }) 313 - return tmpFileUrl 309 + try { 310 + const tmpFileUrl = joinPath(tmpDirUri, filename) 311 + await writeAsStringAsync(tmpFileUrl, encoded, { 312 + encoding: EncodingType.Base64, 313 + }) 314 + 315 + return await cb(tmpFileUrl) 316 + } finally { 317 + safeDeleteAsync(tmpDirUri) 318 + } 314 319 }