All files / lib/sync-engine helper.ts

100% Statements 258/258
100% Branches 44/44
100% Functions 9/9
100% Lines 258/258

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 2581x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 24x 24x 24x 24x 8x 8x 16x 16x 16x 1x 1x 1x 1x 1x 1x 1x 5x 5x 5x 206x 5x 5x 5x 206x 206x 206x 206x 206x 206x 206x 206x 206x 206x 205x 206x 4x 4x 206x 1x 1x 1x 1x 1x 1x 5x 5x 5x 1x 1x 1x 1x 1x 1x 2x 2x 2x 8x 8x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 24x 24x 24x 1x 1x 1x 1x 1x 1x 1x 1x 111x 111x 6x 6x 105x 111x 22x 22x 83x 111x 23x 23x 60x 60x 60x 60x 60x 111x 4x 4x 111x 1x 1x 1x 1x 1x 1x 1x 18x 18x 18x 18x 18x 18x 48x 48x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 18x 18x 18x 18x 18x 18x 1x 1x 1x 1x 1x 1x 1x 6x 6x 6x 6x 8x 8x 2x 2x 2x 6x 6x 6x 6x 6x 6x 1x 1x 1x 1x 1x 1x 1x 4x 4x 4x 4x 4x 4x 4x 1x 1x 1x 1x 1x 1x 1x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 24x 24x 10x 10x 10x 10x 10x 10x 10x 9x 9x 9x 9x
import path from "path";
import {LIBRARY_ERR} from "../../app/error/error-codes.js";
import {iCPSError} from "../../app/error/error.js";
import {CPLAlbum, CPLAsset, CPLMaster} from "../icloud/icloud-photos/query-parser.js";
import {Album} from "../photos-library/model/album.js";
import {Asset, AssetType} from "../photos-library/model/asset.js";
import {PEntity, PLibraryEntities, PLibraryProcessingQueues} from "../photos-library/model/photos-entity.js";
import {Resources} from "../resources/main.js";
import {iCPSEventRuntimeWarning} from "../resources/events-types.js";
 
/**
 * This object exposes various static helpers required to perform a sync
 */
export const SyncEngineHelper = {
    /**
     * @see {@link convertCPLAlbums}
     */
    convertCPLAlbums,
    /**
     * @see {@link convertCPLAssets}
     */
    convertCPLAssets,
    /**
     * @see {@link sortQueue}
     */
    sortQueue,
    /**
     * @see {@link compareQueueElements}
     */
    compareQueueElements,
    /**
     * @see {@link getProcessingQueues}
     */
    getProcessingQueues,
    /**
     * @see {@link resolveHierarchicalDependencies}
     */
    resolveHierarchicalDependencies,
    /**
     * @see {@link removeDuplicateAssets}
     */
    removeDuplicateAssets,
    /**
     * @see {@link deferAssetDeletions}
     */
    deferAssetDeletions,
    /**
     * @see {@link settleAll}
     */
    settleAll,
};
 
/**
 * Waits for all provided promises to settle, before resolving or rejecting
 * In contrast to `Promise.all`, no work is left running in the background once this rejects - which would otherwise outlive a failed sync attempt and could interfere with the next one
 * @param promises - The promises to wait for
 * @returns A promise that resolves to the values of all promises, in order
 * @throws The rejection reason of the first rejected promise (in order of the input), once all promises have settled
 */
async function settleAll<T extends readonly unknown[]>(promises: readonly [...{[K in keyof T]: Promise<T[K]>}]): Promise<T> {
    const results: PromiseSettledResult<unknown>[] = await Promise.allSettled(promises);
    const rejected = results.find((result): result is PromiseRejectedResult => result.status === `rejected`);
    if (rejected) {
        throw rejected.reason;
    }
 
    return results.map(result => (result as PromiseFulfilledResult<unknown>).value) as unknown as T;
}
 
/**
 * Matches CPLAsset/CPLMaster pairs and parses their associated Asset(s)
 * @param cplAssets - The given asset
 * @param cplMasters - The given master
 * @returns An array of all containing assets
 */
function convertCPLAssets(cplAssets: CPLAsset[], cplMasters: CPLMaster[]): Asset[] {
    const cplMasterRecords: Record<string, CPLMaster> = {};
    cplMasters.forEach(masterRecord => {
        cplMasterRecords[masterRecord.recordName] = masterRecord;
    });
    const remoteAssets: Asset[] = [];
    cplAssets.forEach(asset => {
        const master: CPLMaster = cplMasterRecords[asset.masterRef];
        try {
            const parsedOrigFilename = path.parse(Buffer.from(master.filenameEnc, `base64`).toString());
 
            const origFilename = parsedOrigFilename.name;
            const origExt = parsedOrigFilename.ext;
 
            if (master?.resource && master?.resourceType) {
                remoteAssets.push(Asset.fromCPL(master.resource, master.resourceType, origExt, master.modified, origFilename, AssetType.ORIG, asset.recordName, asset.favorite, master.zoneName));
            }
 
            if (asset?.resource && asset?.resourceType) {
                remoteAssets.push(Asset.fromCPL(asset.resource, asset.resourceType, origExt, asset.modified, origFilename, AssetType.EDIT, asset.recordName, asset.favorite, asset.zoneName));
            }
        } catch (err) {
            if (err instanceof iCPSError && err.code === LIBRARY_ERR.UNKNOWN_FILETYPE_DESCRIPTOR.code) {
                Resources.emit(iCPSEventRuntimeWarning.FILETYPE_ERROR, (err.context as any).extension, (err.context as any).descriptor);
            }
 
            Resources.emit(iCPSEventRuntimeWarning.ICLOUD_LOAD_ERROR, err, asset, master);
        }
    });
    return remoteAssets;
}
 
/**
 * Transforms a CPLAlbum into an array of Albums
 * @param cplAlbums - The given CPL Album
 * @returns Once settled, a completely populated Album array
 */
function convertCPLAlbums(cplAlbums: CPLAlbum[]) : Album[] {
    const remoteAlbums: Album[] = [];
    for (const cplAlbum of cplAlbums) {
        remoteAlbums.push(Album.fromCPL(cplAlbum));
    }
 
    return remoteAlbums;
}
 
/**
 * This function will sort a given queue. The sort is performed on a copy of the array, referencing the same objects.
 * Order is defined as follows: For every album in the array, its parent's index (if exists) is always smaller than the index of the album (parent is 'in front' of all of its children)
 * @param unsortedQueue - The unsorted queue.
 * @returns A sorted queue
 */
function sortQueue(unsortedQueue: Album[]): Album[] {
    return [...unsortedQueue].sort((a, b) => SyncEngineHelper.compareQueueElements(unsortedQueue, a, b));
}
 
/**
 * Compares two queue elements, based on the specification of compareFn of Array.sort
 * @param fullQueue - The full queue necessary to check for ancestors
 * @param a - The first element
 * @param b - The second element
 * @returns - Returns a negative value if the first element is less than the second element, zero if they're equal, and a positive value otherwise.
 */
function compareQueueElements(fullQueue: Album[], a: Album, b: Album): number {
    if (a.getUUID() === b.getUUID()) {
        return 0;
    }
 
    if (a.hasAncestor(b, fullQueue)) {
        return 1; // B is ancestor, therefore his index needs to be bigger
    }
 
    if (b.hasAncestor(a, fullQueue)) {
        return -1; // A is ancestor, therefore his index needs to be bigger
    }
 
    try {
        const distanceToRootA = Album.distanceToRoot(a, fullQueue);
        const distanceToRootB = Album.distanceToRoot(b, fullQueue);
        return distanceToRootA - distanceToRootB; // Provide distance based on depth
    } catch (_err) {
        return 0; // If there is a broke in the link, return them as equal
    }
}
 
/**
 * This function diffs two entity arrays (can be either Albums or Assets) and returns the corresponding processing queue
 * @param remoteEntities - The entities fetched from a remote state
 * @param _localEntities - The local entities as read from disk
 * @returns A processing queue, containing the entities that needs to be deleted, added and kept. In the case of albums, this will not take hierarchical dependencies into consideration
 */
function getProcessingQueues<T>(remoteEntities: PEntity<T>[], _localEntities: PLibraryEntities<T>): PLibraryProcessingQueues<T> {
    const localEntities = {..._localEntities};
    // This.logger.debug(`Getting processing queues`);
    const toBeAdded: T[] = [];
    const toBeKept: T[] = [];
    remoteEntities.forEach(remoteEntity => {
        const localEntity = localEntities[remoteEntity.getUUID()];
        if (!localEntity || !remoteEntity.equal(localEntity)) {
            // No local entity OR local entity does not match remote entity -> Remote asset will be added & local asset will not be removed from deletion queue
            Resources.logger(`SyncHelper`).debug(`Adding new remote entity ${remoteEntity.getDisplayName()}`);
            // Making sure entities have all relevant properties
            toBeAdded.push(remoteEntity.apply(localEntity));
        } else {
            // Local asset matches remote asset, nothing to do, but preventing local asset to be deleted
            Resources.logger(`SyncHelper`).debug(`Keeping existing local entity ${remoteEntity.getDisplayName()}`);
            toBeKept.push(remoteEntity.apply(localEntity));
            delete localEntities[remoteEntity.getUUID()];
        }
    });
    // The original library should only hold those records, that have not been referenced by the remote state, removing them
    const toBeDeleted = Object.values(localEntities);
    Resources.logger(`SyncHelper`).debug(`Got ${toBeDeleted.length} remaining local entities that need to be deleted: ${toBeDeleted.map(entity => (entity as any).getDisplayName()).join(`, `)}`);
    return [toBeDeleted, toBeAdded, toBeKept];
}
 
/**
 * Multiple remote assets can reference the same file (e.g. duplicated assets sharing the same master). Downloading such a file concurrently, or re-writing a kept file, leads to partially written or mismatching files.
 * This function makes sure that every file is only written once, by removing assets from the addition queue whose file is already kept or added
 * @param queues - The asset processing queue
 * @returns The asset processing queue without duplicate additions
 */
function removeDuplicateAssets(queues: PLibraryProcessingQueues<Asset>): PLibraryProcessingQueues<Asset> {
    const [toBeDeleted, toBeAdded, toBeKept] = queues;
    const filePaths = new Set(toBeKept.map(asset => asset.getAssetFilePath()));
    const uniqueToBeAdded = toBeAdded.filter(asset => {
        const filePath = asset.getAssetFilePath();
        if (filePaths.has(filePath)) {
            Resources.logger(`SyncHelper`).debug(`Skipping duplicate remote entity ${asset.getDisplayName()}`);
            return false;
        }
 
        filePaths.add(filePath);
        return true;
    });
    return [toBeDeleted, uniqueToBeAdded, toBeKept];
}
 
/**
 * While iCloud reports a running indexing job, the remote state might be incomplete - deleting all local assets missing from it could remove assets that still exist remotely
 * This function keeps those assets and only deletes assets that are replaced by an addition (e.g. because their modification date changed)
 * @param queues - The asset processing queue
 * @returns The asset processing queue, where deletions of assets that are not replaced are moved to the kept queue
 */
function deferAssetDeletions(queues: PLibraryProcessingQueues<Asset>): PLibraryProcessingQueues<Asset> {
    const [toBeDeleted, toBeAdded, toBeKept] = queues;
    const replacedAssets = new Set(toBeAdded.map(asset => asset.getUUID()));
    const replaced = toBeDeleted.filter(asset => replacedAssets.has(asset.getUUID()));
    const deferred = toBeDeleted.filter(asset => !replacedAssets.has(asset.getUUID()));
    return [replaced, toBeAdded, [...toBeKept, ...deferred]];
}
 
/**
 * If an ancestor (not parent) of an Album is marked for deletion, the album needs to be moved (aka deleted & added), since it did not change from a diffing perspective (same parent)
 * @param queues - The album processing queue for the albums
 * @param localAlbumEntities - The local state
 * @returns Updated processing queues with resolved hierarchical dependencies, ready for processing
 */
function resolveHierarchicalDependencies(queues: PLibraryProcessingQueues<Album>, localAlbumEntities: PLibraryEntities<Album>): PLibraryProcessingQueues<Album> {
    // This.logger.debug(`Resolving hierarchical dependencies in album processing queues...`);
    const toBeDeleted = queues[0];
    const toBeAdded = queues[1];
    let toBeKept = queues[2];
 
    // Storing the indices of items that need moving, so they can later be removed from the toBeKept Array
    const removeIndexesFromKept: number[] = [];
    const localAlbums = Object.values(localAlbumEntities);
 
    // Go over all kept albums
    toBeKept.forEach((keptAlbum, index) => {
        // Check if any of the deleted is an ancestor of the kept album
        if (toBeDeleted.some(deletedAlbum => keptAlbum.hasAncestor(deletedAlbum, localAlbums))) {
            // This.logger.debug(`Found hierarchical dependency for album ${keptAlbum.getDisplayName()}`);
            // This means that this kept album actually needs to be deleted & added
            toBeDeleted.push(keptAlbum);
            toBeAdded.push(keptAlbum);
            // Marking the item to be removed from kept
            removeIndexesFromKept.push(index);
        }
    });
    toBeKept = toBeKept.filter((_album, index) => removeIndexesFromKept.indexOf(index) === -1);
    return [toBeDeleted, toBeAdded, toBeKept];
}