Command palette

@yasinsensoy/native

React Native · APIs and utilities

File system

File system: read/write, folders, download/upload, picking files and media, save as, saving to the gallery, opening a file.

14 exports

Import

import { joinPath, FileSystem } from "@yasinsensoy/native";

File system: read/write, folders, download/upload, picking files and media, save as, saving to the gallery, opening a file.

NameTypeDescription
documentDirectorystringThe app's persistent documents folder (backed up).
cacheDirectorystringCache folder (the system may delete it to free space).
tempDirectorystringTemporary folder.
exists(path: string) => Promise<boolean>Whether it exists.
info(path: string) => Promise<FileInfo | null>Info; null if missing.
readFile(path: string, encoding?: FileEncoding) => Promise<string>Reads a file as text (or base64).
writeFile(path: string, data: string, options?: { encoding?: FileEncoding; append?: boolean; }) => Promise<void>Writes to a file (folders are created). append adds to the end.
readJson<T = unknown>(path: string) => Promise<T>Reads JSON.
writeJson(path: string, value: unknown) => Promise<void>Writes JSON.
delete(path: string, options?: { recursive?: boolean; }) => Promise<void>Deletes (does nothing if missing). Folders are deleted with their contents by default.
copy(from: string, to: string) => Promise<FileInfo>Copies (overwrites the target if it exists). On Android the source may be content://.
move(from: string, to: string) => Promise<FileInfo>Moves / renames.
mkdir(path: string) => Promise<void>Creates a folder (with parent folders).
list(path: string) => Promise<FileInfo[]>Contents of a folder (sorted by name).
freeSpace() => Promise<number>Free space (bytes).
download(url: string, options?: DownloadOptions) => Promise<DownloadResult>Downloads (with progress, cancellable, follows redirects). The file is first written temporarily and moved to the target when finished.
upload(url: string, options: UploadOptions) => Promise<UploadResult>Uploads files (multipart/form-data, with progress, cancellable). The file is streamed without loading it into memory; suitable for large videos.
pickDocument(options?: PickDocumentOptions) => Promise<PickedFile[]>The system file picker (no permission needed). An empty array if dismissed.
pickMedia(options?: PickMediaOptions) => Promise<PickedFile[]>The system photo/video picker (no permission needed; Android 11+ Photo Picker, iOS PHPicker). An empty array if dismissed.
saveAs(path: string, options?: { name?: string; mimeType?: string; }) => Promise<string | null>"Save as": the user picks the location and name (Android create document, iOS Files). The saved address, or null if dismissed.
saveToGallery(path: string, options?: SaveToGalleryOptions) => Promise<string>Saves an image/video to the gallery. On iOS it needs ui.config permissions.photosAdd (the code is only compiled then); Android 10+ asks for no permission.
open(path: string, options?: { mimeType?: string; dialogTitle?: string; }) => Promise<void>Opens a file: iOS preview (Quick Look), on Android with a suitable app.

joinPath

function

Joins paths (joinPath(FileSystem.documentDirectory, "notes", "a.txt")).

joinPath(...parts: string[]): string

FileInfo

interface

File or folder info.

NameTypeDescription
pathstringAbsolute path (the address itself for Android content:// addresses).
uristringfile:// address (or content://).
namestringFile or folder name.
sizenumber | nullBytes; null for a folder or when unknown.
isDirectorybooleanWhether it is a folder.
modifiednumber | nullLast modified (ms, epoch).
typestring | nullMIME type (from the extension).

PickedFile

interface

A picked file.

NameTypeDescription
uristringAddress of the file (file://). By default it is copied to the cache: it is persistent, readable and uploadable. On Android copy: false returns a content:// address.
namestringFile name.
sizenumber | nullBytes; null if unknown.
typestring | nullMIME type; null if unknown.
width?numberImage/video width (pixels).
height?numberImage/video height (pixels).
duration?numberVideo duration (s).

Text encoding.

type FileEncoding = "utf8" | "base64"

Options of FileSystem.pickDocument.

NameTypeDefaultDescription
multiple?booleanfalseMultiple files.
limit?numberMaximum selection (with multiple).
types?readonly string[]any typeAllowed types: MIME ("application/pdf", "image/*") or an extension (".docx", iOS).
copy?booleantrueAndroid: copy to the cache (false: a content:// address).

Options of FileSystem.pickMedia.

NameTypeDefaultDescription
mediaType?"image" | "video" | "all""image"
multiple?booleanfalseMultiple selection.
limit?numberMaximum selection (with multiple).
compatible?booleantrueiOS: a compatible format (JPEG/MP4) instead of HEIC/MOV.
copy?booleantrueAndroid: copy to the cache.

Progress.

NameTypeDescription
loadednumberBytes transferred.
totalnumber | nullTotal bytes (null if unknown).
fractionnumber | null0–1 (null if the total is unknown).

Options of FileSystem.download.

NameTypeDefaultDescription
to?stringTarget path. If omitted, under downloads/ in the cache with the name the server gives.
headers?Readonly<Record<string, string>>Request headers.
onProgress?(progress: TransferProgress) => voidProgress (at most ~10 times/s).
signal?AbortSignalCancel: AbortController().signal. A cancelled operation rejects with E_CANCELLED.
timeout?number60000Timeout (ms).

Download result.

NameTypeDescription
pathstringAbsolute path of the saved file.
uristringfile:// address of the saved file.
statusnumberHTTP status code.
headersRecord<string, string>Response headers.
sizenumberBytes.
typestring | nullMIME type from the response; null if unknown.

UploadFile

interface

File to upload.

NameTypeDefaultDescription
pathstringPath or address (file://, also content:// on Android).
field?string"file"Name of the form field.
name?stringthe file's nameName sent to the server.
type?stringfrom the extensionMIME type.

UploadOptions

interface

Options of FileSystem.upload.

NameTypeDefaultDescription
method?"POST" | "PUT" | "PATCH""POST"
filesreadonly (UploadFile | string)[]Files (multipart/form-data).
fields?Readonly<Record<string, string | number | boolean>>Extra form fields.
raw?booleanfalseThe body is only the first file itself (e.g. a PUT to an S3 signed URL).
headers?Readonly<Record<string, string>>Request headers.
onProgress?(progress: TransferProgress) => voidProgress (at most ~10 times/s).
signal?AbortSignalCancel: AbortController().signal. A cancelled operation rejects with E_CANCELLED.
timeout?number60000Timeout (ms).

UploadResult

interface

Upload result.

NameTypeDescription
statusnumberHTTP status code.
bodystringResponse body (text).
headersRecord<string, string>Response headers.

Options of FileSystem.saveToGallery.

NameTypeDescription
album?stringAndroid: album (Pictures/<album>).