Skip to content

Commit

Permalink
refactor: improve uploaders (#522)
Browse files Browse the repository at this point in the history
  • Loading branch information
joaodiaslobo authored Aug 20, 2024
1 parent 0f6c217 commit b18ce9b
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 42 deletions.
28 changes: 7 additions & 21 deletions lib/atomic/uploader.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,23 @@ defmodule Atomic.Uploader do
Put `use Atomic.Uploader` on top of your uploader module to use it.
"""

@versions [:original, :medium, :thumb]
@extensions_whitelist ~w(.svg .jpg .jpeg .png)

defmacro __using__(_) do
defmacro __using__(opts) do
quote do
use Waffle.Definition
use Waffle.Ecto.Definition

def validate({file, _}) do
file.file_name
|> Path.extname()
|> String.downcase()
|> then(&Enum.member?(Atomic.Uploader.extensions_whitelist(), &1))
|> case do
file_extension = file.file_name |> Path.extname() |> String.downcase()

case Enum.member?(extension_whitelist(), file_extension) do
true -> :ok
false -> {:error, "invalid file type"}
false -> {:error, "invalid file extension"}
end
end

def transform(:thumb, _) do
{:convert, "-strip -thumbnail 100x150^ -gravity center -extent 100x150 -format png", :png}
end

def transform(:medium, _) do
{:convert, "-strip -thumbnail 400x600^ -gravity center -extent 400x600 -format png", :png}
def extension_whitelist do
Keyword.get(unquote(opts), :extensions, [])
end

def filename(version, _), do: version
end
end

def versions, do: @versions
def extensions_whitelist, do: @extensions_whitelist
end
12 changes: 9 additions & 3 deletions lib/atomic/uploaders/banner.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,17 @@ defmodule Atomic.Uploaders.Banner do
@moduledoc """
Uploader for department banners.
"""
use Atomic.Uploader
use Atomic.Uploader, extensions: ~w(.jpg .jpeg .png)

alias Atomic.Organizations.Department

def storage_dir(_version, {_file, %Department{} = scope}) do
"uploads/atomic/departments/#{scope.id}/banner"
@versions [:original]

def storage_dir(_version, {_file, %Department{} = department}) do
"uploads/atomic/departments/#{department.id}/banner"
end

def filename(version, _) do
version
end
end
12 changes: 9 additions & 3 deletions lib/atomic/uploaders/logo.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@ defmodule Atomic.Uploaders.Logo do
@moduledoc """
Uploader for organization logos.
"""
use Atomic.Uploader
use Atomic.Uploader, extensions: ~w(.jpg .jpeg .png .svg)
alias Atomic.Organizations.Organization

def storage_dir(_version, {_file, %Organization{} = scope}) do
"uploads/atomic/logos/#{scope.id}"
@versions [:original]

def storage_dir(_version, {_file, %Organization{} = organization}) do
"uploads/atomic/organizations/#{organization.id}/logo"
end

def filename(version, _) do
version
end
end
12 changes: 9 additions & 3 deletions lib/atomic/uploaders/partner_image.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,17 @@ defmodule Atomic.Uploaders.PartnerImage do
@moduledoc """
Uploader for partner images.
"""
use Atomic.Uploader
use Atomic.Uploader, extensions: ~w(.jpg .jpeg .png)

alias Atomic.Organizations.Partner

def storage_dir(_version, {_file, %Partner{} = scope}) do
"uploads/atomic/partners/#{scope.id}"
@versions [:original]

def storage_dir(_version, {_file, %Partner{} = partner}) do
"uploads/atomic/partners/#{partner.id}/logo"
end

def filename(version, _) do
version
end
end
16 changes: 11 additions & 5 deletions lib/atomic/uploaders/post.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,22 @@ defmodule Atomic.Uploaders.Post do
@moduledoc """
Uploader for posts.
"""
use Atomic.Uploader
use Atomic.Uploader, extensions: ~w(.jpg .jpeg .png .svg)

alias Atomic.Activities.Activity
alias Atomic.Organizations.Announcement

def storage_dir(_version, {_file, %Activity{} = scope}) do
"uploads/atomic/activities/#{scope.id}"
@versions [:original]

def storage_dir(_version, {_file, %Activity{} = activity}) do
"uploads/atomic/activities/#{activity.id}/image"
end

def storage_dir(_version, {_file, %Announcement{} = announcement}) do
"uploads/atomic/announcements/#{announcement.id}/image"
end

def storage_dir(_version, {_file, %Announcement{} = scope}) do
"uploads/atomic/announcements/#{scope.id}"
def filename(version, _) do
version
end
end
12 changes: 9 additions & 3 deletions lib/atomic/uploaders/profile_picture.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@ defmodule Atomic.Uploaders.ProfilePicture do
@moduledoc """
Uploader for profile pictures.
"""
use Atomic.Uploader
use Atomic.Uploader, extensions: ~w(.jpg .jpeg .png .gif)
alias Atomic.Accounts.User

def storage_dir(_version, {_file, %User{} = scope}) do
"uploads/atomic/profile_pictures/#{scope.id}"
@versions [:original]

def storage_dir(_version, {_file, %User{} = user}) do
"uploads/atomic/users/#{user.id}/profile_picture"
end

def filename(version, _) do
version
end
end
3 changes: 1 addition & 2 deletions lib/atomic_web/live/activity_live/form_component.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ defmodule AtomicWeb.ActivityLive.FormComponent do
use AtomicWeb, :live_component

alias Atomic.Activities
alias Atomic.Uploader
alias AtomicWeb.Components.ImageUploader

import AtomicWeb.Components.Forms
Expand All @@ -15,7 +14,7 @@ defmodule AtomicWeb.ActivityLive.FormComponent do
socket
|> assign(assigns)
|> assign_form(changeset)
|> allow_upload(:image, accept: Uploader.extensions_whitelist(), max_entries: 1)}
|> allow_upload(:image, accept: Uploaders.Post.extension_whitelist(), max_entries: 1)}
end

@impl true
Expand Down
2 changes: 1 addition & 1 deletion lib/atomic_web/live/department_live/form_component.ex
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ defmodule AtomicWeb.DepartmentLive.FormComponent do

{:ok,
socket
|> allow_upload(:image, accept: Atomic.Uploader.extensions_whitelist(), max_entries: 1)
|> allow_upload(:image, accept: Uploaders.Banner.extension_whitelist(), max_entries: 1)
|> assign(assigns)
|> assign(:changeset, changeset)}
end
Expand Down
2 changes: 1 addition & 1 deletion lib/atomic_web/live/partner_live/form_component.ex
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ defmodule AtomicWeb.PartnerLive.FormComponent do

{:ok,
socket
|> allow_upload(:image, accept: Atomic.Uploader.extensions_whitelist(), max_entries: 1)
|> allow_upload(:image, accept: Uploaders.PartnerImage.extension_whitelist(), max_entries: 1)
|> assign(assigns)
|> assign(:changeset, changeset)}
end
Expand Down

0 comments on commit b18ce9b

Please sign in to comment.