Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions app/models/export.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def build_later
def build
processing!

with_account_context do
with_context do
ZipFile.create_for(file, filename: filename) do |zip|
populate_zip(zip)
end
Expand All @@ -45,9 +45,14 @@ def filename
"fizzy-export-#{id}.zip"
end

def with_account_context
def with_context
Current.set(account: account) do
old_url_options = ActiveStorage::Current.url_options
ActiveStorage::Current.url_options = Rails.application.routes.default_url_options

yield
ensure
ActiveStorage::Current.url_options = old_url_options
end
end

Expand Down
45 changes: 13 additions & 32 deletions app/models/zip_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,38 +40,19 @@ def create_for_s3(attachment, filename:, service:)

writer = Writer.new

begin
# Use S3's upload_stream directly for write-based streaming.
# ActiveStorage's upload method expects a read-based IO, but ZipKit
# needs a write-based stream. The TransferManager's upload_stream
# yields a writable IO that we can stream directly to.
service.send(:upload_stream,
key: blob.key,
content_type: "application/zip",
part_size: 100.megabytes
) do |write_stream|
write_stream.binmode
writer.stream_to(write_stream)
yield writer
writer.close
end
rescue Aws::S3::MultipartUploadError => e
if defined?(Sentry)
Sentry.set_context("zip_file_upload", {
blob_key: blob.key,
writer_byte_size: writer.byte_size,
error_class: e.class.name,
error_message: e.message,
nested_errors: e.errors.map do |err|
{
class: err.class.name,
message: err.message,
backtrace: err.backtrace&.first(20)
}
end
})
end
raise
# Use S3's upload_stream directly for write-based streaming.
# ActiveStorage's upload method expects a read-based IO, but ZipKit
# needs a write-based stream. The TransferManager's upload_stream
# yields a writable IO that we can stream directly to.
service.send(:upload_stream,
key: blob.key,
content_type: "application/zip",
part_size: 100.megabytes
) do |write_stream|
write_stream.binmode
writer.stream_to(write_stream)
yield writer
writer.close
end

blob.update!(byte_size: writer.byte_size, checksum: writer.checksum)
Expand Down
Loading