]> cat aescling's git repositories - mastodon.git/commitdiff
Use file extensions in addition to MIME types for file picker (#5029)
authorunarist <m.unarist@gmail.com>
Wed, 20 Sep 2017 17:07:23 +0000 (02:07 +0900)
committerEugen Rochko <eugen@zeonfederated.com>
Wed, 20 Sep 2017 17:07:23 +0000 (19:07 +0200)
Currently we're using a list of MIME types for `accept` attribute on `input[type="file"]` for filter options of file picker, and actual file extensions will be infered by browsers. However, infered extensions may not include our expected items. For example, "image/jpeg" seems to be infered to
only ".jfif" extension in Firefox.

To ensure common file extensions are in the list, this PR adds file extensions in addition to MIME types. Also having items in both format is encouraged by HTML5 spec.

https://www.w3.org/TR/html5/forms.html#file-upload-state-(type=file)

app/models/media_attachment.rb
app/serializers/initial_state_serializer.rb

index d913e7372a73fbcffdfa085f266e675c2524543c..e4a974f9683df8fbc100bc5be12ead67a388dcea 100644 (file)
@@ -25,6 +25,9 @@ class MediaAttachment < ApplicationRecord
 
   enum type: [:image, :gifv, :video, :unknown]
 
+  IMAGE_FILE_EXTENSIONS = ['.jpg', '.jpeg', '.png', '.gif'].freeze
+  VIDEO_FILE_EXTENSIONS = ['.webm', '.mp4', '.m4v'].freeze
+
   IMAGE_MIME_TYPES = ['image/jpeg', 'image/png', 'image/gif'].freeze
   VIDEO_MIME_TYPES = ['video/webm', 'video/mp4'].freeze
 
index 9ee9bd29c828978cb77261b539e195a3de9466c1..88bbc0dff3ce90daccc5aabb53c3992df7baa40b 100644 (file)
@@ -46,6 +46,6 @@ class InitialStateSerializer < ActiveModel::Serializer
   end
 
   def media_attachments
-    { accept_content_types: MediaAttachment::IMAGE_MIME_TYPES + MediaAttachment::VIDEO_MIME_TYPES }
+    { accept_content_types: MediaAttachment::IMAGE_FILE_EXTENSIONS + MediaAttachment::VIDEO_FILE_EXTENSIONS + MediaAttachment::IMAGE_MIME_TYPES + MediaAttachment::VIDEO_MIME_TYPES }
   end
 end