Browse Source

Refactor collection resources to use storage service

pull/2684/head
Kat 2 years ago
parent
commit
fe6e4472bd
  1. 2
      app/controllers/start_controller.rb
  2. 4
      app/helpers/collection_resources_helper.rb
  3. 34
      app/services/collection_resources_service.rb
  4. 9
      app/services/storage/local_disk_service.rb
  5. 4
      app/services/storage/s3_service.rb

2
app/controllers/start_controller.rb

@ -68,7 +68,7 @@ private
def download_resource(filename, download_filename)
file = CollectionResourcesService.new.get_file(filename)
render_not_found unless file
return render_not_found unless file
send_data(file, disposition: "attachment", filename: download_filename)
end

4
app/helpers/collection_resources_helper.rb

@ -15,8 +15,8 @@ module CollectionResourcesHelper
return [file_pages].compact.join(", ") unless metadata
file_size = number_to_human_size(metadata["Content-Length"].to_i)
file_type = HUMAN_READABLE_CONTENT_TYPE[metadata["Content-Type"].to_sym] || "Unknown File Type"
file_size = number_to_human_size(metadata["content_length"].to_i)
file_type = HUMAN_READABLE_CONTENT_TYPE[metadata["content_type"].to_sym] || "Unknown File Type"
[file_type, file_size, file_pages].compact.join(", ")
end
end

34
app/services/collection_resources_service.rb

@ -1,33 +1,21 @@
class CollectionResourcesService
def initialize
@storage_service = Storage::S3Service.new(Configuration::EnvConfigurationService.new, ENV["COLLECTION_RESOURCES_BUCKET"])
@storage_service = if Rails.env.development?
Storage::LocalDiskService.new
else
Storage::S3Service.new(Configuration::EnvConfigurationService.new, ENV["COLLECTION_RESOURCES_BUCKET"])
end
end
def get_file(file)
storage_service = Storage::S3Service.new(Configuration::EnvConfigurationService.new, ENV["COLLECTION_RESOURCES_BUCKET"])
url = "https://#{storage_service.configuration.bucket_name}.s3.amazonaws.com/#{file}"
uri = URI.parse(url)
response = Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http|
request = Net::HTTP::Get.new(uri)
http.request(request)
end
return unless response.is_a?(Net::HTTPSuccess)
response.body
@storage_service.get_file_io(file)
rescue StandardError
nil
end
def get_file_metadata(file)
storage_service = Storage::S3Service.new(Configuration::EnvConfigurationService.new, ENV["COLLECTION_RESOURCES_BUCKET"])
url = "https://#{storage_service.configuration.bucket_name}.s3.amazonaws.com/#{file}"
uri = URI.parse(url)
response = Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == "https") do |http|
http.request_head(uri)
end
return unless response.is_a?(Net::HTTPSuccess) || response.is_a?(Net::HTTPRedirection)
response
@storage_service.get_file_metadata(file)
rescue StandardError
nil
end
end

9
app/services/storage/local_disk_service.rb

@ -22,5 +22,14 @@ module Storage
f.write data
end
end
def get_file_metadata(filename)
path = Rails.root.join("tmp/storage", filename)
{
"content_length" => File.size(path),
"content_type" => MiniMime.lookup_by_filename(path.to_s)&.content_type || "application/octet-stream",
}
end
end
end

4
app/services/storage/s3_service.rb

@ -39,6 +39,10 @@ module Storage
)
end
def get_file_metadata(file_name)
@client.head_object(bucket: @configuration.bucket_name, key: file_name)
end
private
def create_configuration

Loading…
Cancel
Save