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.

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