Skip to content

Commit 4e1da15

Browse files
authored
Serve SVGs with correct content type (#763)
Closes RaspberryPiFoundation/digital-editor-issues#1286
1 parent 23ad749 commit 4e1da15

3 files changed

Lines changed: 50 additions & 6 deletions

File tree

app/controllers/api/scratch/assets_controller.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,15 @@
33
module Api
44
module Scratch
55
class AssetsController < ScratchController
6+
include ActiveStorage::SetCurrent
7+
68
skip_before_action :authorize_user, only: [:show]
79
skip_before_action :check_scratch_feature, only: [:show]
810

911
def show
1012
filename_with_extension = "#{params[:id]}.#{params[:format]}"
11-
redirect_to rails_storage_redirect_url(ScratchAsset.find_by!(filename: filename_with_extension).file)
13+
scratch_asset = ScratchAsset.find_by!(filename: filename_with_extension)
14+
redirect_to scratch_asset.file.url(content_type: scratch_asset.file.content_type), allow_other_host: true
1215
end
1316

1417
def create

spec/features/scratch/creating_and_showing_a_scratch_asset_spec.rb

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,43 @@
44

55
RSpec.describe 'Creating a Scratch asset', type: :request do
66
let(:basename) { 'test_image_1' }
7+
let(:svg_filename) { 'test_svg_image.svg' }
78
let(:format) { 'png' }
89
let(:filename) { "#{basename}.#{format}" }
910
let(:school) { create(:school) }
1011
let(:teacher) { create(:teacher, school:) }
1112
let(:cookie_headers) { { 'Cookie' => "scratch_auth=#{UserProfileMock::TOKEN}" } }
1213

1314
describe 'GET #show' do
14-
let(:make_request) { get '/api/scratch/assets/internalapi/asset/test_image_1.png/get/' }
15+
context 'when the asset is PNG' do
16+
before do
17+
create(:scratch_asset, :with_file, filename:, asset_path: file_fixture(filename))
18+
end
1519

16-
context 'when the asset exists' do
17-
let!(:scratch_asset) { create(:scratch_asset, :with_file, filename:, asset_path: file_fixture(filename)) }
20+
let(:make_request) { get '/api/scratch/assets/internalapi/asset/test_image_1.png/get/' }
1821

19-
it 'redirects to the asset file URL' do
22+
it 'serves the file with png content type' do
2023
make_request
2124

22-
expect(response).to redirect_to(rails_storage_redirect_url(scratch_asset.file, only_path: true))
25+
follow_redirect! while response.redirect?
26+
27+
expect(response.media_type).to eq('image/png')
28+
end
29+
end
30+
31+
context 'when the asset is SVG' do
32+
before do
33+
create(:scratch_asset, :with_file, filename: svg_filename, asset_path: file_fixture(svg_filename))
34+
end
35+
36+
let(:make_request) { get '/api/scratch/assets/internalapi/asset/test_svg_image.svg/get/' }
37+
38+
it 'serves the file with image/svg+xml content type' do
39+
make_request
40+
41+
follow_redirect! while response.redirect?
42+
43+
expect(response.media_type).to eq('image/svg+xml')
2344
end
2445
end
2546
end
Lines changed: 20 additions & 0 deletions
Loading

0 commit comments

Comments
 (0)