@@ -8,50 +8,56 @@ def show
88 if ( key = decode_verified_key )
99 serve_file ( key [ :key ] , content_type : key [ :content_type ] , disposition : key [ :disposition ] )
1010 else
11- head :not_found
11+ head ( :not_found )
1212 end
1313 rescue ActiveStorage ::FileNotFoundError
14- head :not_found
14+ head ( :not_found )
1515 end
1616
1717 def update
1818 if ( token = decode_verified_token )
19- if acceptable_content? ( token )
20- db_service . upload ( token [ :key ] , request . body , checksum : token [ :checksum ] )
21- else
22- head :unprocessable_entity
23- end
19+ file_uploaded = upload_file ( token , body : request . body )
20+ head ( file_uploaded ? :no_content : :unprocessable_entity )
2421 else
25- head :not_found
22+ head ( :not_found )
2623 end
2724 rescue ActiveStorage ::IntegrityError
28- head :unprocessable_entity
25+ head ( :unprocessable_entity )
2926 end
3027
3128 private
3229
30+ def acceptable_content? ( token )
31+ token [ :content_type ] == request . content_mime_type && token [ :content_length ] == request . content_length
32+ end
33+
3334 def db_service
3435 ActiveStorage ::Blob . service
3536 end
3637
3738 def decode_verified_key
38- ActiveStorage . verifier . verified ( params [ :encoded_key ] , purpose : :blob_key )
39+ key = ActiveStorage . verifier . verified ( params [ :encoded_key ] , purpose : :blob_key )
40+ key &.deep_symbolize_keys
41+ end
42+
43+ def decode_verified_token
44+ token = ActiveStorage . verifier . verified ( params [ :encoded_token ] , purpose : :blob_token )
45+ token &.deep_symbolize_keys
3946 end
4047
4148 def serve_file ( key , content_type :, disposition :)
4249 options = {
4350 type : content_type || DEFAULT_SEND_FILE_TYPE ,
4451 disposition : disposition || DEFAULT_SEND_FILE_DISPOSITION
4552 }
46- send_data db_service . download ( key ) , options
53+ send_data ( db_service . download ( key ) , options )
4754 end
4855
49- def decode_verified_token
50- ActiveStorage . verifier . verified ( params [ :encoded_token ] , purpose : :blob_token )
51- end
56+ def upload_file ( token , body :)
57+ return false unless acceptable_content? ( token )
5258
53- def acceptable_content? ( token )
54- token [ :content_type ] == request . content_mime_type && token [ :content_length ] == request . content_length
59+ db_service . upload ( token [ :key ] , request . body , checksum : token [ :checksum ] )
60+ true
5561 end
5662 end
5763end
0 commit comments