File tree 2 files changed +35
-1
lines changed
app/controllers/decidim/apifiles
spec/controllers/decidim/apifiles
2 files changed +35
-1
lines changed Original file line number Diff line number Diff line change @@ -31,7 +31,7 @@ def create
31
31
32
32
blob = ActiveStorage ::Blob . create_and_upload! (
33
33
io : uploaded_file ,
34
- filename : uploaded_file . original_filename ,
34
+ filename : sanitized_filename ,
35
35
content_type : uploaded_file . content_type
36
36
)
37
37
@@ -46,6 +46,22 @@ def uploaded_file
46
46
@uploaded_file ||= params . require ( :file )
47
47
end
48
48
49
+ # In case the file name contains invalid byte sequences, they could not be
50
+ # stored in the database due to the following exception:
51
+ # ActiveRecord::StatementInvalid:
52
+ # PG::CharacterNotInRepertoire: ERROR: invalid byte sequence for encoding "UTF8": 0xea 0x78 0xe4
53
+ # CONTEXT: unnamed portal parameter $2
54
+ #
55
+ # This replaces all invalid byte sequences before persisting the file to
56
+ # the database.
57
+ def sanitized_filename
58
+ @sanitized_filename || begin
59
+ name = uploaded_file . original_filename
60
+ name = name . scrub unless name . valid_encoding?
61
+ name
62
+ end
63
+ end
64
+
49
65
def uploaded_file_extension
50
66
@uploaded_file_extension ||= File . extname ( uploaded_file . original_filename ) . strip . downcase [ 1 ..-1 ]
51
67
end
Original file line number Diff line number Diff line change 86
86
expect ( response . parsed_body ) . to eq ( { "error" => "unallowed_content_type" } )
87
87
end
88
88
end
89
+
90
+ context "with file name in Windows-1252 encoding" do
91
+ let ( :file ) do
92
+ Decidim ::Dev . test_file ( "Exampledocument.pdf" , "application/pdf" ) . tap do |f |
93
+ # In case we stored a file with such name in the "fixtures" folder,
94
+ # this would not work because `rack-test` fails to generate the
95
+ # `Rack::Test::UploadedFile` due to the weird name. We need to force
96
+ # the name on the instance in order to replicate the bug with
97
+ # storing file names with this encoding. There is no other way to
98
+ # change the "original_filename" of the instance.
99
+ f . instance_variable_set ( :@original_filename , "êxämplö®'.pdf" . encode ( "WINDOWS-1252" ) )
100
+ end
101
+ end
102
+
103
+ it "allows uploading a file" do
104
+ expect ( response ) . to have_http_status ( :ok )
105
+ end
106
+ end
89
107
end
90
108
end
91
109
end
You can’t perform that action at this time.
0 commit comments