All files / app icloud-app.ts

100% Statements 338/338
89.36% Branches 42/47
100% Functions 22/22
100% Lines 338/338

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 3381x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 1x 1x 2x 1x 1x 1x 4x 4x 4x 4x 4x 4x 4x 2x 2x 2x 2x 2x 1x 1x 2x 2x 2x 2x 4x 1x 1x 1x 1x 1x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 7x 7x 7x 7x 7x 1x 1x 1x 6x 6x 6x 5x 7x 1x 1x 1x 7x 25x 25x 25x 25x 25x 7x 7x 7x 25x 25x 25x 25x 25x 25x 3x 3x 2x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 25x 25x 25x 25x 25x 25x 4x 4x 1x 1x 3x 3x 4x 1x 1x 1x 2x 2x 2x 25x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 3x 3x 1x 1x 1x 3x 2x 2x 3x 3x 3x 3x 3x 3x 3x 1x 1x 1x 1x 1x 1x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 4x 4x 4x 4x 4x 1x 1x 4x 4x 4x 2x 2x 4x 4x 8x 8x 8x 8x 8x 4x 4x 4x 8x 1x 1x 1x 1x 1x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 2x 2x 2x 2x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 3x 3x 3x 3x 3x 2x 2x 2x 3x
import {iCloud} from "../lib/icloud/icloud.js";
import {PhotosLibrary} from "../lib/photos-library/photos-library.js";
import * as fs from 'fs';
import {ArchiveEngine} from "../lib/archive-engine/archive-engine.js";
import {SyncEngine} from "../lib/sync-engine/sync-engine.js";
import {iCPSError} from "./error/error.js";
import {Asset} from "../lib/photos-library/model/asset.js";
import {Album} from "../lib/photos-library/model/album.js";
import path from "path";
import {EventEmitter} from "events";
import * as ICLOUD from "../lib/icloud/constants.js";
import * as Logger from '../lib/logger.js';
import {Cron} from "croner";
import {HANDLER_EVENT} from "./event/error-handler.js";
import {EventHandler, registerObjectsToEventHandlers, removeObjectsFromEventHandlers} from "./event/event-handler.js";
import {APP_ERR, AUTH_ERR, LIBRARY_ERR} from "./error/error-codes.js";
import {iCPSAppOptions} from "./factory.js";
 
/**
 * Filename for library lock file located in DATA_DIR
 */
export const LIBRARY_LOCK_FILE = `.library.lock`;
 
export abstract class iCPSApp {
    options: iCPSAppOptions;
 
    constructor(options: iCPSAppOptions) {
        this.options = options;
        // Needs to be done here so all future objects use it / including handlers and everything created down the chain
        Logger.setupLogger(this.options);
    }
 
    /**
     * Executes this app
     * @param eventHandlers - A list of EventHandlers that will be registering relevant objects
     */
    abstract run(...eventHandlers: EventHandler[]): Promise<unknown>
}
 
export class DaemonAppEvents extends EventEmitter {
    static EVENTS = {
        'SCHEDULED': `scheduled`, // Next execution
        'START': `start`,
        'DONE': `done`, // Next execution
        'RETRY': `retry`, // Next execution
    };
}
 
/**
 * This app will allow running in scheduled daemon mode - where a sync is executed based on a cron schedule
 */
export class DaemonApp extends iCPSApp {
    /**
     * Holds the cron job
     */
    job?: Cron;
 
    /**
     * EventEmitter to notify EventHandlers
     */
    event: DaemonAppEvents;
 
    /**
     * Builds the app
     * @param options - CLI Options for the app
     */
    constructor(options: iCPSAppOptions) {
        super(options);
        this.event = new DaemonAppEvents();
    }
 
    /**
     * Schedule the synchronization based on the provided cron string
     * @returns Once the job has been scheduled
     */
    async run(...eventHandlers: EventHandler[]) {
        registerObjectsToEventHandlers(eventHandlers, this.event);
        this.job = new Cron(this.options.schedule, async () => {
            await this.performScheduledSync(eventHandlers);
        });
        this.event.emit(DaemonAppEvents.EVENTS.SCHEDULED, this.job?.next());
    }
 
    /**
     * Perform a scheduled sync using the provided event handlers
     * @param eventHandlers - Event handlers of daemon app
     * @param syncApp - Parametrized for testability - will be freshly initiated if omitted
     */
    async performScheduledSync(eventHandlers: EventHandler[], syncApp: SyncApp = new SyncApp(this.options)) {
        try {
            this.event.emit(DaemonAppEvents.EVENTS.START);
            await syncApp.run(...eventHandlers);
            this.event.emit(DaemonAppEvents.EVENTS.DONE, this.job?.next());
        } catch (err) {
            this.event.emit(HANDLER_EVENT, new iCPSError(APP_ERR.DAEMON).addCause(err));
            this.event.emit(DaemonAppEvents.EVENTS.RETRY, this.job?.next());
        } finally { // Cleaning up
            syncApp = undefined;
        }
    }
}
 
/**
 * This is the base application class which will setup and manage the iCloud connection and local Photos Library
 */
export abstract class iCloudApp extends iCPSApp {
    /**
     * This sessions' iCloud object
     */
    icloud: iCloud;
 
    /**
     * Creates and sets up the necessary infrastructure
     * @param options - The parsed CLI options
     */
    constructor(options: iCPSAppOptions) {
        super(options);
 
        // It's crucial for the data dir to exist, create if it doesn't
        if (!fs.existsSync(this.options.dataDir)) {
            fs.mkdirSync(this.options.dataDir, {"recursive": true});
        }
 
        // Creating necessary objects for this scope
        this.icloud = new iCloud(this);
    }
 
    /**
     * This function acquires the library lock and establishes the iCloud connection.
     * @param eventHandlers - A list of EventHandlers that will be registering relevant objects
     * @returns A promise that resolves once the iCloud service is fully available
     * @throws A iCPSError in case an error occurs
     */
    async run(...eventHandlers: EventHandler[]): Promise<unknown> {
        registerObjectsToEventHandlers(eventHandlers, this.icloud, this.icloud.mfaServer);
 
        try {
            await this.acquireLibraryLock();
        } catch (err) {
            throw new iCPSError(LIBRARY_ERR.LOCK_ACQUISITION)
                .addCause(err);
        }
 
        try {
            await this.icloud.authenticate();
            return;
        } catch (err) {
            throw new iCPSError(AUTH_ERR.FAILED)
                .addCause(err);
        }
    }
 
    /**
     * Removes all established event listeners and releases the library lock
     */
    async clean() {
        removeObjectsFromEventHandlers(this.icloud, this.icloud.mfaServer);
        await this.releaseLibraryLock();
    }
 
    /**
     * Tries to acquire the lock for the local library to execute a sync
     * @throws A Library Error, if the lock could not be acquired
     */
    async acquireLibraryLock() {
        const lockFilePath = path.join(this.options.dataDir, LIBRARY_LOCK_FILE);
        if (fs.existsSync(lockFilePath)) {
            if (!this.options.force) {
                const lockingProcess = (await fs.promises.readFile(lockFilePath, `utf-8`)).toString();
                throw new iCPSError(LIBRARY_ERR.LOCKED)
                    .addMessage(`Locked by PID ${lockingProcess}`);
            }
 
            await fs.promises.rm(lockFilePath, {"force": true});
        }
 
        await fs.promises.writeFile(lockFilePath, process.pid.toString(), {"encoding": `utf-8`});
    }
 
    /**
     * Tries to release the lock for the local library after completing a sync
     * @throws A Library Error, if the lock could not be released
     */
    async releaseLibraryLock() {
        const lockFilePath = path.join(this.options.dataDir, LIBRARY_LOCK_FILE);
        if (!fs.existsSync(lockFilePath)) {
            return;
        }
 
        const lockingProcess = (await fs.promises.readFile(lockFilePath, `utf-8`)).toString();
        if (lockingProcess !== process.pid.toString() && !this.options.force) {
            throw new iCPSError(LIBRARY_ERR.LOCKED)
                .addMessage(`Locked by PID ${lockingProcess}`);
        }
 
        await fs.promises.rm(lockFilePath, {"force": true});
    }
}
 
/**
 * This application will print the locally stored token, acquire a new one (if necessary) and print it to the CLI
 */
export class TokenApp extends iCloudApp {
    /**
     * This function will validate the currently stored account token and print it afterwards
     * @param eventHandlers - A list of EventHandlers that will be registering relevant objects
     * @returns A promise that resolves once the operation has been completed
     * @throws A TokenError in case an error occurs
     */
    async run(...eventHandlers: EventHandler[]): Promise<unknown> {
        try {
            await super.run(...eventHandlers);
            this.icloud.auth.validateAccountTokens();
            this.icloud.emit(ICLOUD.EVENTS.TOKEN, this.icloud.auth.iCloudAccountTokens.trustToken);
            return;
        } catch (err) {
            throw new iCPSError(APP_ERR.TOKEN)
                .addCause(err);
        } finally {
            // Only if this is the initiated class, release the lock
            if (this.constructor.name === TokenApp.name) {
                await this.clean();
            }
        }
    }
}
 
/**
 * This application will perform a synchronization of the provided Photos Library using the authenticated iCloud connection
 */
export class SyncApp extends iCloudApp {
    /**
     * This sessions' Photos Library object
     */
    photosLibrary: PhotosLibrary;
 
    /**
     * This sessions' Sync Engine object
     */
    syncEngine: SyncEngine;
 
    /**
     * Creates and sets up the necessary infrastructure for this app
     * @param options - The parsed CLI options
     */
    constructor(options: iCPSAppOptions) {
        super(options);
        this.photosLibrary = new PhotosLibrary(this);
        this.syncEngine = new SyncEngine(this);
    }
 
    /**
     * Runs the synchronization of the local Photo Library
     * @param eventHandlers - A list of EventHandlers that will be registering relevant objects
     * @returns A Promise that resolves to a tuple containing a list of assets as fetched from the remote state. It can be assumed that this reflects the local state (given a warning free execution of the sync).
     * @throws A SyncError in case an error occurs
     */
    async run(...eventHandlers: EventHandler[]): Promise<unknown> {
        registerObjectsToEventHandlers(eventHandlers, this.photosLibrary, this.syncEngine);
        try {
            await super.run(...eventHandlers);
            return await this.syncEngine.sync();
        } catch (err) {
            throw new iCPSError(APP_ERR.SYNC)
                .addCause(err);
        } finally {
            // If this is the initiated class, release the lock
            if (this.constructor.name === SyncApp.name) {
                await this.clean();
            }
        }
    }
 
    /**
     * Removes all established event listeners and releases the library lock
     */
    async clean() {
        await super.clean();
        removeObjectsFromEventHandlers(this.photosLibrary, this.syncEngine);
    }
}
 
/**
 * This application will first perform a synchronization and then archive a given local path
 */
export class ArchiveApp extends SyncApp {
    /**
     * This sessions' Archive Engine object
     */
    archiveEngine: ArchiveEngine;
 
    /**
     * The local path to be archived
     */
    archivePath: string;
 
    /**
     * Creates and sets up the necessary infrastructure for this app
     * @param options - The parsed CLI options
     * @param archivePath - The path to the folder that should get archived
     */
    constructor(options: iCPSAppOptions, archivePath: string) {
        super(options);
        this.archivePath = archivePath;
        this.archiveEngine = new ArchiveEngine(this);
    }
 
    /**
     * This function will first perform a synchronization run and then attempt to archive the provided path
     * @param eventHandlers - A list of EventHandlers that will be registering relevant objects
     * @returns A promise that resolves once the operation has finished
     * @throws An ArchiveError in case an error occurs
     */
    async run(...eventHandlers: EventHandler[]): Promise<unknown> {
        registerObjectsToEventHandlers(eventHandlers, this.archiveEngine);
        try {
            const [remoteAssets] = await super.run(...eventHandlers) as [Asset[], Album[]];
            await this.archiveEngine.archivePath(this.archivePath, remoteAssets);
            return;
        } catch (err) {
            throw new iCPSError(APP_ERR.ARCHIVE)
                .addCause(err).addContext(`archivePath`, this.archivePath);
        } finally {
            // If this is the initiated class, release the lock
            if (this.constructor.name === ArchiveApp.name) {
                await this.clean();
            }
        }
    }
 
    /**
     * Removes all established event listeners and releases the library lock
     */
    async clean() {
        await super.clean();
        removeObjectsFromEventHandlers(this.archiveEngine);
    }
}