stavro / arc

:paperclip: Flexible file upload and attachment library for Elixir
1.16k stars 210 forks source link

Using arc as standalone storage without saving filename #261

Open pradosh987 opened 6 years ago

pradosh987 commented 6 years ago

I was looking into using arc with phoenix to handle some images. Even though documentation is good enough, I am still confused about filename used in while storing a file and calling url method. General approach is to create a string field in database column and save file name into it. Even ecto_arc is built around same concept. So my question is, suppose I override filename method and make it dependent only model.uuid(actual uuid but not primary key) then why is it necessary to store filename in database in first place, or why is it required to pass filename while calling store or url methods. In my case filename serve no useful purpose. Shouldn't I able to do so by passing model as scope only?

Consider an example

schema "user" do
    field :name, :string
    field :email, :string
    field :has_avatar, :boolean
    field :has_cover, :boolean

    timestamps()
end

defmodule Firefly.Files.Avatar do
  use Arc.Definition
  @versions [:original]

  def filename(version, {_, scope}) do
    "avatar_#{scope.uuid}"
  end

  def storage_dir(version, {file, scope}) do
    "uploads/images/#{scope.uuid}/"
  end
end

defmodule Firefly.Files.Cover do
  use Arc.Definition
  @versions [:original]

  def filename(version, {_, scope}) do
    "cover_#{scope.uuid}"
  end

  def storage_dir(version, {file, scope}) do
    "uploads/images/#{scope.uuid}/"
  end
end

Two boolean fields can tell if user has uploaded avatar and cover image.

Note: I am coming from Ruby on Rails and relatively new to Elixir eco system, so please forgive if I make some mistake or wrong assumption

plasticine commented 6 years ago

Yeah, I’m kinda in the same boat — this design seems really weird to me. I’m not sure what I’m missing. I literally just wrote this code;

MyappStorage.Avatar.url({"", user},:thumb)

Which seems kinda pointless...