Browse Source

Add content type to the files

pull/2690/head
Kat 2 years ago
parent
commit
2d7c79ecf6
  1. 3
      app/services/collection_resources_service.rb
  2. 2
      app/services/storage/local_disk_service.rb
  3. 21
      app/services/storage/s3_service.rb
  4. 2
      spec/services/collection_resources_service_spec.rb

3
app/services/collection_resources_service.rb

@ -24,6 +24,7 @@ class CollectionResourcesService
end end
def upload_collection_resource(filename, file) def upload_collection_resource(filename, file)
@storage_service.write_file(filename, file) content_type = MiniMime.lookup_by_filename(filename)&.content_type
@storage_service.write_file(filename, file, content_type:)
end end
end end

2
app/services/storage/local_disk_service.rb

@ -19,7 +19,7 @@ module Storage
File.open(path, "r") File.open(path, "r")
end end
def write_file(filename, data) def write_file(filename, data, _content_type: nil)
path = Rails.root.join("tmp/storage", filename) path = Rails.root.join("tmp/storage", filename)
FileUtils.mkdir_p(path.dirname) FileUtils.mkdir_p(path.dirname)

21
app/services/storage/s3_service.rb

@ -36,12 +36,21 @@ module Storage
.body.read .body.read
end end
def write_file(file_name, data) def write_file(file_name, data, content_type: nil)
@client.put_object( if content_type.nil?
body: data, @client.put_object(
bucket: @configuration.bucket_name, body: data,
key: file_name, bucket: @configuration.bucket_name,
) key: file_name,
)
else
@client.put_object(
body: data,
bucket: @configuration.bucket_name,
key: file_name,
content_type:,
)
end
end end
def get_file_metadata(file_name) def get_file_metadata(file_name)

2
spec/services/collection_resources_service_spec.rb

@ -12,7 +12,7 @@ describe CollectionResourcesService do
end end
it "calls write_file on S3 service" do it "calls write_file on S3 service" do
expect(storage_service).to receive(:write_file).with("2025_26_lettings_paper_form.pdf", some_file) expect(storage_service).to receive(:write_file).with("2025_26_lettings_paper_form.pdf", some_file, content_type: "application/pdf")
service.upload_collection_resource("2025_26_lettings_paper_form.pdf", some_file) service.upload_collection_resource("2025_26_lettings_paper_form.pdf", some_file)
end end
end end

Loading…
Cancel
Save