2014-05-03 12:34:36 +00:00
|
|
|
# Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
|
|
|
|
|
|
|
|
class Store::File < ApplicationModel
|
|
|
|
include ApplicationLib
|
|
|
|
after_destroy :destroy_provider
|
|
|
|
|
|
|
|
# add new file
|
|
|
|
def self.add(data)
|
2014-05-03 14:52:37 +00:00
|
|
|
sha = Digest::SHA256.hexdigest( data )
|
|
|
|
|
2015-04-27 13:42:53 +00:00
|
|
|
file = Store::File.where( sha: sha ).first
|
2014-05-03 12:34:36 +00:00
|
|
|
if file == nil
|
|
|
|
|
|
|
|
# load backend based on config
|
|
|
|
adapter_name = Setting.get('storage_provider') || 'DB'
|
|
|
|
if !adapter_name
|
2015-04-27 13:20:16 +00:00
|
|
|
raise 'Missing storage_provider setting option'
|
2014-05-03 12:34:36 +00:00
|
|
|
end
|
|
|
|
adapter = self.load_adapter( "Store::Provider::#{ adapter_name }" )
|
2014-05-03 14:52:37 +00:00
|
|
|
adapter.add( data, sha )
|
2014-05-03 12:34:36 +00:00
|
|
|
file = Store::File.create(
|
2015-04-27 13:42:53 +00:00
|
|
|
provider: adapter_name,
|
|
|
|
sha: sha,
|
2014-05-03 12:34:36 +00:00
|
|
|
)
|
|
|
|
end
|
|
|
|
file
|
|
|
|
end
|
|
|
|
|
|
|
|
# read content
|
|
|
|
def content
|
|
|
|
adapter = self.class.load_adapter("Store::Provider::#{ self.provider }")
|
2014-05-03 19:02:38 +00:00
|
|
|
if self.sha
|
|
|
|
c = adapter.get( self.sha )
|
|
|
|
else
|
|
|
|
# fallback until migration is done
|
2015-04-27 13:42:53 +00:00
|
|
|
c = Store::Provider::DB.where( md5: self.md5 ).first.data
|
2014-05-03 19:02:38 +00:00
|
|
|
end
|
|
|
|
c
|
2014-05-03 12:34:36 +00:00
|
|
|
end
|
|
|
|
|
2014-05-03 14:52:37 +00:00
|
|
|
# check data and sha, in case fix it
|
|
|
|
def self.verify(fix_it = nil)
|
2014-05-03 12:34:36 +00:00
|
|
|
success = true
|
|
|
|
Store::File.all.each {|item|
|
|
|
|
content = item.content
|
2014-05-03 14:52:37 +00:00
|
|
|
sha = Digest::SHA256.hexdigest( content )
|
2014-05-03 12:34:36 +00:00
|
|
|
puts "CHECK: Store::File.find(#{item.id}) "
|
2014-05-03 14:52:37 +00:00
|
|
|
if sha != item.sha
|
2014-05-03 12:34:36 +00:00
|
|
|
success = false
|
2014-05-03 14:52:37 +00:00
|
|
|
puts "DIFF: sha diff of Store::File.find(#{item.id}) "
|
2014-05-03 12:34:36 +00:00
|
|
|
if fix_it
|
2014-05-03 14:52:37 +00:00
|
|
|
item.update_attribute( :sha, sha )
|
2014-05-03 12:34:36 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
}
|
|
|
|
success
|
|
|
|
end
|
|
|
|
|
|
|
|
# move file from one to other provider
|
2014-08-24 10:04:32 +00:00
|
|
|
# e. g. Store::File.move('File', 'DB')
|
|
|
|
# e. g. Store::File.move('DB', 'File')
|
2014-05-03 12:34:36 +00:00
|
|
|
def self.move(source, target)
|
|
|
|
adapter_source = load_adapter("Store::Provider::#{ source }")
|
|
|
|
adapter_target = load_adapter("Store::Provider::#{ target }")
|
|
|
|
|
|
|
|
Store::File.all.each {|item|
|
|
|
|
next if item.provider == target
|
|
|
|
content = item.content
|
|
|
|
|
|
|
|
# add to new provider
|
2014-05-03 14:52:37 +00:00
|
|
|
adapter_target.add( content, item.sha )
|
2014-05-03 12:34:36 +00:00
|
|
|
|
|
|
|
# update meta data
|
|
|
|
item.update_attribute( :provider, target )
|
|
|
|
|
|
|
|
# remove from old provider
|
2014-05-03 14:52:37 +00:00
|
|
|
adapter_source.delete( item.sha )
|
2014-05-03 12:34:36 +00:00
|
|
|
|
2014-05-03 14:52:37 +00:00
|
|
|
puts "NOTICE: Moved file #{item.sha} from #{source} to #{target}"
|
2014-05-03 12:34:36 +00:00
|
|
|
}
|
2014-05-03 14:52:37 +00:00
|
|
|
true
|
2014-05-03 12:34:36 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def destroy_provider
|
|
|
|
adapter = self.class.load_adapter("Store::Provider::#{ self.provider }")
|
2014-05-03 14:52:37 +00:00
|
|
|
adapter.delete( self.sha )
|
2014-05-03 12:34:36 +00:00
|
|
|
end
|
2015-04-27 14:15:29 +00:00
|
|
|
end
|