- Notifications
You must be signed in to change notification settings - Fork5
An opinionated file uploader for Elixir projects
License
rzane/upload
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
An opinionated file uploader for Elixir projects.
Upload offers the following features:
- Minimal API
- Reasonable defaults
- Ecto integration
- Multiple storage adapters
The package can be installed by addingupload
to your list of dependencies inmix.exs
:
defdepsdo[{:upload,"~> 0.3.0"}]end
Upload a file:
{:ok,upload}=Upload.cast_path("/path/to/file.txt")
Transfer the upload to storage:
{:ok,upload}=Upload.transfer(upload)
Get the URL for the upload:
url=Upload.get_url(upload)
Get a signed URL for the upload:
{:ok,signed_url}=Upload.get_signed_url(upload)
Add a column to store a logo:
defchangedoaltertable(:companies)doadd:logo,:stringendend
Add a field to your schema:
schema"companies"dofield:logo,:stringend
Cast the upload in your changeset:
defchangeset(company,attrs\\%{})docompany|>cast(attrs,[])|>Upload.Ecto.cast_upload(:logo,prefix:["logos"])end
Upload in the controller:
defcreate(conn,%{"logo"=>logo})dochangeset=Company.changeset(%Company{},%{"logo"=>logo})caseRepo.insert(changeset)do{:ok,company}-># Insert succeeded. Now, you can get the URL:Upload.get_url(company.logo){:error,changeset}-># You know the drill.endend
In order to serve the files, you'll need to setupPlug.Static
.
If you're using Phoenix, you can add this line toendpoint.ex
:
plugPlug.Static,at:"/",from::your_app,gzip:false,only:~w(uploads)
For now, there are four adapters:
Upload.Adapters.Local
- Save files to your local filesystem.Upload.Adapters.S3
- Save files to Amazon S3.Upload.Adapters.Fake
- Don't actually save the files at all.Upload.Adapters.Test
- Keep uploaded files in state, so that you can assert.
Out of the box,Upload
is ready to go with some sane defaults (for development, at least).
Here are the default values:
config:upload,Upload,adapter:Upload.Adapters.Localconfig:upload,Upload.Adapters.Local,storage_path:"priv/static/uploads",public_path:"/uploads"
To use the AWS adapter, you'll to installExAws.
Then, you'll need to following configuration:
config:upload,Upload,adapter:Upload.Adapters.S3config:upload,Upload.Adapters.S3,bucket:"your_bucket_name"
To use this adapter, you'll need to the following configuration:
config:upload,Upload,adapter:Upload.Adapters.Test
In your tests, you can make assertions:
test"files are uploaded"doassert{:ok,_}=start_supervised(Upload.Adapters.Test)assert{:ok,upload}=Upload.cast_path("/path/to/file.txt")assert{:ok,upload}=Upload.transfer(upload)assertmap_size(Upload.Adapters.Test.get_uploads())==1end
This adapter does pretty much nothing. It makes absolutely no attempt to persist uploads. This can be useful in unit tests where you want to completely bypass uploading.
To use this adapter, you'll need the following configuration:
config:upload,Upload,adapter:Upload.Adapters.Fake
First, install the dependencies:
$ mix deps.get
The tests depend on a "fake" Amazon S3 running locally. If you have Docker installed, you can run:
$ bin/fake-s3
Then, you can run the test suite:
$ mix test
About
An opinionated file uploader for Elixir projects