|
| 1 | +// |
| 2 | +// FileClientMigration.swift |
| 3 | +// KlaviyoSwift |
| 4 | +// |
| 5 | +// Migration logic for moving SDK files from Library/ to Library/Application Support/com.klaviyo/ |
| 6 | +// |
| 7 | + |
| 8 | +import Foundation |
| 9 | + |
| 10 | +/// Returns the Application Support directory for Klaviyo files |
| 11 | +/// - Returns: URL pointing to Library/Application Support/com.klaviyo/ |
| 12 | +public func klaviyoApplicationSupportDirectory() -> URL { |
| 13 | + let libraryDirectory = environment.fileClient.libraryDirectory() |
| 14 | + return libraryDirectory |
| 15 | + .appendingPathComponent("Application Support", isDirectory: true) |
| 16 | + .appendingPathComponent("com.klaviyo", isDirectory: true) |
| 17 | +} |
| 18 | + |
| 19 | +/// Migrates Klaviyo files from the old location (Library/) to the new location (Library/Application Support/com.klaviyo/) |
| 20 | +/// This function is idempotent and safe to call multiple times. |
| 21 | +/// |
| 22 | +/// - Parameter apiKey: The API key used to identify Klaviyo files |
| 23 | +/// - Returns: The directory URL where files should be stored (new location if migration succeeded, old location if it failed) |
| 24 | +public func migrateFilesIfNeeded(apiKey: String) -> URL { |
| 25 | + let newDirectory = klaviyoApplicationSupportDirectory() |
| 26 | + let oldDirectory = environment.fileClient.libraryDirectory() |
| 27 | + |
| 28 | + // Step 1: Create new directory if needed |
| 29 | + do { |
| 30 | + try environment.fileClient.createDirectory(newDirectory, true) |
| 31 | + } catch { |
| 32 | + environment.logger.error("Failed to create Application Support directory: \(error.localizedDescription)") |
| 33 | + return oldDirectory |
| 34 | + } |
| 35 | + |
| 36 | + // Step 2: Check if migration already completed |
| 37 | + let stateFileName = "klaviyo-\(apiKey)-state.json" |
| 38 | + let newStateFile = newDirectory.appendingPathComponent(stateFileName, isDirectory: false) |
| 39 | + |
| 40 | + if environment.fileClient.fileExists(newStateFile.path) { |
| 41 | + // Migration already completed |
| 42 | + return newDirectory |
| 43 | + } |
| 44 | + |
| 45 | + // Step 3: Check if old files exist |
| 46 | + let oldStateFile = oldDirectory.appendingPathComponent(stateFileName, isDirectory: false) |
| 47 | + |
| 48 | + if !environment.fileClient.fileExists(oldStateFile.path) { |
| 49 | + // Fresh install - no files to migrate |
| 50 | + return newDirectory |
| 51 | + } |
| 52 | + |
| 53 | + // Step 4: Perform migration |
| 54 | + let filesToMigrate = [ |
| 55 | + "klaviyo-\(apiKey)-state.json", |
| 56 | + "klaviyo-\(apiKey)-events.plist", |
| 57 | + "klaviyo-\(apiKey)-people.plist" |
| 58 | + ] |
| 59 | + |
| 60 | + var migratedFiles: [String] = [] |
| 61 | + |
| 62 | + for fileName in filesToMigrate { |
| 63 | + let oldFilePath = oldDirectory.appendingPathComponent(fileName, isDirectory: false).path |
| 64 | + let newFilePath = newDirectory.appendingPathComponent(fileName, isDirectory: false).path |
| 65 | + |
| 66 | + // Only migrate files that exist |
| 67 | + if environment.fileClient.fileExists(oldFilePath) { |
| 68 | + do { |
| 69 | + try environment.fileClient.copyItem(oldFilePath, newFilePath) |
| 70 | + migratedFiles.append(fileName) |
| 71 | + } catch { |
| 72 | + environment.logger.error("Failed to copy \(fileName): \(error.localizedDescription)") |
| 73 | + // Rollback: remove all migrated files |
| 74 | + rollbackMigration(directory: newDirectory, files: migratedFiles) |
| 75 | + return oldDirectory |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + // Step 5: Verify migration by checking if state file exists and has data |
| 81 | + if migratedFiles.contains(stateFileName) { |
| 82 | + guard environment.fileClient.fileExists(newStateFile.path), |
| 83 | + let stateData = try? environment.dataFromUrl(newStateFile), |
| 84 | + !stateData.isEmpty else { |
| 85 | + environment.logger.error("Failed to verify migrated state file") |
| 86 | + // Rollback: remove all migrated files |
| 87 | + rollbackMigration(directory: newDirectory, files: migratedFiles) |
| 88 | + return oldDirectory |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + // Step 6: Cleanup old files |
| 93 | + for fileName in migratedFiles { |
| 94 | + let oldFilePath = oldDirectory.appendingPathComponent(fileName, isDirectory: false).path |
| 95 | + do { |
| 96 | + try environment.fileClient.removeItem(oldFilePath) |
| 97 | + } catch { |
| 98 | + environment.logger.error("Failed to remove old file \(fileName): \(error.localizedDescription)") |
| 99 | + // Continue anyway - migration succeeded, cleanup is best-effort |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + environment.logger.error("Successfully migrated \(migratedFiles.count) file(s) to Application Support") |
| 104 | + return newDirectory |
| 105 | +} |
| 106 | + |
| 107 | +/// Removes all migrated files from the new directory in case of migration failure |
| 108 | +/// - Parameters: |
| 109 | +/// - directory: The directory containing the files to remove |
| 110 | +/// - files: List of file names to remove |
| 111 | +private func rollbackMigration(directory: URL, files: [String]) { |
| 112 | + for fileName in files { |
| 113 | + let filePath = directory.appendingPathComponent(fileName, isDirectory: false).path |
| 114 | + if environment.fileClient.fileExists(filePath) { |
| 115 | + do { |
| 116 | + try environment.fileClient.removeItem(filePath) |
| 117 | + } catch { |
| 118 | + environment.logger.error("Failed to rollback file \(fileName): \(error.localizedDescription)") |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | +} |
0 commit comments