107 行
3.2 KiB
Ruby
107 行
3.2 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
|
|
module MaterialRepr
|
|
BASE = { only: [:id, :url, :version_no, :file_suppressed_at,
|
|
:file_suppression_reason, :created_at, :updated_at],
|
|
methods: [:content_type],
|
|
include: { tag: TagRepr::BASE,
|
|
created_by_user: UserRepr::BASE,
|
|
updated_by_user: UserRepr::BASE } }.freeze
|
|
|
|
module_function
|
|
|
|
def base material, host:
|
|
material.as_json(BASE).merge(
|
|
file: if material.file.attached? && !material.file_suppressed?
|
|
Rails.application.routes.url_helpers.rails_storage_proxy_url(
|
|
material.file, host:)
|
|
end,
|
|
thumbnail: thumbnail_url(material, host:),
|
|
thumbnail_fallback_text: thumbnail_fallback_text(material),
|
|
thumbnail_fallback_kind: thumbnail_fallback_kind(material),
|
|
media_kind: media_kind(material),
|
|
file_byte_size: material.file_byte_size,
|
|
export_paths: export_paths(material),
|
|
export_items: export_items(material))
|
|
end
|
|
|
|
def many materials, host:
|
|
materials.map { |m| base(m, host:) }
|
|
end
|
|
|
|
def list material, host:
|
|
{ id: material.id,
|
|
version_no: material.version_no,
|
|
url: material.url,
|
|
tag: compact_tag(material.tag),
|
|
thumbnail: thumbnail_url(material, host:),
|
|
thumbnail_fallback_text: thumbnail_fallback_text(material),
|
|
thumbnail_fallback_kind: thumbnail_fallback_kind(material),
|
|
media_kind: media_kind(material),
|
|
content_type: material.content_type,
|
|
file_byte_size: material.file_byte_size,
|
|
file_suppressed_at: material.file_suppressed_at,
|
|
created_at: material.created_at,
|
|
updated_at: material.updated_at,
|
|
export_paths: export_paths(material),
|
|
export_items: export_items(material) }
|
|
end
|
|
|
|
def list_many materials, host:
|
|
materials.map { |m| list(m, host:) }
|
|
end
|
|
|
|
def export_paths material
|
|
material.material_export_items.each_with_object({ }) do |item, hash|
|
|
hash[item.profile] = item.enabled ? item.export_path : ''
|
|
end
|
|
end
|
|
|
|
def export_items material
|
|
material.material_export_items.map do |item|
|
|
{ id: item.id,
|
|
profile: item.profile,
|
|
export_path: item.export_path,
|
|
enabled: item.enabled }
|
|
end
|
|
end
|
|
|
|
def thumbnail_url material, host:
|
|
return nil if material.file_suppressed?
|
|
return nil unless material.thumbnail.attached?
|
|
|
|
Rails.application.routes.url_helpers.rails_storage_proxy_url(
|
|
material.thumbnail, host:)
|
|
end
|
|
|
|
def thumbnail_fallback_text material
|
|
material.tag&.name || material.created_at&.strftime('%Y-%m-%d')
|
|
end
|
|
|
|
def thumbnail_fallback_kind material
|
|
material.tag.present? ? 'tag_name' : 'created_at'
|
|
end
|
|
|
|
def media_kind material
|
|
return 'suppressed' if material.file_suppressed?
|
|
return 'url_only' unless material.file.attached?
|
|
|
|
content_type = material.file.blob.content_type.to_s
|
|
return 'image' if content_type.start_with?('image/')
|
|
return 'video' if content_type.start_with?('video/')
|
|
return 'audio' if content_type.start_with?('audio/')
|
|
|
|
'file_other'
|
|
end
|
|
|
|
def compact_tag tag
|
|
return nil unless tag
|
|
|
|
{ id: tag.id,
|
|
name: tag.name,
|
|
category: tag.category,
|
|
deprecated_at: tag.deprecated_at }
|
|
end
|
|
end
|