Skip to content

Commit 9d73323

Browse files
committed
Rename everything from Upload to UploadOld
1 parent 326b563 commit 9d73323

26 files changed

+215
-404
lines changed

README.md

+2-192
Original file line numberDiff line numberDiff line change
@@ -1,192 +1,2 @@
1-
<h1 align="center">upload</h1>
2-
3-
<div align="center">
4-
5-
![Build](https://github.com/rzane/upload/workflows/Build/badge.svg)
6-
![Version](https://img.shields.io/hexpm/v/upload)
7-
![Coverage](https://img.shields.io/coveralls/github/rzane/upload)
8-
![License](https://img.shields.io/hexpm/l/upload)
9-
10-
</div>
11-
12-
An opinionated file uploader for Elixir projects.
13-
14-
Upload offers the following features:
15-
16-
- Minimal API
17-
- Reasonable defaults
18-
- Ecto integration
19-
- Multiple storage adapters
20-
21-
## Installation
22-
23-
The package can be installed by adding `upload` to your list of dependencies in `mix.exs`:
24-
25-
```elixir
26-
def deps do
27-
[
28-
{:upload, "~> 0.3.0"}
29-
]
30-
end
31-
```
32-
33-
## Usage
34-
35-
Upload a file:
36-
37-
```elixir
38-
{:ok, upload} = Upload.cast_path("/path/to/file.txt")
39-
```
40-
41-
Transfer the upload to storage:
42-
43-
```elixir
44-
{:ok, upload} = Upload.transfer(upload)
45-
```
46-
47-
Get the URL for the upload:
48-
49-
```elixir
50-
url = Upload.get_url(upload)
51-
```
52-
53-
Get a signed URL for the upload:
54-
55-
```elixir
56-
{:ok, signed_url} = Upload.get_signed_url(upload)
57-
```
58-
59-
### Ecto Integration
60-
61-
Add a column to store a logo:
62-
63-
```elixir
64-
def change do
65-
alter table(:companies) do
66-
add :logo, :string
67-
end
68-
end
69-
```
70-
71-
Add a field to your schema:
72-
73-
```elixir
74-
schema "companies" do
75-
field :logo, :string
76-
end
77-
```
78-
79-
Cast the upload in your changeset:
80-
81-
```elixir
82-
def changeset(company, attrs \\ %{}) do
83-
company
84-
|> cast(attrs, [])
85-
|> Upload.Ecto.cast_upload(:logo, prefix: ["logos"])
86-
end
87-
```
88-
89-
Upload in the controller:
90-
91-
```elixir
92-
def create(conn, %{"logo" => logo}) do
93-
changeset = Company.changeset(%Company{}, %{"logo" => logo})
94-
95-
case Repo.insert(changeset) do
96-
{:ok, company} ->
97-
# Insert succeeded. Now, you can get the URL:
98-
Upload.get_url(company.logo)
99-
100-
{:error, changeset} ->
101-
# You know the drill.
102-
end
103-
end
104-
```
105-
106-
### Serving static files
107-
108-
In order to serve the files, you'll need to setup `Plug.Static`.
109-
110-
If you're using Phoenix, you can add this line to `endpoint.ex`:
111-
112-
```elixir
113-
plug Plug.Static, at: "/", from: :your_app, gzip: false, only: ~w(uploads)
114-
```
115-
116-
## Configuration
117-
118-
For now, there are four adapters:
119-
120-
- `Upload.Adapters.Local` - Save files to your local filesystem.
121-
- `Upload.Adapters.S3` - Save files to Amazon S3.
122-
- `Upload.Adapters.Fake` - Don't actually save the files at all.
123-
- `Upload.Adapters.Test` - Keep uploaded files in state, so that you can assert.
124-
125-
### `Upload.Adapters.Local`
126-
127-
Out of the box, `Upload` is ready to go with some sane defaults (for development, at least).
128-
129-
Here are the default values:
130-
131-
```elixir
132-
config :upload, Upload,
133-
adapter: Upload.Adapters.Local
134-
135-
config :upload, Upload.Adapters.Local,
136-
storage_path: "priv/static/uploads",
137-
public_path: "/uploads"
138-
```
139-
140-
### `Upload.Adapters.S3`
141-
142-
To use the AWS adapter, you'll to install [ExAws](https://github.com/ex-aws/ex_aws).
143-
144-
Then, you'll need to following configuration:
145-
146-
```elixir
147-
config :upload, Upload, adapter: Upload.Adapters.S3
148-
config :upload, Upload.Adapters.S3, bucket: "your_bucket_name"
149-
```
150-
151-
### `Upload.Adapters.Test`
152-
153-
To use this adapter, you'll need to the following configuration:
154-
155-
```elixir
156-
config :upload, Upload, adapter: Upload.Adapters.Test
157-
```
158-
159-
In your tests, you can make assertions:
160-
161-
```elixir
162-
test "files are uploaded" do
163-
assert {:ok, _} = start_supervised(Upload.Adapters.Test)
164-
assert {:ok, upload} = Upload.cast_path("/path/to/file.txt")
165-
assert {:ok, upload} = Upload.transfer(upload)
166-
assert map_size(Upload.Adapters.Test.get_uploads()) == 1
167-
end
168-
```
169-
170-
### `Upload.Adapters.Fake`
171-
172-
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.
173-
174-
To use this adapter, you'll need the following configuration:
175-
176-
```elixir
177-
config :upload, Upload, adapter: Upload.Adapters.Fake
178-
```
179-
180-
## Contributing
181-
182-
First, install the dependencies:
183-
184-
$ mix deps.get
185-
186-
The tests depend on a "fake" Amazon S3 running locally. If you have Docker installed, you can run:
187-
188-
$ bin/fake-s3
189-
190-
Then, you can run the test suite:
191-
192-
$ mix test
1+
Version locked version of the Upload library renamed to UploadOld so we can use
2+
the new and old versions of the library at the same time.

config/config.exs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
# and its dependencies with the aid of the Mix.Config module.
33
use Mix.Config
44

5-
config :upload, Upload, adapter: Upload.Adapters.Test
6-
config :upload, Upload.Adapters.S3, bucket: "my_bucket_name"
5+
config :upload, UploadOld, adapter: UploadOld.Adapters.Test
6+
config :upload, UploadOld.Adapters.S3, bucket: "my_bucket_name"
77

88
# Configuration For AWS
99
config :ex_aws,

lib/upload.ex

+26-26
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
defmodule Upload do
1+
defmodule UploadOld do
22
@moduledoc """
33
An opinionated file uploader.
44
"""
55

66
@enforce_keys [:key, :path, :filename]
77
defstruct [:key, :path, :filename, status: :pending]
88

9-
@type t :: %Upload{
9+
@type t :: %UploadOld{
1010
key: String.t(),
1111
filename: String.t(),
1212
path: String.t()
1313
}
1414

15-
@type transferred :: %Upload{
15+
@type transferred :: %UploadOld{
1616
key: String.t(),
1717
filename: String.t(),
1818
path: String.t(),
@@ -26,7 +26,7 @@ defmodule Upload do
2626
Get the adapter from config.
2727
"""
2828
def adapter do
29-
Upload.Config.get(__MODULE__, :adapter, Upload.Adapters.Local)
29+
UploadOld.Config.get(__MODULE__, :adapter, UploadOld.Adapters.Local)
3030
end
3131

3232
@doc """
@@ -35,21 +35,21 @@ defmodule Upload do
3535
3636
### Local
3737
38-
iex> Upload.get_url("123456.png")
39-
"/uploads/123456.png"
38+
iex> UploadOld.get_url("123456.png")
39+
"/UploadOlds/123456.png"
4040
4141
### S3
4242
43-
iex> Upload.get_url("123456.png")
43+
iex> UploadOld.get_url("123456.png")
4444
"https://my_bucket_name.s3.amazonaws.com/123456.png"
4545
4646
### Fake / Test
4747
48-
iex> Upload.get_url("123456.png")
48+
iex> UploadOld.get_url("123456.png")
4949
"123456.png"
5050
5151
"""
52-
@spec get_url(Upload.t() | String.t()) :: String.t()
52+
@spec get_url(UploadOld.t() | String.t()) :: String.t()
5353
def get_url(%__MODULE__{key: key}), do: get_url(key)
5454
def get_url(key) when is_binary(key), do: adapter().get_url(key)
5555

@@ -59,14 +59,14 @@ defmodule Upload do
5959
6060
### Examples
6161
62-
iex> Upload.get_signed_url("123456.png")
62+
iex> UploadOld.get_signed_url("123456.png")
6363
{:ok, "http://yoururl.com/123456.png?X-Amz-Expires=3600..."}
6464
65-
iex> Upload.get_signed_url("123456.png", expires_in: 4200)
65+
iex> UploadOld.get_signed_url("123456.png", expires_in: 4200)
6666
{:ok, "http://yoururl.com/123456.png?X-Amz-Expires=4200..."}
6767
6868
"""
69-
@spec get_signed_url(Upload.t() | String.t(), Keyword.t()) ::
69+
@spec get_signed_url(UploadOld.t() | String.t(), Keyword.t()) ::
7070
{:ok, String.t()} | {:error, String.t()}
7171
def get_signed_url(upload, opts \\ [])
7272
def get_signed_url(%__MODULE__{key: key}, opts), do: get_signed_url(key, opts)
@@ -75,7 +75,7 @@ defmodule Upload do
7575
@doc """
7676
Transfer the file to where it will be stored.
7777
"""
78-
@spec transfer(Upload.t()) :: {:ok, Upload.transferred()} | {:error, String.t()}
78+
@spec transfer(UploadOld.t()) :: {:ok, UploadOld.transferred()} | {:error, String.t()}
7979
def transfer(%__MODULE__{} = upload), do: adapter().transfer(upload)
8080

8181
@doc """
@@ -89,16 +89,16 @@ defmodule Upload do
8989
9090
## Examples
9191
92-
iex> Upload.cast(%Plug.Upload{path: "/path/to/foo.png", filename: "foo.png"})
93-
{:ok, %Upload{path: "/path/to/foo.png", filename: "foo.png", key: "123456.png"}}
92+
iex> UploadOld.cast(%Plug.Upload{path: "/path/to/foo.png", filename: "foo.png"})
93+
{:ok, %UploadOld{path: "/path/to/foo.png", filename: "foo.png", key: "123456.png"}}
9494
95-
iex> Upload.cast(100)
95+
iex> UploadOld.cast(100)
9696
:error
9797
9898
"""
99-
@spec cast(uploadable, list) :: {:ok, Upload.t()} | :error
99+
@spec cast(uploadable, list) :: {:ok, UploadOld.t()} | :error
100100
def cast(uploadable, opts \\ [])
101-
def cast(%Upload{} = upload, _opts), do: {:ok, upload}
101+
def cast(%UploadOld{} = upload, _opts), do: {:ok, upload}
102102

103103
def cast(%Plug.Upload{filename: filename, path: path}, opts) do
104104
do_cast(filename, path, opts)
@@ -124,7 +124,7 @@ defmodule Upload do
124124
"""
125125
@spec cast_path(uploadable_path, list) :: {:ok, Upload.t()} | :error
126126
def cast_path(path, opts \\ [])
127-
def cast_path(%Upload{} = upload, _opts), do: {:ok, upload}
127+
def cast_path(%UploadOld{} = upload, _opts), do: {:ok, upload}
128128

129129
def cast_path(path, opts) when is_binary(path) do
130130
path
@@ -184,22 +184,22 @@ defmodule Upload do
184184
185185
## Examples
186186
187-
iex> Upload.get_extension("foo.png")
187+
iex> UploadOld.get_extension("foo.png")
188188
".png"
189189
190-
iex> Upload.get_extension("foo.PNG")
190+
iex> UploadOld.get_extension("foo.PNG")
191191
".png"
192192
193-
iex> Upload.get_extension("foo")
193+
iex> UploadOld.get_extension("foo")
194194
""
195195
196-
iex> {:ok, upload} = Upload.cast_path("/path/to/foo.png")
197-
...> Upload.get_extension(upload)
196+
iex> {:ok, upload} = UploadOld.cast_path("/path/to/foo.png")
197+
...> UploadOld.get_extension(upload)
198198
".png"
199199
200200
"""
201-
@spec get_extension(String.t() | Upload.t()) :: String.t()
202-
def get_extension(%Upload{filename: filename}) do
201+
@spec get_extension(String.t() | UploadOld.t()) :: String.t()
202+
def get_extension(%UploadOld{filename: filename}) do
203203
get_extension(filename)
204204
end
205205

0 commit comments

Comments
 (0)