2014-02-03 19:23:00 +00:00
|
|
|
# Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
|
2013-06-12 15:59:58 +00:00
|
|
|
|
2013-01-24 08:18:29 +00:00
|
|
|
class Store < ApplicationModel
|
2014-10-19 16:56:53 +00:00
|
|
|
load 'store/object.rb'
|
|
|
|
load 'store/file.rb'
|
2014-05-03 12:34:36 +00:00
|
|
|
|
2012-10-14 21:00:33 +00:00
|
|
|
store :preferences
|
2015-04-27 13:42:53 +00:00
|
|
|
belongs_to :store_object, class_name: 'Store::Object'
|
|
|
|
belongs_to :store_file, class_name: 'Store::File'
|
|
|
|
validates :filename, presence: true
|
2012-10-14 21:00:33 +00:00
|
|
|
|
2014-04-28 07:44:36 +00:00
|
|
|
=begin
|
|
|
|
|
|
|
|
add an attachment to storage
|
|
|
|
|
|
|
|
result = Store.add(
|
|
|
|
:object => 'Ticket::Article',
|
|
|
|
:o_id => 4711,
|
|
|
|
:data => binary_string,
|
|
|
|
:preferences => {
|
|
|
|
:content_type => 'image/png',
|
|
|
|
:content_id => 234,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
result = true
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
2012-04-10 14:06:46 +00:00
|
|
|
def self.add(data)
|
|
|
|
data = data.stringify_keys
|
|
|
|
|
|
|
|
# lookup store_object.id
|
2015-04-27 13:42:53 +00:00
|
|
|
store_object = Store::Object.create_if_not_exists( name: data['object'] )
|
2012-04-10 14:06:46 +00:00
|
|
|
data['store_object_id'] = store_object.id
|
2012-12-02 10:18:55 +00:00
|
|
|
|
2014-05-03 12:34:36 +00:00
|
|
|
# add to real store
|
|
|
|
file = Store::File.add( data['data'] )
|
2012-12-02 10:18:55 +00:00
|
|
|
|
2013-01-24 08:18:29 +00:00
|
|
|
data['size'] = data['data'].to_s.bytesize
|
2012-04-10 14:06:46 +00:00
|
|
|
data['store_file_id'] = file.id
|
|
|
|
|
|
|
|
# not needed attributes
|
|
|
|
data.delete('data')
|
|
|
|
data.delete('object')
|
|
|
|
|
|
|
|
# store meta data
|
|
|
|
store = Store.create(data)
|
|
|
|
|
2014-10-06 20:24:21 +00:00
|
|
|
store
|
2012-04-10 14:06:46 +00:00
|
|
|
end
|
2012-12-02 10:18:55 +00:00
|
|
|
|
2014-04-28 07:44:36 +00:00
|
|
|
=begin
|
|
|
|
|
|
|
|
get attachment of object
|
|
|
|
|
|
|
|
list = Store.list(
|
2015-04-27 13:43:34 +00:00
|
|
|
:object => 'Ticket::Article',
|
|
|
|
:o_id => 4711,
|
2014-04-28 07:44:36 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
result = [store1, store2]
|
|
|
|
|
|
|
|
store1 = {
|
2015-04-27 13:43:34 +00:00
|
|
|
:size => 94123,
|
|
|
|
:filename => 'image.png',
|
|
|
|
:preferences => {
|
2014-04-28 07:44:36 +00:00
|
|
|
:content_type => 'image/png',
|
|
|
|
:content_id => 234,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
store1.content # binary_string
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
2012-04-10 14:06:46 +00:00
|
|
|
def self.list(data)
|
|
|
|
# search
|
2015-04-27 13:42:53 +00:00
|
|
|
store_object_id = Store::Object.lookup( name: data[:object] )
|
2015-04-30 17:51:31 +00:00
|
|
|
stores = Store.where( store_object_id: store_object_id, o_id: data[:o_id].to_i )
|
2015-05-01 12:27:57 +00:00
|
|
|
.order('created_at ASC, id ASC')
|
2015-04-30 17:20:27 +00:00
|
|
|
stores
|
2012-04-10 14:06:46 +00:00
|
|
|
end
|
|
|
|
|
2014-04-28 07:44:36 +00:00
|
|
|
=begin
|
|
|
|
|
2014-10-06 20:24:21 +00:00
|
|
|
remove attachments of object from storage
|
2014-04-28 07:44:36 +00:00
|
|
|
|
|
|
|
result = Store.remove(
|
2015-04-27 13:43:34 +00:00
|
|
|
:object => 'Ticket::Article',
|
|
|
|
:o_id => 4711,
|
2014-04-28 07:44:36 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
result = true
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
2012-04-10 14:06:46 +00:00
|
|
|
def self.remove(data)
|
|
|
|
# search
|
2015-04-27 13:42:53 +00:00
|
|
|
store_object_id = Store::Object.lookup( name: data[:object] )
|
2015-04-30 17:51:31 +00:00
|
|
|
stores = Store.where( store_object_id: store_object_id )
|
2015-05-01 12:27:57 +00:00
|
|
|
.where( o_id: data[:o_id] )
|
|
|
|
.order('created_at ASC, id ASC')
|
2012-04-10 14:06:46 +00:00
|
|
|
stores.each do |store|
|
2014-04-28 07:44:36 +00:00
|
|
|
|
|
|
|
# check backend for references
|
2014-10-06 20:24:21 +00:00
|
|
|
Store.remove_item( store.id )
|
|
|
|
end
|
2015-04-30 17:20:27 +00:00
|
|
|
true
|
2014-10-06 20:24:21 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
remove one attachment from storage
|
2014-04-28 07:44:36 +00:00
|
|
|
|
2014-10-06 20:24:21 +00:00
|
|
|
result = Store.remove_item(store_id)
|
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
result = true
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def self.remove_item(store_id)
|
|
|
|
|
|
|
|
# check backend for references
|
|
|
|
store = Store.find(store_id)
|
2015-04-27 13:42:53 +00:00
|
|
|
files = Store.where( store_file_id: store.store_file_id )
|
2014-10-06 20:24:21 +00:00
|
|
|
if files.count == 1 && files.first.id == store.id
|
|
|
|
Store::File.find( store.store_file_id ).destroy
|
2012-04-10 14:06:46 +00:00
|
|
|
end
|
2014-10-06 20:24:21 +00:00
|
|
|
|
|
|
|
store.destroy
|
2015-04-30 17:20:27 +00:00
|
|
|
true
|
2012-04-10 14:06:46 +00:00
|
|
|
end
|
|
|
|
|
2014-04-28 07:44:36 +00:00
|
|
|
def content
|
2015-05-07 10:15:40 +00:00
|
|
|
file = Store::File.find_by( id: self.store_file_id )
|
2014-05-03 12:34:36 +00:00
|
|
|
if !file
|
|
|
|
raise "No such file #{ self.store_file_id }!"
|
2014-04-28 07:44:36 +00:00
|
|
|
end
|
2014-05-03 12:34:36 +00:00
|
|
|
file.content
|
2012-04-10 14:06:46 +00:00
|
|
|
end
|
|
|
|
|
2014-05-03 12:34:36 +00:00
|
|
|
def provider
|
2015-05-07 10:15:40 +00:00
|
|
|
file = Store::File.find_by( id: self.store_file_id )
|
2014-05-03 12:34:36 +00:00
|
|
|
if !file
|
|
|
|
raise "No such file #{ self.store_file_id }!"
|
2014-04-28 07:44:36 +00:00
|
|
|
end
|
2014-05-03 12:34:36 +00:00
|
|
|
file.provider
|
2014-04-28 07:44:36 +00:00
|
|
|
end
|
2015-04-27 14:15:29 +00:00
|
|
|
end
|