Skip to content

Commit

Permalink
Merge pull request #13 from appunite/fix-formatting
Browse files Browse the repository at this point in the history
Fix formatting
  • Loading branch information
hauleth committed Oct 3, 2018
2 parents 7d941e4 + 2a7d231 commit 8d2fe5a
Show file tree
Hide file tree
Showing 17 changed files with 87 additions and 61 deletions.
3 changes: 2 additions & 1 deletion .formatter.exs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
[
inputs: [
"mix.exs",
"{config,lib,test}/**.{ex,exs}"
"{config,lib,test}/*.{ex,exs}",
"{config,lib,test}/**/*.{ex,exs}"
],
line_length: 79,
import_deps: [:phoenix]
Expand Down
6 changes: 2 additions & 4 deletions lib/imager.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@ defmodule Imager do

require Logger

alias Imager.Tool
alias Imager.Store
alias Imager.Stats

alias Imager.Stats
alias Imager.Store
alias Imager.Tool

@type size :: non_neg_integer() | :unknown
@type mime :: binary()
Expand Down
6 changes: 5 additions & 1 deletion lib/imager/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ defmodule Imager.Application do
ImagerWeb.Endpoint
]

Application.put_env(:sentry, :dsn, Application.get_env(:imager, :sentry_dsn))
Application.put_env(
:sentry,
:dsn,
Application.get_env(:imager, :sentry_dsn)
)

Imager.Stats.start()
{:ok, _} = Logger.add_backend(Sentry.LoggerBackend)
Expand Down
4 changes: 3 additions & 1 deletion lib/imager/config/port_normalization.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ defmodule Imager.Config.PortNormalization do
Normalize port values to integers
"""

def transform(:port, value) when is_binary(value), do: String.to_integer(value)
def transform(:port, value) when is_binary(value),
do: String.to_integer(value)

def transform(:port, value) when is_integer(value), do: value
def transform(_, value), do: value
end
38 changes: 20 additions & 18 deletions lib/imager/config/store_transform.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,38 +12,40 @@ defmodule Imager.Config.StoreTransform do
def transform(:stores, entries) do
Map.new(entries, fn {path, values} ->
with {:ok, store} <- get_store(values),
{:ok, cache} <- get_cache(values)
do
{:ok, cache} <- get_cache(values) do
path = to_string(path)

if path in @reserved, do: raise "'#{path}' is reserved name"
if String.contains?(path, "/"), do: raise "'#{path}' cannot contain '/'"
if path in @reserved, do: raise("'#{path}' is reserved name")

{path, %{
store: store,
cache: cache
}}
if String.contains?(path, "/"),
do: raise("'#{path}' cannot contain '/'")

{path,
%{
store: store,
cache: cache
}}
else
_ -> throw({:invalid, path, entries})
end
end)
catch
{:invalid, path, entries} ->
raise """
Invalid store `#{path}` definition, store needs to be defined in form
Invalid store `#{path}` definition, store needs to be defined in form
[stores.path]
type = {"S3" or "Local"}
[stores.path]
type = {"S3" or "Local"}
or
or
[stores.path.store]
type = {"S3" or "Local"}
[stores.path.cache]
type = {"S3" or "Local"}
[stores.path.store]
type = {"S3" or "Local"}
[stores.path.cache]
type = {"S3" or "Local"}
Got #{inspect entries}
"""
Got #{inspect(entries)}
"""
end

def transform(_k, value), do: value
Expand Down
16 changes: 11 additions & 5 deletions lib/imager/store.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ defmodule Imager.Store do
@type mime :: binary()

@type t :: %{
store: store(),
cache: store()
}
store: store(),
cache: store()
}
@type store :: {module(), keyword()}

@callback retrieve(path :: binary(), opts :: keyword()) ::
Expand All @@ -28,9 +28,15 @@ defmodule Imager.Store do
@doc """
Retreive file from store.
"""
@spec retrieve(store, binary, keyword) :: {:ok, {size, mime, stream}} | :error
@spec retrieve(store, binary, keyword) ::
{:ok, {size, mime, stream}} | :error
def retrieve({store, glob_opts}, path, options) do
Stats.increment("imager.store.retrieve", 1, Stats.tags(~w(module:#{store})))
Stats.increment(
"imager.store.retrieve",
1,
Stats.tags(~w(module:#{store}))
)

store.retrieve(path, Keyword.merge(glob_opts, options))
end

Expand Down
2 changes: 2 additions & 0 deletions lib/imager/store/local.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ defmodule Imager.Store.Local do
def retrieve(path, opts) do
dir = Keyword.get(opts, :dir, ".")
full_path = Path.join(dir, path)

extname =
case Path.extname(path) do
"." <> extname -> extname
extname -> extname
end

mime = MIME.type(extname)

with {:ok, %File.Stat{size: size}} <- File.stat(full_path) do
Expand Down
12 changes: 7 additions & 5 deletions lib/imager/store/s3.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
defmodule Imager.Store.S3 do
@behaviour Imager.Store

alias ExAws.S3

@moduledoc """
S3 compatible storage. It will try to stream files as much as possible
in both ways.
Expand Down Expand Up @@ -30,7 +32,7 @@ defmodule Imager.Store.S3 do
fn ->
%{body: body} =
bucket
|> ExAws.S3.initiate_multipart_upload(path, content_type: mime)
|> S3.initiate_multipart_upload(path, content_type: mime)
|> ExAws.request!(config)

{body.upload_id, 1, [], <<>>}
Expand All @@ -44,7 +46,7 @@ defmodule Imager.Store.S3 do
if byte_size(chunk) > 5 * 1024 * 1024 do
%{headers: headers} =
bucket
|> ExAws.S3.upload_part(path, id, idx, chunk)
|> S3.upload_part(path, id, idx, chunk)
|> ExAws.request!(config)

etag = header_find(headers, "Etag")
Expand All @@ -59,7 +61,7 @@ defmodule Imager.Store.S3 do
# 5Mi, so now we upload last chunk
%{headers: headers} =
bucket
|> ExAws.S3.upload_part(path, id, idx, data)
|> S3.upload_part(path, id, idx, data)
|> ExAws.request!(config)

etag = header_find(headers, "Etag")
Expand Down Expand Up @@ -88,7 +90,7 @@ defmodule Imager.Store.S3 do
defp get_chunk(bucket, path, {start_byte, end_byte}, config) do
%{body: body} =
bucket
|> ExAws.S3.get_object(path, range: "bytes=#{start_byte}-#{end_byte}")
|> S3.get_object(path, range: "bytes=#{start_byte}-#{end_byte}")
|> ExAws.request!(config)

body
Expand All @@ -97,7 +99,7 @@ defmodule Imager.Store.S3 do
defp get_file_size(bucket, path, config) do
with {:ok, %{headers: headers}} <-
bucket
|> ExAws.S3.head_object(path)
|> S3.head_object(path)
|> ExAws.request(config),
value when not is_nil(value) <-
header_find(headers, "Content-Length"),
Expand Down
20 changes: 11 additions & 9 deletions lib/imager/tool.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,23 @@ defmodule Imager.Tool do
Describe tool used by the current instance to convert images
"""

@type command :: {:thumbnail, binary()}
| {:strip, any()}
| {:gravity, binary()}
| {:extent, binary()}
| {:flatten, any()}
| {:background, any()}
| {:format, binary()}
@type command ::
{:thumbnail, binary()}
| {:strip, any()}
| {:gravity, binary()}
| {:extent, binary()}
| {:flatten, any()}
| {:background, any()}
| {:format, binary()}
@type commands :: [command]

require Logger

defmodule UnknownOption do
defexception [:option, :value, plug_status: 422]

def exception({option, value}), do: %__MODULE__{option: option, value: value}
def exception({option, value}),
do: %__MODULE__{option: option, value: value}

def message(%__MODULE__{option: option, value: value}) do
"Unknown command #{option} with value #{value}"
Expand Down Expand Up @@ -72,5 +74,5 @@ defmodule Imager.Tool do
defp string({"gravity", orientation}), do: {:gravity, orientation}
defp string({"strip", _}), do: {:strip, true}
defp string({"thumbnail", size}), do: {:thumbnail, size}
defp string(option), do: raise UnknownOption, option
defp string(option), do: raise(UnknownOption, option)
end
2 changes: 1 addition & 1 deletion lib/imager_web/controllers/health.ex
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
defmodule ImagerWeb.Controllers.Health do
use Phoenix.Controller, namespace: ImagerWeb

@description Mix.Project.config[:description]
@description Mix.Project.config()[:description]

def get(conn, _params) do
json(conn, %{
Expand Down
7 changes: 4 additions & 3 deletions lib/imager_web/instrumenter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ defmodule ImagerWeb.Instrumenter do
@moduledoc """
Instrument application.
This module gathers statistics about running application and sends them to StatsD compatible server.
This module gathers statistics about running application and sends them
to StatsD compatible server.
"""

def phoenix_controller_call(:start, _, %{conn: conn}) do
Expand All @@ -24,6 +25,6 @@ defmodule ImagerWeb.Instrumenter do
defp module_name("Elixir." <> name), do: name
defp module_name(name) when is_bitstring(name), do: name

defp module_name(name) when is_atom(name), do: module_name(Atom.to_string(name))
defp module_name(name) when is_atom(name),
do: module_name(Atom.to_string(name))
end

8 changes: 4 additions & 4 deletions lib/imager_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ defmodule ImagerWeb.Router do
import Phoenix.Controller

pipeline :api do
plug :accepts, ["json"]
plug(:accepts, ["json"])
end

scope "/", ImagerWeb.Controllers do
pipe_through :api
pipe_through(:api)

get "/health", Health, :get
get("/health", Health, :get)

get "/:store/*path", Image, :get
get("/:store/*path", Image, :get)
end
end
11 changes: 6 additions & 5 deletions test/imager/tool_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@ defmodule Imager.ToolTest do
# Defaults to PNG
assert {"image/png", _} = Tool.result("/file.jpg", [])

assert {"image/jpeg", _} = Tool.result("/file.jpg", [format: "jpg"])
assert {"image/jpeg", _} = Tool.result("/file.jpg", format: "jpg")
end

test "creates joined path with commands" do
assert {_, "/file_strip_thumbnail-190x190.png"} = Tool.result("/file.png", [
strip: true,
thumbnail: "190x190"
])
assert {_, "/file_strip_thumbnail-190x190.png"} =
Tool.result("/file.png",
strip: true,
thumbnail: "190x190"
)
end

test "creates proper path for each command" do
Expand Down
2 changes: 1 addition & 1 deletion test/imager_web/controllers/image_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ defmodule ImagerWeb.Controllers.ImageTest do
Application.put_env(:imager, :stores, %{
"test" => %{
store: {Imager.Store.Dummy, dir: "test/fixtures/"},
cache: {Imager.Store.Dummy, dir: "test/fixtures/"},
cache: {Imager.Store.Dummy, dir: "test/fixtures/"}
}
})
end
Expand Down
9 changes: 7 additions & 2 deletions test/imager_web/plug/verify_token_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ defmodule ImagerWeb.Plug.VerifyTokenTest do
end

test "has assigned empty actions", %{opts: opts} do
conn =
conn =
"/image.jpg"
|> get()
|> VerifyToken.call(opts)
Expand Down Expand Up @@ -69,7 +69,8 @@ defmodule ImagerWeb.Plug.VerifyTokenTest do

describe "passthrough with allowance and token" do
setup do
opts = VerifyToken.init(jwk: jwk("example"), allow_unsigned_passthrough: true)
opts =
VerifyToken.init(jwk: jwk("example"), allow_unsigned_passthrough: true)

{:ok, opts: opts}
end
Expand Down Expand Up @@ -123,6 +124,7 @@ defmodule ImagerWeb.Plug.VerifyTokenTest do

test "disallows connections with invalid path", %{key: key, opts: opts} do
token = sign(key, "/invalid.jpg", "strip=true")

conn =
"/image.jpg"
|> get(strip: true, token: token)
Expand All @@ -134,6 +136,7 @@ defmodule ImagerWeb.Plug.VerifyTokenTest do

test "disallows connections with invalid actions", %{key: key, opts: opts} do
token = sign(key, "/image.jpg", "thumbnail=120x120")

conn =
"/image.jpg"
|> get(strip: true, token: token)
Expand All @@ -145,6 +148,7 @@ defmodule ImagerWeb.Plug.VerifyTokenTest do

test "allows connections with valid token", %{key: key, opts: opts} do
token = sign(key, "/image.jpg", "strip=true")

conn =
"/image.jpg"
|> get(strip: true, token: token)
Expand All @@ -165,6 +169,7 @@ defmodule ImagerWeb.Plug.VerifyTokenTest do

test "fails with incorrect key", %{opts: opts} do
token = sign(jwk("invalid"), "/image.jpg", "strip=true")

conn =
"/image.jpg"
|> get(strip: true, token: token)
Expand Down
1 change: 0 additions & 1 deletion test/support/conn_case.ex
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ defmodule ImagerWeb.ConnCase do
end
end


setup _tags do
{:ok, conn: Phoenix.ConnTest.build_conn()}
end
Expand Down
1 change: 1 addition & 0 deletions test/support/dummy_store.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ defmodule Imager.Store.Dummy do
end

result

:error ->
if ref = Keyword.get(opts, :ref) do
send(self(), {:missed, ref, path})
Expand Down

0 comments on commit 8d2fe5a

Please sign in to comment.