2014-05-03 12:34:36 +00:00
|
|
|
# Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
|
2015-04-27 22:21:30 +00:00
|
|
|
class Store::Provider::File
|
2015-04-27 21:44:41 +00:00
|
|
|
|
2015-05-06 12:56:12 +00:00
|
|
|
# write file to fs
|
2015-04-27 22:21:30 +00:00
|
|
|
def self.add(data, sha)
|
2015-04-27 21:44:41 +00:00
|
|
|
|
2015-05-06 12:56:12 +00:00
|
|
|
# install file
|
|
|
|
permission = '600'
|
2016-02-02 12:50:49 +00:00
|
|
|
if !File.exist?(get_locaton(sha))
|
2015-07-03 15:18:01 +00:00
|
|
|
Rails.logger.debug "storge write '#{get_locaton(sha)}' (#{permission})"
|
2016-02-02 12:50:49 +00:00
|
|
|
file = File.new(get_locaton(sha), 'wb')
|
|
|
|
file.write(data)
|
2015-05-06 12:56:12 +00:00
|
|
|
file.close
|
2015-04-27 22:21:30 +00:00
|
|
|
end
|
2016-02-02 12:50:49 +00:00
|
|
|
File.chmod(permission.to_i(8), get_locaton(sha))
|
2015-04-27 21:44:41 +00:00
|
|
|
|
2015-05-06 12:56:12 +00:00
|
|
|
# check sha
|
2016-02-02 12:50:49 +00:00
|
|
|
local_sha = Digest::SHA256.hexdigest(get(sha))
|
2015-05-06 12:56:12 +00:00
|
|
|
if sha != local_sha
|
2015-07-03 15:18:01 +00:00
|
|
|
fail "ERROR: Corrupt file in fs #{get_locaton(sha)}, sha should be #{sha} but is #{local_sha}"
|
2015-04-27 22:21:30 +00:00
|
|
|
end
|
2015-05-06 12:56:12 +00:00
|
|
|
|
|
|
|
true
|
2015-04-27 22:21:30 +00:00
|
|
|
end
|
2015-04-27 21:44:41 +00:00
|
|
|
|
2015-04-27 22:21:30 +00:00
|
|
|
# read file from fs
|
2015-05-06 12:56:12 +00:00
|
|
|
def self.get(sha)
|
2015-07-03 15:18:01 +00:00
|
|
|
Rails.logger.debug "read from fs #{get_locaton(sha)}"
|
2016-02-02 12:50:49 +00:00
|
|
|
if !File.exist?(get_locaton(sha))
|
2015-07-03 15:18:01 +00:00
|
|
|
fail "ERROR: No such file #{get_locaton(sha)}"
|
2015-04-27 22:21:30 +00:00
|
|
|
end
|
2016-02-02 12:50:49 +00:00
|
|
|
data = File.open(get_locaton(sha), 'rb')
|
2015-04-27 22:21:30 +00:00
|
|
|
content = data.read
|
2015-04-27 21:44:41 +00:00
|
|
|
|
2015-04-27 22:21:30 +00:00
|
|
|
# check sha
|
2016-02-02 12:50:49 +00:00
|
|
|
local_sha = Digest::SHA256.hexdigest(content)
|
2015-04-27 22:21:30 +00:00
|
|
|
if local_sha != sha
|
2015-07-03 15:18:01 +00:00
|
|
|
fail "ERROR: Corrupt file in fs #{get_locaton(sha)}, sha should be #{sha} but is #{local_sha}"
|
2015-04-27 22:21:30 +00:00
|
|
|
end
|
|
|
|
content
|
|
|
|
end
|
2015-04-27 21:44:41 +00:00
|
|
|
|
2015-05-06 12:56:12 +00:00
|
|
|
# unlink file from fs
|
|
|
|
def self.delete(sha)
|
|
|
|
if File.exist?( get_locaton(sha) )
|
2015-07-03 15:18:01 +00:00
|
|
|
Rails.logger.info "storge remove '#{get_locaton(sha)}'"
|
2015-05-06 12:56:12 +00:00
|
|
|
File.delete( get_locaton(sha) )
|
2015-04-27 22:21:30 +00:00
|
|
|
end
|
2015-05-06 12:56:12 +00:00
|
|
|
end
|
2015-04-27 21:44:41 +00:00
|
|
|
|
2015-05-06 12:56:12 +00:00
|
|
|
# generate file location
|
|
|
|
def self.get_locaton(sha)
|
2015-04-27 22:21:30 +00:00
|
|
|
|
2015-05-06 12:56:12 +00:00
|
|
|
# generate directory
|
2015-05-06 13:06:50 +00:00
|
|
|
base = "#{Rails.root}/storage/fs/"
|
2016-02-29 07:25:52 +00:00
|
|
|
parts = sha.scan(/.{1,6}/)
|
|
|
|
path = parts[ 1..6 ].join('/') + '/'
|
|
|
|
file = parts[ 7..parts.count ].join('')
|
2015-05-06 12:56:12 +00:00
|
|
|
location = "#{base}/#{path}"
|
|
|
|
|
|
|
|
# create directory if not exists
|
2016-02-02 12:50:49 +00:00
|
|
|
if !File.exist?(location)
|
|
|
|
FileUtils.mkdir_p(location)
|
2015-05-06 12:56:12 +00:00
|
|
|
end
|
|
|
|
location += file
|
2014-05-03 12:34:36 +00:00
|
|
|
end
|
2015-05-06 12:56:12 +00:00
|
|
|
|
2015-04-27 14:15:29 +00:00
|
|
|
end
|