Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

An opinionated file uploader for Elixir projects

License

NotificationsYou must be signed in to change notification settings

rzane/upload

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

83 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

BuildVersionCoverageLicense

An opinionated file uploader for Elixir projects.

Upload offers the following features:

  • Minimal API
  • Reasonable defaults
  • Ecto integration
  • Multiple storage adapters

Installation

The package can be installed by addingupload to your list of dependencies inmix.exs:

defdepsdo[{:upload,"~> 0.3.0"}]end

Usage

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)

Ecto Integration

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

Serving static files

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)

Configuration

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.

Upload.Adapters.Local

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"

Upload.Adapters.S3

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"

Upload.Adapters.Test

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

Upload.Adapters.Fake

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

Contributing

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

[8]ページ先頭

©2009-2025 Movatter.jp