Skip to content

Commit

Permalink
Add logging for file upload
Browse files Browse the repository at this point in the history
Add logging when a file is uploaded to S3 for a file upload question,
or when validation fails due to a file being too big.

This information can help us analyse what file types people are
attempting to or successfully uploading and do analysis of the file
sizes.
  • Loading branch information
stephencdaly committed Jan 15, 2025
1 parent 97085fe commit 04aac36
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
8 changes: 8 additions & 0 deletions app/models/question/file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ def before_save
tempfile = file.tempfile
key = file_upload_s3_key(tempfile)
upload_to_s3(tempfile, key)
Rails.logger.info("Uploaded file to S3 for file upload question", {
file_size_in_bytes: file.size,
file_type: file.content_type,
})

self.original_filename = file.original_filename
self.uploaded_file_key = key
Expand All @@ -42,6 +46,10 @@ def file_from_s3

def validate_file_size
if file.present? && file.size > FILE_UPLOAD_MAX_SIZE_IN_MB.megabytes
Rails.logger.info("File upload question validation failed: file too big", {
file_size_in_bytes: file.size,
file_type: file.content_type,
})
errors.add(:file, :too_big)
end
end
Expand Down
27 changes: 25 additions & 2 deletions spec/models/question/file_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
let(:bucket) { "an-s3-bucket" }
let(:key) { Faker::Alphanumeric.alphanumeric }
let(:tempfile) { Tempfile.new(%w[temp-file .png]) }
let(:file_size_in_bytes) { 2.megabytes }
let(:file_type) { "image/png" }

after do
tempfile.unlink
Expand All @@ -36,10 +38,13 @@
allow(mock_s3_client).to receive(:put_object)
allow(Settings.aws).to receive(:file_upload_s3_bucket_name).and_return(bucket)

allow(uploaded_file).to receive_messages(original_filename: original_filename, tempfile: tempfile)
allow(uploaded_file).to receive_messages(original_filename: original_filename, tempfile: tempfile,
size: file_size_in_bytes, content_type: file_type)

allow(SecureRandom).to receive(:uuid).and_return key

allow(Rails.logger).to receive(:info).at_least(:once)

question.file = uploaded_file

question.before_save
Expand All @@ -60,6 +65,13 @@
it "sets the original_filename" do
expect(question.original_filename).to eq original_filename
end

it "logs information about the file" do
question.validate
expect(Rails.logger).to have_received(:info).with("Uploaded file to S3 for file upload question",
{ file_size_in_bytes:,
file_type: })
end
end

context "when no file was selected (question was optional)" do
Expand Down Expand Up @@ -133,16 +145,27 @@

context "when the file size is greater than 7MB" do
let(:uploaded_file) { instance_double(ActionDispatch::Http::UploadedFile) }
let(:file_size_in_bytes) { 7.megabytes + 1 }
let(:file_type) { "image/png" }

before do
allow(uploaded_file).to receive(:size).and_return(7.megabytes + 1)
allow(uploaded_file).to receive_messages(size: file_size_in_bytes, content_type: file_type)
question.file = uploaded_file

allow(Rails.logger).to receive(:info).at_least(:once)
end

it "returns an error" do
expect(question).not_to be_valid
expect(question.errors[:file]).to include "The selected file must be smaller than 7MB"
end

it "logs information about the file" do
question.validate
expect(Rails.logger).to have_received(:info).with("File upload question validation failed: file too big",
{ file_size_in_bytes:,
file_type: })
end
end

context "when the file is valid" do
Expand Down

0 comments on commit 04aac36

Please sign in to comment.