2014-05-03 12:34:36 +00:00
|
|
|
# Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
|
|
|
|
|
|
|
|
class Store::Provider::File
|
|
|
|
|
2014-05-03 14:52:37 +00:00
|
|
|
def self.add(data, sha)
|
|
|
|
write_to_fs(data, sha)
|
2014-05-03 12:34:36 +00:00
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2014-05-03 14:52:37 +00:00
|
|
|
def self.get(sha)
|
|
|
|
read_from_fs(sha)
|
2014-05-03 12:34:36 +00:00
|
|
|
end
|
|
|
|
|
2014-05-03 14:52:37 +00:00
|
|
|
def self.delete(sha)
|
|
|
|
unlink_from_fs(sha)
|
2014-05-03 12:34:36 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
# generate file location
|
2014-05-03 14:52:37 +00:00
|
|
|
def self.get_locaton(sha)
|
2014-05-03 12:34:36 +00:00
|
|
|
|
|
|
|
# generate directory
|
|
|
|
base = Rails.root.to_s + '/storage/fs/'
|
2014-05-03 14:52:37 +00:00
|
|
|
parts = sha.scan(/.{1,3}/)
|
|
|
|
path = parts[ 1 .. 10 ].join('/') + '/'
|
|
|
|
file = parts[ 11 .. parts.count ].join('')
|
2014-05-03 12:34:36 +00:00
|
|
|
location = "#{base}/#{path}"
|
|
|
|
|
|
|
|
# create directory if not exists
|
|
|
|
if !File.exist?( location )
|
|
|
|
FileUtils.mkdir_p( location )
|
|
|
|
end
|
|
|
|
location += file
|
|
|
|
end
|
|
|
|
|
|
|
|
# unlink file from fs
|
2014-05-03 14:52:37 +00:00
|
|
|
def self.unlink_from_fs(sha)
|
|
|
|
if File.exist?( get_locaton(sha) )
|
|
|
|
puts "NOTICE: storge remove '#{ get_locaton(sha) }'"
|
|
|
|
File.delete( get_locaton(sha) )
|
2014-05-03 12:34:36 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# read file from fs
|
2014-05-03 14:52:37 +00:00
|
|
|
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) }"
|
2014-05-03 12:34:36 +00:00
|
|
|
end
|
2014-05-03 14:52:37 +00:00
|
|
|
data = File.open( get_locaton(sha), 'rb' )
|
2014-05-03 12:34:36 +00:00
|
|
|
content = data.read
|
|
|
|
|
2014-05-03 14:52:37 +00:00
|
|
|
# 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}"
|
2014-05-03 12:34:36 +00:00
|
|
|
end
|
|
|
|
content
|
|
|
|
end
|
|
|
|
|
|
|
|
# write file to fs
|
2014-05-03 14:52:37 +00:00
|
|
|
def self.write_to_fs(data,sha)
|
2014-05-03 12:34:36 +00:00
|
|
|
|
|
|
|
# install file
|
|
|
|
permission = '600'
|
2014-05-03 14:52:37 +00:00
|
|
|
if !File.exist?( get_locaton(sha) )
|
|
|
|
puts "NOTICE: storge write '#{ get_locaton(sha) }' (#{permission})"
|
|
|
|
file = File.new( get_locaton(sha), 'wb' )
|
2014-05-03 12:34:36 +00:00
|
|
|
file.write( data )
|
|
|
|
file.close
|
|
|
|
end
|
2014-05-03 14:52:37 +00:00
|
|
|
File.chmod( permission.to_i(8), get_locaton(sha) )
|
2014-05-03 12:34:36 +00:00
|
|
|
|
2014-05-03 14:52:37 +00:00
|
|
|
# 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}"
|
2014-05-03 12:34:36 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|