integrate ugoiras into zip+webm+preview

This commit is contained in:
r888888888
2014-10-09 17:05:47 -07:00
parent 0a61aac231
commit 3bb06c2be4
28 changed files with 1800 additions and 1125 deletions

View File

@@ -0,0 +1,4 @@
class PixivUgoiraFrameData < ActiveRecord::Base
attr_accessible :post_id, :data
serialize :data
end

View File

@@ -26,6 +26,7 @@ class Post < ActiveRecord::Base
belongs_to :parent, :class_name => "Post"
has_one :upload, :dependent => :destroy
has_one :artist_commentary, :dependent => :destroy
has_one :pixiv_ugoira_frame_data, :class_name => "PixivUgoiraFrameData"
has_many :flags, :class_name => "PostFlag", :dependent => :destroy
has_many :appeals, :class_name => "PostAppeal", :dependent => :destroy
has_many :versions, lambda {order("post_versions.updated_at ASC, post_versions.id ASC")}, :class_name => "PostVersion", :dependent => :destroy
@@ -70,12 +71,20 @@ class Post < ActiveRecord::Base
def large_file_path
if has_large?
"#{Rails.root}/public/data/sample/#{file_path_prefix}#{Danbooru.config.large_image_prefix}#{md5}.jpg"
"#{Rails.root}/public/data/sample/#{file_path_prefix}#{Danbooru.config.large_image_prefix}#{md5}.#{large_file_ext}"
else
file_path
end
end
def large_file_ext
if is_ugoira?
"webm"
else
"jpg"
end
end
def preview_file_path
"#{Rails.root}/public/data/preview/#{file_path_prefix}#{md5}.jpg"
end
@@ -129,7 +138,7 @@ class Post < ActiveRecord::Base
end
def is_video?
file_ext =~ /webm/i
file_ext =~ /webm|zip/i
end
def has_preview?

View File

@@ -112,6 +112,7 @@ class Upload < ActiveRecord::Base
post.distribute_files
if post.save
CurrentUser.increment!(:post_upload_count)
ugoira_service.process(post)
update_attributes(:status => "completed", :post_id => post.id)
else
update_attribute(:status, "error: " + post.errors.full_messages.join(", "))
@@ -140,6 +141,10 @@ class Upload < ActiveRecord::Base
delete_temp_file
end
def ugoira_service
@ugoira_service ||= PixivUgoiraService.new
end
def convert_to_post
Post.new.tap do |p|
p.tag_string = tag_string
@@ -190,16 +195,32 @@ class Upload < ActiveRecord::Base
def is_video?
%w(webm).include?(file_ext)
end
def is_ugoira?
%w(zip).include?(file_ext)
end
end
module ResizerMethods
def generate_resizes(source_path)
generate_resize_for(Danbooru.config.small_image_width, Danbooru.config.small_image_width, source_path, 85)
if is_image? && image_width > Danbooru.config.large_image_width
generate_resize_for(Danbooru.config.large_image_width, nil, source_path)
end
end
def generate_video_preview_for(width, height, output_path)
dimension_ratio = image_width.to_f / image_height
if dimension_ratio > 1
height = (width / dimension_ratio).to_i
else
width = (height * dimension_ratio).to_i
end
video.screenshot(output_path, {:seek_time => 0, :resolution => "#{width}x#{height}"})
FileUtils.chmod(0664, output_path)
end
def generate_resize_for(width, height, source_path, quality = 90)
unless File.exists?(source_path)
raise Error.new("file not found")
@@ -208,15 +229,10 @@ class Upload < ActiveRecord::Base
output_path = resized_file_path_for(width)
if is_image?
Danbooru.resize(source_path, output_path, width, height, quality)
elsif is_ugoira?
ugoira_service.generate_resizes(source_path, resized_file_path_for(Danbooru.config.large_image_width), resized_file_path_for(Danbooru.config.small_image_width))
elsif is_video?
dimension_ratio = image_width.to_f / image_height
if dimension_ratio > 1
height = (width / dimension_ratio).to_i
else
width = (height * dimension_ratio).to_i
end
video.screenshot(output_path, {:seek_time => 0, :resolution => "#{width}x#{height}"})
FileUtils.chmod(0664, output_path)
generate_video_preview_for(width, height, output_path)
end
end
end
@@ -227,6 +243,9 @@ class Upload < ActiveRecord::Base
if is_video?
self.image_width = video.width
self.image_height = video.height
elsif is_ugoira?
self.image_width = ugoira_service.width
self.image_height = ugoira_service.height
else
File.open(file_path, "rb") do |file|
image_size = ImageSpec.new(file)
@@ -238,13 +257,13 @@ class Upload < ActiveRecord::Base
# Does this file have image dimensions?
def has_dimensions?
%w(jpg gif png swf webm).include?(file_ext)
%w(jpg gif png swf webm zip).include?(file_ext)
end
end
module ContentTypeMethods
def is_valid_content_type?
file_ext =~ /jpg|gif|png|swf|webm/
file_ext =~ /jpg|gif|png|swf|webm|zip/
end
def content_type_to_file_ext(content_type)
@@ -264,6 +283,9 @@ class Upload < ActiveRecord::Base
when "video/webm"
"webm"
when "application/zip"
"zip"
else
"bin"
end
@@ -286,6 +308,9 @@ class Upload < ActiveRecord::Base
when /^\x1a\x45\xdf\xa3/
"video/webm"
when /^PK\x03\x04/
"application/zip"
else
"application/octet-stream"
end
@@ -321,23 +346,17 @@ class Upload < ActiveRecord::Base
source =~ /^https?:\/\// && file_path.blank?
end
def is_ugoira?
def has_ugoira_tag?
tag_string =~ /\bugoira\b/i
end
# Downloads the file to destination_path
def download_from_source(destination_path)
self.file_path = destination_path
if is_ugoira?
converter = PixivUgoiraConverter.new(source, destination_path, :webm)
converter.process!
self.source = source
else
download = Downloads::File.new(source, destination_path)
download.download!
self.source = download.source
end
download = Downloads::File.new(source, destination_path, :is_ugoira => has_ugoira_tag?)
download.download!
self.source = download.source
ugoira_service.load(download.data)
end
end