Files
redux-scraper/app/lib/vips_util.rb
2025-06-26 18:59:31 +00:00

31 lines
876 B
Ruby

# typed: strict
module VipsUtil
extend T::Sig
sig do
type_parameters(:T)
.params(
media_path: String,
load_gif: T.proc.returns(T.nilable(T.type_parameter(:T))),
on_load_failed:
T
.proc
.params(detected_content_type: String)
.returns(T.nilable(T.type_parameter(:T))),
)
.returns(T.nilable(T.type_parameter(:T)))
end
def self.try_load_gif(media_path, load_gif:, on_load_failed:)
begin
load_gif.call
rescue Vips::Error
raise unless $!.message.include?("gifload: no frames in GIF")
content_type = T.let(`file -i #{media_path}`, T.nilable(String))
raise unless $?.success?
content_type = content_type&.split(":")&.last&.split(";")&.first&.strip
raise unless content_type
return on_load_failed.call(content_type)
end
end
end