このコミットが含まれているのは:
2026-06-27 05:30:23 +09:00
コミット c836369dfc
5個のファイルの変更34行の追加12行の削除
+1 -4
ファイルの表示
@@ -523,10 +523,7 @@ class MaterialsController < ApplicationController
content_type: file.content_type, content_type: file.content_type,
) )
if file_sha256.present? MaterialFileSha256.assign_metadata_sha256!(blob, file_sha256)
blob.metadata['sha256'] = file_sha256
blob.save! if blob.changed?
end
blob blob
ensure ensure
+4 -1
ファイルの表示
@@ -1,6 +1,9 @@
class Material < ApplicationRecord class Material < ApplicationRecord
include MyDiscard include MyDiscard
SOURCE_KINDS = ['uri', 'google_drive_path', 'google_drive_file',
'legacy_drive_path'].freeze
default_scope -> { kept } default_scope -> { kept }
belongs_to :parent, class_name: 'Material', optional: true belongs_to :parent, class_name: 'Material', optional: true
@@ -20,7 +23,7 @@ class Material < ApplicationRecord
validates :tag_id, uniqueness: true, allow_nil: true validates :tag_id, uniqueness: true, allow_nil: true
validates :source_kind, validates :source_kind,
inclusion: { in: MaterialSyncSuppression::SOURCE_KINDS }, inclusion: { in: SOURCE_KINDS },
allow_blank: true allow_blank: true
validate :file_must_be_attached validate :file_must_be_attached
+26 -3
ファイルの表示
@@ -1,16 +1,39 @@
require 'digest' require 'digest'
require 'json'
class MaterialFileSha256 class MaterialFileSha256
def self.blob_metadata blob
metadata = blob.metadata
return metadata if metadata.is_a?(Hash)
return { } if metadata.blank?
JSON.parse(metadata)
rescue JSON::ParserError
{ }
end
def self.metadata_sha256 blob
blob_metadata(blob)['sha256'].presence
end
def self.assign_metadata_sha256! blob, sha256
return if sha256.blank?
metadata = blob_metadata(blob)
metadata['sha256'] = sha256
blob.metadata = metadata
blob.save! if blob.changed?
end
def self.from_blob blob, allow_download: false def self.from_blob blob, allow_download: false
sha256 = blob.metadata['sha256'] sha256 = metadata_sha256(blob)
return sha256 if sha256.present? return sha256 if sha256.present?
return nil unless allow_download return nil unless allow_download
begin begin
blob.open do |file| blob.open do |file|
sha256 = Digest::SHA256.file(file.path).hexdigest sha256 = Digest::SHA256.file(file.path).hexdigest
blob.metadata['sha256'] = sha256 assign_metadata_sha256!(blob, sha256)
blob.save! if blob.changed?
sha256 sha256
end end
rescue ActiveStorage::FileNotFoundError, ArgumentError => error rescue ActiveStorage::FileNotFoundError, ArgumentError => error
+2 -3
ファイルの表示
@@ -148,8 +148,7 @@ class MaterialSyncImporter
io: tempfile, io: tempfile,
filename: @attributes[:filename], filename: @attributes[:filename],
content_type: @attributes[:content_type]) content_type: @attributes[:content_type])
blob.metadata['sha256'] = file_sha256 if file_sha256.present? MaterialFileSha256.assign_metadata_sha256!(blob, file_sha256)
blob.save! if blob.changed?
tempfile.rewind tempfile.rewind
blob blob
end end
@@ -226,7 +225,7 @@ class MaterialSyncImporter
if expected_sha256.present? if expected_sha256.present?
return false unless material.file.attached? return false unless material.file.attached?
return material.file.blob.metadata['sha256'] == expected_sha256 return MaterialFileSha256.metadata_sha256(material.file.blob) == expected_sha256
end end
!material.file.attached? !material.file.attached?
+1 -1
ファイルの表示
@@ -57,7 +57,7 @@ class MaterialVersionRecorder < VersionRecorder
file_content_type: blob.content_type, file_content_type: blob.content_type,
file_byte_size: blob.byte_size, file_byte_size: blob.byte_size,
file_checksum: blob.checksum, file_checksum: blob.checksum,
file_sha256: blob.metadata['sha256'] } file_sha256: MaterialFileSha256.metadata_sha256(blob) }
end end
def empty_file_snapshot def empty_file_snapshot