Просмотр исходного кода

Maintenance: sha generated centralized.

Martin Edenhofer 2 лет назад
Родитель
Сommit
48925be7cc

+ 15 - 3
app/models/store/file.rb

@@ -18,7 +18,7 @@ do also verify of written data
 =end
 
     def self.add(data, verify = true)
-      sha = Digest::SHA256.hexdigest(data)
+      sha = checksum(data)
 
       file = Store::File.find_by(sha: sha)
       if file.nil?
@@ -39,7 +39,7 @@ do also verify of written data
         # verify
         if verify
           read_data = adapter.get(sha)
-          read_sha = Digest::SHA256.hexdigest(read_data)
+          read_sha = checksum(read_data)
           if sha != read_sha
             raise "Content not written correctly (provider #{adapter_name})."
           end
@@ -79,7 +79,7 @@ in case of fixing sha hash use:
     def self.verify(fix_it = nil)
       success = true
       Store::File.find_each(batch_size: 10) do |item|
-        sha = Digest::SHA256.hexdigest(item.content)
+        sha = checksum(item.content)
         logger.info "CHECK: Store::File.find(#{item.id})"
         next if sha == item.sha
 
@@ -127,6 +127,18 @@ nice move to keep system responsive
       true
     end
 
+=begin
+
+generate a checksum for the given content
+
+  Store::File.checksum(binary_data)
+
+=end
+
+    def self.checksum(content)
+      Digest::SHA256.hexdigest(content)
+    end
+
     private
 
     def destroy_provider

+ 1 - 1
app/models/store/provider/file.rb

@@ -28,7 +28,7 @@ class Store::Provider::File
 
     Rails.logger.debug { "read from fs #{location}" }
     content   = File.binread(location)
-    local_sha = Digest::SHA256.hexdigest(content)
+    local_sha = Store::File.checksum(content)
 
     # check sha
     raise "File corrupted: path #{location} does not match SHA digest (#{local_sha})" if local_sha != sha

+ 2 - 2
spec/models/store/file_spec.rb

@@ -17,7 +17,7 @@ RSpec.describe Store::File, type: :model do
     context 'with a preconfigured storage provider' do
       before { Setting.set('storage_provider', 'File') }
 
-      after { Store::Provider::File.delete(Digest::SHA256.hexdigest('foo')) }
+      after { Store::Provider::File.delete(described_class.checksum('foo')) }
 
       it 'defaults to the "DB" provider' do
         expect(file.provider).to eq('File')
@@ -57,7 +57,7 @@ RSpec.describe Store::File, type: :model do
   describe '.move' do
     before { Setting.set('storage_provider', nil) }
 
-    after { Store::Provider::File.delete(Digest::SHA256.hexdigest('foo')) }
+    after { Store::Provider::File.delete(described_class.checksum('foo')) }
 
     let(:storage_path) { Rails.root.join('storage/fs') }
 

+ 1 - 1
spec/models/store/provider/file_spec.rb

@@ -8,7 +8,7 @@ RSpec.describe Store::Provider::File do
   after { FileUtils.rm_rf(Rails.root.join('storage/fs', sha[0, 4])) }
 
   let(:data) { 'foo' }
-  let(:sha) { Digest::SHA256.hexdigest(data) }
+  let(:sha) { Store::File.checksum(data) }
   let(:filepath) { Rails.root.join('storage/fs/2c26/b46b/68ffc/68ff9/9b453c1/d304134/13422d706483bfa0f98a5e886266e7ae') }
 
   describe '.get_location' do