Browse Source
* Get collection resources from S3 * Remove resource files from codebase * Stub some collection resource requests * Use env var for bucket name * Update some tests * Extract methods into a servicepull/2659/head
kosiakkatrina
3 months ago
committed by
GitHub
33 changed files with 152 additions and 88 deletions
@ -1,15 +1,22 @@
|
||||
module CollectionResourcesHelper |
||||
HUMAN_READABLE_CONTENT_TYPE = { "application/pdf": "PDF", |
||||
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet": "Microsoft Excel", |
||||
"application/vnd.ms-excel": "Microsoft Excel (Old Format)", |
||||
"application/msword": "Microsoft Word", |
||||
"application/vnd.openxmlformats-officedocument.wordprocessingml.document": "Microsoft Word (DOCX)", |
||||
"image/jpeg": "JPEG Image", |
||||
"image/png": "PNG Image", |
||||
"text/plain": "Text Document", |
||||
"text/html": "HTML Document" }.freeze |
||||
|
||||
def file_type_size_and_pages(file, number_of_pages: nil) |
||||
extension_mapping = { |
||||
"xlsx" => "Microsoft Excel", |
||||
"pdf" => "PDF", |
||||
} |
||||
extension = File.extname(file)[1..] |
||||
file_pages = number_of_pages ? pluralize(number_of_pages, "page") : nil |
||||
metadata = CollectionResourcesService.new.get_file_metadata(file) |
||||
|
||||
file_type = extension_mapping.fetch(extension, extension) |
||||
return [file_pages].compact.join(", ") unless metadata |
||||
|
||||
file_size = number_to_human_size(File.size("public/files/#{file}"), precision: 0, significant: false) |
||||
file_pages = number_of_pages ? pluralize(number_of_pages, "page") : nil |
||||
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 |
||||
|
@ -0,0 +1,33 @@
|
||||
class CollectionResourcesService |
||||
def initialize |
||||
@storage_service = Storage::S3Service.new(Configuration::EnvConfigurationService.new, ENV["COLLECTION_RESOURCES_BUCKET"]) |
||||
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 |
||||
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 |
||||
end |
||||
end |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading…
Reference in new issue