From 89ea2cf41ba977f0d9ce4cc522a580d302c1844c Mon Sep 17 00:00:00 2001 From: GriffinJ Date: Fri, 24 Apr 2020 07:40:55 -0400 Subject: [PATCH] Ensuring that File System uploads are no longer dependent upon ActiveStorage; Providing the ability to purge downloaded files from the ActiveStorage cache --- .../browse_everything/uploads_controller.rb | 3 +-- app/jobs/browse_everything/upload_job.rb | 8 +++++++- app/models/browse_everything/upload_file.rb | 16 ++++++++++++++++ ...200423125901_add_file_attrs_upload_files.rb | 11 +++++++++++ lib/browse_everything/upload.rb | 18 +++++++++++------- 5 files changed, 46 insertions(+), 10 deletions(-) create mode 100644 db/migrate/20200423125901_add_file_attrs_upload_files.rb diff --git a/app/controllers/browse_everything/uploads_controller.rb b/app/controllers/browse_everything/uploads_controller.rb index ff333b0f..71171dbe 100644 --- a/app/controllers/browse_everything/uploads_controller.rb +++ b/app/controllers/browse_everything/uploads_controller.rb @@ -28,8 +28,7 @@ def create # This will be the job which asynchronously downloads the files in # ActiveStorage Models - upload_job = upload.job - upload_job.perform_now + upload.perform_job respond_to do |format| format.json_api { render status: :created, json: @serializer.serialized_json } end diff --git a/app/jobs/browse_everything/upload_job.rb b/app/jobs/browse_everything/upload_job.rb index f3d42d89..aa25c503 100644 --- a/app/jobs/browse_everything/upload_job.rb +++ b/app/jobs/browse_everything/upload_job.rb @@ -66,7 +66,13 @@ def create_upload_file(bytestream:) end upload_file = UploadFile.new(name: bytestream.name) - upload_file.bytestream.attach(io: io, filename: bytestream.name, content_type: bytestream.media_type) + if bytestream.file_uri? + upload_file.file_path = file_path + upload_file.file_name = bytestream.name + upload_file.file_content_type = bytestream.media_type + else + upload_file.bytestream.attach(io: io, filename: bytestream.name, content_type: bytestream.media_type) + end upload_file.save upload_file.reload end diff --git a/app/models/browse_everything/upload_file.rb b/app/models/browse_everything/upload_file.rb index 81612052..744d9c0a 100644 --- a/app/models/browse_everything/upload_file.rb +++ b/app/models/browse_everything/upload_file.rb @@ -4,5 +4,21 @@ module BrowseEverything class UploadFile < ApplicationRecord self.table_name = 'browse_everything_upload_files' has_one_attached :bytestream + + def file_bytestream? + file_path && File.exist?(file_path) + end + + def download + if file_bytestream? + File.read(file_path) + else + bytestream.download + end + end + + def purge_bytestream + bytestream.purge if bytestream + end end end diff --git a/db/migrate/20200423125901_add_file_attrs_upload_files.rb b/db/migrate/20200423125901_add_file_attrs_upload_files.rb new file mode 100644 index 00000000..ef2d8839 --- /dev/null +++ b/db/migrate/20200423125901_add_file_attrs_upload_files.rb @@ -0,0 +1,11 @@ +# frozen_string_literal: true + +class AddFileAttrsUploadFiles < ActiveRecord::Migration[(Rails.version =~ /5.1/ ? 5.1 : 5.2)] + def change + change_table :browse_everything_upload_files do |t| + t.string :file_path + t.string :file_name + t.string :file_content_type + end + end +end diff --git a/lib/browse_everything/upload.rb b/lib/browse_everything/upload.rb index fae73fc2..9977c2f1 100644 --- a/lib/browse_everything/upload.rb +++ b/lib/browse_everything/upload.rb @@ -162,13 +162,8 @@ def serializer # Sessions are responsible for managing the relationships to authorizations delegate :authorizations, :auth_code, to: :session - # Create a new ActiveJob object for supporting asynchronous uploads - # Maybe what could be done is that there is an UploadedFile Model with - # ActiveStorage which is retrieved? - # If that is the preferred approach, blocking until the ActiveJob completes - # needs to be supported... - def job - self.class.job_class.new(**default_job_args) + def perform_job + job.perform(upload_id: id) end # These are the ActiveStorage files retrieved from the server and saved on @@ -195,6 +190,15 @@ def session private + # Create a new ActiveJob object for supporting asynchronous uploads + # Maybe what could be done is that there is an UploadedFile Model with + # ActiveStorage which is retrieved? + # If that is the preferred approach, blocking until the ActiveJob completes + # needs to be supported... + def job + self.class.job_class.new(**default_job_args) + end + # There should be a BrowseEverything.metadata_adapter layer here for # providing closer Valkyrie integration def orm