All files / lib/sync-engine/helpers fetchAndLoad-helpers.ts

100% Statements 58/58
100% Branches 9/9
100% Functions 2/2
100% Lines 58/58

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 581x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 203x 2x 2x 2x 203x 203x 203x 203x 203x 203x 203x 203x 203x 203x 202x 203x 4x 4x 203x 1x 1x 1x 1x 1x 1x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 8x 8x 1x 1x 1x
import path from "path";
import {SYNC_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";
 
/**
 * 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
 */
export function convertCPLAssets(cplAssets: CPLAsset[], cplMasters: CPLMaster[]): Asset[] {
    const cplMasterRecords = {};
    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) {
            // In case missing filetype descriptor is thrown, adding asset context to error
            throw new iCPSError(SYNC_ERR.CONVERSION)
                .addCause(err)
                .addContext(`cplAsset`, asset)
                .addContext(`cplMaster`, 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
 */
export function convertCPLAlbums(cplAlbums: CPLAlbum[]) : Album[] {
    const remoteAlbums: Album[] = [];
    for (const cplAlbum of cplAlbums) {
        remoteAlbums.push(Album.fromCPL(cplAlbum));
    }
 
    return remoteAlbums;
}