From 5f5058b3fc7c3d8dd76522a9b2f812d890714515 Mon Sep 17 00:00:00 2001 From: Elio Hasrouni Date: Tue, 19 Nov 2024 09:43:44 -0500 Subject: [PATCH] Add direct upload --- .../maintenance_tasks/tasks/show.html.erb | 2 +- lib/maintenance_tasks.rb | 7 ++++ test/system/maintenance_tasks/runs_test.rb | 37 ++++++++++++++++++- 3 files changed, 44 insertions(+), 2 deletions(-) diff --git a/app/views/maintenance_tasks/tasks/show.html.erb b/app/views/maintenance_tasks/tasks/show.html.erb index d7fd6345..16d89fe3 100644 --- a/app/views/maintenance_tasks/tasks/show.html.erb +++ b/app/views/maintenance_tasks/tasks/show.html.erb @@ -9,7 +9,7 @@ <% if @task.csv_task? %>
<%= form.label :csv_file %> - <%= form.file_field :csv_file, accept: "text/csv" %> + <%= form.file_field :csv_file, accept: "text/csv", direct_upload: MaintenanceTasks.direct_upload %>
<% end %> <% parameter_names = @task.parameter_names %> diff --git a/lib/maintenance_tasks.rb b/lib/maintenance_tasks.rb index 643a2ca2..e8ec2f02 100644 --- a/lib/maintenance_tasks.rb +++ b/lib/maintenance_tasks.rb @@ -96,4 +96,11 @@ module MaintenanceTasks # # @return [ActiveSupport::Duration] the threshold in seconds after which a task is considered stuck. mattr_accessor :stuck_task_duration, default: 5.minutes + + # @!attribute direct_upload + # @scope class + # Controls direct upload support for CSV files. + # + # @return [Boolean] whether to enable direct upload for CSV files. + mattr_accessor :direct_upload, default: false end diff --git a/test/system/maintenance_tasks/runs_test.rb b/test/system/maintenance_tasks/runs_test.rb index 23188d4e..c9d2ff4e 100644 --- a/test/system/maintenance_tasks/runs_test.rb +++ b/test/system/maintenance_tasks/runs_test.rb @@ -60,10 +60,15 @@ class RunsTest < ApplicationSystemTestCase MaintenanceTasks.metadata = nil end - test "run a CSV Task" do + test "run a CSV Task without direct upload" do visit maintenance_tasks_path click_on("Maintenance::ImportPostsTask") + + assert_selector("input[type=file]") do |input| + assert_nil(input["data-direct-upload-url"]) + end + attach_file("csv_file", "test/fixtures/files/sample.csv") click_on "Run" @@ -72,6 +77,26 @@ class RunsTest < ApplicationSystemTestCase assert_text "Waiting to start." end + test "run a CSV Task with direct upload" do + with_direct_upload(true) do + visit maintenance_tasks_path + + click_on("Maintenance::ImportPostsTask") + + attach_file("csv_file", "test/fixtures/files/sample.csv") + click_on "Run" + upload_url = page.server_url + "/rails/active_storage/direct_uploads" + + assert_selector("input[type=file]") do |input| + assert_equal(input["data-direct-upload-url"], upload_url) + end + + assert_title "Maintenance::ImportPostsTask" + assert_text "Enqueued" + assert_text "Waiting to start." + end + end + test "run a Task that accepts parameters" do visit maintenance_tasks_path @@ -272,5 +297,15 @@ class RunsTest < ApplicationSystemTestCase assert_text "Validation failed: Status Cannot transition run from status cancelling to pausing" end + + private + + def with_direct_upload(enabled) + previous = MaintenanceTasks.direct_upload + MaintenanceTasks.direct_upload = enabled + yield + ensure + MaintenanceTasks.direct_upload = previous + end end end