media assets: move more file-handling logic into MediaAsset.

Move more of the file-handling logic from UploadService and
StorageManager into MediaAsset. This is part of refactoring posts and
uploads to allow multiple images per post.
This commit is contained in:
evazion
2021-10-17 21:59:09 -05:00
parent 8b3ab04724
commit 1d034a3223
10 changed files with 238 additions and 104 deletions

View File

@@ -109,13 +109,13 @@ class StorageManager
case type
when :preview
"#{base_dir}/preview/#{subdir}#{file}"
"/preview/#{subdir}#{file}"
when :crop
"#{base_dir}/crop/#{subdir}#{file}"
"/crop/#{subdir}#{file}"
when :large
"#{base_dir}/sample/#{subdir}#{file}"
"/sample/#{subdir}#{file}"
when :original
"#{base_dir}/original/#{subdir}#{file}"
"/original/#{subdir}#{file}"
end
end
@@ -145,4 +145,8 @@ class StorageManager
tags = post.presenter.humanized_essential_tag_string.gsub(/[^a-z0-9]+/, "_").gsub(/(?:^_+)|(?:_+$)/, "").gsub(/_{2,}/, "_")
"__#{tags}__"
end
def full_path(path)
File.join(base_dir, path)
end
end

View File

@@ -3,7 +3,7 @@ class StorageManager::Local < StorageManager
DEFAULT_PERMISSIONS = 0o644
def store(io, dest_path)
temp_path = dest_path + "-" + SecureRandom.uuid + ".tmp"
temp_path = full_path(dest_path) + "-" + SecureRandom.uuid + ".tmp"
FileUtils.mkdir_p(File.dirname(temp_path))
io.rewind
@@ -11,17 +11,17 @@ class StorageManager::Local < StorageManager
raise Error, "store failed: #{bytes_copied}/#{io.size} bytes copied" if bytes_copied != io.size
FileUtils.chmod(DEFAULT_PERMISSIONS, temp_path)
File.rename(temp_path, dest_path)
File.rename(temp_path, full_path(dest_path))
rescue StandardError => e
FileUtils.rm_f(temp_path)
raise Error, e
end
def delete(path)
FileUtils.rm_f(path)
FileUtils.rm_f(full_path(path))
end
def open(path)
File.open(path, "r", binmode: true)
File.open(full_path(path), "r", binmode: true)
end
end

View File

@@ -35,6 +35,6 @@ class StorageManager::Rclone < StorageManager
end
def key(path)
":#{remote}:#{bucket}#{path}"
":#{remote}:#{bucket}#{full_path(path)}"
end
end

View File

@@ -19,6 +19,7 @@ class StorageManager::SFTP < StorageManager
end
def store(file, dest_path)
dest_path = full_path(dest_path)
temp_upload_path = dest_path + "-" + SecureRandom.uuid + ".tmp"
dest_backup_path = dest_path + "-" + SecureRandom.uuid + ".bak"
@@ -42,7 +43,7 @@ class StorageManager::SFTP < StorageManager
def delete(dest_path)
each_host do |_host, sftp|
force { sftp.remove!(dest_path) }
force { sftp.remove!(full_path(dest_path)) }
end
end
@@ -50,7 +51,7 @@ class StorageManager::SFTP < StorageManager
file = Tempfile.new(binmode: true)
Net::SFTP.start(hosts.first, nil, ssh_options) do |sftp|
sftp.download!(dest_path, file.path)
sftp.download!(full_path(dest_path), file.path)
end
file

View File

@@ -2,52 +2,10 @@ class UploadService
module Utils
module_function
def distribute_files(file, record, type, original_post_id: nil)
# need to do this for hybrid storage manager
post = Post.new
post.id = original_post_id if original_post_id.present?
post.md5 = record.md5
post.file_ext = record.file_ext
[Danbooru.config.storage_manager, Danbooru.config.backup_storage_manager].each do |sm|
sm.store_file(file, post, type)
end
end
def is_downloadable?(source)
source =~ %r{\Ahttps?://}
end
def generate_resizes(media_file)
preview_file = media_file.preview(Danbooru.config.small_image_width, Danbooru.config.small_image_width)
crop_file = media_file.crop(Danbooru.config.small_image_width, Danbooru.config.small_image_width)
if media_file.is_ugoira?
sample_file = media_file.convert
elsif media_file.is_image? && media_file.width > Danbooru.config.large_image_width
sample_file = media_file.preview(Danbooru.config.large_image_width, media_file.height)
else
sample_file = nil
end
[preview_file, crop_file, sample_file]
end
def process_resizes(upload, file, original_post_id, media_file: nil)
media_file ||= upload.media_file
preview_file, crop_file, sample_file = Utils.generate_resizes(media_file)
begin
Utils.distribute_files(file, upload, :original, original_post_id: original_post_id) if file.present?
Utils.distribute_files(sample_file, upload, :large, original_post_id: original_post_id) if sample_file.present?
Utils.distribute_files(preview_file, upload, :preview, original_post_id: original_post_id) if preview_file.present?
Utils.distribute_files(crop_file, upload, :crop, original_post_id: original_post_id) if crop_file.present?
ensure
preview_file.try(:close!)
crop_file.try(:close!)
sample_file.try(:close!)
end
end
def process_file(upload, file, original_post_id: nil)
upload.file = file
media_file = upload.media_file
@@ -61,9 +19,7 @@ class UploadService
upload.validate!(:file)
upload.tag_string = "#{upload.tag_string} #{Utils.automatic_tags(media_file)}"
process_resizes(upload, file, original_post_id)
MediaAsset.create!(file: media_file)
MediaAsset.upload!(media_file)
end
def automatic_tags(media_file)