2014-05-03 12:34:36 +00:00
|
|
|
# Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
|
|
|
|
|
2015-04-27 21:44:41 +00:00
|
|
|
class Store
|
|
|
|
class File < ApplicationModel
|
|
|
|
include ApplicationLib
|
|
|
|
after_destroy :destroy_provider
|
2014-05-03 12:34:36 +00:00
|
|
|
|
2015-07-26 21:35:56 +00:00
|
|
|
=begin
|
2015-07-26 21:21:51 +00:00
|
|
|
|
|
|
|
add new file to store
|
|
|
|
|
|
|
|
store_file_id = Store::File.add(binary_data)
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
2015-04-27 21:44:41 +00:00
|
|
|
def self.add(data)
|
|
|
|
sha = Digest::SHA256.hexdigest( data )
|
2014-05-03 14:52:37 +00:00
|
|
|
|
2015-05-07 10:15:40 +00:00
|
|
|
file = Store::File.find_by( sha: sha )
|
2015-05-07 10:11:45 +00:00
|
|
|
if file.nil?
|
2014-05-03 12:34:36 +00:00
|
|
|
|
2015-04-27 21:44:41 +00:00
|
|
|
# load backend based on config
|
|
|
|
adapter_name = Setting.get('storage_provider') || 'DB'
|
|
|
|
if !adapter_name
|
2015-05-07 11:27:07 +00:00
|
|
|
fail 'Missing storage_provider setting option'
|
2015-04-27 21:44:41 +00:00
|
|
|
end
|
2015-07-03 15:18:01 +00:00
|
|
|
adapter = load_adapter( "Store::Provider::#{adapter_name}" )
|
2015-04-27 21:44:41 +00:00
|
|
|
adapter.add( data, sha )
|
|
|
|
file = Store::File.create(
|
|
|
|
provider: adapter_name,
|
|
|
|
sha: sha,
|
|
|
|
)
|
2014-05-03 12:34:36 +00:00
|
|
|
end
|
2015-04-27 21:44:41 +00:00
|
|
|
file
|
2014-05-03 12:34:36 +00:00
|
|
|
end
|
|
|
|
|
2015-07-26 21:21:51 +00:00
|
|
|
=begin
|
|
|
|
|
|
|
|
read content of a file
|
|
|
|
|
|
|
|
store = Store::File.find(123)
|
|
|
|
|
|
|
|
store.content # returns binary
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
2015-04-27 21:44:41 +00:00
|
|
|
def content
|
2015-07-03 15:18:01 +00:00
|
|
|
adapter = self.class.load_adapter("Store::Provider::#{provider}")
|
2015-05-07 12:10:38 +00:00
|
|
|
if sha
|
|
|
|
c = adapter.get( sha )
|
2015-04-27 21:44:41 +00:00
|
|
|
else
|
|
|
|
# fallback until migration is done
|
2015-05-07 12:10:38 +00:00
|
|
|
c = Store::Provider::DB.find_by( md5: md5 ).data
|
2015-04-27 21:44:41 +00:00
|
|
|
end
|
|
|
|
c
|
2014-05-03 19:02:38 +00:00
|
|
|
end
|
2014-05-03 12:34:36 +00:00
|
|
|
|
2015-07-26 21:21:51 +00:00
|
|
|
=begin
|
|
|
|
|
|
|
|
file system check of store, check data and sha (in case fix it)
|
|
|
|
|
|
|
|
Store::File.verify
|
|
|
|
|
|
|
|
read each file which should be in backend and verify agsinst sha hash
|
|
|
|
|
|
|
|
in case of fixing sha hash use:
|
|
|
|
|
|
|
|
Store::File.verify(true)
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
2015-04-27 21:44:41 +00:00
|
|
|
def self.verify(fix_it = nil)
|
|
|
|
success = true
|
|
|
|
Store::File.all.each {|item|
|
|
|
|
content = item.content
|
|
|
|
sha = Digest::SHA256.hexdigest( content )
|
2015-05-04 18:58:28 +00:00
|
|
|
logger.info "CHECK: Store::File.find(#{item.id}) "
|
2015-05-07 09:04:40 +00:00
|
|
|
|
|
|
|
next if sha == item.sha
|
|
|
|
|
|
|
|
success = false
|
|
|
|
logger.error "DIFF: sha diff of Store::File.find(#{item.id}) "
|
|
|
|
if fix_it
|
|
|
|
item.update_attribute( :sha, sha )
|
2014-05-03 12:34:36 +00:00
|
|
|
end
|
2015-04-27 21:44:41 +00:00
|
|
|
}
|
|
|
|
success
|
|
|
|
end
|
2014-05-03 12:34:36 +00:00
|
|
|
|
2015-07-26 21:21:51 +00:00
|
|
|
=begin
|
|
|
|
|
|
|
|
move file from one to other provider
|
|
|
|
|
|
|
|
move files from file backend to db
|
|
|
|
|
|
|
|
Store::File.move('File', 'DB')
|
|
|
|
|
|
|
|
move files from db backend to fs
|
|
|
|
|
|
|
|
Store::File.move('DB', 'File')
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
2015-04-27 21:44:41 +00:00
|
|
|
def self.move(source, target)
|
2015-07-03 15:18:01 +00:00
|
|
|
adapter_source = load_adapter("Store::Provider::#{source}")
|
|
|
|
adapter_target = load_adapter("Store::Provider::#{target}")
|
2014-05-03 12:34:36 +00:00
|
|
|
|
2015-04-27 21:44:41 +00:00
|
|
|
Store::File.all.each {|item|
|
|
|
|
next if item.provider == target
|
|
|
|
content = item.content
|
2014-05-03 12:34:36 +00:00
|
|
|
|
2015-04-27 21:44:41 +00:00
|
|
|
# add to new provider
|
|
|
|
adapter_target.add( content, item.sha )
|
2014-05-03 12:34:36 +00:00
|
|
|
|
2015-04-27 21:44:41 +00:00
|
|
|
# update meta data
|
|
|
|
item.update_attribute( :provider, target )
|
2014-05-03 12:34:36 +00:00
|
|
|
|
2015-04-27 21:44:41 +00:00
|
|
|
# remove from old provider
|
|
|
|
adapter_source.delete( item.sha )
|
2014-05-03 12:34:36 +00:00
|
|
|
|
2015-05-05 08:11:31 +00:00
|
|
|
logger.info "Moved file #{item.sha} from #{source} to #{target}"
|
2015-04-27 21:44:41 +00:00
|
|
|
}
|
|
|
|
true
|
|
|
|
end
|
2014-05-03 12:34:36 +00:00
|
|
|
|
2015-04-27 21:44:41 +00:00
|
|
|
private
|
2014-05-03 12:34:36 +00:00
|
|
|
|
2015-04-27 21:44:41 +00:00
|
|
|
def destroy_provider
|
2015-07-03 15:18:01 +00:00
|
|
|
adapter = self.class.load_adapter("Store::Provider::#{provider}")
|
2015-05-07 12:10:38 +00:00
|
|
|
adapter.delete( sha )
|
2015-04-27 21:44:41 +00:00
|
|
|
end
|
2014-05-03 12:34:36 +00:00
|
|
|
end
|
2015-04-27 14:15:29 +00:00
|
|
|
end
|