Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for changing/adding file extension #108

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion lib/waffle/definition/versioning.ex
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ defmodule Waffle.Definition.Versioning do
quote do
@versions [:original]
@before_compile Waffle.Definition.Versioning

def extension(_, {file, _}), do: Path.extname(file.file_name)
defoverridable extension: 2
end
end

Expand All @@ -38,7 +41,7 @@ defmodule Waffle.Definition.Versioning do
case conversion do
:skip -> nil
{_, _, ext} -> "#{name}.#{ext}"
_ -> "#{name}#{Path.extname(file.file_name)}"
_ -> "#{name}#{definition.extension(version, {file, scope})}"
end
end

Expand Down
21 changes: 21 additions & 0 deletions test/actions/store_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ defmodule WaffleTest.Actions.Store do
def transform(:skipped, _), do: :skip
def transform(_, _), do: :noaction
def __versions, do: [:original, :thumb, :skipped]
def extension(_, {file, _}), do: Path.extname(file.file_name)
end

defmodule DummyDefinitionWithHeaders do
Expand All @@ -24,6 +25,7 @@ defmodule WaffleTest.Actions.Store do

def transform(_, _), do: :noaction
def __versions, do: [:original, :thumb, :skipped]
def extension(_, {file, _}), do: Path.extname(file.file_name)
def remote_file_headers(%URI{host: "www.google.com"}), do: [{"User-Agent", "MyApp"}]
end

Expand All @@ -36,6 +38,16 @@ defmodule WaffleTest.Actions.Store do
def __versions, do: [:original, :thumb, :skipped]
end

defmodule DummyDefinitionWithExtension do
use Waffle.Actions.Store
use Waffle.Definition.Storage

def validate({file, _}), do: true
def transform(_, _), do: :noaction
def __versions, do: [:original]
def extension(_, {file, _}), do: ".jpeg"
end

test "checks file existence" do
assert DummyDefinition.store("non-existent-file.png") == {:error, :invalid_file_path}
end
Expand Down Expand Up @@ -151,6 +163,15 @@ defmodule WaffleTest.Actions.Store do
end
end

test "sets extension from extension/2" do
with_mock Waffle.Storage.S3,
put: fn DummyDefinitionWithExtension, _, {%{file_name: "image.jpeg", path: @img}, nil} ->
{:ok, "resp"}
end do
assert DummyDefinitionWithExtension.store(@img) == {:ok, "image.png"}
end
end

test "sets remote filename from content-disposition header when available" do
with_mocks([
{
Expand Down
1 change: 1 addition & 0 deletions test/actions/url_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ defmodule WaffleTest.Actions.Url do
def __versions, do: [:original, :thumb, :skipped]
def transform(:skipped, _), do: :skip
def transform(_, _), do: :noaction
def extension(_, {file, _}), do: Path.extname(file.file_name)
def default_url(version, scope) when is_nil(scope), do: "dummy-#{version}"
def default_url(version, scope), do: "dummy-#{version}-#{scope}"
def __storage, do: Waffle.Storage.S3
Expand Down
Loading