Move checksum calculation to Upload.hexdigest

parent 39374921
...@@ -27,6 +27,16 @@ class Upload < ActiveRecord::Base ...@@ -27,6 +27,16 @@ class Upload < ActiveRecord::Base
) )
end end
def self.hexdigest(absolute_path)
return unless exist?(absolute_path)
Digest::SHA256.file(absolute_path).hexdigest
end
def self.exist?(absolute_path)
File.exist?(absolute_path)
end
def absolute_path def absolute_path
return path unless relative_path? return path unless relative_path?
...@@ -36,11 +46,11 @@ class Upload < ActiveRecord::Base ...@@ -36,11 +46,11 @@ class Upload < ActiveRecord::Base
def calculate_checksum def calculate_checksum
return unless exist? return unless exist?
self.checksum = Digest::SHA256.file(absolute_path).hexdigest self.checksum = self.class.hexdigest(absolute_path)
end end
def exist? def exist?
File.exist?(absolute_path) self.class.exist?(absolute_path)
end end
private private
......
...@@ -30,11 +30,7 @@ module Gitlab ...@@ -30,11 +30,7 @@ module Gitlab
end end
def matches_checksum?(recorded_file) def matches_checksum?(recorded_file)
message[:checksum] == calculate_checksum(recorded_file.absolute_path) message[:checksum] == Upload.hexdigest(recorded_file.absolute_path)
end
def calculate_checksum(absolute_path)
Digest::SHA256.file(absolute_path).hexdigest
end end
def success(file) def success(file)
......
...@@ -90,6 +90,28 @@ describe Upload, type: :model do ...@@ -90,6 +90,28 @@ describe Upload, type: :model do
end end
end end
describe '.hexdigest' do
it 'calculates the SHA256 sum' do
expected = Digest::SHA256.file(__FILE__).hexdigest
expect(described_class.hexdigest(__FILE__)).to eq expected
end
it 'returns nil for a non-existant file' do
expect(described_class.hexdigest("#{__FILE__}-nope")).to be_nil
end
end
describe '.exist?' do
it 'returns true when the file exists' do
expect(described_class.exist?(__FILE__)).to eq true
end
it 'returns false when the file does not exist' do
expect(described_class.exist?("#{__FILE__}-nope")).to eq false
end
end
describe '#absolute_path' do describe '#absolute_path' do
it 'returns the path directly when already absolute' do it 'returns the path directly when already absolute' do
path = '/path/to/namespace/project/secret/file.jpg' path = '/path/to/namespace/project/secret/file.jpg'
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment