437 行
15 KiB
Ruby
437 行
15 KiB
Ruby
class MaterialsController < ApplicationController
|
|
rescue_from MaterialZipExporter::EmptyExportError, with: :render_zip_empty
|
|
rescue_from MaterialZipExporter::DuplicatePathError, with: :render_zip_duplicate_path
|
|
rescue_from MaterialZipExporter::MissingFileError, with: :render_zip_missing_file
|
|
|
|
def index
|
|
page = (params[:page].presence || 1).to_i
|
|
limit = (params[:limit].presence || 20).to_i
|
|
|
|
page = 1 if page < 1
|
|
limit = 1 if limit < 1
|
|
|
|
offset = (page - 1) * limit
|
|
|
|
filters = material_index_filters
|
|
q = Material.includes(:material_export_items,
|
|
thumbnail_attachment: :blob,
|
|
file_attachment: :blob,
|
|
tag: :tag_name)
|
|
q = q.where(file_suppressed_at: nil) if filters[:suppression] == 'active'
|
|
q = q.where.not(file_suppressed_at: nil) if filters[:suppression] == 'suppressed'
|
|
q = q.where(tag_id: nil) if filters[:tag_state] == 'untagged'
|
|
q = q.where.not(tag_id: nil) if filters[:tag_state] == 'tagged'
|
|
q = q.where('materials.created_at >= ?', filters[:created_from]) if filters[:created_from]
|
|
q = q.where('materials.created_at <= ?', filters[:created_to]) if filters[:created_to]
|
|
q = q.where('materials.updated_at >= ?', filters[:updated_from]) if filters[:updated_from]
|
|
q = q.where('materials.updated_at <= ?', filters[:updated_to]) if filters[:updated_to]
|
|
|
|
q = material_index_join_tag_name(q) if material_index_needs_tag_name?(filters)
|
|
q = material_index_join_file_blob(q) if material_index_needs_file_blob?(filters)
|
|
q = apply_material_query(q, filters[:q]) if filters[:q].present?
|
|
q = apply_material_media_kind(q, filters[:media_kind])
|
|
|
|
count = q.distinct.count(:id)
|
|
materials =
|
|
q
|
|
.order(Arel.sql(material_index_order_sql(filters)))
|
|
.limit(limit)
|
|
.offset(offset)
|
|
.to_a
|
|
|
|
render json: { materials: MaterialRepr.list_many(materials, host: request.base_url),
|
|
count: }
|
|
end
|
|
|
|
def show
|
|
material =
|
|
Material
|
|
.includes(:tag, :material_export_items)
|
|
.with_attached_thumbnail
|
|
.with_attached_file
|
|
.find_by(id: params[:id])
|
|
return head :not_found unless material
|
|
|
|
wiki_page_body = material.tag&.tag_name&.wiki_page&.current_revision&.body
|
|
|
|
render json: MaterialRepr.base(material, host: request.base_url).merge(wiki_page_body:)
|
|
end
|
|
|
|
def create
|
|
return head :unauthorized unless current_user
|
|
return head :forbidden unless current_user.gte_member?
|
|
|
|
tag_name_raw = params[:tag].to_s.strip
|
|
file = params[:file]
|
|
file_sha256 = MaterialFileSha256.from_upload(file)
|
|
url = params[:url].to_s.strip.presence
|
|
return render_unprocessable_entity('タグは必須です.', field: :tag) if tag_name_raw.blank?
|
|
if file.blank? && url.blank?
|
|
return render_validation_error fields: { file: ['ファイルまたは URL は必須です.'],
|
|
url: ['ファイルまたは URL は必須です.'] }
|
|
end
|
|
block = MaterialImportBlockMatcher.match_for_sha256(file_sha256)
|
|
return render_material_import_block(block) if block
|
|
|
|
uploaded_blob = build_uploaded_material_blob!(file, file_sha256)
|
|
material = nil
|
|
|
|
begin
|
|
Material.transaction do
|
|
tag_name = TagName.find_undiscard_or_create_by!(name: tag_name_raw)
|
|
tag = tag_name.tag
|
|
tag = Tag.create!(tag_name:, category: :material) unless tag
|
|
|
|
material = Material.new(tag:, url:,
|
|
created_by_user: current_user,
|
|
updated_by_user: current_user)
|
|
material.file.attach(uploaded_blob) if uploaded_blob
|
|
material.save!
|
|
upsert_export_paths!(material)
|
|
MaterialVersionRecorder.record!(material:, event_type: :create,
|
|
created_by_user: current_user)
|
|
end
|
|
rescue StandardError
|
|
uploaded_blob&.purge_later
|
|
raise
|
|
end
|
|
|
|
if material
|
|
MaterialThumbnailGenerator.generate!(material)
|
|
material.reload
|
|
render json: MaterialRepr.base(material, host: request.base_url), status: :created
|
|
else
|
|
render_validation_error material
|
|
end
|
|
end
|
|
|
|
def update
|
|
return head :unauthorized unless current_user
|
|
return head :forbidden unless current_user.gte_member?
|
|
|
|
material = Material.with_attached_file.find_by(id: params[:id])
|
|
return head :not_found unless material
|
|
|
|
tag_name_raw = params[:tag].to_s.strip
|
|
file = params[:file]
|
|
file_sha256 = MaterialFileSha256.from_upload(file)
|
|
url = params.key?(:url) ? params[:url].to_s.strip.presence : material.url
|
|
return render_unprocessable_entity('タグは必須です.', field: :tag) if tag_name_raw.blank?
|
|
if file.blank? && url.blank? && !material.file.attached?
|
|
return render_validation_error fields: { file: ['ファイルまたは URL は必須です.'],
|
|
url: ['ファイルまたは URL は必須です.'] }
|
|
end
|
|
block = MaterialImportBlockMatcher.match_for_sha256(file_sha256)
|
|
return render_material_import_block(block) if block
|
|
|
|
uploaded_blob = build_uploaded_material_blob!(file, file_sha256)
|
|
|
|
begin
|
|
Material.transaction do
|
|
MaterialVersionRecorder.ensure_snapshot!(material, created_by_user: current_user)
|
|
tag_name = TagName.find_undiscard_or_create_by!(name: tag_name_raw)
|
|
tag = tag_name.tag
|
|
tag = Tag.create!(tag_name:, category: :material) unless tag
|
|
|
|
material.assign_attributes(tag:, url:, updated_by_user: current_user)
|
|
if uploaded_blob
|
|
material.file.attach(uploaded_blob)
|
|
clear_file_suppression!(material)
|
|
elsif params.key?(:url) && url.present? && file.blank?
|
|
material.file.detach
|
|
end
|
|
material.save!
|
|
upsert_export_paths!(material)
|
|
MaterialVersionRecorder.record!(material:, event_type: :update,
|
|
created_by_user: current_user)
|
|
end
|
|
rescue StandardError
|
|
uploaded_blob&.purge_later
|
|
raise
|
|
end
|
|
|
|
MaterialThumbnailGenerator.generate!(material)
|
|
material.reload
|
|
|
|
render json: MaterialRepr.base(material, host: request.base_url)
|
|
end
|
|
|
|
def destroy
|
|
return head :unauthorized unless current_user
|
|
return head :forbidden unless current_user.gte_member?
|
|
|
|
material = Material.find_by(id: params[:id])
|
|
return head :not_found unless material
|
|
|
|
Material.transaction do
|
|
MaterialVersionRecorder.ensure_snapshot!(material, created_by_user: current_user)
|
|
material.discard!
|
|
MaterialVersionRecorder.record!(material:, event_type: :discard,
|
|
created_by_user: current_user)
|
|
end
|
|
head :no_content
|
|
end
|
|
|
|
def download
|
|
zip = MaterialZipExporter.new(profile: params[:profile],
|
|
tag_id: params[:tag_id]).export
|
|
profile = params[:profile].presence || 'legacy_drive'
|
|
send_data zip,
|
|
type: 'application/zip',
|
|
disposition: 'attachment',
|
|
filename: "btrc-materials-#{ profile }.zip"
|
|
end
|
|
|
|
def suppress_file
|
|
return head :unauthorized unless current_user
|
|
return head :forbidden unless current_user.admin?
|
|
|
|
material = Material.with_attached_file.find_by(id: params[:id])
|
|
return head :not_found unless material
|
|
|
|
reason = params[:reason].to_s.strip.presence
|
|
return render_unprocessable_entity('理由は必須です.', field: :reason) unless reason
|
|
purge = bool?(:purge)
|
|
file_snapshot = purge_material_file_snapshot(material) if purge
|
|
attachment = purge && material.file.attached? ? material.file.attachment : nil
|
|
|
|
Material.transaction do
|
|
MaterialVersionRecorder.ensure_snapshot!(material, created_by_user: current_user)
|
|
material.update!(file_suppressed_at: Time.current,
|
|
file_suppressed_by_user: current_user,
|
|
file_suppression_reason: reason,
|
|
updated_by_user: current_user)
|
|
MaterialVersionRecorder.record!(material:, event_type: :suppress,
|
|
created_by_user: current_user,
|
|
file_snapshot:)
|
|
end
|
|
# Enqueue failure raises here after the suppress metadata has been committed.
|
|
# In that case the file remains suppressed in UI/ZIP and purge can be retried.
|
|
attachment&.purge_later
|
|
material.reload if purge
|
|
|
|
render json: MaterialRepr.base(material, host: request.base_url)
|
|
end
|
|
|
|
private
|
|
|
|
def material_index_filters
|
|
tag_state = params[:tag_state].to_s.presence
|
|
tag_state = 'untagged' if bool?(:unclassified)
|
|
tag_state = 'all' unless ['all', 'tagged', 'untagged'].include?(tag_state)
|
|
|
|
media_kind = params[:media_kind].to_s.presence
|
|
unless ['all', 'image', 'video', 'audio', 'file_other', 'url_only'].include?(media_kind)
|
|
media_kind = 'all'
|
|
end
|
|
|
|
suppression = params[:suppression].to_s.presence
|
|
suppression = 'active' unless ['active', 'suppressed', 'all'].include?(suppression)
|
|
|
|
sort = params[:sort].to_s.presence
|
|
unless ['created_at', 'updated_at', 'tag_name', 'media_kind', 'file_byte_size',
|
|
'version_no', 'id'].include?(sort)
|
|
sort = 'created_at'
|
|
end
|
|
|
|
direction = params[:direction].to_s.downcase
|
|
direction = 'desc' unless ['asc', 'desc'].include?(direction)
|
|
|
|
{ q: params[:q].to_s.strip.presence,
|
|
tag_state:,
|
|
media_kind:,
|
|
suppression:,
|
|
created_from: parse_time_param(:created_from),
|
|
created_to: parse_time_param(:created_to),
|
|
updated_from: parse_time_param(:updated_from),
|
|
updated_to: parse_time_param(:updated_to),
|
|
sort:,
|
|
direction: }
|
|
end
|
|
|
|
def parse_time_param name
|
|
value = params[name].to_s.strip
|
|
return nil if value.blank?
|
|
|
|
Time.zone.parse(value)
|
|
rescue ArgumentError
|
|
nil
|
|
end
|
|
|
|
def material_index_needs_tag_name? filters
|
|
filters[:q].present? || filters[:sort] == 'tag_name'
|
|
end
|
|
|
|
def material_index_needs_file_blob? filters
|
|
filters[:q].present? ||
|
|
filters[:media_kind] != 'all' ||
|
|
['media_kind', 'file_byte_size'].include?(filters[:sort])
|
|
end
|
|
|
|
def material_index_join_tag_name q
|
|
q.left_joins(tag: :tag_name)
|
|
end
|
|
|
|
def material_index_join_file_blob q
|
|
q.joins(<<~SQL.squish)
|
|
LEFT JOIN active_storage_attachments material_file_attachments
|
|
ON material_file_attachments.record_type = 'Material'
|
|
AND material_file_attachments.record_id = materials.id
|
|
AND material_file_attachments.name = 'file'
|
|
LEFT JOIN active_storage_blobs material_file_blobs
|
|
ON material_file_blobs.id = material_file_attachments.blob_id
|
|
SQL
|
|
end
|
|
|
|
def apply_material_query q, term
|
|
like = "%#{ ActiveRecord::Base.sanitize_sql_like(term) }%"
|
|
q.where('tag_names.name LIKE :q OR materials.url LIKE :q OR ' \
|
|
'material_file_blobs.filename LIKE :q',
|
|
q: like)
|
|
end
|
|
|
|
def apply_material_media_kind q, media_kind
|
|
case media_kind
|
|
when 'image'
|
|
q.where('material_file_blobs.content_type LIKE ?', 'image/%')
|
|
when 'video'
|
|
q.where('material_file_blobs.content_type LIKE ?', 'video/%')
|
|
when 'audio'
|
|
q.where('material_file_blobs.content_type LIKE ?', 'audio/%')
|
|
when 'file_other'
|
|
q.where('material_file_attachments.id IS NOT NULL')
|
|
.where.not('material_file_blobs.content_type LIKE ?', 'image/%')
|
|
.where.not('material_file_blobs.content_type LIKE ?', 'video/%')
|
|
.where.not('material_file_blobs.content_type LIKE ?', 'audio/%')
|
|
when 'url_only'
|
|
q.where('material_file_attachments.id IS NULL').where.not(url: [nil, ''])
|
|
else
|
|
q
|
|
end
|
|
end
|
|
|
|
def material_index_order_sql filters
|
|
direction = filters[:direction] == 'asc' ? 'ASC' : 'DESC'
|
|
sort_sql =
|
|
case filters[:sort]
|
|
when 'tag_name'
|
|
'tag_names.name'
|
|
when 'media_kind'
|
|
material_media_kind_sql
|
|
when 'file_byte_size'
|
|
'material_file_blobs.byte_size'
|
|
when 'updated_at'
|
|
'materials.updated_at'
|
|
when 'version_no'
|
|
'materials.version_no'
|
|
when 'id'
|
|
'materials.id'
|
|
else
|
|
'materials.created_at'
|
|
end
|
|
|
|
"#{ sort_sql } #{ direction }, materials.id #{ direction }"
|
|
end
|
|
|
|
def material_media_kind_sql
|
|
"CASE " \
|
|
"WHEN material_file_attachments.id IS NULL AND materials.url IS NOT NULL THEN 5 " \
|
|
"WHEN material_file_blobs.content_type LIKE 'image/%' THEN 1 " \
|
|
"WHEN material_file_blobs.content_type LIKE 'video/%' THEN 2 " \
|
|
"WHEN material_file_blobs.content_type LIKE 'audio/%' THEN 3 " \
|
|
"WHEN material_file_attachments.id IS NOT NULL THEN 4 " \
|
|
"ELSE 6 END"
|
|
end
|
|
|
|
def upsert_export_paths! material
|
|
raw = params[:export_paths]
|
|
return if raw.blank?
|
|
|
|
export_paths = raw.respond_to?(:to_unsafe_h) ? raw.to_unsafe_h : raw.to_h
|
|
export_paths.each do |profile, export_path|
|
|
profile = profile.to_s
|
|
export_path = export_path.to_s.strip
|
|
item = material.material_export_items.find_or_initialize_by(profile:)
|
|
|
|
if export_path.blank?
|
|
item.destroy! if item.persisted?
|
|
next
|
|
end
|
|
|
|
item.export_path = export_path
|
|
item.enabled = true
|
|
item.created_by_user ||= current_user
|
|
item.save!
|
|
end
|
|
end
|
|
|
|
def render_zip_empty
|
|
render_unprocessable_entity('ZIP export 対象の素材がありません.')
|
|
end
|
|
|
|
def render_zip_duplicate_path error
|
|
render_unprocessable_entity("ZIP export path が重複してゐます: #{ error.message }")
|
|
end
|
|
|
|
def render_zip_missing_file error
|
|
missing_files = error.missing_files.map do |missing_file|
|
|
{ material_id: missing_file.material_id,
|
|
export_path: missing_file.export_path,
|
|
blob_id: missing_file.blob_id,
|
|
filename: missing_file.filename }
|
|
end
|
|
|
|
render json: { type: 'validation_error',
|
|
message: 'ZIP export に必要な素材ファイルが欠損しています.',
|
|
errors: { },
|
|
base_errors: ['ZIP export に必要な素材ファイルが欠損しています.'],
|
|
missing_files: },
|
|
status: :unprocessable_entity
|
|
end
|
|
|
|
def build_uploaded_material_blob! file, file_sha256
|
|
return nil unless file
|
|
|
|
file.tempfile.rewind
|
|
|
|
blob = ActiveStorage::Blob.create_and_upload!(
|
|
io: file.tempfile,
|
|
filename: file.original_filename,
|
|
content_type: file.content_type,
|
|
)
|
|
|
|
if file_sha256.present?
|
|
blob.metadata['sha256'] = file_sha256
|
|
blob.save! if blob.changed?
|
|
end
|
|
|
|
blob
|
|
ensure
|
|
file.tempfile.rewind if file&.tempfile
|
|
end
|
|
|
|
def clear_file_suppression! material
|
|
material.file_suppressed_at = nil
|
|
material.file_suppressed_by_user = nil
|
|
material.file_suppression_reason = nil
|
|
end
|
|
|
|
def purge_material_file_snapshot material
|
|
return nil unless material.file.attached?
|
|
|
|
blob = material.file.blob
|
|
|
|
{ file_blob_id: blob.id,
|
|
file_filename: blob.filename.to_s,
|
|
file_content_type: blob.content_type,
|
|
file_byte_size: blob.byte_size,
|
|
file_checksum: blob.checksum,
|
|
file_sha256: blob.metadata['sha256'] ||
|
|
MaterialFileSha256.from_blob(blob, allow_download: true) }
|
|
end
|
|
|
|
def render_material_import_block block
|
|
render_validation_error fields: { file: ["抑止された素材です: #{ block.reason }"] }
|
|
end
|
|
end
|