You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

167 lines
4.6 KiB

  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.reorder(nil).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_likes!(tar)
  36. dump_bookmarks!(tar)
  37. dump_actor!(tar)
  38. end
  39. end
  40. end
  41. archive_filename = ['archive', Time.now.utc.strftime('%Y%m%d%H%M%S'), SecureRandom.hex(16)].join('-') + '.tar.gz'
  42. @backup.dump = ActionDispatch::Http::UploadedFile.new(tempfile: tmp_file, filename: archive_filename)
  43. @backup.processed = true
  44. @backup.save!
  45. ensure
  46. tmp_file.close
  47. tmp_file.unlink
  48. end
  49. def dump_media_attachments!(tar)
  50. MediaAttachment.attached.where(account: account).reorder(nil).find_in_batches do |media_attachments|
  51. media_attachments.each do |m|
  52. download_to_tar(tar, m.file, m.file.path)
  53. end
  54. GC.start
  55. end
  56. end
  57. def dump_outbox!(tar)
  58. json = Oj.dump(collection)
  59. tar.add_file_simple('outbox.json', 0o444, json.bytesize) do |io|
  60. io.write(json)
  61. end
  62. end
  63. def dump_actor!(tar)
  64. actor = serialize(account, ActivityPub::ActorSerializer)
  65. actor[:icon][:url] = 'avatar' + File.extname(actor[:icon][:url]) if actor[:icon]
  66. actor[:image][:url] = 'header' + File.extname(actor[:image][:url]) if actor[:image]
  67. actor[:outbox] = 'outbox.json'
  68. actor[:likes] = 'likes.json'
  69. actor[:bookmarks] = 'bookmarks.json'
  70. download_to_tar(tar, account.avatar, 'avatar' + File.extname(account.avatar.path)) if account.avatar.exists?
  71. download_to_tar(tar, account.header, 'header' + File.extname(account.header.path)) if account.header.exists?
  72. json = Oj.dump(actor)
  73. tar.add_file_simple('actor.json', 0o444, json.bytesize) do |io|
  74. io.write(json)
  75. end
  76. end
  77. def dump_likes!(tar)
  78. collection = serialize(ActivityPub::CollectionPresenter.new(id: 'likes.json', type: :ordered, size: 0, items: []), ActivityPub::CollectionSerializer)
  79. Status.reorder(nil).joins(:favourites).includes(:account).merge(account.favourites).find_in_batches do |statuses|
  80. statuses.each do |status|
  81. collection[:totalItems] += 1
  82. collection[:orderedItems] << ActivityPub::TagManager.instance.uri_for(status)
  83. end
  84. GC.start
  85. end
  86. json = Oj.dump(collection)
  87. tar.add_file_simple('likes.json', 0o444, json.bytesize) do |io|
  88. io.write(json)
  89. end
  90. end
  91. def dump_bookmarks!(tar)
  92. collection = serialize(ActivityPub::CollectionPresenter.new(id: 'bookmarks.json', type: :ordered, size: 0, items: []), ActivityPub::CollectionSerializer)
  93. Status.reorder(nil).joins(:bookmarks).includes(:account).merge(account.bookmarks).find_in_batches do |statuses|
  94. statuses.each do |status|
  95. collection[:totalItems] += 1
  96. collection[:orderedItems] << ActivityPub::TagManager.instance.uri_for(status)
  97. end
  98. GC.start
  99. end
  100. json = Oj.dump(collection)
  101. tar.add_file_simple('bookmarks.json', 0o444, json.bytesize) do |io|
  102. io.write(json)
  103. end
  104. end
  105. def collection_presenter
  106. ActivityPub::CollectionPresenter.new(
  107. id: 'outbox.json',
  108. type: :ordered,
  109. size: account.statuses_count,
  110. items: []
  111. )
  112. end
  113. def serialize(object, serializer)
  114. ActiveModelSerializers::SerializableResource.new(
  115. object,
  116. serializer: serializer,
  117. adapter: ActivityPub::Adapter
  118. ).as_json
  119. end
  120. CHUNK_SIZE = 1.megabyte
  121. def download_to_tar(tar, attachment, filename)
  122. adapter = Paperclip.io_adapters.for(attachment)
  123. tar.add_file_simple(filename, 0o444, adapter.size) do |io|
  124. while (buffer = adapter.read(CHUNK_SIZE))
  125. io.write(buffer)
  126. end
  127. end
  128. end
  129. end