backup_service.rb 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. # frozen_string_literal: true
  2. require 'rubygems/package'
  3. class BackupService < BaseService
  4. attr_reader :account, :backup, :collection
  5. def call(backup)
  6. @backup = backup
  7. @account = backup.user.account
  8. build_json!
  9. build_archive!
  10. end
  11. private
  12. def build_json!
  13. @collection = serialize(collection_presenter, ActivityPub::CollectionSerializer)
  14. account.statuses.with_includes.find_in_batches do |statuses|
  15. statuses.each do |status|
  16. item = serialize(status, ActivityPub::ActivitySerializer)
  17. item.delete(:'@context')
  18. unless item[:type] == 'Announce' || item[:object][:attachment].blank?
  19. item[:object][:attachment].each do |attachment|
  20. attachment[:url] = Addressable::URI.parse(attachment[:url]).path.gsub(/\A\/system\//, '')
  21. end
  22. end
  23. @collection[:orderedItems] << item
  24. end
  25. GC.start
  26. end
  27. end
  28. def build_archive!
  29. tmp_file = Tempfile.new(%w(archive .tar.gz))
  30. File.open(tmp_file, 'wb') do |file|
  31. Zlib::GzipWriter.wrap(file) do |gz|
  32. Gem::Package::TarWriter.new(gz) do |tar|
  33. dump_media_attachments!(tar)
  34. dump_outbox!(tar)
  35. dump_actor!(tar)
  36. end
  37. end
  38. end
  39. archive_filename = ['archive', Time.now.utc.strftime('%Y%m%d%H%M%S'), SecureRandom.hex(2)].join('-') + '.tar.gz'
  40. @backup.dump = ActionDispatch::Http::UploadedFile.new(tempfile: tmp_file, filename: archive_filename)
  41. @backup.processed = true
  42. @backup.save!
  43. ensure
  44. tmp_file.close
  45. tmp_file.unlink
  46. end
  47. def dump_media_attachments!(tar)
  48. MediaAttachment.attached.where(account: account).find_in_batches do |media_attachments|
  49. media_attachments.each do |m|
  50. download_to_tar(tar, m.file, m.file.path)
  51. end
  52. GC.start
  53. end
  54. end
  55. def dump_outbox!(tar)
  56. json = Oj.dump(collection)
  57. tar.add_file_simple('outbox.json', 0o444, json.bytesize) do |io|
  58. io.write(json)
  59. end
  60. end
  61. def dump_actor!(tar)
  62. actor = serialize(account, ActivityPub::ActorSerializer)
  63. actor[:icon][:url] = 'avatar' + File.extname(actor[:icon][:url]) if actor[:icon]
  64. actor[:image][:url] = 'header' + File.extname(actor[:image][:url]) if actor[:image]
  65. download_to_tar(tar, account.avatar, 'avatar' + File.extname(account.avatar.path)) if account.avatar.exists?
  66. download_to_tar(tar, account.header, 'header' + File.extname(account.header.path)) if account.header.exists?
  67. json = Oj.dump(actor)
  68. tar.add_file_simple('actor.json', 0o444, json.bytesize) do |io|
  69. io.write(json)
  70. end
  71. tar.add_file_simple('key.pem', 0o444, account.private_key.bytesize) do |io|
  72. io.write(account.private_key)
  73. end
  74. end
  75. def collection_presenter
  76. ActivityPub::CollectionPresenter.new(
  77. id: account_outbox_url(account),
  78. type: :ordered,
  79. size: account.statuses_count,
  80. items: []
  81. )
  82. end
  83. def serialize(object, serializer)
  84. ActiveModelSerializers::SerializableResource.new(
  85. object,
  86. serializer: serializer,
  87. adapter: ActivityPub::Adapter
  88. ).as_json
  89. end
  90. CHUNK_SIZE = 1.megabyte
  91. def download_to_tar(tar, attachment, filename)
  92. adapter = Paperclip.io_adapters.for(attachment)
  93. tar.add_file_simple(filename, 0o444, adapter.size) do |io|
  94. while (buffer = adapter.read(CHUNK_SIZE))
  95. io.write(buffer)
  96. end
  97. end
  98. end
  99. end