addresses #2499: Support uploading mp4 video

This commit is contained in:
r888888888
2015-08-25 17:02:57 -07:00
parent c81e028462
commit 92c68db37a
2 changed files with 25 additions and 7 deletions

View File

@@ -153,10 +153,18 @@ class Post < ActiveRecord::Base
file_ext =~ /swf/i file_ext =~ /swf/i
end end
def is_video? def is_webm?
file_ext =~ /webm/i file_ext =~ /webm/i
end end
def is_mp4?
file_ext =~ /mp4/i
end
def is_video?
is_webm? || is_mp4?
end
def is_ugoira? def is_ugoira?
file_ext =~ /zip/i file_ext =~ /zip/i
end end
@@ -620,10 +628,14 @@ class Post < ActiveRecord::Base
tags << "flash" tags << "flash"
end end
if is_video? if is_webm?
tags << "webm" tags << "webm"
end end
if is_mp4?
tags << "mp4"
end
if is_ugoira? if is_ugoira?
tags << "ugoira" tags << "ugoira"
end end

View File

@@ -59,7 +59,7 @@ class Upload < ActiveRecord::Base
def validate_file_content_type def validate_file_content_type
unless is_valid_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 end
if is_ugoira? && ugoira_service.empty? if is_ugoira? && ugoira_service.empty?
@@ -211,7 +211,7 @@ class Upload < ActiveRecord::Base
end end
def is_video? def is_video?
%w(webm).include?(file_ext) %w(webm mp4).include?(file_ext)
end end
def is_ugoira? def is_ugoira?
@@ -281,13 +281,13 @@ class Upload < ActiveRecord::Base
# Does this file have image dimensions? # Does this file have image dimensions?
def has_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
end end
module ContentTypeMethods module ContentTypeMethods
def is_valid_content_type? def is_valid_content_type?
file_ext =~ /jpg|gif|png|swf|webm|zip/ file_ext =~ /jpg|gif|png|swf|webm|mp4|zip/
end end
def content_type_to_file_ext(content_type) def content_type_to_file_ext(content_type)
@@ -307,6 +307,9 @@ class Upload < ActiveRecord::Base
when "video/webm" when "video/webm"
"webm" "webm"
when "video/mp4"
"mp4"
when "application/zip" when "application/zip"
"zip" "zip"
@@ -316,7 +319,7 @@ class Upload < ActiveRecord::Base
end end
def file_header_to_content_type(source_path) def file_header_to_content_type(source_path)
case File.read(source_path, 10) case File.read(source_path, 16)
when /^\xff\xd8/n when /^\xff\xd8/n
"image/jpeg" "image/jpeg"
@@ -332,6 +335,9 @@ class Upload < ActiveRecord::Base
when /^\x1a\x45\xdf\xa3/n when /^\x1a\x45\xdf\xa3/n
"video/webm" "video/webm"
when /^....ftyp(?:isom|3gp5|mp42|MSNV)/
"video/mp4"
when /^PK\x03\x04/ when /^PK\x03\x04/
"application/zip" "application/zip"