GitXplorerGitXplorer
m

file-storage

public
145 stars
1 forks
0 issues

Commits

List of commits on branch main.
Verified
d5cacbc7f0dd1aae88d800fb18f99be0cc27628a

Update README.md

mmjackson committed 9 days ago
Verified
b22c8460a07eadabbd5bbec71184953c929c4009

Update README.md

mmjackson committed 9 days ago
Unverified
82cb3ad761dd0a271f2e0887f7135a3e8b25212a

Update README

mmjackson committed 24 days ago
Unverified
0093ad7f5b0222d041b0f29ee77cd755bd90d761

Version 0.2.0

mmjackson committed 25 days ago
Unverified
cb96eb6456a6adf7587516b0b1f9ca955f2257a4

Update CHANGELOG

mmjackson committed 25 days ago
Verified
0bb677f57e1cb72503c11bcbc171390706c60ab9

Merge pull request #1 from sergiodxa/use-exports-to-split-storage-classes

mmjackson committed 25 days ago

README

The README file for this repository.

IMPORTANT: This repository has moved to @mjackson/remix-the-web

file-storage

file-storage is a key/value interface for storing File objects in JavaScript. Similar to how localStorage allows you to store key/value pairs of strings in the browser, file-storage allows you to store key/value pairs of files on the server.

Features

  • Simple, intuitive key/value API (like Web Storage, but for Files instead of strings)
  • A generic FileStorage interface that works for various large object storage backends (can be adapted to AWS S3, Cloudflare R2, etc.)
  • Support streaming file content to and from storage
  • Preserves all File metadata including file.name, file.type, and file.lastModified

Installation

Install from npm:

npm install @mjackson/file-storage

Usage

import { LocalFileStorage } from "@mjackson/file-storage/local";

let storage = new LocalFileStorage("./user/files");

let file = new File(["hello world"], "hello.txt", { type: "text/plain" });
let key = "hello-key";

// Put the file in storage.
await storage.set(key, file);

// Then, sometime later...
let fileFromStorage = await storage.get(key);
// All of the original file's metadata is intact
fileFromStorage.name; // 'hello.txt'
fileFromStorage.type; // 'text/plain'

// To remove from storage
await storage.remove(key);

The FileStorage interface allows you to implement your own file storage for custom storage backends:

import { type FileStorage } from "@mjackson/file-storage";

class CustomFileStorage implements FileStorage {
  /**
   * Returns `true` if a file with the given key exists, `false` otherwise.
   */
  has(key: string): boolean | Promise<boolean> {
    // ...
  }
  /**
   * Puts a file in storage at the given key.
   */
  set(key: string, file: File): void | Promise<void> {
    // ...
  }
  /**
   * Returns the file with the given key, or `null` if no such key exists.
   */
  get(key: string): File | null | Promise<File | null> {
    // ...
  }
  /**
   * Removes the file with the given key from storage.
   */
  remove(key: string): void | Promise<void> {
    // ...
  }
}

Related Packages

  • form-data-parser - Pairs well with this library for storing FileUpload objects received in multipart/form-data requests
  • lazy-file - The streaming File implementation used internally to stream files from storage

License

See LICENSE