|
|
@ -1,59 +1,67 @@
|
|
|
|
# frozen_string_literal: true
|
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
|
|
|
|
require 'rubygems/package'
|
|
|
|
require 'zip'
|
|
|
|
|
|
|
|
|
|
|
|
class BackupService < BaseService
|
|
|
|
class BackupService < BaseService
|
|
|
|
include Payloadable
|
|
|
|
include Payloadable
|
|
|
|
|
|
|
|
include ContextHelper
|
|
|
|
|
|
|
|
|
|
|
|
attr_reader :account, :backup, :collection
|
|
|
|
attr_reader :account, :backup
|
|
|
|
|
|
|
|
|
|
|
|
def call(backup)
|
|
|
|
def call(backup)
|
|
|
|
@backup = backup
|
|
|
|
@backup = backup
|
|
|
|
@account = backup.user.account
|
|
|
|
@account = backup.user.account
|
|
|
|
|
|
|
|
|
|
|
|
build_json!
|
|
|
|
|
|
|
|
build_archive!
|
|
|
|
build_archive!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
private
|
|
|
|
private
|
|
|
|
|
|
|
|
|
|
|
|
def build_json!
|
|
|
|
def build_outbox_json!(file)
|
|
|
|
@collection = serialize(collection_presenter, ActivityPub::CollectionSerializer)
|
|
|
|
skeleton = serialize(collection_presenter, ActivityPub::CollectionSerializer)
|
|
|
|
|
|
|
|
skeleton[:@context] = full_context
|
|
|
|
|
|
|
|
skeleton[:orderedItems] = ['!PLACEHOLDER!']
|
|
|
|
|
|
|
|
skeleton = Oj.dump(skeleton)
|
|
|
|
|
|
|
|
prepend, append = skeleton.split('"!PLACEHOLDER!"')
|
|
|
|
|
|
|
|
add_comma = false
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
file.write(prepend)
|
|
|
|
|
|
|
|
|
|
|
|
account.statuses.with_includes.reorder(nil).find_in_batches do |statuses|
|
|
|
|
account.statuses.with_includes.reorder(nil).find_in_batches do |statuses|
|
|
|
|
statuses.each do |status|
|
|
|
|
file.write(',') if add_comma
|
|
|
|
item = serialize_payload(ActivityPub::ActivityPresenter.from_status(status), ActivityPub::ActivitySerializer, signer: @account)
|
|
|
|
add_comma = true
|
|
|
|
item.delete(:@context)
|
|
|
|
|
|
|
|
|
|
|
|
file.write(statuses.map do |status|
|
|
|
|
|
|
|
|
item = serialize_payload(ActivityPub::ActivityPresenter.from_status(status), ActivityPub::ActivitySerializer)
|
|
|
|
|
|
|
|
item.delete('@context')
|
|
|
|
|
|
|
|
|
|
|
|
unless item[:type] == 'Announce' || item[:object][:attachment].blank?
|
|
|
|
unless item[:type] == 'Announce' || item[:object][:attachment].blank?
|
|
|
|
item[:object][:attachment].each do |attachment|
|
|
|
|
item[:object][:attachment].each do |attachment|
|
|
|
|
attachment[:url] = Addressable::URI.parse(attachment[:url]).path.gsub(/\A\/system\//, '')
|
|
|
|
attachment[:url] = Addressable::URI.parse(attachment[:url]).path.delete_prefix('/system/')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
@collection[:orderedItems] << item
|
|
|
|
Oj.dump(item)
|
|
|
|
end
|
|
|
|
end.join(','))
|
|
|
|
|
|
|
|
|
|
|
|
GC.start
|
|
|
|
GC.start
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
file.write(append)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def build_archive!
|
|
|
|
def build_archive!
|
|
|
|
tmp_file = Tempfile.new(%w(archive .tar.gz))
|
|
|
|
tmp_file = Tempfile.new(%w(archive .zip))
|
|
|
|
|
|
|
|
|
|
|
|
File.open(tmp_file, 'wb') do |file|
|
|
|
|
Zip::File.open(tmp_file, create: true) do |zipfile|
|
|
|
|
Zlib::GzipWriter.wrap(file) do |gz|
|
|
|
|
dump_outbox!(zipfile)
|
|
|
|
Gem::Package::TarWriter.new(gz) do |tar|
|
|
|
|
dump_media_attachments!(zipfile)
|
|
|
|
dump_media_attachments!(tar)
|
|
|
|
dump_likes!(zipfile)
|
|
|
|
dump_outbox!(tar)
|
|
|
|
dump_bookmarks!(zipfile)
|
|
|
|
dump_likes!(tar)
|
|
|
|
dump_actor!(zipfile)
|
|
|
|
dump_bookmarks!(tar)
|
|
|
|
|
|
|
|
dump_actor!(tar)
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
archive_filename = "#{['archive', Time.now.utc.strftime('%Y%m%d%H%M%S'), SecureRandom.hex(16)].join('-')}.tar.gz"
|
|
|
|
archive_filename = "#{['archive', Time.now.utc.strftime('%Y%m%d%H%M%S'), SecureRandom.hex(16)].join('-')}.zip"
|
|
|
|
|
|
|
|
|
|
|
|
@backup.dump = ActionDispatch::Http::UploadedFile.new(tempfile: tmp_file, filename: archive_filename)
|
|
|
|
@backup.dump = ActionDispatch::Http::UploadedFile.new(tempfile: tmp_file, filename: archive_filename)
|
|
|
|
@backup.processed = true
|
|
|
|
@backup.processed = true
|
|
|
@ -63,27 +71,28 @@ class BackupService < BaseService
|
|
|
|
tmp_file.unlink
|
|
|
|
tmp_file.unlink
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def dump_media_attachments!(tar)
|
|
|
|
def dump_media_attachments!(zipfile)
|
|
|
|
MediaAttachment.attached.where(account: account).reorder(nil).find_in_batches do |media_attachments|
|
|
|
|
MediaAttachment.attached.where(account: account).reorder(nil).find_in_batches do |media_attachments|
|
|
|
|
media_attachments.each do |m|
|
|
|
|
media_attachments.each do |m|
|
|
|
|
next unless m.file&.path
|
|
|
|
path = m.file&.path
|
|
|
|
|
|
|
|
next unless path
|
|
|
|
|
|
|
|
|
|
|
|
download_to_tar(tar, m.file, m.file.path)
|
|
|
|
path = path.gsub(/\A.*\/system\//, '')
|
|
|
|
|
|
|
|
path = path.gsub(/\A\/+/, '')
|
|
|
|
|
|
|
|
download_to_zip(zipfile, m.file, path)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
GC.start
|
|
|
|
GC.start
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def dump_outbox!(tar)
|
|
|
|
def dump_outbox!(zipfile)
|
|
|
|
json = Oj.dump(collection)
|
|
|
|
zipfile.get_output_stream('outbox.json') do |io|
|
|
|
|
|
|
|
|
build_outbox_json!(io)
|
|
|
|
tar.add_file_simple('outbox.json', 0o444, json.bytesize) do |io|
|
|
|
|
|
|
|
|
io.write(json)
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def dump_actor!(tar)
|
|
|
|
def dump_actor!(zipfile)
|
|
|
|
actor = serialize(account, ActivityPub::ActorSerializer)
|
|
|
|
actor = serialize(account, ActivityPub::ActorSerializer)
|
|
|
|
|
|
|
|
|
|
|
|
actor[:icon][:url] = "avatar#{File.extname(actor[:icon][:url])}" if actor[:icon]
|
|
|
|
actor[:icon][:url] = "avatar#{File.extname(actor[:icon][:url])}" if actor[:icon]
|
|
|
@ -92,51 +101,66 @@ class BackupService < BaseService
|
|
|
|
actor[:likes] = 'likes.json'
|
|
|
|
actor[:likes] = 'likes.json'
|
|
|
|
actor[:bookmarks] = 'bookmarks.json'
|
|
|
|
actor[:bookmarks] = 'bookmarks.json'
|
|
|
|
|
|
|
|
|
|
|
|
download_to_tar(tar, account.avatar, "avatar#{File.extname(account.avatar.path)}") if account.avatar.exists?
|
|
|
|
download_to_zip(tar, account.avatar, "avatar#{File.extname(account.avatar.path)}") if account.avatar.exists?
|
|
|
|
download_to_tar(tar, account.header, "header#{File.extname(account.header.path)}") if account.header.exists?
|
|
|
|
download_to_zip(tar, account.header, "header#{File.extname(account.header.path)}") if account.header.exists?
|
|
|
|
|
|
|
|
|
|
|
|
json = Oj.dump(actor)
|
|
|
|
json = Oj.dump(actor)
|
|
|
|
|
|
|
|
|
|
|
|
tar.add_file_simple('actor.json', 0o444, json.bytesize) do |io|
|
|
|
|
zipfile.get_output_stream('actor.json') do |io|
|
|
|
|
io.write(json)
|
|
|
|
io.write(json)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def dump_likes!(tar)
|
|
|
|
def dump_likes!(zipfile)
|
|
|
|
collection = serialize(ActivityPub::CollectionPresenter.new(id: 'likes.json', type: :ordered, size: 0, items: []), ActivityPub::CollectionSerializer)
|
|
|
|
skeleton = serialize(ActivityPub::CollectionPresenter.new(id: 'likes.json', type: :ordered, size: 0, items: []), ActivityPub::CollectionSerializer)
|
|
|
|
|
|
|
|
skeleton.delete(:totalItems)
|
|
|
|
|
|
|
|
skeleton[:orderedItems] = ['!PLACEHOLDER!']
|
|
|
|
|
|
|
|
skeleton = Oj.dump(skeleton)
|
|
|
|
|
|
|
|
prepend, append = skeleton.split('"!PLACEHOLDER!"')
|
|
|
|
|
|
|
|
|
|
|
|
Status.reorder(nil).joins(:favourites).includes(:account).merge(account.favourites).find_in_batches do |statuses|
|
|
|
|
zipfile.get_output_stream('likes.json') do |io|
|
|
|
|
statuses.each do |status|
|
|
|
|
io.write(prepend)
|
|
|
|
collection[:totalItems] += 1
|
|
|
|
|
|
|
|
collection[:orderedItems] << ActivityPub::TagManager.instance.uri_for(status)
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
GC.start
|
|
|
|
add_comma = false
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
json = Oj.dump(collection)
|
|
|
|
Status.reorder(nil).joins(:favourites).includes(:account).merge(account.favourites).find_in_batches do |statuses|
|
|
|
|
|
|
|
|
io.write(',') if add_comma
|
|
|
|
|
|
|
|
add_comma = true
|
|
|
|
|
|
|
|
|
|
|
|
tar.add_file_simple('likes.json', 0o444, json.bytesize) do |io|
|
|
|
|
io.write(statuses.map do |status|
|
|
|
|
io.write(json)
|
|
|
|
Oj.dump(ActivityPub::TagManager.instance.uri_for(status))
|
|
|
|
|
|
|
|
end.join(','))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
GC.start
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
io.write(append)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def dump_bookmarks!(tar)
|
|
|
|
def dump_bookmarks!(zipfile)
|
|
|
|
collection = serialize(ActivityPub::CollectionPresenter.new(id: 'bookmarks.json', type: :ordered, size: 0, items: []), ActivityPub::CollectionSerializer)
|
|
|
|
skeleton = serialize(ActivityPub::CollectionPresenter.new(id: 'bookmarks.json', type: :ordered, size: 0, items: []), ActivityPub::CollectionSerializer)
|
|
|
|
|
|
|
|
skeleton.delete(:totalItems)
|
|
|
|
|
|
|
|
skeleton[:orderedItems] = ['!PLACEHOLDER!']
|
|
|
|
|
|
|
|
skeleton = Oj.dump(skeleton)
|
|
|
|
|
|
|
|
prepend, append = skeleton.split('"!PLACEHOLDER!"')
|
|
|
|
|
|
|
|
|
|
|
|
Status.reorder(nil).joins(:bookmarks).includes(:account).merge(account.bookmarks).find_in_batches do |statuses|
|
|
|
|
zipfile.get_output_stream('bookmarks.json') do |io|
|
|
|
|
statuses.each do |status|
|
|
|
|
io.write(prepend)
|
|
|
|
collection[:totalItems] += 1
|
|
|
|
|
|
|
|
collection[:orderedItems] << ActivityPub::TagManager.instance.uri_for(status)
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
GC.start
|
|
|
|
add_comma = false
|
|
|
|
end
|
|
|
|
Status.reorder(nil).joins(:bookmarks).includes(:account).merge(account.bookmarks).find_in_batches do |statuses|
|
|
|
|
|
|
|
|
io.write(',') if add_comma
|
|
|
|
|
|
|
|
add_comma = true
|
|
|
|
|
|
|
|
|
|
|
|
json = Oj.dump(collection)
|
|
|
|
io.write(statuses.map do |status|
|
|
|
|
|
|
|
|
Oj.dump(ActivityPub::TagManager.instance.uri_for(status))
|
|
|
|
|
|
|
|
end.join(','))
|
|
|
|
|
|
|
|
|
|
|
|
tar.add_file_simple('bookmarks.json', 0o444, json.bytesize) do |io|
|
|
|
|
GC.start
|
|
|
|
io.write(json)
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
io.write(append)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
@ -159,10 +183,10 @@ class BackupService < BaseService
|
|
|
|
|
|
|
|
|
|
|
|
CHUNK_SIZE = 1.megabyte
|
|
|
|
CHUNK_SIZE = 1.megabyte
|
|
|
|
|
|
|
|
|
|
|
|
def download_to_tar(tar, attachment, filename)
|
|
|
|
def download_to_zip(zipfile, attachment, filename)
|
|
|
|
adapter = Paperclip.io_adapters.for(attachment)
|
|
|
|
adapter = Paperclip.io_adapters.for(attachment)
|
|
|
|
|
|
|
|
|
|
|
|
tar.add_file_simple(filename, 0o444, adapter.size) do |io|
|
|
|
|
zipfile.get_output_stream(filename) do |io|
|
|
|
|
while (buffer = adapter.read(CHUNK_SIZE))
|
|
|
|
while (buffer = adapter.read(CHUNK_SIZE))
|
|
|
|
io.write(buffer)
|
|
|
|
io.write(buffer)
|
|
|
|
end
|
|
|
|
end
|
|
|
|