From 92c68db37a587fa048793e54c37939e1fc957640 Mon Sep 17 00:00:00 2001 From: r888888888 Date: Tue, 25 Aug 2015 17:02:57 -0700 Subject: [PATCH] addresses #2499: Support uploading mp4 video --- app/models/post.rb | 16 ++++++++++++++-- app/models/upload.rb | 16 +++++++++++----- 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/app/models/post.rb b/app/models/post.rb index b1201b32f..965f7b150 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -153,10 +153,18 @@ class Post < ActiveRecord::Base file_ext =~ /swf/i end - def is_video? + def is_webm? file_ext =~ /webm/i end + def is_mp4? + file_ext =~ /mp4/i + end + + def is_video? + is_webm? || is_mp4? + end + def is_ugoira? file_ext =~ /zip/i end @@ -620,10 +628,14 @@ class Post < ActiveRecord::Base tags << "flash" end - if is_video? + if is_webm? tags << "webm" end + if is_mp4? + tags << "mp4" + end + if is_ugoira? tags << "ugoira" end diff --git a/app/models/upload.rb b/app/models/upload.rb index 9015ff447..e0c0c0c97 100644 --- a/app/models/upload.rb +++ b/app/models/upload.rb @@ -59,7 +59,7 @@ class Upload < ActiveRecord::Base def validate_file_content_type unless is_valid_content_type? - raise "invalid content type (only JPEG, PNG, GIF, SWF, and WebM files are allowed)" + raise "invalid content type (only JPEG, PNG, GIF, SWF, MP4, and WebM files are allowed)" end if is_ugoira? && ugoira_service.empty? @@ -211,7 +211,7 @@ class Upload < ActiveRecord::Base end def is_video? - %w(webm).include?(file_ext) + %w(webm mp4).include?(file_ext) end def is_ugoira? @@ -281,13 +281,13 @@ class Upload < ActiveRecord::Base # Does this file have image dimensions? def has_dimensions? - %w(jpg gif png swf webm zip).include?(file_ext) + %w(jpg gif png swf webm mp4 zip).include?(file_ext) end end module ContentTypeMethods def is_valid_content_type? - file_ext =~ /jpg|gif|png|swf|webm|zip/ + file_ext =~ /jpg|gif|png|swf|webm|mp4|zip/ end def content_type_to_file_ext(content_type) @@ -307,6 +307,9 @@ class Upload < ActiveRecord::Base when "video/webm" "webm" + when "video/mp4" + "mp4" + when "application/zip" "zip" @@ -316,7 +319,7 @@ class Upload < ActiveRecord::Base end def file_header_to_content_type(source_path) - case File.read(source_path, 10) + case File.read(source_path, 16) when /^\xff\xd8/n "image/jpeg" @@ -332,6 +335,9 @@ class Upload < ActiveRecord::Base when /^\x1a\x45\xdf\xa3/n "video/webm" + when /^....ftyp(?:isom|3gp5|mp42|MSNV)/ + "video/mp4" + when /^PK\x03\x04/ "application/zip"