From e9f7bf382eda5d50858bc03e2f0b832cbcebd4bd Mon Sep 17 00:00:00 2001 From: Benjamin Neff Date: Tue, 23 Nov 2021 03:20:47 +0100 Subject: [PATCH] Send new remote_photo_path in migration message --- app/services/import_service.rb | 2 +- app/services/migration_service.rb | 13 ++++++++++- lib/diaspora/federation/entities.rb | 7 +++--- spec/integration/migration_service_spec.rb | 26 ++++++++++++++++++++++ 4 files changed, 43 insertions(+), 5 deletions(-) diff --git a/app/services/import_service.rb b/app/services/import_service.rb index 9185ee90c..b7805e11a 100644 --- a/app/services/import_service.rb +++ b/app/services/import_service.rb @@ -10,7 +10,7 @@ class ImportService def import_by_files(path_to_profile, path_to_photos, username, opts={}) if path_to_profile.present? logger.info "Import for profile #{username} at path #{path_to_profile} requested" - import_user_profile(path_to_profile, username, opts) + import_user_profile(path_to_profile, username, opts.merge(photo_migration: path_to_photos.present?)) end user = User.find_by(username: username) diff --git a/app/services/migration_service.rb b/app/services/migration_service.rb index 0e6b0d917..78c6c38f6 100644 --- a/app/services/migration_service.rb +++ b/app/services/migration_service.rb @@ -60,7 +60,8 @@ class MigrationService new_person: archive_importer.user.person, old_private_key: archive_importer.serialized_private_key, old_person_diaspora_id: archive_importer.archive_author_diaspora_id, - archive_contacts: archive_importer.contacts + archive_contacts: archive_importer.contacts, + remote_photo_path: remote_photo_path ) end @@ -131,6 +132,16 @@ class MigrationService File.delete(@intermediate_file) end + def remote_photo_path + return unless opts.fetch(:photo_migration, false) + + if AppConfig.environment.s3.enable? + return "https://#{AppConfig.environment.s3.bucket.get}.s3.amazonaws.com/uploads/images/" + end + + "#{AppConfig.pod_uri}uploads/images/" + end + class ArchiveValidationFailed < RuntimeError end diff --git a/lib/diaspora/federation/entities.rb b/lib/diaspora/federation/entities.rb index 4a77f8ef7..fa3425f57 100644 --- a/lib/diaspora/federation/entities.rb +++ b/lib/diaspora/federation/entities.rb @@ -26,9 +26,10 @@ module Diaspora def self.account_migration(account_migration) DiasporaFederation::Entities::AccountMigration.new( - author: account_migration.sender.diaspora_handle, - profile: profile(account_migration.new_person.profile), - signature: account_migration.signature + author: account_migration.sender.diaspora_handle, + profile: profile(account_migration.new_person.profile), + remote_photo_path: account_migration.remote_photo_path, + signature: account_migration.signature ) end diff --git a/spec/integration/migration_service_spec.rb b/spec/integration/migration_service_spec.rb index 67f8513ee..84de21a6a 100644 --- a/spec/integration/migration_service_spec.rb +++ b/spec/integration/migration_service_spec.rb @@ -312,6 +312,32 @@ describe MigrationService do end end + context "photo migration" do + it "doesn't include a new remote_photo_path" do + service = MigrationService.new(archive_file.path, new_username) + service.send(:find_or_create_user) + account_migration = service.send(:account_migration) + expect(account_migration.remote_photo_path).to be_nil + end + + it "includes url to new pod image upload folder in remote_photo_path" do + service = MigrationService.new(archive_file.path, new_username, photo_migration: true) + service.send(:find_or_create_user) + account_migration = service.send(:account_migration) + expect(account_migration.remote_photo_path).to eq("#{AppConfig.pod_uri}uploads/images/") + end + + it "includes url to S3 image upload folder in remote_photo_path when S3 is enabled" do + AppConfig.environment.s3.enable = true + AppConfig.environment.s3.bucket = "test-bucket" + + service = MigrationService.new(archive_file.path, new_username, photo_migration: true) + service.send(:find_or_create_user) + account_migration = service.send(:account_migration) + expect(account_migration.remote_photo_path).to eq("https://test-bucket.s3.amazonaws.com/uploads/images/") + end + end + context "compressed archives" do it "uncompresses gz archive" do gz_compressed_file = create_gz_archive