file.rb 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. # Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
  2. class Store < ApplicationModel
  3. class File < ApplicationModel
  4. include ApplicationLib
  5. after_destroy :destroy_provider
  6. =begin
  7. add new file to store
  8. store_file_id = Store::File.add(binary_data)
  9. do also verify of written data
  10. store_file_id = Store::File.add(binary_data, true)
  11. =end
  12. def self.add(data, verify = true)
  13. sha = Digest::SHA256.hexdigest(data)
  14. file = Store::File.find_by(sha: sha)
  15. if file.nil?
  16. # load backend based on config
  17. adapter_name = Setting.get('storage_provider') || 'DB'
  18. if !adapter_name
  19. raise 'Missing storage_provider setting option'
  20. end
  21. adapter = "Store::Provider::#{adapter_name}".constantize
  22. adapter.add(data, sha)
  23. file = Store::File.create(
  24. provider: adapter_name,
  25. sha: sha,
  26. )
  27. # verify
  28. if verify
  29. read_data = adapter.get(sha)
  30. read_sha = Digest::SHA256.hexdigest(read_data)
  31. if sha != read_sha
  32. raise "Content not written correctly (provider #{adapter_name})."
  33. end
  34. end
  35. end
  36. file
  37. end
  38. =begin
  39. read content of a file
  40. store = Store::File.find(123)
  41. store.content # returns binary
  42. =end
  43. def content
  44. "Store::Provider::#{provider}".constantize.get(sha)
  45. end
  46. =begin
  47. file system check of store, check data and sha (in case fix it)
  48. Store::File.verify
  49. read each file which should be in backend and verify against sha hash
  50. in case of fixing sha hash use:
  51. Store::File.verify(true)
  52. =end
  53. def self.verify(fix_it = nil)
  54. success = true
  55. file_ids = Store::File.all.pluck(:id)
  56. file_ids.each do |item_id|
  57. item = Store::File.find(item_id)
  58. content = item.content
  59. sha = Digest::SHA256.hexdigest(content)
  60. logger.info "CHECK: Store::File.find(#{item.id})"
  61. next if sha == item.sha
  62. success = false
  63. logger.error "DIFF: sha diff of Store::File.find(#{item.id}) current:#{sha}/db:#{item.sha}/provider:#{item.provider}"
  64. store = Store.find_by(store_file_id: item.id)
  65. logger.error "STORE: #{store.inspect}"
  66. if fix_it
  67. item.update_attribute(:sha, sha) # rubocop:disable Rails/SkipsModelValidations
  68. end
  69. end
  70. success
  71. end
  72. =begin
  73. move file from one to other provider
  74. move files from file backend to db
  75. Store::File.move('File', 'DB')
  76. move files from db backend to fs
  77. Store::File.move('DB', 'File')
  78. nice move to keep system responsive
  79. Store::File.move('DB', 'File', delay_in_sec) # e. g. 1
  80. =end
  81. def self.move(source, target, delay = nil)
  82. adapter_source = "Store::Provider::#{source}".constantize
  83. adapter_target = "Store::Provider::#{target}".constantize
  84. file_ids = Store::File.all.pluck(:id)
  85. file_ids.each do |item_id|
  86. item = Store::File.find(item_id)
  87. next if item.provider == target
  88. content = item.content
  89. # add to new provider
  90. adapter_target.add(content, item.sha)
  91. # update meta data
  92. item.update_attribute(:provider, target) # rubocop:disable Rails/SkipsModelValidations
  93. # remove from old provider
  94. adapter_source.delete(item.sha)
  95. logger.info "Moved file #{item.sha} from #{source} to #{target}"
  96. sleep delay if delay
  97. end
  98. true
  99. end
  100. private
  101. def destroy_provider
  102. "Store::Provider::#{provider}".constantize.delete(sha)
  103. end
  104. end
  105. end