upload_cache.rb 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. # A wrapper class around Store that handles temporary attachment uploads
  3. # and provides an interface for those.
  4. class UploadCache
  5. attr_reader :id
  6. # Initializes a UploadCache for a given form_id.
  7. #
  8. # @example
  9. # cache = UploadCache.new(form_id)
  10. #
  11. # @return [UploadCache]
  12. def initialize(id)
  13. @id = id
  14. end
  15. # Adds a Store item with the given attributes for the form_id.
  16. #
  17. # @see Store#create!
  18. #
  19. # @example
  20. # cache = UploadCache.new(form_id)
  21. # store = cache.add(
  22. # filename: file.original_filename,
  23. # data: file.read,
  24. # preferences: {
  25. # 'Content-Type' => 'application/octet-stream'
  26. # }
  27. # )
  28. #
  29. # @return [Store] the created Store item
  30. def add(args)
  31. Store.create!(
  32. args.merge(
  33. object: store_object,
  34. o_id: id,
  35. )
  36. )
  37. end
  38. # Provides all Store items associated to the form_id.
  39. #
  40. # @see Store#list
  41. #
  42. # @example
  43. # attachments = UploadCache.new(form_id).attachments
  44. #
  45. # @return [Array<Store>] an enumerator of Store items
  46. def attachments
  47. Store.list(
  48. object: store_object,
  49. o_id: id,
  50. )
  51. end
  52. # Removes all Store items associated to the form_id.
  53. #
  54. # @see Store#remove
  55. #
  56. # @example
  57. # UploadCache.new(form_id).destroy
  58. #
  59. def destroy
  60. Store.remove(
  61. object: store_object,
  62. o_id: id,
  63. )
  64. end
  65. # Removes all Store items associated to the form_id.
  66. #
  67. # @see Store#remove
  68. #
  69. # @example
  70. # UploadCache.new(form_id).remove_item(store_id)
  71. #
  72. # @raise [Exceptions::UnprocessableEntity] in cases where a Store item should get deleted that is not an UploadCache item
  73. #
  74. def remove_item(store_id = nil)
  75. store = Store.find(store_id)
  76. if store.o_id != id || store.store_object_id != store_object_id
  77. raise Exceptions::UnprocessableEntity, "Attempt to delete Store item #{store_id} that is not bound to UploadCache object"
  78. end
  79. Store.remove_item(store_id)
  80. end
  81. private
  82. def store_object
  83. self.class.name
  84. end
  85. def store_object_id
  86. Store::Object.lookup(name: store_object).id
  87. end
  88. end