Setting content type for mp4 files on s3

I add user-uploaded videos to the RoRs site using paperclip gem and s3 memory. For some reason, I canโ€™t understand when the user downloads the mp4 file, s3 sets the content type for this file as application/mp4 instead of video/mp4 .

Note that I registered the mime mp4 type in the initialization file:

Mime::Type.lookup_by_extension('mp4').to_s => "video/mp4"

Here is the relevant part of my Post model:

  has_attached_file :video, :storage => :s3, :s3_credentials => "#{Rails.root.to_s}/config/s3.yml", :path => "/video/:id/:filename" validates_attachment_content_type :video, :content_type => ['video/mp4'], :message => "Sorry, this site currently only supports MP4 video" 

What I miss in the settings of the paper clip and / or s3.

#### Update #####

For some reason I don't know Rails, my default mime types for files containing mp4 are as follows:

  MIME::Types.type_for("my_video.mp4").to_s => "[application/mp4, audio/mp4, video/mp4, video/vnd.objectvideo]" 

So, when paperclip sends the mp4 file to s3, it seems to identify the mime file type as the first default "application / mp4". This is why s3 identifies the file as having the content type โ€œapplication / mp4โ€. Since I want to enable streaming of these mp4 files, I need a folder to identify the file as having a mime type of "video / mp4".

Is there a way to change the paperclip (possibly in the before_post_process filter) to allow this, or is there a way to change the rails through the initialization file to identify mp4 files as "video / mp4". If I could do what is best.

thanks for the help

+6
source share
2 answers

It turns out that I needed to set the content3 header to s3 in the model by default. This is not the best solution for me, because at some point I can start to allow video containers other than mp4. But it makes me move on to the next problem.

  has_attached_file :video, :storage => :s3, :s3_credentials => "#{Rails.root.to_s}/config/s3.yml", :path => "/video/:id/:filename", :s3_headers => { "Content-Type" => "video/mp4" } 
+7
source

I have done the following:

 ... MIN_VIDEO_SIZE = 0.megabytes MAX_VIDEO_SIZE = 2048.megabytes VALID_VIDEO_CONTENT_TYPES = ["video/mp4", /\Avideo/] # Note: The regular expression /\Avideo/ will match anything that starts with "video" has_attached_file :video, { url: BASE_URL, path: "video/:id_partition/:filename" } validates_attachment :video, size: { in: MIN_VIDEO_SIZE..MAX_VIDEO_SIZE }, content_type: { content_type: VALID_VIDEO_CONTENT_TYPES } before_validation :validate_video_content_type, on: :create before_post_process :validate_video_content_type def validate_video_content_type if video_content_type == "application/octet-stream" # Finds the first match and returns it. # Alternatively you could use the ".select" method instead which would find all mime types that match any of the VALID_VIDEO_CONTENT_TYPES mime_type = MIME::Types.type_for(video_file_name).find do |type| type.to_s.match Regexp.union(VALID_VIDEO_CONTENT_TYPES) end self.video_content_type = mime_type.to_s unless mime_type.blank? end end ... 
+1
source

Source: https://habr.com/ru/post/921161/


All Articles