このコミットが含まれているのは:
2026-07-16 18:16:20 +09:00
コミット f76fbe6711
14個のファイルの変更445行の追加43行の削除
+35 -2
ファイルの表示
@@ -2,6 +2,8 @@ class Post < ApplicationRecord
require 'mini_magick'
require 'stringio'
class RemoteThumbnailFetchFailed < StandardError; end
ORIGINAL_CREATED_INVALID_MESSAGE = 'オリジナルの作成日時の形式が不正です.'.freeze
ORIGINAL_CREATED_MINUTE_PRECISION_MESSAGE =
'オリジナルの作成日時は分単位で入力してください.'.freeze
@@ -12,7 +14,9 @@ class Post < ApplicationRecord
def self.resized_thumbnail_attachment(upload)
upload.rewind
image = MiniMagick::Image.read(upload.read)
image.resize '180x180'
image.resize '180x180^'
image.gravity 'Center'
image.extent '180x180'
image.format 'jpg'
{ io: StringIO.new(image.to_blob),
@@ -125,7 +129,11 @@ class Post < ApplicationRecord
'%d:%02d' % [min, s]
end
remainder_ms.positive? ? "#{ base }.#{ remainder_ms.to_s.rjust(3, '0') }" : base
if remainder_ms.positive?
"#{ base }.#{ remainder_ms.to_s.rjust(3, '0') }"
else
base
end
end
def snapshot_parent_post_ids = parents.order(:id).pluck(:id)
@@ -147,8 +155,33 @@ class Post < ApplicationRecord
thumbnail.attach(self.class.resized_thumbnail_attachment(StringIO.new(thumbnail.download)))
end
def attach_thumbnail_from_url! raw_url
response = Preview::ThumbnailFetcher.fetch_image_response(raw_url)
thumbnail.attach(
io: StringIO.new(response.body),
filename: remote_thumbnail_filename(response.url),
content_type: response.content_type)
resized_thumbnail!
rescue Preview::UrlSafety::UnsafeUrl,
Preview::ThumbnailFetcher::GenerationFailed,
Preview::HttpFetcher::FetchFailed,
Preview::HttpFetcher::FetchTimeout,
Preview::HttpFetcher::ResponseTooLarge,
MiniMagick::Error => e
raise RemoteThumbnailFetchFailed, e.message
end
private
def remote_thumbnail_filename url
filename = File.basename(URI.parse(url).path.to_s)
return filename if filename.present? && filename != '/'
'thumbnail'
rescue URI::InvalidURIError
'thumbnail'
end
def validate_original_created_range
f = parse_original_created_value(:original_created_from)
b = parse_original_created_value(:original_created_before)