Repository navigation
fix: give expo File uploads a filesystem uri (closes #1057) #1295
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
dewa3355
wants to merge
2
commits into
pingdotgg:main
Choose a base branch
from
dewa3355:feature/1057-expo-file-rn-formdata
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@uploadthing/expo": patch | ||
| --- | ||
|
|
||
| fix: give expo File uploads a filesystem uri |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| import * as FileSystem from "expo-file-system"; | ||
|
|
||
| const BASE64_ALPHABET = | ||
| "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; | ||
|
|
||
| /** | ||
| * Binary-safe base64 encoder. Reads the buffer as a Uint8Array in 3-byte | ||
| * groups (each byte masked to 0-255) so we never go through `btoa`/`Blob` | ||
| * APIs that are unreliable with binary data in React Native. | ||
| */ | ||
| const arrayBufferToBase64 = (buffer: ArrayBuffer): string => { | ||
| const bytes = new Uint8Array(buffer); | ||
| let result = ""; | ||
|
|
||
| for (let i = 0; i < bytes.length; i += 3) { | ||
| const remaining = bytes.length - i; | ||
| const byte1 = (bytes[i] ?? 0) & 0xff; | ||
| const byte2 = remaining > 1 ? (bytes[i + 1] ?? 0) & 0xff : 0; | ||
| const byte3 = remaining > 2 ? (bytes[i + 2] ?? 0) & 0xff : 0; | ||
|
|
||
| result += BASE64_ALPHABET.charAt(byte1 >> 2); | ||
| result += BASE64_ALPHABET.charAt(((byte1 & 0x03) << 4) | (byte2 >> 4)); | ||
| result += | ||
| remaining > 1 | ||
| ? BASE64_ALPHABET.charAt(((byte2 & 0x0f) << 2) | (byte3 >> 6)) | ||
| : "="; | ||
| result += remaining > 2 ? BASE64_ALPHABET.charAt(byte3 & 0x3f) : "="; | ||
| } | ||
|
|
||
| return result; | ||
| }; | ||
|
|
||
| /** | ||
| * According to React Native's FormData implementation: | ||
| * "a "blob", which in React Native just means an object with a uri attribute" | ||
| * @see https://github.com/facebook/react-native/blob/030663bb0634fc76f811cdc63e4d09e7ca32f3d4/packages/react-native/Libraries/Network/FormData.js#L78C1-L82C48 | ||
| * | ||
| * React Native 0.74 File defines `name`/`type` as getter-only, so assigning | ||
| * those onto the input File can throw. Return a separate wrapper with own | ||
| * `uri`/`type`/`name` whose prototype is the original file (preserving size, | ||
| * bytes, and methods). | ||
| */ | ||
| const wrapRNFormDataFile = ( | ||
| file: File, | ||
| properties: { uri: string; type: string; name: string }, | ||
| ): File => Object.setPrototypeOf({ ...properties }, file) as File; | ||
|
|
||
| export const toRNFormDataFile = async (file: File): Promise<File> => { | ||
| const type = file.type || "application/octet-stream"; | ||
| const name = file.name || "upload"; | ||
| const fileWithUri = file as File & { uri?: unknown }; | ||
|
|
||
| if (typeof fileWithUri.uri === "string") { | ||
| return wrapRNFormDataFile(file, { | ||
| uri: fileWithUri.uri, | ||
| type, | ||
| name, | ||
| }); | ||
| } | ||
|
|
||
| const cacheDirectory = FileSystem.cacheDirectory; | ||
| if (!cacheDirectory) { | ||
| throw new Error("Expo FileSystem cache directory is unavailable"); | ||
| } | ||
|
|
||
| // Empty path segments should fall back, so `||` is intentional over `??`. | ||
| // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing | ||
| const basename = name.split(/[\\/]/).pop() || "upload"; | ||
| const uri = `${cacheDirectory}${Date.now()}-${Math.random()}-${basename}`; | ||
| await FileSystem.writeAsStringAsync( | ||
| uri, | ||
| arrayBufferToBase64(await file.arrayBuffer()), | ||
| { encoding: FileSystem.EncodingType.Base64 }, | ||
| ); | ||
|
|
||
| return wrapRNFormDataFile(file, { uri, type, name }); | ||
| }; | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Each URI-less upload writes a complete copy to a unique cache path, but no success, failure, or abort path removes it, so repeated direct uploads continually consume application cache storage until the operating system evicts the files or later writes fail.
Knowledge Base Used: Expo SDK
Prompt To Fix With AI
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Skipping for now — OS cache eviction covers these unique paths, and wiring delete-on-success/abort through FormData upload lifecycle is a larger change than this PR. Happy to follow up in a dedicated issue if maintainers want it.