diff --git a/SELFHOSTING.md b/SELFHOSTING.md index 6c102f7ef..bef329218 100644 --- a/SELFHOSTING.md +++ b/SELFHOSTING.md @@ -134,7 +134,7 @@ Required environment variables: | DB_HOST | Hostname | "domain.where.database.lives" | | DB_NAME | Name of the database in the PostgreSQL| "self_prod" | | SECRET_KEY_BASE | 64-bit sequence of random characters | \ | -| STATIC_PATH | Path to folder where uploaded files can be stored | "/home/self/uploads" | +| STATIC_PATH | Path to folder where uploaded files can be stored | "/tmp" | | UNSPLASH_ACCESS_KEY | Application access key registered on [Unsplash](https://unsplash.com/) (Image Catalog) | "hcejpnHRuFWL-fKXLYqhGBt1Dz0_tTjeNifgD01VkGE" | | UNSPLASH_APP_NAME | Application name registered on [Unsplash](https://unsplash.com/) (Image Catalog) | "Self" | diff --git a/core/config/dev.exs b/core/config/dev.exs index 8ea779f8a..475bdc7b1 100644 --- a/core/config/dev.exs +++ b/core/config/dev.exs @@ -1,5 +1,12 @@ import Config +upload_path = + File.cwd!() + |> Path.join("priv") + |> Path.join("static") + |> Path.join("uploads") + |> tap(&File.mkdir_p!/1) + # Only in tests, remove the complexity from the password hashing algorithm config :bcrypt_elixir, :log_rounds, 1 @@ -65,13 +72,7 @@ config :core, Systems.Email.Mailer, config :core, :apns_backend, Core.APNS.LoggingBackend -config :core, - :static_path, - File.cwd!() - |> Path.join("priv") - |> Path.join("static") - |> Path.join("uploads") - |> tap(&File.mkdir_p!/1) +config :core, :upload_path, upload_path config :core, :admins, @@ -85,23 +86,9 @@ config :core, # access_key_id: "my_access_key", # secret_access_key: "a_super_secret" -config :core, :content, - backend: Systems.Content.LocalFS, - local_fs_root_path: - File.cwd!() - |> Path.join("priv") - |> Path.join("static") - |> Path.join("content") - |> tap(&File.mkdir_p!/1) - -config :core, :feldspar, - backend: Systems.Feldspar.LocalFS, - local_fs_root_path: - File.cwd!() - |> Path.join("priv") - |> Path.join("static") - |> Path.join("feldspar_apps") - |> tap(&File.mkdir_p!/1) +config :core, :content, backend: Systems.Content.LocalFS + +config :core, :feldspar, backend: Systems.Feldspar.LocalFS try do import_config "dev.secret.exs" diff --git a/core/config/runtime.exs b/core/config/runtime.exs index 4981cf4ce..ee3e2ac5f 100644 --- a/core/config/runtime.exs +++ b/core/config/runtime.exs @@ -5,7 +5,7 @@ if config_env() == :prod do app_domain = System.fetch_env!("APP_DOMAIN") app_mail_domain = System.fetch_env!("APP_MAIL_DOMAIN") app_mail_noreply = "no-reply@#{app_mail_domain}" - static_path = System.fetch_env!("STATIC_PATH") + upload_path = System.fetch_env!("STATIC_PATH") # Allow enabling of features from an environment variable config :core, @@ -19,7 +19,7 @@ if config_env() == :prod do :admins, System.get_env("APP_ADMINS", "") |> String.split() |> Systems.Admin.Public.compile() - config :core, :static_path, static_path + config :core, :upload_path, upload_path config :core, CoreWeb.Endpoint, cache_static_manifest: "priv/static/cache_manifest.json", @@ -133,9 +133,7 @@ if config_env() == :prod do public_url: System.get_env("PUBLIC_S3_URL"), prefix: content_s3_prefix else - config :core, :content, - backend: Systems.Content.LocalFS, - local_fs_root_path: static_path + config :core, :content, backend: Systems.Content.LocalFS end if feldspar_s3_prefix = System.get_env("FELDSPAR_S3_PREFIX") do @@ -147,9 +145,7 @@ if config_env() == :prod do public_url: System.get_env("PUBLIC_S3_URL"), prefix: feldspar_s3_prefix else - config :core, :feldspar, - backend: Systems.Feldspar.LocalFS, - local_fs_root_path: static_path + config :core, :feldspar, backend: Systems.Feldspar.LocalFS end config :core, diff --git a/core/config/test.exs b/core/config/test.exs index e4ff51ca2..1db5ddb23 100644 --- a/core/config/test.exs +++ b/core/config/test.exs @@ -55,10 +55,8 @@ config :core, :bundle, :next config :core, :banking_backend, Systems.Banking.Dummy -config :core, :content, - backend: Systems.Content.LocalFS, - local_fs_root_path: "/tmp" +config :core, :upload_path, "/tmp" -config :core, :feldspar, - backend: Systems.Feldspar.LocalFS, - local_fs_root_path: "/tmp" +config :core, :content, backend: Systems.Content.LocalFS + +config :core, :feldspar, backend: Systems.Feldspar.LocalFS diff --git a/core/lib/core_web/controllers/uploaded_file.ex b/core/lib/core_web/controllers/uploaded_file.ex index e6b6f450c..e0fa0e0da 100644 --- a/core/lib/core_web/controllers/uploaded_file.ex +++ b/core/lib/core_web/controllers/uploaded_file.ex @@ -1,9 +1,9 @@ defmodule CoreWeb.UploadedFileController do use CoreWeb, :controller - import CoreWeb.FileUploader, only: [get_static_path: 1] + import CoreWeb.FileUploader, only: [get_upload_path: 1] def get(conn, %{"filename" => name}) do - path = get_static_path(name) + path = get_upload_path(name) send_file(conn, 200, path) end end diff --git a/core/lib/core_web/file_uploader.ex b/core/lib/core_web/file_uploader.ex index 938a41589..712bc932f 100644 --- a/core/lib/core_web/file_uploader.ex +++ b/core/lib/core_web/file_uploader.ex @@ -2,20 +2,23 @@ defmodule CoreWeb.FileUploader do @moduledoc """ """ - @allowed_filename_pattern ~r"^[a-z0-9][a-z0-9\-]+[a-z0-9]\.[a-z]{3,4}$" + @allowed_filename_pattern ~r"^[a-z0-9][a-z0-9\-]+[a-z0-9](\.[a-z]{3,4})?$" @callback process_file(socket :: Socket.t(), uploaded_file :: any()) :: Socket.t() - def get_static_path(filename) do + def get_upload_path(filename) do unless Regex.match?(@allowed_filename_pattern, filename) do throw(:invalid_filename) end - root = Application.get_env(:core, :static_path, "priv/static/uploads") + root = Application.get_env(:core, :upload_path, "priv/static/uploads") Path.join(root, filename) end - defmacro __using__(accept) do + defmacro __using__(opts) do + store = Keyword.get(opts, :store, Systems.Content.Public) + accept = Keyword.get(opts, :accept, ~w"*.*") + quote do @behaviour CoreWeb.FileUploader @@ -43,11 +46,9 @@ defmodule CoreWeb.FileUploader do def consume_file(socket, entry) do consume_uploaded_entry(socket, entry, fn %{path: tmp_path} -> - file = "#{entry.uuid}.#{ext(entry)}" - local_full_path = CoreWeb.FileUploader.get_static_path(file) - File.cp!(tmp_path, local_full_path) - local_relative_path = CoreWeb.Endpoint.static_path("/uploads/#{file}") - {:ok, {local_relative_path, local_full_path, entry.client_name}} + path = apply(unquote(store), :store, [tmp_path, entry.client_name]) + public_url = apply(unquote(store), :get_public_url, [path]) + {:ok, {path, public_url, entry.client_name}} end) end diff --git a/core/lib/core_web/live/user/forms/profile.ex b/core/lib/core_web/live/user/forms/profile.ex index a575a6ba9..c805137c7 100644 --- a/core/lib/core_web/live/user/forms/profile.ex +++ b/core/lib/core_web/live/user/forms/profile.ex @@ -1,6 +1,6 @@ defmodule CoreWeb.User.Forms.Profile do use CoreWeb.LiveForm - use CoreWeb.FileUploader, ~w(.png .jpg .jpeg) + use CoreWeb.FileUploader, accept: ~w(.png .jpg .jpeg) alias Core.Accounts alias Core.Accounts.UserProfileEdit @@ -11,9 +11,9 @@ defmodule CoreWeb.User.Forms.Profile do @impl true def process_file( %{assigns: %{entity: entity}} = socket, - {local_relative_path, _local_full_path, _remote_file} + {_path, photo_url, _original_filename} ) do - save(socket, entity, :auto_save, %{photo_url: local_relative_path}) + save(socket, entity, :auto_save, %{photo_url: photo_url}) end # Handle Selector Update diff --git a/core/systems/admin/import_rewards_page.ex b/core/systems/admin/import_rewards_page.ex index b4b7949a5..1a31528d3 100644 --- a/core/systems/admin/import_rewards_page.ex +++ b/core/systems/admin/import_rewards_page.ex @@ -1,7 +1,7 @@ defmodule Systems.Admin.ImportRewardsPage do use CoreWeb, :live_view use CoreWeb.Layouts.Workspace.Component, :import_rewards - use CoreWeb.FileUploader, ~w(.csv) + use CoreWeb.FileUploader, accept: ~w(.csv) import CoreWeb.Layouts.Workspace.Component @@ -19,10 +19,11 @@ defmodule Systems.Admin.ImportRewardsPage do @impl true def process_file( %{assigns: %{entity: entity}} = socket, - {_local_relative_path, local_full_path, remote_file} + {path, _url, original_filename} ) do changeset = - entity |> Admin.ImportRewardsModel.changeset(%{session_key: Path.rootname(remote_file)}) + entity + |> Admin.ImportRewardsModel.changeset(%{session_key: Path.rootname(original_filename)}) socket |> assign( @@ -30,8 +31,8 @@ defmodule Systems.Admin.ImportRewardsPage do lines_error: [], lines_unknown: [], lines_valid: [], - local_file: local_full_path, - uploaded_file: remote_file + local_file: path, + uploaded_file: original_filename ) end diff --git a/core/systems/assignment/crew_page.ex b/core/systems/assignment/crew_page.ex index f88fa0d6a..ee8ce8ee5 100644 --- a/core/systems/assignment/crew_page.ex +++ b/core/systems/assignment/crew_page.ex @@ -117,11 +117,13 @@ defmodule Systems.Assignment.CrewPage do key: key } - assignment - |> Storage.Private.storage_info() - |> Storage.Public.store(panel_info, data, meta_data) - - socket + if storage_info = Storage.Private.storage_info(assignment) do + Storage.Public.store(storage_info, panel_info, data, meta_data) + else + message = "Please setup connection to a data storage" + Logger.error(message) + socket |> put_flash(:error, message) + end end @impl true diff --git a/core/systems/assignment/info_form.ex b/core/systems/assignment/info_form.ex index d01388156..5892e7305 100644 --- a/core/systems/assignment/info_form.ex +++ b/core/systems/assignment/info_form.ex @@ -1,7 +1,7 @@ defmodule Systems.Assignment.InfoForm do use CoreWeb.LiveForm, :fabric use Fabric.LiveComponent - use CoreWeb.FileUploader, ~w(.png .jpg .jpeg .svg) + use CoreWeb.FileUploader, accept: ~w(.png .jpg .jpeg .svg) import Core.ImageCatalog, only: [image_catalog: 0] import Frameworks.Pixel.Form @@ -11,17 +11,12 @@ defmodule Systems.Assignment.InfoForm do alias CoreWeb.UI.ImageCatalogPicker alias Systems.Assignment - alias Systems.Content @impl true def process_file( %{assigns: %{entity: entity}} = socket, - {_local_relative_path, local_full_path, _remote_file} + {_path, logo_url, _original_filename} ) do - logo_url = - Content.Public.store(local_full_path) - |> Content.Public.get_public_url() - socket |> save(entity, :auto_save, %{logo_url: logo_url}) end diff --git a/core/systems/benchmark/import_form.ex b/core/systems/benchmark/import_form.ex index efe579954..22edf477c 100644 --- a/core/systems/benchmark/import_form.ex +++ b/core/systems/benchmark/import_form.ex @@ -1,12 +1,12 @@ defmodule Systems.Benchmark.ImportForm do use CoreWeb, :live_component - use CoreWeb.FileUploader, ~w(.csv) + use CoreWeb.FileUploader, accept: ~w(.csv) @impl true - def process_file(socket, {_local_relative_path, local_full_path, remote_file}) do + def process_file(socket, {path, _url, original_filename}) do socket - |> assign(csv_local_path: local_full_path) - |> assign(csv_remote_file: remote_file) + |> assign(csv_local_path: path) + |> assign(csv_remote_file: original_filename) end @impl true diff --git a/core/systems/content/_public.ex b/core/systems/content/_public.ex index 6fd06056a..cfc5d1bdd 100644 --- a/core/systems/content/_public.ex +++ b/core/systems/content/_public.ex @@ -11,8 +11,8 @@ defmodule Systems.Content.Public do alias Systems.Content.TextItemModel, as: TextItem alias Systems.Content.TextBundleModel, as: TextBundle - def store(file) do - Content.Private.get_backend().store(file) + def store(path, original_filename) do + Content.Private.get_backend().store(path, original_filename) end def get_public_url(id) do diff --git a/core/systems/content/local_fs.ex b/core/systems/content/local_fs.ex index 131ed0db1..e392bdc04 100644 --- a/core/systems/content/local_fs.ex +++ b/core/systems/content/local_fs.ex @@ -1,38 +1,28 @@ defmodule Systems.Content.LocalFS do - alias CoreWeb.Endpoint + use CoreWeb, :verified_routes - def store(tmp_path) do - uuid = Ecto.UUID.generate() - extname = Path.extname(tmp_path) - id = "#{uuid}#{extname}" - path = get_path(id) - File.cp!(tmp_path, path) - id + def public_path, do: "/uploads" + + def get_public_url(path) do + filename = Path.basename(path) + ~p"/uploads/#{filename}" end - def storage_path(id) do - get_path(id) + def store(path, original_filename) do + uuid = Ecto.UUID.generate() + root_path = get_root_path() + new_path = "#{root_path}/#{uuid}_#{original_filename}" + File.cp!(path, new_path) + new_path end - def get_public_url(id) do - "#{Endpoint.url()}/#{public_path()}/#{id}" + def get_root_path do + Application.get_env(:core, :upload_path) end - def remove(id) do - with {:ok, _} <- File.rm_rf(get_path(id)) do + def remove(path) do + with {:ok, _} <- File.rm_rf(path) do :ok end end - - defp get_path(id) do - Path.join(get_root_path(), id) - end - - def get_root_path do - :core - |> Application.get_env(:content, []) - |> Access.fetch!(:local_fs_root_path) - end - - def public_path, do: "/content" end diff --git a/core/systems/content/s3.ex b/core/systems/content/s3.ex index c4fbd8ab1..ce4eea6ca 100644 --- a/core/systems/content/s3.ex +++ b/core/systems/content/s3.ex @@ -1,7 +1,7 @@ defmodule Systems.Content.S3 do alias ExAws.S3 - def store(file) do + def store(file, _original_filename) do bucket = Access.fetch!(s3_settings(), :bucket) uuid = Ecto.UUID.generate() extname = Path.extname(file) diff --git a/core/systems/document/tool_form.ex b/core/systems/document/tool_form.ex index 097fc5125..7f0c95b02 100644 --- a/core/systems/document/tool_form.ex +++ b/core/systems/document/tool_form.ex @@ -1,8 +1,6 @@ defmodule Systems.Document.ToolForm do use CoreWeb.LiveForm - use CoreWeb.FileUploader, ~w(.pdf) - - alias CoreWeb.Endpoint + use CoreWeb.FileUploader, accept: ~w(.pdf) alias Systems.{ Document @@ -11,12 +9,10 @@ defmodule Systems.Document.ToolForm do @impl true def process_file( %{assigns: %{entity: entity}} = socket, - {local_relative_path, _local_full_path, remote_file} + {_path, url, original_filename} ) do - ref = "#{Endpoint.url()}#{local_relative_path}" - socket - |> save(entity, %{ref: ref, name: remote_file}) + |> save(entity, %{ref: url, name: original_filename}) end @impl true diff --git a/core/systems/feldspar/_public.ex b/core/systems/feldspar/_public.ex index a2b305430..fc040f802 100644 --- a/core/systems/feldspar/_public.ex +++ b/core/systems/feldspar/_public.ex @@ -19,8 +19,8 @@ defmodule Systems.Feldspar.Public do |> Ecto.Changeset.put_assoc(:auth_node, auth_node) end - def store(zip_file) do - get_backend().store(zip_file) + def store(zip_file, original_filename) do + get_backend().store(zip_file, original_filename) end def get_public_url(id) do diff --git a/core/systems/feldspar/local_fs.ex b/core/systems/feldspar/local_fs.ex index 96c168601..cfb78518c 100644 --- a/core/systems/feldspar/local_fs.ex +++ b/core/systems/feldspar/local_fs.ex @@ -1,37 +1,37 @@ defmodule Systems.Feldspar.LocalFS do - alias CoreWeb.Endpoint + use CoreWeb, :verified_routes - def store(zip_file) do - id = Ecto.UUID.generate() - path = get_path(id) - File.mkdir!(path) - :zip.unzip(to_charlist(zip_file), cwd: to_charlist(path)) - id + def public_path, do: "/feldspar/apps" + + def get_public_url(id) do + ~p"/feldspar/apps/#{id}" end - def storage_path(id) do - get_path(id) + def store(zip_file, original_filename) do + uuid = Ecto.UUID.generate() + base_folder = Path.basename(original_filename, ".zip") + folder = "#{uuid}_#{base_folder}" + path = get_path(folder) + File.mkdir!(path) + :zip.unzip(to_charlist(zip_file), cwd: to_charlist(path)) + folder end - def get_public_url(id) do - "#{Endpoint.url()}/#{public_path()}/#{id}" + def storage_path(folder) do + get_path(folder) end - def remove(id) do - with {:ok, _} <- File.rm_rf(get_path(id)) do + def remove(folder) do + with {:ok, _} <- File.rm_rf(get_path(folder)) do :ok end end - defp get_path(id) do - Path.join(get_root_path(), id) + defp get_path(folder) do + Path.join(get_root_path(), folder) end def get_root_path do - :core - |> Application.get_env(:feldspar, []) - |> Access.fetch!(:local_fs_root_path) + Application.get_env(:core, :upload_path) end - - def public_path, do: "/feldspar/apps" end diff --git a/core/systems/feldspar/s3.ex b/core/systems/feldspar/s3.ex index 15ff5a1e7..bac931661 100644 --- a/core/systems/feldspar/s3.ex +++ b/core/systems/feldspar/s3.ex @@ -1,7 +1,7 @@ defmodule Systems.Feldspar.S3 do alias ExAws.S3 - def store(zip_file) do + def store(zip_file, _original_filename) do id = Ecto.UUID.generate() :ok = upload_zip_content(zip_file, id) id diff --git a/core/systems/feldspar/tool_form.ex b/core/systems/feldspar/tool_form.ex index be83851d2..1f51cb253 100644 --- a/core/systems/feldspar/tool_form.ex +++ b/core/systems/feldspar/tool_form.ex @@ -1,6 +1,6 @@ defmodule Systems.Feldspar.ToolForm do use CoreWeb.LiveForm - use CoreWeb.FileUploader, ~w(.zip) + use CoreWeb.FileUploader, store: Systems.Feldspar.Public, accept: ~w(.zip) alias Systems.{ Feldspar @@ -9,14 +9,10 @@ defmodule Systems.Feldspar.ToolForm do @impl true def process_file( %{assigns: %{entity: entity}} = socket, - {_local_relative_path, local_full_path, remote_file} + {_path, url, original_filename} ) do - archive_ref = - Feldspar.Public.store(local_full_path) - |> Feldspar.Public.get_public_url() - socket - |> save(entity, %{archive_ref: archive_ref, archive_name: remote_file}) + |> save(entity, %{archive_ref: url, archive_name: original_filename}) end @impl true diff --git a/core/systems/promotion/form_view.ex b/core/systems/promotion/form_view.ex index 8048c11dd..174e3d715 100644 --- a/core/systems/promotion/form_view.ex +++ b/core/systems/promotion/form_view.ex @@ -1,6 +1,6 @@ defmodule Systems.Promotion.FormView do use CoreWeb.LiveForm - use CoreWeb.FileUploader, ~w(.png .jpg .jpeg) + use CoreWeb.FileUploader, accept: ~w(.png .jpg .jpeg) alias Systems.{ Promotion @@ -18,10 +18,10 @@ defmodule Systems.Promotion.FormView do @impl true def process_file( %{assigns: %{entity: entity}} = socket, - {local_relative_path, _local_full_path, _remote_file} + {_path, url, _original_filename} ) do socket - |> save(entity, %{banner_photo_url: local_relative_path}) + |> save(entity, %{banner_photo_url: url}) end @impl true diff --git a/core/systems/rate/_public.ex b/core/systems/rate/_public.ex index 510a9d8fb..bc13b4ae4 100644 --- a/core/systems/rate/_public.ex +++ b/core/systems/rate/_public.ex @@ -6,8 +6,8 @@ defmodule Systems.Rate.Public do def request_permission(service, client_id, packet_size) when is_atom(service) and is_binary(client_id) and is_integer(packet_size) do case Systems.Rate.Server.request_permission(service, client_id, packet_size) do - :granted -> :granted {:denied, reason} -> raise RateLimitError, reason + _ -> :granted end end end diff --git a/core/systems/storage/_public.ex b/core/systems/storage/_public.ex index 8c076a66e..3d9d01e22 100644 --- a/core/systems/storage/_public.ex +++ b/core/systems/storage/_public.ex @@ -12,17 +12,18 @@ defmodule Systems.Storage.Public do ) do packet_size = String.length(data) - with :granted <- Rate.Public.request_permission(key, remote_ip, packet_size) do - %{ - backend: backend, - endpoint: endpoint, - panel_info: panel_info, - data: data, - meta_data: meta_data - } - |> Storage.Delivery.new() - |> Oban.insert() - end + # raises error when request is denied + Rate.Public.request_permission(key, remote_ip, packet_size) + + %{ + backend: backend, + endpoint: endpoint, + panel_info: panel_info, + data: data, + meta_data: meta_data + } + |> Storage.Delivery.new() + |> Oban.insert() end end diff --git a/core/test/core_web/file_uploader_test.exs b/core/test/core_web/file_uploader_test.exs index 49e1ab916..84218019e 100644 --- a/core/test/core_web/file_uploader_test.exs +++ b/core/test/core_web/file_uploader_test.exs @@ -2,14 +2,18 @@ defmodule CoreWeb.FileUploader.Test do use ExUnit.Case, async: true alias CoreWeb.FileUploader - describe "get_static_path/1" do + describe "get_upload_path/1" do + test "throws error for files that would be outside root" do + assert catch_throw(FileUploader.get_upload_path("../test.jpg")) + end + test "throws error for paths that would be outside root" do - assert catch_throw(FileUploader.get_static_path("../test.jpg")) + assert catch_throw(FileUploader.get_upload_path("../test")) end test "return path for valid filename" do - assert FileUploader.get_static_path("f94eae50-5bad-4c50-82ad-f0cf067394c1.jpg") == - "priv/static/uploads/f94eae50-5bad-4c50-82ad-f0cf067394c1.jpg" + assert FileUploader.get_upload_path("f94eae50-5bad-4c50-82ad-f0cf067394c1.jpg") == + "/tmp/f94eae50-5bad-4c50-82ad-f0cf067394c1.jpg" end end end diff --git a/core/test/systems/content/local_fs_test.exs b/core/test/systems/content/local_fs_test.exs index 440cfbed2..9555ed5d1 100644 --- a/core/test/systems/content/local_fs_test.exs +++ b/core/test/systems/content/local_fs_test.exs @@ -5,8 +5,7 @@ defmodule Systems.Content.LocalFSTest do describe "store/1" do test "extracts stores file on disk" do - id = LocalFS.store(Path.join(__DIR__, "hello.svg")) - path = LocalFS.storage_path(id) + path = LocalFS.store(Path.join(__DIR__, "hello.svg"), "original_file.svg") assert File.exists?(path) end end @@ -22,9 +21,8 @@ defmodule Systems.Content.LocalFSTest do describe "remove/1" do test "removes folder" do - id = LocalFS.store(Path.join(__DIR__, "hello.svg")) - path = LocalFS.storage_path(id) - assert :ok == LocalFS.remove(id) + path = LocalFS.store(Path.join(__DIR__, "hello.svg"), "original_file.svg") + assert :ok == LocalFS.remove(path) refute File.exists?(path) end end diff --git a/core/test/systems/content/plug_test.exs b/core/test/systems/content/plug_test.exs index fa4f6dd7e..aa9fbdf2b 100644 --- a/core/test/systems/content/plug_test.exs +++ b/core/test/systems/content/plug_test.exs @@ -6,10 +6,12 @@ defmodule Systems.Content.PlugTest do alias Systems.Content.Plug setup do - conf = Application.get_env(:core, :content, []) + upload_path = Application.get_env(:core, :upload_path) + backend = Application.fetch_env!(:core, :content) |> Access.fetch!(:backend) on_exit(fn -> - Application.put_env(:core, :content, conf) + Application.put_env(:core, :upload_path, upload_path) + Application.put_env(:core, :content, backend: backend) end) folder_name = "temp_#{:crypto.strong_rand_bytes(16) |> Base.encode16()}" @@ -24,18 +26,19 @@ defmodule Systems.Content.PlugTest do File.rm_rf!(tmp_dir) end) - conf = - conf - |> Keyword.put(:backend, Systems.Content.LocalFS) - |> Keyword.put(:local_fs_root_path, tmp_dir) + Application.put_env( + :core, + :upload_path, + tmp_dir + ) Application.put_env( :core, :content, - conf + backend: Systems.Content.LocalFS ) - {:ok, tmp_dir: tmp_dir, app_conf: conf} + {:ok, tmp_dir: tmp_dir} end test "call with LocalFS backend serves static content", %{tmp_dir: tmp_dir} do @@ -43,20 +46,16 @@ defmodule Systems.Content.PlugTest do |> Path.join("plug_test.txt") |> File.write("hello world!") - opts = Plug.init(at: "/content") - conn = Plug.call(conn(:get, "/content/plug_test.txt"), opts) + opts = Plug.init(at: "/uploads") + conn = Plug.call(conn(:get, "/uploads/plug_test.txt"), opts) assert "hello world!" == conn.resp_body end - test "call with other backends doesn't serve static content", %{app_conf: conf} do - Application.put_env( - :core, - :feldspar, - Keyword.put(conf, :backend, Systems.Content.FakeBackend) - ) + test "call with other backends doesn't serve static content" do + Application.put_env(:core, :content, backend: Systems.Content.FakeBackend) - opts = Plug.init(at: "/txt") - conn = Plug.call(conn(:get, "/txt/plug_test.txt"), opts) + opts = Plug.init(at: "/uploads") + conn = Plug.call(conn(:get, "/uploads/plug_test.txt"), opts) assert nil == conn.resp_body end end diff --git a/core/test/systems/content/s3_test.exs b/core/test/systems/content/s3_test.exs index 4d899b507..bedfae0a9 100644 --- a/core/test/systems/content/s3_test.exs +++ b/core/test/systems/content/s3_test.exs @@ -35,7 +35,7 @@ defmodule Systems.Content.S3Test do } = args end) - id = S3.store(Path.join(__DIR__, "hello.svg")) + id = S3.store(Path.join(__DIR__, "hello.svg"), "original_file.svg") assert is_binary(id) refute id == "" end diff --git a/core/test/systems/feldspar/local_fs_test.exs b/core/test/systems/feldspar/local_fs_test.exs index 8d1004e37..417216a35 100644 --- a/core/test/systems/feldspar/local_fs_test.exs +++ b/core/test/systems/feldspar/local_fs_test.exs @@ -5,7 +5,7 @@ defmodule Systems.Feldspar.LocalFSTest do describe "store/1" do test "extracts zip and stores files on disk" do - id = LocalFS.store(Path.join(__DIR__, "hello.zip")) + id = LocalFS.store(Path.join(__DIR__, "hello.zip"), "original_file.zip") path = LocalFS.storage_path(id) assert ["index.html"] == File.ls!(path) end @@ -26,7 +26,7 @@ defmodule Systems.Feldspar.LocalFSTest do describe "remove/1" do test "removes folder" do - id = LocalFS.store(Path.join(__DIR__, "hello.zip")) + id = LocalFS.store(Path.join(__DIR__, "hello.zip"), "original_file.zip") path = LocalFS.storage_path(id) assert :ok == LocalFS.remove(id) refute File.exists?(path) diff --git a/core/test/systems/feldspar/plug_test.exs b/core/test/systems/feldspar/plug_test.exs index 42e9b74cb..91f4096bc 100644 --- a/core/test/systems/feldspar/plug_test.exs +++ b/core/test/systems/feldspar/plug_test.exs @@ -6,10 +6,12 @@ defmodule Systems.Feldspar.PlugTest do alias Systems.Feldspar.Plug setup do - conf = Application.get_env(:core, :feldspar, []) + upload_path = Application.get_env(:core, :upload_path) + backend = Application.fetch_env!(:core, :feldspar) |> Access.fetch!(:backend) on_exit(fn -> - Application.put_env(:core, :feldspar, conf) + Application.put_env(:core, :upload_path, upload_path) + Application.put_env(:core, :feldspar, backend: backend) end) folder_name = "temp_#{:crypto.strong_rand_bytes(16) |> Base.encode16()}" @@ -24,18 +26,10 @@ defmodule Systems.Feldspar.PlugTest do File.rm_rf!(tmp_dir) end) - conf = - conf - |> Keyword.put(:backend, Systems.Feldspar.LocalFS) - |> Keyword.put(:local_fs_root_path, tmp_dir) + Application.put_env(:core, :upload_path, tmp_dir) + Application.put_env(:core, :feldspar, backend: Systems.Feldspar.LocalFS) - Application.put_env( - :core, - :feldspar, - conf - ) - - {:ok, tmp_dir: tmp_dir, app_conf: conf} + {:ok, tmp_dir: tmp_dir} end test "call with LocalFS backend serves static content", %{tmp_dir: tmp_dir} do @@ -48,12 +42,8 @@ defmodule Systems.Feldspar.PlugTest do assert "hello world!" == conn.resp_body end - test "call with other backends doesn't serve static content", %{app_conf: conf} do - Application.put_env( - :core, - :feldspar, - Keyword.put(conf, :backend, Systems.Feldspar.FakeBackend) - ) + test "call with other backends doesn't serve static content" do + Application.put_env(:core, :feldspar, backend: Systems.Feldspar.FakeBackend) opts = Plug.init(at: "/web_apps") conn = Plug.call(conn(:get, "/web_apps/plug_test.txt"), opts) diff --git a/core/test/systems/feldspar/s3_test.exs b/core/test/systems/feldspar/s3_test.exs index 88e8ef206..6d08fb39f 100644 --- a/core/test/systems/feldspar/s3_test.exs +++ b/core/test/systems/feldspar/s3_test.exs @@ -32,7 +32,7 @@ defmodule Systems.Feldspar.S3Test do } = args end) - id = S3.store(Path.join(__DIR__, "hello.zip")) + id = S3.store(Path.join(__DIR__, "hello.zip"), "original_file.zip") assert is_binary(id) refute id == "" end