store.rb 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. # Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
  2. class Store < ApplicationModel
  3. load 'store/object.rb'
  4. load 'store/file.rb'
  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. store
  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. stores
  63. end
  64. =begin
  65. remove attachments of object from 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. Store.remove_item(store.id)
  82. end
  83. true
  84. end
  85. =begin
  86. remove one attachment from storage
  87. result = Store.remove_item(store_id)
  88. returns
  89. result = true
  90. =end
  91. def self.remove_item(store_id)
  92. # check backend for references
  93. store = Store.find(store_id)
  94. files = Store.where(store_file_id: store.store_file_id)
  95. if files.count == 1 && files.first.id == store.id
  96. Store::File.find(store.store_file_id).destroy
  97. end
  98. store.destroy
  99. true
  100. end
  101. def content
  102. file = Store::File.find_by(id: store_file_id)
  103. if !file
  104. raise "No such file #{store_file_id}!"
  105. end
  106. file.content
  107. end
  108. def provider
  109. file = Store::File.find_by(id: store_file_id)
  110. if !file
  111. raise "No such file #{store_file_id}!"
  112. end
  113. file.provider
  114. end
  115. end