Skip to content

Commit

Permalink
Add filename to uploads
Browse files Browse the repository at this point in the history
This adds a new column to the upload model which we'll use to store the
filename. This is necessary as we delete attachments if they're scanned
as a virus, so we need to store the name elsewhere so it can be
rendered.
  • Loading branch information
thomasleese committed Apr 10, 2024
1 parent 3b79aeb commit a18a12a
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 4 deletions.
2 changes: 2 additions & 0 deletions app/forms/concerns/uploadable_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,15 @@ def create_uploads!
if original_attachment.present?
document.uploads.create!(
attachment: original_attachment,
filename: original_attachment.filename,
translation: false,
)
end

if translated_attachment.present?
document.uploads.create!(
attachment: translated_attachment,
filename: translated_attachment.filename,
translation: true,
)
end
Expand Down
1 change: 1 addition & 0 deletions app/models/upload.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# Table name: uploads
#
# id :bigint not null, primary key
# filename :string default(""), not null
# malware_scan_result :string default("pending"), not null
# translation :boolean not null
# created_at :datetime not null
Expand Down
7 changes: 4 additions & 3 deletions config/analytics.yml
Original file line number Diff line number Diff line change
Expand Up @@ -390,12 +390,13 @@
- updated_at
- work_history_id
:uploads:
- id
- created_at
- document_id
- filename
- id
- malware_scan_result
- translation
- created_at
- updated_at
- malware_scan_result
:work_histories:
- application_form_id
- canonical_contact_email
Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20240410150439_add_filename_to_uploads.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddFilenameToUploads < ActiveRecord::Migration[7.1]
def change
add_column :uploads, :filename, :string, null: false, default: ""
end
end
3 changes: 2 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions lib/tasks/uploads.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

namespace :uploads do
desc "Resend blob data for uploads that have a pending malware scan result"
task update_filename: :environment do
Upload
.where(filename: "")
.with_attached_attachment
.find_each do |upload|
upload.update!(filename: upload.attachment.filename || "unknown")
end
end
end
1 change: 1 addition & 0 deletions spec/factories/uploads.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# Table name: uploads
#
# id :bigint not null, primary key
# filename :string default(""), not null
# malware_scan_result :string default("pending"), not null
# translation :boolean not null
# created_at :datetime not null
Expand Down
1 change: 1 addition & 0 deletions spec/models/upload_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# Table name: uploads
#
# id :bigint not null, primary key
# filename :string default(""), not null
# malware_scan_result :string default("pending"), not null
# translation :boolean not null
# created_at :datetime not null
Expand Down

0 comments on commit a18a12a

Please sign in to comment.