backup_service.rb 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. # frozen_string_literal: true
  2. require 'rubygems/package'
  3. class BackupService < BaseService
  4. include Payloadable
  5. attr_reader :account, :backup, :collection
  6. def call(backup)
  7. @backup = backup
  8. @account = backup.user.account
  9. build_json!
  10. build_archive!
  11. end
  12. private
  13. def build_json!
  14. @collection = serialize(collection_presenter, ActivityPub::CollectionSerializer)
  15. account.statuses.with_includes.reorder(nil).find_in_batches do |statuses|
  16. statuses.each do |status|
  17. item = serialize_payload(ActivityPub::ActivityPresenter.from_status(status), ActivityPub::ActivitySerializer, signer: @account)
  18. item.delete(:'@context')
  19. unless item[:type] == 'Announce' || item[:object][:attachment].blank?
  20. item[:object][:attachment].each do |attachment|
  21. attachment[:url] = Addressable::URI.parse(attachment[:url]).path.gsub(/\A\/system\//, '')
  22. end
  23. end
  24. @collection[:orderedItems] << item
  25. end
  26. GC.start
  27. end
  28. end
  29. def build_archive!
  30. tmp_file = Tempfile.new(%w(archive .tar.gz))
  31. File.open(tmp_file, 'wb') do |file|
  32. Zlib::GzipWriter.wrap(file) do |gz|
  33. Gem::Package::TarWriter.new(gz) do |tar|
  34. dump_media_attachments!(tar)
  35. dump_outbox!(tar)
  36. dump_likes!(tar)
  37. dump_bookmarks!(tar)
  38. dump_actor!(tar)
  39. end
  40. end
  41. end
  42. archive_filename = ['archive', Time.now.utc.strftime('%Y%m%d%H%M%S'), SecureRandom.hex(16)].join('-') + '.tar.gz'
  43. @backup.dump = ActionDispatch::Http::UploadedFile.new(tempfile: tmp_file, filename: archive_filename)
  44. @backup.processed = true
  45. @backup.save!
  46. ensure
  47. tmp_file.close
  48. tmp_file.unlink
  49. end
  50. def dump_media_attachments!(tar)
  51. MediaAttachment.attached.where(account: account).reorder(nil).find_in_batches do |media_attachments|
  52. media_attachments.each do |m|
  53. next unless m.file&.path
  54. download_to_tar(tar, m.file, m.file.path)
  55. end
  56. GC.start
  57. end
  58. end
  59. def dump_outbox!(tar)
  60. json = Oj.dump(collection)
  61. tar.add_file_simple('outbox.json', 0o444, json.bytesize) do |io|
  62. io.write(json)
  63. end
  64. end
  65. def dump_actor!(tar)
  66. actor = serialize(account, ActivityPub::ActorSerializer)
  67. actor[:icon][:url] = 'avatar' + File.extname(actor[:icon][:url]) if actor[:icon]
  68. actor[:image][:url] = 'header' + File.extname(actor[:image][:url]) if actor[:image]
  69. actor[:outbox] = 'outbox.json'
  70. actor[:likes] = 'likes.json'
  71. actor[:bookmarks] = 'bookmarks.json'
  72. download_to_tar(tar, account.avatar, 'avatar' + File.extname(account.avatar.path)) if account.avatar.exists?
  73. download_to_tar(tar, account.header, 'header' + File.extname(account.header.path)) if account.header.exists?
  74. json = Oj.dump(actor)
  75. tar.add_file_simple('actor.json', 0o444, json.bytesize) do |io|
  76. io.write(json)
  77. end
  78. end
  79. def dump_likes!(tar)
  80. collection = serialize(ActivityPub::CollectionPresenter.new(id: 'likes.json', type: :ordered, size: 0, items: []), ActivityPub::CollectionSerializer)
  81. Status.reorder(nil).joins(:favourites).includes(:account).merge(account.favourites).find_in_batches do |statuses|
  82. statuses.each do |status|
  83. collection[:totalItems] += 1
  84. collection[:orderedItems] << ActivityPub::TagManager.instance.uri_for(status)
  85. end
  86. GC.start
  87. end
  88. json = Oj.dump(collection)
  89. tar.add_file_simple('likes.json', 0o444, json.bytesize) do |io|
  90. io.write(json)
  91. end
  92. end
  93. def dump_bookmarks!(tar)
  94. collection = serialize(ActivityPub::CollectionPresenter.new(id: 'bookmarks.json', type: :ordered, size: 0, items: []), ActivityPub::CollectionSerializer)
  95. Status.reorder(nil).joins(:bookmarks).includes(:account).merge(account.bookmarks).find_in_batches do |statuses|
  96. statuses.each do |status|
  97. collection[:totalItems] += 1
  98. collection[:orderedItems] << ActivityPub::TagManager.instance.uri_for(status)
  99. end
  100. GC.start
  101. end
  102. json = Oj.dump(collection)
  103. tar.add_file_simple('bookmarks.json', 0o444, json.bytesize) do |io|
  104. io.write(json)
  105. end
  106. end
  107. def collection_presenter
  108. ActivityPub::CollectionPresenter.new(
  109. id: 'outbox.json',
  110. type: :ordered,
  111. size: account.statuses_count,
  112. items: []
  113. )
  114. end
  115. def serialize(object, serializer)
  116. ActiveModelSerializers::SerializableResource.new(
  117. object,
  118. serializer: serializer,
  119. adapter: ActivityPub::Adapter
  120. ).as_json
  121. end
  122. CHUNK_SIZE = 1.megabyte
  123. def download_to_tar(tar, attachment, filename)
  124. adapter = Paperclip.io_adapters.for(attachment)
  125. tar.add_file_simple(filename, 0o444, adapter.size) do |io|
  126. while (buffer = adapter.read(CHUNK_SIZE))
  127. io.write(buffer)
  128. end
  129. end
  130. rescue Errno::ENOENT, Seahorse::Client::NetworkingError
  131. Rails.logger.warn "Could not backup file #{filename}: file not found"
  132. end
  133. end