radiator icon indicating copy to clipboard operation
radiator copied to clipboard

Storage: Support external URLs

Open eteubert opened this issue 6 years ago • 0 comments

Currently only S3 compatible storage works. However, we want to be more flexible. One use case is having external storage where Radiator simply knows the file URL and nothing else.

My first attempt is below. The idea is to still use Arc but add a :storage_type to the audiofile schema and switch to a different url/store implementation when it's "external_url".

Supposedly Arc.Actions.Url.url/3 is overridable, however my url/3 implementation never gets called and dialyzer also complains that it can never be called ¯\(ツ)

diff --git a/lib/radiator/media/audio_file.ex b/lib/radiator/media/audio_file.ex
index 03ee231..e1cb21f 100644
--- a/lib/radiator/media/audio_file.ex
+++ b/lib/radiator/media/audio_file.ex
@@ -8,6 +8,7 @@ defmodule Radiator.Media.AudioFile do
   import Arc.Ecto.Changeset
 
   alias Radiator.Media
+  alias __MODULE__
 
   schema "audio_files" do
     field :file, Media.AudioFile.Type
@@ -15,6 +16,9 @@ defmodule Radiator.Media.AudioFile do
     field :mime_type, :string
     field :byte_length, :integer
 
+    field :storage_type, :string
+    field :external_url, :string
+
     has_many :attachments,
              {"episode_attachments", Media.EpisodeAttachment},
              foreign_key: :audio_id
@@ -39,6 +43,20 @@ defmodule Radiator.Media.AudioFile do
     RadiatorWeb.Router.Helpers.tracking_url(RadiatorWeb.Endpoint, :show, audio.id)
   end
 
+  # arc override
+  def url(
+        {_file, %AudioFile{storage_type: "external_url", external_url: url}},
+        _version,
+        _options
+      ) do
+    url
+  end
+
+  # default implementation
+  def url(file, version, options) do
+    super(file, version, options)
+  end
+
   # arc override
   def storage_dir(_version, {_file, audio}) do
     "audio/#{audio.id}"
diff --git a/priv/repo/migrations/20190420102616_create_audio.exs b/priv/repo/migrations/20190420102616_create_audio.exs
index 9f17d79..827f23b 100644
--- a/priv/repo/migrations/20190420102616_create_audio.exs
+++ b/priv/repo/migrations/20190420102616_create_audio.exs
@@ -8,6 +8,9 @@ defmodule Radiator.Repo.Migrations.CreateAudioFile do
       add :byte_length, :integer
       add :file, :string
 
+      add :storage_type, :string
+      add :external_url, :text
+
       timestamps()
     end
   end

(diff based on the public-file-access branch)

eteubert avatar May 14 '19 12:05 eteubert