|
@@ -2,29 +2,29 @@
|
|
|
|
|
|
class Store::Provider::File
|
|
|
|
|
|
- def self.add(data, md5)
|
|
|
- write_to_fs(data, md5)
|
|
|
+ def self.add(data, sha)
|
|
|
+ write_to_fs(data, sha)
|
|
|
true
|
|
|
end
|
|
|
|
|
|
- def self.get(md5)
|
|
|
- read_from_fs(md5)
|
|
|
+ def self.get(sha)
|
|
|
+ read_from_fs(sha)
|
|
|
end
|
|
|
|
|
|
- def self.delete(md5)
|
|
|
- unlink_from_fs(md5)
|
|
|
+ def self.delete(sha)
|
|
|
+ unlink_from_fs(sha)
|
|
|
end
|
|
|
|
|
|
private
|
|
|
|
|
|
# generate file location
|
|
|
- def self.get_locaton(md5)
|
|
|
+ def self.get_locaton(sha)
|
|
|
|
|
|
# generate directory
|
|
|
base = Rails.root.to_s + '/storage/fs/'
|
|
|
- parts = md5.scan(/.{1,3}/)
|
|
|
- path = parts[ 1 .. 7 ].join('/') + '/'
|
|
|
- file = parts[ 8 .. parts.count ].join('')
|
|
|
+ parts = sha.scan(/.{1,3}/)
|
|
|
+ path = parts[ 1 .. 10 ].join('/') + '/'
|
|
|
+ file = parts[ 11 .. parts.count ].join('')
|
|
|
location = "#{base}/#{path}"
|
|
|
|
|
|
# create directory if not exists
|
|
@@ -35,47 +35,47 @@ class Store::Provider::File
|
|
|
end
|
|
|
|
|
|
# unlink file from fs
|
|
|
- def self.unlink_from_fs(md5)
|
|
|
- if File.exist?( get_locaton(md5) )
|
|
|
- puts "NOTICE: storge remove '#{ get_locaton(md5) }'"
|
|
|
- File.delete( get_locaton(md5) )
|
|
|
+ def self.unlink_from_fs(sha)
|
|
|
+ if File.exist?( get_locaton(sha) )
|
|
|
+ puts "NOTICE: storge remove '#{ get_locaton(sha) }'"
|
|
|
+ File.delete( get_locaton(sha) )
|
|
|
end
|
|
|
end
|
|
|
|
|
|
# read file from fs
|
|
|
- def self.read_from_fs(md5)
|
|
|
- puts "read from fs #{ get_locaton(md5) }"
|
|
|
- if !File.exist?( get_locaton(md5) )
|
|
|
- raise "ERROR: No such file #{ get_locaton(md5) }"
|
|
|
+ def self.read_from_fs(sha)
|
|
|
+ puts "read from fs #{ get_locaton(sha) }"
|
|
|
+ if !File.exist?( get_locaton(sha) )
|
|
|
+ raise "ERROR: No such file #{ get_locaton(sha) }"
|
|
|
end
|
|
|
- data = File.open( get_locaton(md5), 'rb' )
|
|
|
+ data = File.open( get_locaton(sha), 'rb' )
|
|
|
content = data.read
|
|
|
|
|
|
- # check md5
|
|
|
- local_md5 = Digest::MD5.hexdigest( content )
|
|
|
- if local_md5 != md5
|
|
|
- raise "ERROR: Corrupt file in fs #{ get_locaton(md5) }, md5 should be #{md5} but is #{local_md5}"
|
|
|
+ # check sha
|
|
|
+ local_sha = Digest::SHA256.hexdigest( content )
|
|
|
+ if local_sha != sha
|
|
|
+ raise "ERROR: Corrupt file in fs #{ get_locaton(sha) }, sha should be #{sha} but is #{local_sha}"
|
|
|
end
|
|
|
content
|
|
|
end
|
|
|
|
|
|
# write file to fs
|
|
|
- def self.write_to_fs(data,md5)
|
|
|
+ def self.write_to_fs(data,sha)
|
|
|
|
|
|
# install file
|
|
|
permission = '600'
|
|
|
- if !File.exist?( get_locaton(md5) )
|
|
|
- puts "NOTICE: storge write '#{ get_locaton(md5) }' (#{permission})"
|
|
|
- file = File.new( get_locaton(md5), 'wb' )
|
|
|
+ if !File.exist?( get_locaton(sha) )
|
|
|
+ puts "NOTICE: storge write '#{ get_locaton(sha) }' (#{permission})"
|
|
|
+ file = File.new( get_locaton(sha), 'wb' )
|
|
|
file.write( data )
|
|
|
file.close
|
|
|
end
|
|
|
- File.chmod( permission.to_i(8), get_locaton(md5) )
|
|
|
+ File.chmod( permission.to_i(8), get_locaton(sha) )
|
|
|
|
|
|
- # check md5
|
|
|
- local_md5 = Digest::MD5.hexdigest( read_from_fs(md5) )
|
|
|
- if md5 != local_md5
|
|
|
- raise "ERROR: Corrupt file in fs #{ get_locaton(md5) }, md5 should be #{md5} but is #{local_md5}"
|
|
|
+ # check sha
|
|
|
+ local_sha = Digest::SHA256.hexdigest( read_from_fs(sha) )
|
|
|
+ if sha != local_sha
|
|
|
+ raise "ERROR: Corrupt file in fs #{ get_locaton(sha) }, sha should be #{sha} but is #{local_sha}"
|
|
|
end
|
|
|
|
|
|
true
|