store.rb 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. # Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
  2. class Store < ApplicationModel
  3. require 'store/object'
  4. require 'store/file'
  5. store :preferences
  6. belongs_to :store_object, :class_name => 'Store::Object'
  7. belongs_to :store_file, :class_name => 'Store::File'
  8. validates :filename, :presence => true
  9. =begin
  10. add an attachment to storage
  11. result = Store.add(
  12. :object => 'Ticket::Article',
  13. :o_id => 4711,
  14. :data => binary_string,
  15. :preferences => {
  16. :content_type => 'image/png',
  17. :content_id => 234,
  18. }
  19. )
  20. returns
  21. result = true
  22. =end
  23. def self.add(data)
  24. data = data.stringify_keys
  25. # lookup store_object.id
  26. store_object = Store::Object.create_if_not_exists( :name => data['object'] )
  27. data['store_object_id'] = store_object.id
  28. # add to real store
  29. file = Store::File.add( data['data'] )
  30. data['size'] = data['data'].to_s.bytesize
  31. data['store_file_id'] = file.id
  32. # not needed attributes
  33. data.delete('data')
  34. data.delete('object')
  35. # store meta data
  36. store = Store.create(data)
  37. true
  38. end
  39. =begin
  40. get attachment of object
  41. list = Store.list(
  42. :object => 'Ticket::Article',
  43. :o_id => 4711,
  44. )
  45. returns
  46. result = [store1, store2]
  47. store1 = {
  48. :size => 94123,
  49. :filename => 'image.png',
  50. :preferences => {
  51. :content_type => 'image/png',
  52. :content_id => 234,
  53. }
  54. }
  55. store1.content # binary_string
  56. =end
  57. def self.list(data)
  58. # search
  59. store_object_id = Store::Object.lookup( :name => data[:object] )
  60. stores = Store.where( :store_object_id => store_object_id, :o_id => data[:o_id].to_i ).
  61. order('created_at ASC, id ASC')
  62. return stores
  63. end
  64. =begin
  65. remove an attachment to storage
  66. result = Store.remove(
  67. :object => 'Ticket::Article',
  68. :o_id => 4711,
  69. )
  70. returns
  71. result = true
  72. =end
  73. def self.remove(data)
  74. # search
  75. store_object_id = Store::Object.lookup( :name => data[:object] )
  76. stores = Store.where( :store_object_id => store_object_id ).
  77. where( :o_id => data[:o_id] ).
  78. order('created_at ASC, id ASC')
  79. stores.each do |store|
  80. # check backend for references
  81. files = Store.where( :store_file_id => store.store_file_id )
  82. if files.count == 1 && files.first.id == store.id
  83. Store::File.find( store.store_file_id ).destroy
  84. end
  85. store.destroy
  86. end
  87. return true
  88. end
  89. def content
  90. file = Store::File.where( :id => self.store_file_id ).first
  91. if !file
  92. raise "No such file #{ self.store_file_id }!"
  93. end
  94. file.content
  95. end
  96. def provider
  97. file = Store::File.where( :id => self.store_file_id ).first
  98. if !file
  99. raise "No such file #{ self.store_file_id }!"
  100. end
  101. file.provider
  102. end
  103. end