このコミットが含まれているのは:
2026-06-24 01:26:26 +09:00
コミット c2102c8f96
+45 -10
ファイルの表示
@@ -54,7 +54,10 @@ class MaterialsController < ApplicationController
block = MaterialImportBlockMatcher.match_for_sha256(file_sha256) block = MaterialImportBlockMatcher.match_for_sha256(file_sha256)
return render_material_import_block(block) if block return render_material_import_block(block) if block
uploaded_blob = build_uploaded_material_blob!(file, file_sha256)
material = nil material = nil
begin
Material.transaction do Material.transaction do
tag_name = TagName.find_undiscard_or_create_by!(name: tag_name_raw) tag_name = TagName.find_undiscard_or_create_by!(name: tag_name_raw)
tag = tag_name.tag tag = tag_name.tag
@@ -63,13 +66,16 @@ class MaterialsController < ApplicationController
material = Material.new(tag:, url:, material = Material.new(tag:, url:,
created_by_user: current_user, created_by_user: current_user,
updated_by_user: current_user) updated_by_user: current_user)
material.file.attach(file) if file material.file.attach(uploaded_blob) if uploaded_blob
apply_file_sha256!(material, file_sha256)
material.save! material.save!
upsert_export_paths!(material) upsert_export_paths!(material)
MaterialVersionRecorder.record!(material:, event_type: :create, MaterialVersionRecorder.record!(material:, event_type: :create,
created_by_user: current_user) created_by_user: current_user)
end end
rescue StandardError
uploaded_blob&.purge_later
raise
end
if material if material
render json: MaterialRepr.base(material, host: request.base_url), status: :created render json: MaterialRepr.base(material, host: request.base_url), status: :created
@@ -97,20 +103,31 @@ class MaterialsController < ApplicationController
block = MaterialImportBlockMatcher.match_for_sha256(file_sha256) block = MaterialImportBlockMatcher.match_for_sha256(file_sha256)
return render_material_import_block(block) if block return render_material_import_block(block) if block
uploaded_blob = build_uploaded_material_blob!(file, file_sha256)
begin
Material.transaction do Material.transaction do
MaterialVersionRecorder.ensure_snapshot!(material, created_by_user: current_user) MaterialVersionRecorder.ensure_snapshot!(material, created_by_user: current_user)
tag_name = TagName.find_undiscard_or_create_by!(name: tag_name_raw) tag_name = TagName.find_undiscard_or_create_by!(name: tag_name_raw)
tag = tag_name.tag tag = tag_name.tag
tag = Tag.create!(tag_name:, category: :material) unless tag tag = Tag.create!(tag_name:, category: :material) unless tag
material.update!(tag:, url:, updated_by_user: current_user) material.assign_attributes(tag:, url:, updated_by_user: current_user)
material.file.attach(file) if file if uploaded_blob
apply_file_sha256!(material, file_sha256) material.file.attach(uploaded_blob)
material.file.detach if params.key?(:url) && url.present? && file.blank? clear_file_suppression!(material)
elsif params.key?(:url) && url.present? && file.blank?
material.file.detach
end
material.save!
upsert_export_paths!(material) upsert_export_paths!(material)
MaterialVersionRecorder.record!(material:, event_type: :update, MaterialVersionRecorder.record!(material:, event_type: :update,
created_by_user: current_user) created_by_user: current_user)
end end
rescue StandardError
uploaded_blob&.purge_later
raise
end
render json: MaterialRepr.base(material, host: request.base_url) render json: MaterialRepr.base(material, host: request.base_url)
end end
@@ -220,15 +237,33 @@ class MaterialsController < ApplicationController
status: :unprocessable_entity status: :unprocessable_entity
end end
def apply_file_sha256! material, file_sha256 def build_uploaded_material_blob! file, file_sha256
return if file_sha256.blank? return nil unless file
return unless material.file.attached?
blob = material.file.blob 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.metadata['sha256'] = file_sha256
blob.save! if blob.changed? blob.save! if blob.changed?
end 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 def purge_material_file_snapshot material
return nil unless material.file.attached? return nil unless material.file.attached?