Skip to content

Commit

Permalink
Upgrade deps, upgrade Elixir and OTP version, add styler and fix dial…
Browse files Browse the repository at this point in the history
…yzer
  • Loading branch information
simonprev committed Sep 5, 2024
1 parent b060cb4 commit 782fd1a
Show file tree
Hide file tree
Showing 28 changed files with 92 additions and 59 deletions.
1 change: 1 addition & 0 deletions .formatter.exs
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
".credo.exs",
"{config,lib,test,rel}/**/*.{ex,exs}"
],
plugins: [Styler],
line_length: 180
]
4 changes: 2 additions & 2 deletions .tool-versions
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
elixir 1.14.0-otp-25
erlang 25.0.2
erlang 27.0
elixir 1.17.1-otp-27
10 changes: 6 additions & 4 deletions lib/plug_image_processing.ex
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
defmodule PlugImageProcessing do
@moduledoc false
alias PlugImageProcessing.Info
alias PlugImageProcessing.Middleware
alias PlugImageProcessing.Middlewares.SignatureKey
alias PlugImageProcessing.Operation
alias PlugImageProcessing.Source
alias Vix.Vips.Image

@type image :: Vix.Vips.Image.t()
@type image :: Image.t()
@type config :: PlugImageProcessing.Config.t()
@type image_metadata :: PlugImageProcessing.ImageMetadata.t()

Expand Down Expand Up @@ -55,7 +57,7 @@ defmodule PlugImageProcessing do
end)

case image do
%Vix.Vips.Image{} = image -> {:ok, image}
%Image{} = image -> {:ok, image}
error -> error
end
end
Expand Down Expand Up @@ -85,7 +87,7 @@ defmodule PlugImageProcessing do
end
end

@spec get_image(map(), String.t(), config()) :: {:ok, image(), String.t() | nil, String.t()} | {:error, atom()}
@spec get_image(map(), String.t(), config()) :: {:ok, image(), String.t() | nil, String.t()} | {:error, atom()} | {:redirect, String.t()}
def get_image(params, operation_name, config) do
source = Enum.find_value(config.sources, &Source.cast(struct(&1), params))

Expand All @@ -98,6 +100,6 @@ defmodule PlugImageProcessing do

@spec write_to_buffer(image(), String.t()) :: {:ok, binary()} | {:error, term()}
def write_to_buffer(image, file_extension) do
Vix.Vips.Image.write_to_buffer(image, file_extension)
Image.write_to_buffer(image, file_extension)
end
end
1 change: 1 addition & 0 deletions lib/plug_image_processing/config.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
defmodule PlugImageProcessing.Config do
@moduledoc false
alias PlugImageProcessing.Middlewares
alias PlugImageProcessing.Operations
alias PlugImageProcessing.Sources
Expand Down
1 change: 1 addition & 0 deletions lib/plug_image_processing/image_metadata.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
defmodule PlugImageProcessing.ImageMetadata do
@moduledoc false
@derive Jason.Encoder
defstruct channels: nil, has_alpha: nil, height: nil, width: nil

Expand Down
5 changes: 3 additions & 2 deletions lib/plug_image_processing/middlewares/allowed_origins.ex
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
defmodule PlugImageProcessing.Middlewares.AllowedOrigins do
@moduledoc false
defstruct config: nil

defimpl PlugImageProcessing.Middleware do
require Logger

import Plug.Conn

require Logger

def enabled?(middleware, conn) do
not is_nil(conn.params["url"]) and is_list(middleware.config.allowed_origins)
end
Expand Down
1 change: 1 addition & 0 deletions lib/plug_image_processing/middlewares/cache_headers.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
defmodule PlugImageProcessing.Middlewares.CacheHeaders do
@moduledoc false
defstruct config: nil

defimpl PlugImageProcessing.Middleware do
Expand Down
7 changes: 4 additions & 3 deletions lib/plug_image_processing/middlewares/signature_key.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
defmodule PlugImageProcessing.Middlewares.SignatureKey do
@moduledoc false
defstruct config: nil

def generate_signature(url, config) do
Expand All @@ -11,17 +12,17 @@ defmodule PlugImageProcessing.Middlewares.SignatureKey do
|> URI.decode_query()
|> Enum.sort_by(fn {key, _} -> key end)
|> Map.new()
|> Map.drop(["sign"])
|> Map.delete("sign")
|> URI.encode_query()

Base.url_encode64(:crypto.mac(:hmac, :sha256, config.url_signature_key, url_path <> url_query))
end

defimpl PlugImageProcessing.Middleware do
require Logger

import Plug.Conn

require Logger

def enabled?(middleware, _conn), do: is_binary(middleware.config.url_signature_key)

def run(middleware, conn) do
Expand Down
11 changes: 7 additions & 4 deletions lib/plug_image_processing/operations/crop.ex
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
defmodule PlugImageProcessing.Operations.Crop do
defstruct image: nil, left: 0, top: 0, width: nil, height: nil, gravity: nil

@moduledoc false
import PlugImageProcessing.Options

alias Vix.Vips.Operation

defstruct image: nil, left: 0, top: 0, width: nil, height: nil, gravity: nil

def new(image, params, _config) do
with {:ok, width} <- cast_integer(params["width"]),
{:ok, left} <- cast_integer(params["left"], 0),
Expand Down Expand Up @@ -30,11 +33,11 @@ defmodule PlugImageProcessing.Operations.Crop do
end

def process(%{gravity: "smart"} = operation, _config) do
Vix.Vips.Operation.smartcrop(operation.image, operation.width, operation.height)
Operation.smartcrop(operation.image, operation.width, operation.height)
end

def process(operation, _config) do
Vix.Vips.Operation.extract_area(
Operation.extract_area(
operation.image,
operation.left,
operation.top,
Expand Down
1 change: 1 addition & 0 deletions lib/plug_image_processing/operations/echo.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
defmodule PlugImageProcessing.Operations.Echo do
@moduledoc false
defstruct image: nil

def new(image, _params, _config) do
Expand Down
1 change: 1 addition & 0 deletions lib/plug_image_processing/operations/extract_area.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
defmodule PlugImageProcessing.Operations.ExtractArea do
@moduledoc false
import PlugImageProcessing.Options

def new(image, params, _config) do
Expand Down
5 changes: 3 additions & 2 deletions lib/plug_image_processing/operations/flip.ex
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
defmodule PlugImageProcessing.Operations.Flip do
defstruct image: nil, direction: nil

@moduledoc false
import PlugImageProcessing.Options

defstruct image: nil, direction: nil

def new(image, params, _config) do
with {:ok, direction} <- cast_direction(params["flip"], :VIPS_DIRECTION_HORIZONTAL),
{:ok, direction} <- cast_direction(params["direction"], direction),
Expand Down
5 changes: 3 additions & 2 deletions lib/plug_image_processing/operations/info.ex
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
defmodule PlugImageProcessing.Operations.Info do
defstruct image: nil

@moduledoc false
alias Vix.Vips.Image

defstruct image: nil

def new(_image, _params, _config) do
{:error, :invalid_operation}
end
Expand Down
5 changes: 3 additions & 2 deletions lib/plug_image_processing/operations/pipeline.ex
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
defmodule PlugImageProcessing.Operations.Pipeline do
defstruct image: nil, operations: nil

@moduledoc false
import PlugImageProcessing.Options

defstruct image: nil, operations: nil

def new(image, params, _config) do
with {:ok, operations} <- cast_json(params["operations"]) do
{:ok,
Expand Down
11 changes: 7 additions & 4 deletions lib/plug_image_processing/operations/resize.ex
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
defmodule PlugImageProcessing.Operations.Resize do
defstruct image: nil, width: nil, height: nil

@moduledoc false
import PlugImageProcessing.Options

alias Vix.Vips.Image

defstruct image: nil, width: nil, height: nil

def new(image, params, _config) do
with {:ok, width} <- cast_integer(params["w"] || params["width"]),
{:ok, height} <- cast_integer(params["h"] || params["height"]) do
Expand All @@ -25,8 +28,8 @@ defmodule PlugImageProcessing.Operations.Resize do
end

def process(operation, _config) do
hscale = operation.width / Vix.Vips.Image.width(operation.image) * 1.0
vscale = if operation.height, do: operation.height / Vix.Vips.Image.height(operation.image)
hscale = operation.width / Image.width(operation.image) * 1.0
vscale = if operation.height, do: operation.height / Image.height(operation.image)

options = PlugImageProcessing.Options.build(vscale: vscale)

Expand Down
1 change: 1 addition & 0 deletions lib/plug_image_processing/operations/smartcrop.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
defmodule PlugImageProcessing.Operations.Smartcrop do
@moduledoc false
import PlugImageProcessing.Options

def new(image, params, _config) do
Expand Down
5 changes: 3 additions & 2 deletions lib/plug_image_processing/operations/watermark_image.ex
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
defmodule PlugImageProcessing.Operations.WatermarkImage do
defstruct image: nil, sub: nil, left: nil, top: nil, right: nil, bottom: nil, http_client: nil

@moduledoc false
import PlugImageProcessing.Options

defstruct image: nil, sub: nil, left: nil, top: nil, right: nil, bottom: nil, http_client: nil

def new(image, params, config) do
with {:ok, sub} <- cast_remote_image(params["image"], "watermarkimage", config),
{:ok, left} <- cast_integer(params["left"]),
Expand Down
7 changes: 5 additions & 2 deletions lib/plug_image_processing/options.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
defmodule PlugImageProcessing.Options do
@moduledoc false
alias PlugImageProcessing.Sources.URL

def build(options) do
options
|> Enum.map(fn
Expand All @@ -11,7 +14,7 @@ defmodule PlugImageProcessing.Options do

def encode_suffix(options) do
options = Enum.map_join(options, ",", fn {key, value} -> "#{key}=#{value}" end)
if options !== "", do: "[#{options}]", else: options
if options === "", do: options, else: "[#{options}]"
end

def cast_direction(value, default \\ nil)
Expand All @@ -25,7 +28,7 @@ defmodule PlugImageProcessing.Options do
def cast_boolean(_, default), do: {:ok, default}

def cast_remote_image(url, operation_name, config) do
with %PlugImageProcessing.Sources.URL{} = source <- PlugImageProcessing.Source.cast(%PlugImageProcessing.Sources.URL{}, %{"url" => url}),
with %URL{} = source <- PlugImageProcessing.Source.cast(%URL{}, %{"url" => url}),
{:ok, image, _, _} <- PlugImageProcessing.Source.get_image(source, operation_name, config) do
{:ok, image}
end
Expand Down
1 change: 1 addition & 0 deletions lib/plug_image_processing/sources/http_client/hackney.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
defmodule PlugImageProcessing.Sources.HTTPClient.Hackney do
@moduledoc false
@behaviour PlugImageProcessing.Sources.HTTPClient

def get(url, max_length) do
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
defmodule PlugImageProcessing.Sources.HTTPClient do
@moduledoc false
@callback get(url :: String.t(), max_length :: non_neg_integer()) :: {:ok, binary(), Keyword.t()} | {:http_error, any()} | {:error, any()}
end
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
defmodule PlugImageProcessing.Sources.HTTPClientCache.Default do
@moduledoc false
@behaviour PlugImageProcessing.Sources.HTTPClientCache

def invalid_source?(_source), do: false
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
defmodule PlugImageProcessing.Sources.HTTPClientCache do
@moduledoc false
@typep source :: PlugImageProcessing.Sources.URL.t()
@callback invalid_source?(source()) :: boolean()
@callback fetch_source(source()) :: nil | {:ok, binary(), Keyword.t()}
Expand Down
9 changes: 5 additions & 4 deletions lib/plug_image_processing/sources/url.ex
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
defmodule PlugImageProcessing.Sources.URL do
@moduledoc false
alias PlugImageProcessing.Options

defstruct uri: nil, params: nil

@type t :: %__MODULE__{}

alias PlugImageProcessing.Options

@types_extensions_mapping %{
"jpg" => ".jpg",
"jpeg" => ".jpg",
Expand Down Expand Up @@ -145,10 +146,10 @@ defmodule PlugImageProcessing.Sources.URL do
end

defimpl PlugImageProcessing.Source do
require Logger

alias PlugImageProcessing.Sources.URL

require Logger

def get_image(source, operation_name, config) do
with :ok <- maybe_redirect(source, operation_name, config),
{:ok, body, content_type, file_suffix} when is_binary(file_suffix) and is_binary(body) <- fetch_remote_image(source, config),
Expand Down
1 change: 1 addition & 0 deletions lib/plug_image_processing/web.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
defmodule PlugImageProcessing.Web do
@moduledoc false
use Plug.Builder

import Plug.Conn
Expand Down
5 changes: 3 additions & 2 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ defmodule PlugImageProcessing.Mixfile do
[
app: :plug_image_processing,
version: @version,
elixir: "~> 1.13",
elixir: "~> 1.17",
package: package(),
elixirc_paths: elixirc_paths(Mix.env()),
compilers: Mix.compilers(),
Expand Down Expand Up @@ -42,9 +42,10 @@ defmodule PlugImageProcessing.Mixfile do
{:hackney, "~> 1.18"},
{:telemetry, "~> 1.0"},
{:jason, "~> 1.0"},
{:telemetry_metrics, "~> 0.6"},
{:telemetry_metrics, "~> 0.6 or ~> 1.0"},

# Linting
{:styler, "~> 1.0", only: [:dev, :test], runtime: false},
{:credo, "~> 1.1", only: [:dev, :test]},
{:credo_envvar, "~> 0.1", only: [:dev, :test], runtime: false},
{:credo_naming, "~> 2.0", only: [:dev, :test], runtime: false},
Expand Down
Loading

0 comments on commit 782fd1a

Please sign in to comment.