-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bd686a3
commit 27e8fd7
Showing
29 changed files
with
897 additions
and
852 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,3 @@ | ||
import Config | ||
|
||
config :dropkick, | ||
storage: Dropkick.Storage.Memory, | ||
secret_key_base: Base.encode64(String.duplicate("x", 12)) | ||
|
||
import_config "#{Mix.env()}.exs" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
import Config | ||
|
||
config :dropkick, storage: Dropkick.Storage.Disk | ||
config :dropkick, | ||
repo: TestRepo, | ||
storage: Dropkick.Storage.Disk, | ||
folder: "uploads" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,106 +1,3 @@ | ||
defmodule Dropkick do | ||
@moduledoc """ | ||
This module provides functions that you can use to interact directly with uploads and attachments. | ||
""" | ||
|
||
alias Dropkick.Attachment | ||
|
||
@doc """ | ||
Creates a version of the attachment with some transformation. | ||
Transformations validated against an attachment `content_type`. | ||
The current transformations supported are: | ||
## `image/*` | ||
Image transformations uses the [`image`](https://hexdocs.pm/image) library behind the scenes | ||
- `{:thumbnail, size, opts}`: Generates an image thumbnail, receives the same options | ||
as [`Image.thumbnail/3`](https://hexdocs.pm/image/Image.html#thumbnail/3) | ||
""" | ||
def transform(%Attachment{} = atch, transforms) do | ||
atch | ||
|> transform_stream(transforms) | ||
|> Stream.filter(&match?({:ok, _}, &1)) | ||
|> Enum.reduce(atch, fn {:ok, version}, atch -> | ||
Map.update!(atch, :versions, fn versions -> [version | versions] end) | ||
end) | ||
end | ||
|
||
@doc """ | ||
Extracts context from the attachment. | ||
""" | ||
def contextualize(%Attachment{} = atch) do | ||
key = Dropkick.Attachable.key(atch) | ||
|
||
%{ | ||
extension: Path.extname(key), | ||
directory: Path.dirname(key), | ||
filename: Path.basename(key) | ||
} | ||
end | ||
|
||
@doc """ | ||
Extracts metadata from the attachment. | ||
""" | ||
def extract_metadata(%Attachment{content_type: "image/" <> _} = atch) do | ||
# If our attachment is an image, we try to extract additional information. | ||
# Depending on the complexity we should probably move this into a 'Metadata' module in the future. | ||
case Dropkick.Attachable.content(atch) do | ||
{:ok, content} -> | ||
{mimetype, width, height, variant} = ExImageInfo.info(content) | ||
|
||
%{ | ||
mimetype: mimetype, | ||
dimension: "#{width}x#{height}", | ||
variant: variant | ||
} | ||
|
||
_ -> | ||
%{} | ||
end | ||
end | ||
|
||
# If we don't yet support extracting metadata from the content type we do nothing. | ||
# In the future this could be expanded to other formats as long as we have a proper lib in the ecosystem do do that. | ||
def extract_metadata(%Attachment{} = atch), do: atch | ||
|
||
@doc """ | ||
Calls the underlyning storage's `put` function. | ||
Check the module `Dropkick.Storage` for documentation about the available options. | ||
""" | ||
def put(attachable, opts \\ []), | ||
do: Dropkick.Storage.current().put(attachable, opts) | ||
|
||
@doc """ | ||
Calls the underlyning storage's `read` function. | ||
Check the module `Dropkick.Storage` for documentation about the available options. | ||
""" | ||
def read(attachable, opts \\ []), | ||
do: Dropkick.Storage.current().read(attachable, opts) | ||
|
||
@doc """ | ||
Calls the underlyning storage's `copy` function. | ||
Check the module `Dropkick.Storage` for documentation about the available options. | ||
""" | ||
def copy(attachable, dest, opts \\ []), | ||
do: Dropkick.Storage.current().copy(attachable, dest, opts) | ||
|
||
@doc """ | ||
Calls the underlyning storage's `delete` function. | ||
Check the module `Dropkick.Storage` for documentation about the available options. | ||
""" | ||
def delete(attachable, opts \\ []), | ||
do: Dropkick.Storage.current().delete(attachable, opts) | ||
|
||
defp transform_stream(atch, transforms) do | ||
Task.Supervisor.async_stream_nolink(Dropkick.TransformTaskSupervisor, transforms, fn | ||
{:thumbnail, size, params} -> | ||
with {:ok, transform} <- Dropkick.Transform.thumbnail(atch, size, params), | ||
{:ok, version} <- put(transform, folder: Path.dirname(transform.key)) do | ||
version | ||
end | ||
|
||
transform -> | ||
raise "Not a valid transform param #{inspect(transform)}" | ||
end) | ||
end | ||
@moduledoc File.read!("README.md") | ||
end |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.