-
-
Notifications
You must be signed in to change notification settings - Fork 730
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10661 from mkllnk/report-download
[Hidden] Provide download link for reports generated in the background
- Loading branch information
Showing
8 changed files
with
89 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,10 @@ | ||
# frozen_string_literal: true | ||
|
||
# Renders a report and saves it to a temporary file. | ||
class ReportJob < ActiveJob::Base | ||
def perform(report_class, user, params, format) | ||
# Renders a report and stores it in a given blob. | ||
class ReportJob < ApplicationJob | ||
def perform(report_class, user, params, format, blob) | ||
report = report_class.new(user, params, render: true) | ||
result = report.render_as(format) | ||
write(result) | ||
end | ||
|
||
def done? | ||
@done ||= File.file?(filename) | ||
end | ||
|
||
def result | ||
@result ||= read_result | ||
end | ||
|
||
private | ||
|
||
def write(result) | ||
File.write(filename, result, mode: "wb") | ||
end | ||
|
||
def read_result | ||
File.read(filename) | ||
ensure | ||
File.unlink(filename) | ||
end | ||
|
||
def filename | ||
Rails.root.join("tmp/report-#{job_id}") | ||
blob.store(result) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# frozen_string_literal: true | ||
|
||
# Stores a generated report. | ||
class ReportBlob < ActiveStorage::Blob | ||
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: filename, | ||
byte_size: 0, | ||
checksum: "0", | ||
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) | ||
save! | ||
end | ||
|
||
def content_stored? | ||
@content_stored ||= reload.checksum != "0" | ||
end | ||
|
||
def result | ||
@result ||= download | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters