mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-24 20:36:49 +00:00
Store report's filename within the blob
For future downloads outside the ReportsController.
This commit is contained in:
@@ -57,7 +57,9 @@ module Admin
|
||||
|
||||
def render_report_as(format)
|
||||
if OpenFoodNetwork::FeatureToggle.enabled?(:background_reports, spree_current_user)
|
||||
blob = ReportBlob.create_for_upload_later!
|
||||
filename = report_filename
|
||||
filename = "#{filename}html" if report_format.blank?
|
||||
blob = ReportBlob.create_for_upload_later!(filename)
|
||||
ReportJob.perform_later(
|
||||
report_class, spree_current_user, params, format, blob
|
||||
)
|
||||
|
||||
@@ -2,20 +2,24 @@
|
||||
|
||||
# Stores a generated report.
|
||||
class ReportBlob < ActiveStorage::Blob
|
||||
def self.create_for_upload_later!
|
||||
def self.create_for_upload_later!(filename)
|
||||
# ActiveStorage discourages modifying a blob later but we need a blob
|
||||
# before we know anything about the report file. It enables us to use the
|
||||
# same blob in the controller to read the result.
|
||||
create_before_direct_upload!(
|
||||
filename: "tbd",
|
||||
filename: filename,
|
||||
byte_size: 0,
|
||||
checksum: "0",
|
||||
content_type: "application/octet-stream",
|
||||
content_type: content_type(filename),
|
||||
).tap do |blob|
|
||||
ActiveStorage::PurgeJob.set(wait: 1.month).perform_later(blob)
|
||||
end
|
||||
end
|
||||
|
||||
def self.content_type(filename)
|
||||
MIME::Types.of(filename).first&.to_s || "application/octet-stream"
|
||||
end
|
||||
|
||||
def store(content)
|
||||
io = StringIO.new(content)
|
||||
upload(io, identify: false)
|
||||
|
||||
@@ -9,7 +9,7 @@ describe ReportJob do
|
||||
let(:enterprise) { create(:enterprise) }
|
||||
let(:params) { {} }
|
||||
let(:format) { :csv }
|
||||
let(:blob) { ReportBlob.create_for_upload_later! }
|
||||
let(:blob) { ReportBlob.create_for_upload_later!("report.csv") }
|
||||
|
||||
it "generates a report" do
|
||||
job = perform_enqueued_jobs(only: ReportJob) do
|
||||
@@ -29,6 +29,10 @@ describe ReportJob do
|
||||
end
|
||||
|
||||
def expect_csv_report
|
||||
blob.reload
|
||||
expect(blob.filename.to_s).to eq "report.csv"
|
||||
expect(blob.content_type).to eq "text/csv"
|
||||
|
||||
table = CSV.parse(blob.result)
|
||||
expect(table[0][1]).to eq "Relationship"
|
||||
expect(table[1][1]).to eq "owns"
|
||||
|
||||
Reference in New Issue
Block a user