uploads: autotag animated_gif/png during upload.

Move animated_gif / animated_png autotagging to take place during
uploading, instead of during tag editing. We can't generally assume the
file will be present on the local filesystem after uploading.
This commit is contained in:
evazion
2018-03-18 11:55:24 -05:00
parent 2286ccfca8
commit 60dcfbfbdd
3 changed files with 23 additions and 34 deletions

View File

@@ -86,7 +86,7 @@ class APNGInspector
end end
@corrupted = !read_success || actl_corrupted @corrupted = !read_success || actl_corrupted
return !@corrupted self
end end
def corrupted? def corrupted?
@@ -109,4 +109,4 @@ class APNGInspector
return framedata.unpack("N".freeze)[0] return framedata.unpack("N".freeze)[0]
end end
end end

View File

@@ -255,24 +255,6 @@ class Post < ApplicationRecord
file_ext =~ /jpg|jpeg|gif|png/i file_ext =~ /jpg|jpeg|gif|png/i
end end
def is_animated_gif?
if file_ext =~ /gif/i && File.exists?(file_path)
return Magick::Image.ping(file_path).length > 1
else
return false
end
end
def is_animated_png?
if file_ext =~ /png/i && File.exists?(file_path)
apng = APNGInspector.new(file_path)
apng.inspect!
return apng.animated?
else
return false
end
end
def is_flash? def is_flash?
file_ext =~ /swf/i file_ext =~ /swf/i
end end
@@ -765,7 +747,6 @@ class Post < ApplicationRecord
return tags if !Danbooru.config.enable_dimension_autotagging return tags if !Danbooru.config.enable_dimension_autotagging
tags -= %w(incredibly_absurdres absurdres highres lowres huge_filesize flash webm mp4) tags -= %w(incredibly_absurdres absurdres highres lowres huge_filesize flash webm mp4)
tags -= %w(animated_gif animated_png) if new_record?
if has_dimensions? if has_dimensions?
if image_width >= 10_000 || image_height >= 10_000 if image_width >= 10_000 || image_height >= 10_000
@@ -794,14 +775,6 @@ class Post < ApplicationRecord
tags << "huge_filesize" tags << "huge_filesize"
end end
if is_animated_gif?
tags << "animated_gif"
end
if is_animated_png?
tags << "animated_png"
end
if is_flash? if is_flash?
tags << "flash" tags << "flash"
end end

View File

@@ -80,10 +80,14 @@ class Upload < ApplicationRecord
end end
end end
def tag_audio def automatic_tags
if is_video? && video.audio_channels.present? return "" unless Danbooru.config.enable_dimension_autotagging
self.tag_string = "#{tag_string} video_with_sound"
end tags = []
tags << "video_with_sound" if is_video_with_audio?
tags << "animated_gif" if is_animated_gif?
tags << "animated_png" if is_animated_png?
tags.join(" ")
end end
def validate_video_duration def validate_video_duration
@@ -109,9 +113,9 @@ class Upload < ApplicationRecord
calculate_hash(file_path) calculate_hash(file_path)
validate_md5_uniqueness validate_md5_uniqueness
validate_md5_confirmation validate_md5_confirmation
tag_audio
validate_video_duration validate_video_duration
calculate_file_size(file_path) calculate_file_size(file_path)
self.tag_string = "#{tag_string} #{automatic_tags}"
self.image_width, self.image_height = calculate_dimensions self.image_width, self.image_height = calculate_dimensions
generate_resizes(file_path) generate_resizes(file_path)
move_file move_file
@@ -219,9 +223,21 @@ class Upload < ApplicationRecord
%w(webm mp4).include?(file_ext) %w(webm mp4).include?(file_ext)
end end
def is_video_with_audio?
is_video? && video.audio_channels.present?
end
def is_ugoira? def is_ugoira?
%w(zip).include?(file_ext) %w(zip).include?(file_ext)
end end
def is_animated_gif?
file_ext == "gif" && Magick::Image.ping(file_path).length > 1
end
def is_animated_png?
file_ext == "png" && APNGInspector.new(file_path).inspect!.animated?
end
end end
module ResizerMethods module ResizerMethods