module Streams class CoverSlatePaths def self.slates_custom_root ENV.fetch("MEDIAMTX_SLATES_CUSTOM_DIR", "/slates/custom") end # ActiveStorage checksum is base64 e può contenere "/", "+", "=" — non usabile come path. def self.safe_digest(checksum) checksum.to_s.tr("+/", "-_").delete("=") end def self.filename_for(record) return nil unless record.cover_slate.attached? digest = safe_digest(record.cover_slate.blob.checksum) prefix = record.class.name.underscore "#{prefix}-#{record.id}-#{digest}.mp4" end def self.expected_path(record) name = filename_for(record) return nil if name.blank? File.join(slates_custom_root, name) end def self.local_path_for(record) path = expected_path(record) return nil if path.blank? return path if File.exist?(path) nil end end end