All files / lib/icloud/icloud-photos query-parser.ts

65.64% Statements 258/393
100% Branches 3/3
27.27% Functions 3/11
65.64% Lines 258/393

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 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 3931x 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 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 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 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 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              
/**
 * This file contains various helper classes, that help parsing responses from the iCloud API
 * These parsers will not try to validate data, only make it easily accessible
 */
 
import {iCPSError} from '../../../app/error/error.js';
import {AlbumAssets} from '../../photos-library/model/album.js';
import {QUERY_PARSER_ERR} from '../../../app/error/error-codes.js';
 
/**
 * Represents a resources returned from the API, called 'asset id' as per API. Can be found as a record on a CPLAsset or CPLMaster
 */
export class AssetID {
    /**
     * Checksum of the file:
     * 28 chars, probably base64 encoded (21 bytes / 168 bit)
     * Unsure how this is computed
     */
    fileChecksum: string;
    /**
     * File size in bytes
     */
    size: number;
    /**
     * Probably base64 encoded
     */
    wrappingKey: string;
    /**
     * An unknown checksum, different form fileChecksum.
     * Probably base64 encoded
     */
    referenceChecksum: string;
    /**
     * The backend URL for this asset
     */
    downloadURL: string;

    /**
     * Parses from a record sourced through the API
     * @param assetId - The plain JSON object, as returned by the API
     */
    static parseFromQuery(assetId: unknown): AssetID {
        if (!isAssetIDQuery(assetId)) {
            throw new iCPSError(QUERY_PARSER_ERR.ASSET_ID_FORMAT)
                .addContext(`query`, assetId);
        }

        const asset = new AssetID();
        asset.fileChecksum = assetId.value.fileChecksum;
        asset.size = assetId.value.size;
        asset.wrappingKey = assetId.value.wrappingKey;
        asset.referenceChecksum = assetId.value.referenceChecksum;
        asset.downloadURL = assetId.value.downloadURL;
        return asset;
    }
}
 
/**
 * Expected query response format for AssetID
 */
type AssetIDQuery = {
    value: {
        fileChecksum: string,
        size: number
        wrappingKey: string
        referenceChecksum: string
        downloadURL: string
    };
    type: string;
}
 
/**
 * Checks if the provided object is an AssetIDQuery
 * @param obj - The unknown object
 * @returns True if object conforms to AssetIDQuery
 */
function isAssetIDQuery(obj: unknown): obj is AssetIDQuery {
    return (obj as AssetIDQuery).value.fileChecksum !== undefined
        && (obj as AssetIDQuery).value.size !== undefined
        && (obj as AssetIDQuery).value.wrappingKey !== undefined
        && (obj as AssetIDQuery).value.referenceChecksum !== undefined
        && (obj as AssetIDQuery).value.downloadURL !== undefined
        && (obj as AssetIDQuery).type !== undefined
        && (obj as AssetIDQuery).type === `ASSETID`;
}
 
/**
 * CPLAsset is a single asset shown in Photos.app. This asset is linked to a Master and might include a current resource
 */
export class CPLAsset {
    /**
     * UUID of the asset
     */
    recordName: string;
    /**
     * Name of the application that made changes to the file
     */
    adjustmentType?: string;
    /**
     * The hash of a CPLMaster record
     */
    masterRef: string;
    /**
     * The 'current' resource
     */
    resource: AssetID;
    /**
     * The type of resource
     */
    resourceType: string;
    /**
     * If the resource is favorite, '1' if 'true'
     */
    favorite: number;
    /**
     * Timestamp in nanoseconds since epoch
     */
    modified: number;
 
    /**
     * The zone name where the asset resides
     */
    zoneName: string;
 
    /**
     * Parses from a record sourced through the API
     * @param cplRecord - The plain JSON object, as returned by the API
     */
    static parseFromQuery(cplRecord: unknown): CPLAsset {
        if (!isCPLAssetQuery(cplRecord)) {
            throw new iCPSError(QUERY_PARSER_ERR.CPLASSET_FORMAT)
                .addContext(`query`, cplRecord);
        }

        const asset = new CPLAsset();
        asset.recordName = cplRecord.recordName;
        asset.masterRef = cplRecord.fields.masterRef.value.recordName;
        asset.favorite = cplRecord.fields.isFavorite?.value ?? 0;
        asset.modified = cplRecord.modified.timestamp;
        asset.zoneName = cplRecord.zoneID.zoneName;

        if (cplRecord.fields.adjustmentType?.value) {
            asset.adjustmentType = cplRecord.fields.adjustmentType.value;
            if (cplRecord.fields.resJPEGFullRes) {
                asset.resource = AssetID.parseFromQuery(cplRecord.fields.resJPEGFullRes);
                asset.resourceType = cplRecord.fields.resJPEGFullFileType.value;
            } else if (cplRecord.fields.resVidFullRes) {
                asset.resource = AssetID.parseFromQuery(cplRecord.fields.resVidFullRes);
                asset.resourceType = cplRecord.fields.resVidFullFileType.value;
            }
        }

        return asset;
    }
}
 
/**
 * Expected query result for CPLAsset
 */
type CPLAssetQuery = {
    recordType: string
    recordName: string,
    modified: {
        timestamp: number
    }
    fields: {
        masterRef: {
            value: {
                recordName: string
            }
        },
        isFavorite?: {
            value: number
        }
        adjustmentType?: {
            value: string
        }
        // Set if adjustmentType exists
        resJPEGFullRes?: AssetIDQuery
        resJPEGFullFileType?: {
            value: string
        },
        // Set if adjustmentType exists
        resVidFullRes?: AssetIDQuery
        resVidFullFileType?: {
            value: string
        }
    }
    zoneID: {
        zoneName: string
    }
}
 
/**
 * Checks if the provided object is a CPLAsset Query
 * @param obj - The unknown object
 * @returns True if object conforms to CPLAssetQuery
 */
function isCPLAssetQuery(obj: unknown): obj is CPLAssetQuery {
    return (obj as CPLAssetQuery).recordType === `CPLAsset`
        && (obj as CPLAssetQuery).recordName !== undefined
        && (obj as CPLAssetQuery).fields.masterRef?.value.recordName !== undefined
        && (obj as CPLAssetQuery).recordName !== undefined
        && (obj as CPLAssetQuery).modified?.timestamp !== undefined
        && (obj as CPLAssetQuery).zoneID.zoneName !== undefined
        && (
            (obj as CPLAssetQuery).fields.adjustmentType?.value === undefined // Adjustment Type is optional, but if it is provided one of the below needs to be true
            || (obj as CPLAssetQuery).fields.adjustmentType?.value === `com.apple.video.slomo` // No additional asset for Slo-Mo videos
            || ((obj as CPLAssetQuery).fields.resJPEGFullRes !== undefined && isAssetIDQuery((obj as CPLAssetQuery).fields.resJPEGFullRes) && (obj as CPLAssetQuery).fields.resJPEGFullFileType?.value !== undefined)
            || ((obj as CPLAssetQuery).fields.resVidFullRes !== undefined && isAssetIDQuery((obj as CPLAssetQuery).fields.resVidFullRes) && (obj as CPLAssetQuery).fields.resVidFullFileType?.value !== undefined)
        );
}
 
/**
 * CPLMaster is the original file associated to a CPLAsset. This contains the original filename of the file and an unmodified version of the BLOB
 */
export class CPLMaster {
    /**
     * (Unique?) hash of the file
     */
    recordName: string;
    /**
     *  The master asset attached to this record
     */
    resource: AssetID;
    /**
     * The type of asset attached to this record
     */
    resourceType: string;
    /**
     * Base64 encoded filename
     */
    filenameEnc: String;
    /**
     * Timestamp in nanoseconds since epoch
     */
    modified: number;
 
    /**
     * The zone name where the asset resides
     */
    zoneName: string;
 
    // Can optionally have the following keys (indicating that this is a live photo, the following keys hold the information about the 'video' part of this):
    // resOriginalVidComplRes -> AssetID
    // resOriginalVidComplFileType -> Filetyp (seems to always be com.apple.quicktime-movie)
    //
    // Those two need to be parsed here and then processed in SyncEngine.convertCPLAssets
 
    // Can optionally have the following key (indicating it was edited)
    // mediaMetaDataEnc
    // unknown base64 format, eg:
    // "YnBsaXN0MDDbAQIDBAUGBwgJCgsMWGhpamtsurtlaFZ7RXhpZn1We1RJRkZ9WERQSVdpZHRoW1Byb2ZpbGVOYW1lW1BpeGVsSGVpZ2h0WkNvbG9yTW9kZWxce01ha2VyQXBwbGV9VURlcHRoWlBpeGVsV2lkdGhbT3JpZW50YXRpb25ZRFBJSGVpZ2h03xAjDQ4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TwZRBlYGWQZcBl4GYAZnBmkGbgZwBnUGdwh7CH0AAAAAAAACAQAAAAAAAAC8AAAAAAAAAAAAAAAAAAAIgA=="
 
    /**
     * Parses from a record sourced through the API
     * @param cplRecord - The plain JSON object, as returned by the API
     */
    static parseFromQuery(cplRecord: unknown): CPLMaster {
        if (!isCPLMasterQuery(cplRecord)) {
            throw new iCPSError(QUERY_PARSER_ERR.CPLMASTER_FORMAT)
                .addContext(`query`, cplRecord);
        }

        const master = new CPLMaster();
        master.recordName = cplRecord.recordName;
        master.modified = cplRecord.modified.timestamp;
        master.resource = AssetID.parseFromQuery(cplRecord.fields.resOriginalRes);
        master.resourceType = cplRecord.fields.resOriginalFileType.value; // Orig could also be JPEG to save storage
        master.filenameEnc = cplRecord.fields.filenameEnc.value;
        master.zoneName = cplRecord.zoneID.zoneName;
        return master;
    }
}
 
/**
 * Expected query result for CPLMaster
 */
type CPLMasterQuery = {
    recordType: string
    recordName: string
    modified: {
        timestamp: number
    }
    fields: {
        resOriginalRes: AssetID
        resOriginalFileType: {
            value: string
        }
        filenameEnc: {
            value: string
        }
    }
    zoneID: {
        zoneName: string
    }
}
 
/**
 * Checks if the provided object is a CPLMaster Query
 * @param obj - The unknown object
 * @returns True if object conforms to CPLMasterQuery
 */
function isCPLMasterQuery(obj: unknown): obj is CPLMasterQuery {
    return (obj as CPLMasterQuery).recordType === `CPLMaster`
        && (obj as CPLMasterQuery).recordName !== undefined
        && (obj as CPLMasterQuery).modified.timestamp !== undefined
        && (obj as CPLMasterQuery).fields.resOriginalRes !== undefined && isAssetIDQuery((obj as CPLMasterQuery).fields.resOriginalRes)
        && (obj as CPLMasterQuery).fields.resOriginalFileType.value !== undefined
        && (obj as CPLMasterQuery).fields.filenameEnc.value !== undefined
        && (obj as CPLMasterQuery).zoneID.zoneName !== undefined;
}
 
/**
 * CPLAlbum is the representation of either a folder or an album. It might contains a list of assets.
 */
export class CPLAlbum {
    /**
     * UUID of the album
     */
    recordName: string;
    /**
     * AlbumType representation (e.g. Album or Folder)
     */
    albumType: number;
    /**
     * Base64 encoded album name
     */
    albumNameEnc: string;
    /**
     * The UUID of the parent folder
     */
    parentId: string;
    /**
     * Timestamp in nanoseconds since epoch
     */
    modified: number;
    /**
     * A list of assets contained in this album
     */
    assets?: AlbumAssets;
 
    static parseFromQuery(cplRecord: unknown, assets?: AlbumAssets): CPLAlbum {
        if (!isCPLAlbumQuery(cplRecord)) {
            throw new iCPSError(QUERY_PARSER_ERR.CPLALBUM_FORMAT)
                .addContext(`query`, cplRecord);
        }

        const album = new CPLAlbum();
        album.recordName = cplRecord.recordName;
        album.modified = cplRecord.modified.timestamp;
        album.albumType = cplRecord.fields.albumType.value;
        album.albumNameEnc = cplRecord.fields.albumNameEnc.value;
        album.parentId = cplRecord.fields.parentId?.value;
        album.assets = assets;
        return album;
    }
}
 
/**
 * Expected query result for CPLAlbum
 */
 type CPLAlbumQuery = {
    recordType: string
    recordName: string
    modified: {
        timestamp: number
    }
    fields: {
        albumType: {
            value: number
        }
        albumNameEnc: {
            value: string
        }
        parentId?: {
            value: string
        }
    }
}
 
/**
 * Checks if the provided object is a CPLAlbum Query
 * @param obj - The unknown object
 * @returns True if object conforms to CPLAlbumQuery
 */
function isCPLAlbumQuery(obj: unknown): obj is CPLAlbumQuery {
    return (obj as CPLAlbumQuery).recordType === `CPLAlbum`
        && (obj as CPLAlbumQuery).recordName !== undefined
        && (obj as CPLAlbumQuery).modified.timestamp !== undefined
        && (obj as CPLAlbumQuery).fields.albumType.value !== undefined
        && (obj as CPLAlbumQuery).fields.albumNameEnc.value !== undefined;
}