trabajo-afectivo/app/models/store/provider/file.rb

69 lines
1.8 KiB
Ruby
Raw Normal View History

# Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
class Store::Provider::File
2015-04-27 21:44:41 +00:00
# write file to fs
def self.add(data, sha)
2015-04-27 21:44:41 +00:00
# install file
permission = '600'
if !File.exist?( get_locaton(sha) )
Rails.logger.debug "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(sha) )
2015-04-27 21:44:41 +00:00
# check sha
local_sha = Digest::SHA256.hexdigest( get(sha) )
if sha != local_sha
fail "ERROR: Corrupt file in fs #{get_locaton(sha)}, sha should be #{sha} but is #{local_sha}"
end
true
end
2015-04-27 21:44:41 +00:00
# read file from fs
def self.get(sha)
Rails.logger.debug "read from fs #{get_locaton(sha)}"
if !File.exist?( get_locaton(sha) )
fail "ERROR: No such file #{get_locaton(sha)}"
end
2015-05-04 18:58:28 +00:00
data = File.open( get_locaton(sha), 'rb' )
content = data.read
2015-04-27 21:44:41 +00:00
# check sha
local_sha = Digest::SHA256.hexdigest( content )
if local_sha != sha
fail "ERROR: Corrupt file in fs #{get_locaton(sha)}, sha should be #{sha} but is #{local_sha}"
end
content
end
2015-04-27 21:44:41 +00:00
# unlink file from fs
def self.delete(sha)
if File.exist?( get_locaton(sha) )
Rails.logger.info "storge remove '#{get_locaton(sha)}'"
File.delete( get_locaton(sha) )
end
end
2015-04-27 21:44:41 +00:00
# generate file location
def self.get_locaton(sha)
# generate directory
2015-05-06 13:06:50 +00:00
base = "#{Rails.root}/storage/fs/"
parts = sha.scan(/.{1,4}/)
path = parts[ 1..10 ].join('/') + '/'
file = parts[ 11..parts.count ].join('')
location = "#{base}/#{path}"
# create directory if not exists
if !File.exist?( location )
FileUtils.mkdir_p( location )
end
location += file
end
end