Packages

Files for Phoenix: uploads checked by their bytes, stored under safe keys on the local disk or in a store of your own, and served safely.

Current section

Files

Jump to
storehouse README.md
Raw

README.md

# Storehouse

![Storehouse — Files for Phoenix](https://raw.githubusercontent.com/hattoriai/storehouse/main/media/banner.png)

<p>
  <a href="https://hex.pm/packages/storehouse">
    <img alt="Hex Version" src="https://img.shields.io/hexpm/v/storehouse.svg">
  </a>
  <a href="https://hexdocs.pm/storehouse">
    <img src="https://img.shields.io/badge/docs-hexdocs-blue" alt="HexDocs">
  </a>
  <a href="https://github.com/hattoriai/storehouse/actions">
    <img alt="CI Status" src="https://github.com/hattoriai/storehouse/actions/workflows/ci.yml/badge.svg">
  </a>
</p>

**Files for Phoenix, complete: everything an app does with the files
people give it, in one library, safe by default.**

Photos people upload, a document's attachments, the exports your app
makes. Storehouse takes a file in by any route, checks it by its bytes,
keeps it on the local disk or in S3 (or R2, MinIO, Tigris), makes what
the app shows of it, serves it safely, and deletes it when nothing uses
it any more.

> Early release. Before 1.0, a minor version may change the API.

## Why Storehouse

* **One library for the whole life of a file.** Upload, check, store,
  thumbnails and previews, private links, a CDN in front, exports, and
  deletion that follows your records. Add it once; the next feature your
  app needs from its files is usually already there.
* **Safe without configuration.** Types from the bytes, not the name.
  Keys an upload cannot choose. Headers that keep a file from running on
  your origin. Uploads signed with their size and checksum. Zips unpacked
  only after every entry is checked.
* **Every way a file comes in.** LiveView, straight to the bucket from
  any frontend (a JavaScript client on npm), very large files in parts
  that resume after a reload, tus for Uppy and mobile apps, a URL the app
  fetches.
* **It grows with the app.** The local disk in development, S3 in
  production, several stores and mirrors later, a CDN in front: the same
  calls. PostgreSQL, MySQL or SQLite.

### When something smaller fits

If a form saves one file to a folder, `Plug.Upload` and `File.cp/2` are
enough. [Capsule](https://hex.pm/packages/capsule) gives a few building
blocks to assemble your own way; [Waffle](https://hex.pm/packages/waffle)
is a simple uploader with versions made at upload;
[phx_media_library](https://hex.pm/packages/phx_media_library) attaches
media collections to records, with LiveView components;
[attached](https://hex.pm/packages/attached) follows Active Storage, with
a table of files and variants made when asked for. Storehouse is for
apps that want all of it handled, and handled carefully. The comparison
below has the details.

## Install

You need Elixir 1.17+. `Storehouse.Live` also needs Phoenix LiveView
1.1+; the rest works in any Plug app.

Add Storehouse to the deps in `mix.exs`:

```elixir
{:storehouse, "~> 0.6"}
```

On Elixir 1.17, add `{:jason, "~> 1.4"}` too. 1.18 and later have their
own `JSON`.

With [Igniter](https://hexdocs.pm/igniter), one command adds it and sets
it up: the local disk in development and tests, the store of `--store`
in production (`s3`, `r2`, `tigris`, `minio`, `b2`, `spaces`, `gcs`, or
`local`), and the plug that serves the local disk's files.

```sh
mix igniter.install storehouse --store r2
```

`mix storehouse.install` does the same in an app that has Storehouse
already. Without Igniter, the next section's steps do it by hand.

## Your first file

Tell Storehouse where to keep files, and serve them from your endpoint:

```elixir
# config/runtime.exs
config :storehouse, Storehouse.Local, root: "/var/lib/my_app/uploads"

# lib/my_app_web/endpoint.ex, before the router
plug Storehouse.Local.Plug
```

Store a file:

```elixir
{:ok, file} = Storehouse.store("priv/seeds/palette.png")

file
#=> %Storehouse.File{
#     key: "2026/10/0b6…-palette.png",
#     url: "/uploads/2026/10/0b6…-palette.png",
#     name: "palette.png",
#     content_type: "image/png",
#     bytes: 48_211,
#     width: 1600,
#     height: 1000,
#     preview: nil
#   }
```

Keep it in a record:

```elixir
schema "users" do
  field :avatar, Storehouse.Ecto.File
end

user
|> User.changeset(attrs)
|> Storehouse.Ecto.put_file(:avatar, upload, accept: Storehouse.Check.images(), max_bytes: 5_000_000)
|> then(&Storehouse.Ecto.update(Repo, &1))
```

A list field (`{:array, Storehouse.Ecto.File}`) holds many, with
`add_files/4` and `remove_file/3`. The [Ecto](guides/ecto.md) guide has
both, and how each Active Storage call maps to Storehouse.

Or store the files of a LiveView upload:

```elixir
{socket, results} = Storehouse.Live.consume(socket, :photos, accept: Storehouse.Check.images())
```

The [Stores](guides/stores.md) guide sets up S3, development and tests.

## What you get

* **Files checked by their bytes.** Images, camera photos (HEIC, AVIF,
  DNG), PDF, MP4, WebM and QuickTime video, ZIP and Office files. An HTML
  page renamed `.png` is refused or sent as a download. → [Serving](guides/serving.md)
* **Your choice of store.** The local disk, S3 and the stores that speak
  its API, or your own. Several at once, and files copied between them.
  → [Stores](guides/stores.md)
* **Uploads straight to the bucket.** Signed in your app, checked when
  they land. Very large files go in parts and resume after a reload.
  → [Direct uploads](guides/direct_uploads.md)
* **Files in Ecto records.** A column, no join table, on PostgreSQL,
  MySQL or SQLite. Temporary until the record is saved, deleted once it
  no longer has them (after a transaction commits, too), and the files
  no record has found by `mix storehouse.orphans`.
  → [Ecto](guides/ecto.md)
* **The same file once.** Found by its checksum before it is uploaded,
  or as it lands. → [Stores](guides/stores.md#the-same-file-again)
* **Private files.** Signed URLs that expire, or files streamed through
  your app after it checks the person. → [Private files](guides/private_files.md)
* **Variants, placeholders and previews.** Thumbnails and `srcset` sizes
  with libvips or imgproxy, a tiny preview while a photo loads, and a
  PDF's first page or a video's frame, with its length.
  → [Variants](guides/variants.md), [After the upload](guides/analysis.md)
* **A photo's metadata.** Its Exif read (camera, lens, exposure), and
  where it was taken removed, without re-encoding it.
  → [After the upload](guides/analysis.md)
* **Archives.** A zip of thousands of stored files made as a stream,
  split, encrypted, with a link that expires; and a zip someone uploads,
  unpacked safely. → [Stores](guides/stores.md#archives-a-zip-of-files)
* **Any frontend.** JSON endpoints and a JavaScript client for React,
  Vue or a native app (on npm as
  [`@hattoriai/storehouse`](https://www.npmjs.com/package/@hattoriai/storehouse)),
  and tus for Uppy and mobile clients.
  → [Uploads from any client](guides/any_client.md)
* **Work in the background.** Analysis, deletions and mirrors run on
  Oban, or on a process of your app's. → [Work out of the request](guides/background.md)
* **Tests without a store.** An in-memory store, safe under
  `async: true`, with assertions. → [Stores](guides/stores.md#in-tests)

The full list is in [Features](guides/features.md).

## Guides

[Stores](guides/stores.md) ·
[Serving](guides/serving.md) ·
[LiveView](guides/liveview.md) ·
[Direct uploads](guides/direct_uploads.md) ·
[Uploads from any client](guides/any_client.md) ·
[Private files](guides/private_files.md) ·
[Ecto](guides/ecto.md) ·
[After the upload](guides/analysis.md) ·
[Variants](guides/variants.md) ·
[Work out of the request](guides/background.md) ·
[Features](guides/features.md)

## How Storehouse compares

The Elixir libraries that keep an app's files, as their code describes
them in November 2026. Capsule's column includes its companion packages,
capsule_supplement and capsule_ecto. A correction is welcome as an issue.

| | Storehouse 0.6 | [Waffle](https://hex.pm/packages/waffle) 2.1 | [Capsule](https://hex.pm/packages/capsule) 1.0 | [phx_media_library](https://hex.pm/packages/phx_media_library) 0.10 | [attached](https://hex.pm/packages/attached) 0.2 |
|---|---|---|---|---|---|
| Type checked from the bytes | Yes | No | No | Yes | Partly |
| Safe serving headers | Yes | No | No | Partly | No |
| CDN cache headers and purges | Yes | Partly | No | Partly | Partly |
| Private files through the app | Yes | No | No | Yes | No |
| Signed URLs that expire | Yes | Yes | No | Yes | Yes |
| Uploads straight to the store | Yes | No | No | Yes | Partly |
| Streamed uploads, no temporary file | Yes | Partly | No | No | No |
| Temporary until saved | Yes | No | Partly | Partly | Partly |
| Checksums | Yes | No | No | Yes | Yes |
| Files from a URL | Yes | Yes | Yes | Yes | No |
| LiveView uploads | Yes | Yes | No | Yes | Partly |
| Reading files back | Yes | No | Yes | Yes | Yes |
| Your own metadata | Yes | No | Yes | Yes | Yes |
| Photo metadata edited without re-encoding | Yes | No | No | No | No |
| Several stores at once | Yes | Partly | Yes | Yes | No |
| Background processing | Yes | No | No | Yes | Yes |
| Variants | Yes | Yes | No | Yes | Yes |
| Placeholders | Yes | No | No | Yes | No |
| PDF and video previews | Yes | No | No | Yes | Yes |
| A table of files | Optional | No | No | Yes | Yes |
| Duplicate uploads detected | Yes | No | No | No | No |
| JavaScript client, non-LiveView endpoints | Yes | No | No | Partly | No |
| Resumable and multipart uploads | Yes | No | No | No | No |
| In-memory store for tests | Yes | No | Yes | Yes | No |
| Required dependencies | Plug, Req, Telemetry | None | None | Ecto SQL, Req, MIME, Telemetry | Ecto SQL, Oban, Plug, Jason |

## Roadmap

* Audio: its types checked by their bytes, and its length.
* An AVIF's and a HEIC's Exif, read as a JPEG's is.
* Azure and native Google Cloud Storage. Google Cloud Storage works
  through its S3 API today.

## Development

```sh
mix deps.get
mix test
mix credo --strict
mix dialyzer
node --test test/js/*.test.mjs
```

## License

MIT. See [LICENSE](https://github.com/hattoriai/storehouse/blob/main/LICENSE).