uploads: clean up process_upload.
* Remove `initialize_status` (status already defaults to pending in database) * Remove `has_dimensions?` (always returns true) * Remove `async_conversion?` (dead code) * Remove `validate_file_exists` (unneeded checks) * Simplify `calculate_dimensions` * Merge `file_header_to_content_type` with `content_type_to_file_ext` (content type isn't used elsewhere)
This commit is contained in:
@@ -10,7 +10,6 @@ class Upload < ApplicationRecord
|
|||||||
belongs_to :uploader, :class_name => "User"
|
belongs_to :uploader, :class_name => "User"
|
||||||
belongs_to :post
|
belongs_to :post
|
||||||
before_validation :initialize_uploader, :on => :create
|
before_validation :initialize_uploader, :on => :create
|
||||||
before_validation :initialize_status, :on => :create
|
|
||||||
before_create :convert_cgi_file
|
before_create :convert_cgi_file
|
||||||
after_destroy :delete_temp_file
|
after_destroy :delete_temp_file
|
||||||
validate :uploader_is_not_limited, :on => :create
|
validate :uploader_is_not_limited, :on => :create
|
||||||
@@ -53,12 +52,6 @@ class Upload < ApplicationRecord
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def validate_file_exists
|
|
||||||
unless file_path && File.exists?(file_path)
|
|
||||||
raise "file does not exist"
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
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, MP4, and WebM files are allowed)"
|
raise "invalid content type (only JPEG, PNG, GIF, SWF, MP4, and WebM files are allowed)"
|
||||||
@@ -75,12 +68,6 @@ class Upload < ApplicationRecord
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def validate_md5_confirmation_after_move
|
|
||||||
if !md5_confirmation.blank? && md5_confirmation != Digest::MD5.file(md5_file_path).hexdigest
|
|
||||||
raise "md5 mismatch"
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def rating_given
|
def rating_given
|
||||||
if rating.present?
|
if rating.present?
|
||||||
return true
|
return true
|
||||||
@@ -112,13 +99,12 @@ class Upload < ApplicationRecord
|
|||||||
def process_upload
|
def process_upload
|
||||||
CurrentUser.scoped(uploader, uploader_ip_addr) do
|
CurrentUser.scoped(uploader, uploader_ip_addr) do
|
||||||
update_attribute(:status, "processing")
|
update_attribute(:status, "processing")
|
||||||
self.source = strip_source
|
|
||||||
|
self.source = source.to_s.strip
|
||||||
if is_downloadable?
|
if is_downloadable?
|
||||||
self.downloaded_source, self.source = download_from_source(temp_file_path)
|
self.downloaded_source, self.source = download_from_source(temp_file_path)
|
||||||
end
|
end
|
||||||
validate_file_exists
|
self.file_ext = file_header_to_file_ext(file_path)
|
||||||
self.content_type = file_header_to_content_type(file_path)
|
|
||||||
self.file_ext = content_type_to_file_ext(content_type)
|
|
||||||
validate_file_content_type
|
validate_file_content_type
|
||||||
calculate_hash(file_path)
|
calculate_hash(file_path)
|
||||||
validate_md5_uniqueness
|
validate_md5_uniqueness
|
||||||
@@ -126,12 +112,9 @@ class Upload < ApplicationRecord
|
|||||||
tag_audio
|
tag_audio
|
||||||
validate_video_duration
|
validate_video_duration
|
||||||
calculate_file_size(file_path)
|
calculate_file_size(file_path)
|
||||||
if has_dimensions?
|
self.image_width, self.image_height = calculate_dimensions
|
||||||
calculate_dimensions(file_path)
|
|
||||||
end
|
|
||||||
generate_resizes(file_path)
|
generate_resizes(file_path)
|
||||||
move_file
|
move_file
|
||||||
validate_md5_confirmation_after_move
|
|
||||||
save
|
save
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -175,10 +158,6 @@ class Upload < ApplicationRecord
|
|||||||
delete_temp_file
|
delete_temp_file
|
||||||
end
|
end
|
||||||
|
|
||||||
def async_conversion?
|
|
||||||
is_ugoira?
|
|
||||||
end
|
|
||||||
|
|
||||||
def ugoira_service
|
def ugoira_service
|
||||||
@ugoira_service ||= PixivUgoiraService.new
|
@ugoira_service ||= PixivUgoiraService.new
|
||||||
end
|
end
|
||||||
@@ -288,27 +267,17 @@ class Upload < ApplicationRecord
|
|||||||
|
|
||||||
module DimensionMethods
|
module DimensionMethods
|
||||||
# Figures out the dimensions of the image.
|
# Figures out the dimensions of the image.
|
||||||
def calculate_dimensions(file_path)
|
def calculate_dimensions
|
||||||
if is_video?
|
if is_video?
|
||||||
self.image_width = video.width
|
[video.width, video.height]
|
||||||
self.image_height = video.height
|
|
||||||
elsif is_ugoira?
|
elsif is_ugoira?
|
||||||
ugoira_service.calculate_dimensions(file_path)
|
ugoira_service.calculate_dimensions(file_path)
|
||||||
self.image_width = ugoira_service.width
|
[ugoira_service.width, ugoira_service.height]
|
||||||
self.image_height = ugoira_service.height
|
|
||||||
else
|
else
|
||||||
File.open(file_path, "rb") do |file|
|
image_size = ImageSpec.new(file_path)
|
||||||
image_size = ImageSpec.new(file)
|
[image_size.width, image_size.height]
|
||||||
self.image_width = image_size.width
|
|
||||||
self.image_height = image_size.height
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# Does this file have image dimensions?
|
|
||||||
def has_dimensions?
|
|
||||||
%w(jpg gif png swf webm mp4 zip).include?(file_ext)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
module ContentTypeMethods
|
module ContentTypeMethods
|
||||||
@@ -316,61 +285,26 @@ class Upload < ApplicationRecord
|
|||||||
file_ext =~ /jpg|gif|png|swf|webm|mp4|zip/
|
file_ext =~ /jpg|gif|png|swf|webm|mp4|zip/
|
||||||
end
|
end
|
||||||
|
|
||||||
def content_type_to_file_ext(content_type)
|
def file_header_to_file_ext(file_path)
|
||||||
case content_type
|
case File.read(file_path, 16)
|
||||||
when "image/jpeg"
|
when /^\xff\xd8/n
|
||||||
"jpg"
|
"jpg"
|
||||||
|
when /^GIF87a/, /^GIF89a/
|
||||||
when "image/gif"
|
|
||||||
"gif"
|
"gif"
|
||||||
|
when /^\x89PNG\r\n\x1a\n/n
|
||||||
when "image/png"
|
|
||||||
"png"
|
"png"
|
||||||
|
when /^CWS/, /^FWS/, /^ZWS/
|
||||||
when "application/x-shockwave-flash"
|
|
||||||
"swf"
|
"swf"
|
||||||
|
when /^\x1a\x45\xdf\xa3/n
|
||||||
when "video/webm"
|
|
||||||
"webm"
|
"webm"
|
||||||
|
when /^....ftyp(?:isom|3gp5|mp42|MSNV|avc1)/
|
||||||
when "video/mp4"
|
|
||||||
"mp4"
|
"mp4"
|
||||||
|
when /^PK\x03\x04/
|
||||||
when "application/zip"
|
|
||||||
"zip"
|
"zip"
|
||||||
|
|
||||||
else
|
else
|
||||||
"bin"
|
"bin"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def file_header_to_content_type(source_path)
|
|
||||||
case File.read(source_path, 16)
|
|
||||||
when /^\xff\xd8/n
|
|
||||||
"image/jpeg"
|
|
||||||
|
|
||||||
when /^GIF87a/, /^GIF89a/
|
|
||||||
"image/gif"
|
|
||||||
|
|
||||||
when /^\x89PNG\r\n\x1a\n/n
|
|
||||||
"image/png"
|
|
||||||
|
|
||||||
when /^CWS/, /^FWS/, /^ZWS/
|
|
||||||
"application/x-shockwave-flash"
|
|
||||||
|
|
||||||
when /^\x1a\x45\xdf\xa3/n
|
|
||||||
"video/webm"
|
|
||||||
|
|
||||||
when /^....ftyp(?:isom|3gp5|mp42|MSNV|avc1)/
|
|
||||||
"video/mp4"
|
|
||||||
|
|
||||||
when /^PK\x03\x04/
|
|
||||||
"application/zip"
|
|
||||||
|
|
||||||
else
|
|
||||||
"application/octet-stream"
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
module FilePathMethods
|
module FilePathMethods
|
||||||
@@ -405,10 +339,6 @@ class Upload < ApplicationRecord
|
|||||||
end
|
end
|
||||||
|
|
||||||
module DownloaderMethods
|
module DownloaderMethods
|
||||||
def strip_source
|
|
||||||
source.to_s.strip
|
|
||||||
end
|
|
||||||
|
|
||||||
# Determines whether the source is downloadable
|
# Determines whether the source is downloadable
|
||||||
def is_downloadable?
|
def is_downloadable?
|
||||||
source =~ /^https?:\/\// && file_path.blank?
|
source =~ /^https?:\/\// && file_path.blank?
|
||||||
@@ -442,10 +372,6 @@ class Upload < ApplicationRecord
|
|||||||
end
|
end
|
||||||
|
|
||||||
module StatusMethods
|
module StatusMethods
|
||||||
def initialize_status
|
|
||||||
self.status = "pending"
|
|
||||||
end
|
|
||||||
|
|
||||||
def is_pending?
|
def is_pending?
|
||||||
status == "pending"
|
status == "pending"
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user