Skip to content

Commit

Permalink
Ensuring that File System uploads are no longer dependent upon Active…
Browse files Browse the repository at this point in the history
…Storage; Providing the ability to purge downloaded files from the ActiveStorage cache
  • Loading branch information
jrgriffiniii committed Apr 24, 2020
1 parent f1d3b9b commit 89ea2cf
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 10 deletions.
3 changes: 1 addition & 2 deletions app/controllers/browse_everything/uploads_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 7 additions & 1 deletion app/jobs/browse_everything/upload_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions app/models/browse_everything/upload_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
11 changes: 11 additions & 0 deletions db/migrate/20200423125901_add_file_attrs_upload_files.rb
Original file line number Diff line number Diff line change
@@ -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
18 changes: 11 additions & 7 deletions lib/browse_everything/upload.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 89ea2cf

Please sign in to comment.