use Blob's with uploadBlob
This commit is contained in:
@@ -1,14 +1,11 @@
|
|||||||
import 'react-native-gesture-handler' // must be first
|
import 'react-native-gesture-handler' // must be first
|
||||||
import {LogBox} from 'react-native'
|
|
||||||
|
|
||||||
import '#/platform/polyfills'
|
import '#/platform/polyfills'
|
||||||
import {IS_TEST} from '#/env'
|
|
||||||
|
import {LogBox} from 'react-native'
|
||||||
import {registerRootComponent} from 'expo'
|
import {registerRootComponent} from 'expo'
|
||||||
import {doPolyfill} from '#/lib/api/api-polyfill'
|
|
||||||
|
|
||||||
import App from '#/App'
|
import App from '#/App'
|
||||||
|
import {IS_TEST} from '#/env'
|
||||||
doPolyfill()
|
|
||||||
|
|
||||||
if (IS_TEST) {
|
if (IS_TEST) {
|
||||||
LogBox.ignoreAllLogs() // suppress all logs in tests
|
LogBox.ignoreAllLogs() // suppress all logs in tests
|
||||||
|
|||||||
+2
-3
@@ -1,9 +1,8 @@
|
|||||||
import '#/platform/markBundleStartTime'
|
import '#/platform/markBundleStartTime'
|
||||||
|
|
||||||
import '#/platform/polyfills'
|
import '#/platform/polyfills'
|
||||||
|
|
||||||
import {registerRootComponent} from 'expo'
|
import {registerRootComponent} from 'expo'
|
||||||
import {doPolyfill} from '#/lib/api/api-polyfill'
|
|
||||||
import App from '#/App'
|
import App from '#/App'
|
||||||
|
|
||||||
doPolyfill()
|
|
||||||
registerRootComponent(App)
|
registerRootComponent(App)
|
||||||
|
|||||||
@@ -1,85 +0,0 @@
|
|||||||
import RNFS from 'react-native-fs'
|
|
||||||
import {BskyAgent, jsonToLex, stringifyLex} from '@atproto/api'
|
|
||||||
|
|
||||||
const GET_TIMEOUT = 15e3 // 15s
|
|
||||||
const POST_TIMEOUT = 60e3 // 60s
|
|
||||||
|
|
||||||
export function doPolyfill() {
|
|
||||||
BskyAgent.configure({fetch: fetchHandler})
|
|
||||||
}
|
|
||||||
|
|
||||||
interface FetchHandlerResponse {
|
|
||||||
status: number
|
|
||||||
headers: Record<string, string>
|
|
||||||
body: any
|
|
||||||
}
|
|
||||||
|
|
||||||
async function fetchHandler(
|
|
||||||
reqUri: string,
|
|
||||||
reqMethod: string,
|
|
||||||
reqHeaders: Record<string, string>,
|
|
||||||
reqBody: any,
|
|
||||||
): Promise<FetchHandlerResponse> {
|
|
||||||
const reqMimeType = reqHeaders['Content-Type'] || reqHeaders['content-type']
|
|
||||||
if (reqMimeType && reqMimeType.startsWith('application/json')) {
|
|
||||||
reqBody = stringifyLex(reqBody)
|
|
||||||
} else if (
|
|
||||||
typeof reqBody === 'string' &&
|
|
||||||
(reqBody.startsWith('/') || reqBody.startsWith('file:'))
|
|
||||||
) {
|
|
||||||
if (reqBody.endsWith('.jpeg') || reqBody.endsWith('.jpg')) {
|
|
||||||
// HACK
|
|
||||||
// React native has a bug that inflates the size of jpegs on upload
|
|
||||||
// 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
|
|
||||||
}
|
|
||||||
// NOTE
|
|
||||||
// React native treats bodies with {uri: string} as file uploads to pull from cache
|
|
||||||
// -prf
|
|
||||||
reqBody = {uri: reqBody}
|
|
||||||
}
|
|
||||||
|
|
||||||
const controller = new AbortController()
|
|
||||||
const to = setTimeout(
|
|
||||||
() => controller.abort(),
|
|
||||||
reqMethod === 'post' ? POST_TIMEOUT : GET_TIMEOUT,
|
|
||||||
)
|
|
||||||
|
|
||||||
const res = await fetch(reqUri, {
|
|
||||||
method: reqMethod,
|
|
||||||
headers: reqHeaders,
|
|
||||||
body: reqBody,
|
|
||||||
signal: controller.signal,
|
|
||||||
})
|
|
||||||
|
|
||||||
const resStatus = res.status
|
|
||||||
const resHeaders: Record<string, string> = {}
|
|
||||||
res.headers.forEach((value: string, key: string) => {
|
|
||||||
resHeaders[key] = value
|
|
||||||
})
|
|
||||||
const resMimeType = resHeaders['Content-Type'] || resHeaders['content-type']
|
|
||||||
let resBody
|
|
||||||
if (resMimeType) {
|
|
||||||
if (resMimeType.startsWith('application/json')) {
|
|
||||||
resBody = jsonToLex(await res.json())
|
|
||||||
} else if (resMimeType.startsWith('text/')) {
|
|
||||||
resBody = await res.text()
|
|
||||||
} else if (resMimeType === 'application/vnd.ipld.car') {
|
|
||||||
resBody = await res.arrayBuffer()
|
|
||||||
} else {
|
|
||||||
throw new Error('Non-supported mime type')
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
clearTimeout(to)
|
|
||||||
|
|
||||||
return {
|
|
||||||
status: resStatus,
|
|
||||||
headers: resHeaders,
|
|
||||||
body: resBody,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,3 +0,0 @@
|
|||||||
export function doPolyfill() {
|
|
||||||
// no polyfill is needed on web
|
|
||||||
}
|
|
||||||
+4
-33
@@ -6,7 +6,6 @@ import {
|
|||||||
AppBskyFeedThreadgate,
|
AppBskyFeedThreadgate,
|
||||||
BskyAgent,
|
BskyAgent,
|
||||||
ComAtprotoLabelDefs,
|
ComAtprotoLabelDefs,
|
||||||
ComAtprotoRepoUploadBlob,
|
|
||||||
RichText,
|
RichText,
|
||||||
} from '@atproto/api'
|
} from '@atproto/api'
|
||||||
import {AtUri} from '@atproto/api'
|
import {AtUri} from '@atproto/api'
|
||||||
@@ -15,10 +14,13 @@ import {logger} from '#/logger'
|
|||||||
import {ThreadgateSetting} from '#/state/queries/threadgate'
|
import {ThreadgateSetting} from '#/state/queries/threadgate'
|
||||||
import {isNetworkError} from 'lib/strings/errors'
|
import {isNetworkError} from 'lib/strings/errors'
|
||||||
import {shortenLinks, stripInvalidMentions} from 'lib/strings/rich-text-manip'
|
import {shortenLinks, stripInvalidMentions} from 'lib/strings/rich-text-manip'
|
||||||
import {isNative, isWeb} from 'platform/detection'
|
import {isNative} from 'platform/detection'
|
||||||
import {ImageModel} from 'state/models/media/image'
|
import {ImageModel} from 'state/models/media/image'
|
||||||
import {LinkMeta} from '../link-meta/link-meta'
|
import {LinkMeta} from '../link-meta/link-meta'
|
||||||
import {safeDeleteAsync} from '../media/manip'
|
import {safeDeleteAsync} from '../media/manip'
|
||||||
|
import {uploadBlob} from './upload-blob'
|
||||||
|
|
||||||
|
export {uploadBlob}
|
||||||
|
|
||||||
export interface ExternalEmbedDraft {
|
export interface ExternalEmbedDraft {
|
||||||
uri: string
|
uri: string
|
||||||
@@ -28,25 +30,6 @@ export interface ExternalEmbedDraft {
|
|||||||
localThumb?: ImageModel
|
localThumb?: ImageModel
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function uploadBlob(
|
|
||||||
agent: BskyAgent,
|
|
||||||
blob: string,
|
|
||||||
encoding: string,
|
|
||||||
): Promise<ComAtprotoRepoUploadBlob.Response> {
|
|
||||||
if (isWeb) {
|
|
||||||
// `blob` should be a data uri
|
|
||||||
return agent.uploadBlob(convertDataURIToUint8Array(blob), {
|
|
||||||
encoding,
|
|
||||||
})
|
|
||||||
} else {
|
|
||||||
// `blob` should be a path to a file in the local FS
|
|
||||||
return agent.uploadBlob(
|
|
||||||
blob, // this will be special-cased by the fetch monkeypatch in /src/state/lib/api.ts
|
|
||||||
{encoding},
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
interface PostOpts {
|
interface PostOpts {
|
||||||
rawText: string
|
rawText: string
|
||||||
replyTo?: string
|
replyTo?: string
|
||||||
@@ -308,15 +291,3 @@ export async function createThreadgate(
|
|||||||
},
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// helpers
|
|
||||||
// =
|
|
||||||
|
|
||||||
function convertDataURIToUint8Array(uri: string): Uint8Array {
|
|
||||||
var raw = window.atob(uri.substring(uri.indexOf(';base64,') + 8))
|
|
||||||
var binary = new Uint8Array(new ArrayBuffer(raw.length))
|
|
||||||
for (let i = 0; i < raw.length; i++) {
|
|
||||||
binary[i] = raw.charCodeAt(i)
|
|
||||||
}
|
|
||||||
return binary
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -0,0 +1,82 @@
|
|||||||
|
import RNFS from 'react-native-fs'
|
||||||
|
import {BskyAgent, ComAtprotoRepoUploadBlob} from '@atproto/api'
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param encoding Allows overriding the blob's type
|
||||||
|
*/
|
||||||
|
export async function uploadBlob(
|
||||||
|
agent: BskyAgent,
|
||||||
|
input: string | Blob,
|
||||||
|
encoding?: string,
|
||||||
|
): Promise<ComAtprotoRepoUploadBlob.Response> {
|
||||||
|
if (typeof input === 'string' && input.startsWith('file:')) {
|
||||||
|
const blob = await asBlob(input)
|
||||||
|
return agent.uploadBlob(blob, {encoding})
|
||||||
|
}
|
||||||
|
|
||||||
|
if (typeof input === 'string' && input.startsWith('/')) {
|
||||||
|
const blob = await asBlob(`file://${input}`)
|
||||||
|
return agent.uploadBlob(blob, {encoding})
|
||||||
|
}
|
||||||
|
|
||||||
|
if (typeof input === 'string' && input.startsWith('data:')) {
|
||||||
|
const blob = await fetch(input).then(r => r.blob())
|
||||||
|
return agent.uploadBlob(blob, {encoding})
|
||||||
|
}
|
||||||
|
|
||||||
|
if (input instanceof Blob) {
|
||||||
|
return agent.uploadBlob(input, {encoding})
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new TypeError(`Invalid uploadBlob input: ${typeof input}`)
|
||||||
|
}
|
||||||
|
|
||||||
|
async function asBlob(uri: string): Promise<Blob> {
|
||||||
|
return withSafeFile(uri, async safeUri => {
|
||||||
|
// Note
|
||||||
|
// Android does not support `fetch()` on `file://` URIs. for this reason, we
|
||||||
|
// use XMLHttpRequest instead of simply calling:
|
||||||
|
|
||||||
|
// return fetch(safeUri.replace('file:///', 'file:/')).then(r => r.blob())
|
||||||
|
|
||||||
|
return await new Promise((resolve, reject) => {
|
||||||
|
const xhr = new XMLHttpRequest()
|
||||||
|
xhr.onload = () => resolve(xhr.response)
|
||||||
|
xhr.onerror = () => reject(new Error('Failed to load blob'))
|
||||||
|
xhr.responseType = 'blob'
|
||||||
|
xhr.open('GET', safeUri, true)
|
||||||
|
xhr.send(null)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// HACK
|
||||||
|
// React native has a bug that inflates the size of jpegs on upload
|
||||||
|
// we get around that by renaming the file ext to .bin
|
||||||
|
// see https://github.com/facebook/react-native/issues/27099
|
||||||
|
// -prf
|
||||||
|
async function withSafeFile<T>(
|
||||||
|
uri: string,
|
||||||
|
fn: (path: string) => Promise<T>,
|
||||||
|
): Promise<T> {
|
||||||
|
if (uri.endsWith('.jpeg') || uri.endsWith('.jpg')) {
|
||||||
|
// Since we don't "own" the file, we should avoid renaming or modifying it.
|
||||||
|
// Instead, let's copy it to a temporary file and use that (then remove the
|
||||||
|
// temporary file).
|
||||||
|
const newPath = uri.replace(/\.jpe?g$/, '.bin')
|
||||||
|
try {
|
||||||
|
await RNFS.copyFile(uri, newPath)
|
||||||
|
} catch {
|
||||||
|
// Failed to copy the file, just use the original
|
||||||
|
return await fn(uri)
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
return await fn(newPath)
|
||||||
|
} finally {
|
||||||
|
// Remove the temporary file
|
||||||
|
await RNFS.unlink(newPath)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return fn(uri)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
import {BskyAgent, ComAtprotoRepoUploadBlob} from '@atproto/api'
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @note It is recommended, on web, to use the `file` instance of the file
|
||||||
|
* selector input element, rather than a `data:` URL, to avoid
|
||||||
|
* loading the file into memory. `File` extends `Blob` "file" instances can
|
||||||
|
* be passed directly to this function.
|
||||||
|
*/
|
||||||
|
export async function uploadBlob(
|
||||||
|
agent: BskyAgent,
|
||||||
|
input: string | Blob,
|
||||||
|
encoding?: string,
|
||||||
|
): Promise<ComAtprotoRepoUploadBlob.Response> {
|
||||||
|
if (typeof input === 'string' && input.startsWith('data:')) {
|
||||||
|
const blob = await fetch(input).then(r => r.blob())
|
||||||
|
return agent.uploadBlob(blob, {encoding})
|
||||||
|
}
|
||||||
|
|
||||||
|
if (input instanceof Blob) {
|
||||||
|
return agent.uploadBlob(input, {
|
||||||
|
encoding,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new TypeError(`Invalid uploadBlob input: ${typeof input}`)
|
||||||
|
}
|
||||||
@@ -218,13 +218,7 @@ export async function safeDeleteAsync(path: string) {
|
|||||||
// Normalize is necessary for Android, otherwise it doesn't delete.
|
// Normalize is necessary for Android, otherwise it doesn't delete.
|
||||||
const normalizedPath = normalizePath(path)
|
const normalizedPath = normalizePath(path)
|
||||||
try {
|
try {
|
||||||
await Promise.allSettled([
|
await deleteAsync(normalizedPath, {idempotent: true})
|
||||||
deleteAsync(normalizedPath, {idempotent: true}),
|
|
||||||
// HACK: Try this one too. Might exist due to api-polyfill hack.
|
|
||||||
deleteAsync(normalizedPath.replace(/\.jpe?g$/, '.bin'), {
|
|
||||||
idempotent: true,
|
|
||||||
}),
|
|
||||||
])
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error('Failed to delete file', e)
|
console.error('Failed to delete file', e)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user