upload.rb 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. module Store::Provider::S3::Upload
  3. class << self
  4. def complete(sha, parts, id)
  5. Store::Provider::S3.client.complete_multipart_upload(
  6. bucket: bucket,
  7. key: sha,
  8. multipart_upload: { parts: parts },
  9. upload_id: id
  10. )
  11. end
  12. def create(sha)
  13. info = Store::Provider::S3.client.create_multipart_upload(
  14. bucket: bucket,
  15. key: sha
  16. )
  17. info['upload_id']
  18. end
  19. def process(data, sha, id)
  20. divide(data).each_with_index.map do |chunk, index|
  21. number = index + 1
  22. part = Store::Provider::S3.client.upload_part(
  23. {
  24. body: chunk,
  25. bucket: bucket,
  26. key: sha,
  27. part_number: number,
  28. upload_id: id
  29. }
  30. )
  31. {
  32. etag: part.etag,
  33. part_number: number
  34. }
  35. end
  36. end
  37. private
  38. def divide(data)
  39. size = Store::Provider::S3::Config.max_chunk_size
  40. Array.new(((data.length + size - 1) / size)) do |index|
  41. data.byteslice(index * size, size)
  42. end
  43. end
  44. def bucket
  45. Store::Provider::S3::Config.bucket
  46. end
  47. end
  48. end