migrate to expo-file-system

This commit is contained in:
Hailey
2024-04-06 14:32:35 -07:00
parent a6babaceaf
commit 243a5bebb7
8 changed files with 325 additions and 167 deletions
+23 -5
View File
@@ -1,5 +1,5 @@
import {BskyAgent, stringifyLex, jsonToLex} from '@atproto/api'
import RNFS from 'react-native-fs'
import {cacheDirectory, copyAsync, moveAsync} from 'expo-file-system'
import {BskyAgent, jsonToLex, stringifyLex} from '@atproto/api'
const GET_TIMEOUT = 15e3 // 15s
const POST_TIMEOUT = 60e3 // 60s
@@ -33,9 +33,27 @@ async function fetchHandler(
// we get around that by renaming the file ext to .bin
// see https://github.com/facebook/react-native/issues/27099
// -prf
const newPath = reqBody.replace(/\.jpe?g$/, '.bin')
await RNFS.moveFile(reqBody, newPath)
reqBody = newPath
// On some platforms, moving this file is not possible. We will attempt to move it (this is optimal, since
// we don't create duplicates) and if there is an error, we will instead copy the file to the cache directory
const fileName = reqBody.split('/').pop() ?? ''
const newPath = `${cacheDirectory ?? ''}${fileName.replace(
/\.jpe?g$/,
'.bin',
)}`
try {
await moveAsync({
from: reqBody,
to: newPath,
})
reqBody = newPath
} catch (e) {
await copyAsync({
from: reqBody,
to: newPath,
})
reqBody = newPath
}
}
// NOTE
// React native treats bodies with {uri: string} as file uploads to pull from cache
+13 -7
View File
@@ -1,6 +1,7 @@
import {deleteAsync} from 'expo-file-system'
import {
AppBskyEmbedImages,
AppBskyEmbedExternal,
AppBskyEmbedImages,
AppBskyEmbedRecord,
AppBskyEmbedRecordWithMedia,
AppBskyFeedThreadgate,
@@ -11,13 +12,14 @@ import {
RichText,
} from '@atproto/api'
import {AtUri} from '@atproto/api'
import {isNetworkError} from 'lib/strings/errors'
import {LinkMeta} from '../link-meta/link-meta'
import {isWeb} from 'platform/detection'
import {ImageModel} from 'state/models/media/image'
import {shortenLinks} from 'lib/strings/rich-text-manip'
import {logger} from '#/logger'
import {ThreadgateSetting} from '#/state/queries/threadgate'
import {isNetworkError} from 'lib/strings/errors'
import {shortenLinks} from 'lib/strings/rich-text-manip'
import {isWeb} from 'platform/detection'
import {ImageModel} from 'state/models/media/image'
import {LinkMeta} from '../link-meta/link-meta'
export interface ExternalEmbedDraft {
uri: string
@@ -39,10 +41,14 @@ export async function uploadBlob(
})
} else {
// `blob` should be a path to a file in the local FS
return agent.uploadBlob(
const res = await agent.uploadBlob(
blob, // this will be special-cased by the fetch monkeypatch in /src/state/lib/api.ts
{encoding},
)
try {
deleteAsync(blob)
} catch (e) {} // Don't need to handle
return res
}
}