All files / lib/photos-library/model asset.ts

100% Statements 350/350
93.33% Branches 28/30
100% Functions 13/13
100% Lines 350/350

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 3501x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 1x 1x 1x 1x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 207x 207x 207x 207x 207x 207x 207x 207x 207x 207x 207x 207x 207x 207x 207x 413x 413x 413x 413x 413x 413x 413x 413x 413x 31x 31x 31x 31x 31x 31x 31x 31x 413x 413x 413x 413x 413x 413x 413x 14x 14x 14x 14x 14x 14x 413x 413x 413x 413x 413x 413x 413x 23x 23x 413x 413x 413x 413x 413x 413x 413x 38x 38x 38x 38x 38x 413x 413x 413x 413x 413x 413x 276x 276x 276x 276x 276x 413x 413x 413x 413x 413x 413x 118x 118x 118x 118x 118x 413x 413x 413x 413x 413x 413x 74x 74x 413x 413x 413x 413x 413x 413x 413x 413x 32x 32x 32x 32x 2x 2x 2x 2x 30x 32x 2x 2x 2x 28x 32x 12x 12x 12x 12x 16x 16x 16x 413x 413x 413x 413x 413x 413x 413x 413x 413x 40x 40x 40x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 413x 107x 107x 413x
import path from 'path';
import {AssetID} from '../../icloud/icloud-photos/query-parser.js';
import {FileType} from './file-type.js';
import {Stats} from 'fs';
import fs from 'fs/promises';
import {PEntity} from './photos-entity.js';
import {iCPSError} from '../../../app/error/error.js';
import {LIBRARY_ERR} from '../../../app/error/error-codes.js';
import {Zones} from '../../icloud/icloud-photos/query-builder.js';
import {PRIMARY_ASSET_DIR, SHARED_ASSET_DIR} from '../constants.js';
 
/**
 * Representing the possible asset types
 */
export enum AssetType {
    /**
     * Shows that this is the original file
     */
    ORIG = 0,
    /**
     * Shows that this is the latest edit
     */
    EDIT = 1,
    /**
     * Shows that this is a live photo
     */
    LIVE = 2
}
/**
 * This class represents an Asset in the Photo Library
 */
export class Asset implements PEntity<Asset> {
    /**
     * Checksum of the asset
     */
    fileChecksum: string;
    /**
     * File size in bytes of this asset
     */
    size: number;
    /**
     * Modified timestamp as epoch timestamp (ms since epoch)
     */
    modified: number;
    /**
     * The file type of this asset
     */
    fileType: FileType;
    /**
     * Shows which version of the asset this is
     */
    assetType: AssetType;
    /**
     * The original filename of this asset
     */
    origFilename: string;
 
    /**
     * The zone this file is belonging to
     */
    zone: Zones;
 
    /**
     * The wrapping key of this asset (unknown usage, taken from backend, only present if fetched from CPL)
     */
    wrappingKey?: string;
    /**
     * The reference checksum of this asset (unknown usage, taken from backend, only present if fetched from CPL)
     */
    referenceChecksum?: string;
    /**
     * The download URL of this asset (only present if fetched from CPL)
     */
    downloadURL?: string;
    /**
     * Record name of the associated CPL Asset
     */
    recordName?: string;
    /**
     * Flag, if this asset is favorite
     */
    isFavorite?: boolean;
 
    /**
     * Creates a new Asset object
     * @param fileChecksum -
     * @param size -
     * @param fileType -
     * @param modified -
     * @param zone - Which zone is this asset belonging to
     * @param assetType - If this asset is the original or an edit
     * @param origFilename - The original filename, extracted from the parent object
     * @param wrappingKey -
     * @param referenceChecksum -
     * @param downloadURL -
     */
    constructor(fileChecksum: string, size: number, fileType: FileType, modified: number, zone: Zones, assetType?: AssetType, origFilename?: string, wrappingKey?: string, referenceChecksum?: string, downloadURL?: string, recordName?: string, isFavorite?: boolean) {
        this.fileChecksum = fileChecksum;
        this.size = size;
        this.fileType = fileType;
        this.modified = modified;
        this.zone = zone;
        this.assetType = assetType;
        this.origFilename = origFilename;
        this.wrappingKey = wrappingKey;
        this.referenceChecksum = referenceChecksum;
        this.downloadURL = downloadURL;
        this.recordName = recordName;
        this.isFavorite = isFavorite;
    }
 
    /**
     * Creates an Asset from the information provided by the backend
     * @param asset - The AssetID object returned from the backend
     * @param fileTypeDescriptor - The assetType string, describing the filetype
     * @param fileTypeExt - The assetTypes's extension as derived from the encoded filename
     * @param modified - The modified date as returned from the backend (in ms since epoch)
     * @param origFilename - The original filename, extracted from the parent object
     * @param assetType - If this asset is the original or an edit
     * @param zone - Specifies the zone this asset is belonging to
     * @returns An Asset based on the backend objects
     */
    static fromCPL(asset: AssetID, fileTypeDescriptor: string, fileTypeExt: string, modified: number, origFilename: string, assetType: AssetType, recordName: string, isFavorite: number, zone: string): Asset {
        return new Asset(
            asset.fileChecksum,
            asset.size,
            FileType.fromAssetType(fileTypeDescriptor, fileTypeExt),
            modified,
            zone === `PrimarySync` ? Zones.Primary : Zones.Shared,
            assetType,
            origFilename,
            asset.wrappingKey,
            asset.referenceChecksum,
            asset.downloadURL,
            recordName,
            isFavorite === 1,
        );
    }
 
    /**
     * Creates an Asset from a given file
     * @param fileName - The file name of the file
     * @param stats - The metadata associated with the file
     * @param zone - Specifies the zone this asset is belonging to
     * @returns An Asset based on the file information
     */
    static fromFile(fileName: string, stats: Stats, zone: Zones): Asset {
        return new Asset(
            Buffer.from(path.basename(fileName, path.extname(fileName)), `base64url`).toString(`base64`),
            stats.size,
            FileType.fromExtension(path.extname(fileName)),
            stats.mtimeMs,
            zone,
        );
    }
 
    /**
     * Compares the provided asset to this asset instance
     * @param asset - The asset to compare to
     * @returns True if provided asset matches this instance (based on fileChecksum, fileType, size and modified timestamp)
     */
    equal(asset: Asset): boolean {
        return asset
                && this.fileChecksum === asset.fileChecksum
                && this.fileType.equal(asset.fileType)
                && this.size === asset.size
                && this.withinRange(this.modified, asset.modified, 10);
    }
 
    /**
     * Should only be called on a 'remote' entity. Will apply the local entity's properties to the remote one
     * @param _localEntity - The local entity
     * @returns This object with the applied properties
     */
    apply(_localEntity: Asset): Asset {
        return this;
    }
 
    /**
     *
     * @param dataDir - The photos data dir
     * @returns The full asset file path under the provided directory
     */
    getAssetFilePath(dir: string) {
        return path.format({
            "dir": path.join(dir, this.zone === Zones.Primary ? PRIMARY_ASSET_DIR : SHARED_ASSET_DIR),
            "name": this.getAssetFilename(),
        });
    }
 
    /**
     *
     * @returns A filename safe-encoded UUID of this instance with the correct file extension
     */
    getAssetFilename(): string {
        return path.format({
            "name": Buffer.from(this.fileChecksum, `base64`).toString(`base64url`), // Since checksum seems to be base64 encoded
            "ext": this.fileType.getExtension(),
        });
    }
 
    /**
     *
     * @returns The human readable / pretty printed filename of this asset, based on the filename of the original file imported.
     */
    getPrettyFilename(): string {
        return path.format({
            "name": this.origFilename + (this.assetType === AssetType.EDIT ? `-edited` : ``) + (this.assetType === AssetType.LIVE ? `-live` : ``),
            "ext": this.fileType.getExtension(),
        });
    }
 
    /**
     *
     * @returns The UUID of this instance
     */
    getUUID(): string {
        return this.fileChecksum;
    }
 
    /**
     * Verifies that the object representation matches the given file
     * @param filePath - The path, where the file is expected
     * @returns True if the provided file matches this object representation
     * @throws An error, if verification fails
     */
    async verify(filePath: string): Promise<boolean> {
        let fileStat: Stats;
        try {
            fileStat = await fs.stat(filePath);
        } catch (err) {
            throw new iCPSError(LIBRARY_ERR.ASSET_NOT_FOUND)
                .addCause(err)
                .addMessage(filePath);
        }
 
        if (fileStat.size !== this.size) {
            throw new iCPSError(LIBRARY_ERR.ASSET_SIZE)
                .addMessage(`${filePath} size ${fileStat.size}, iCloud ${this.size}`);
        }
 
        if (!this.withinRange(fileStat.mtimeMs, this.modified, 1000)) {
            throw new iCPSError(LIBRARY_ERR.ASSET_MODIFICATION_TIME)
                .addMessage(`${filePath} modification time ${fileStat.mtimeMs}, iCloud ${this.modified}`)
                .addContext(`out-of-range`, fileStat.mtimeMs - this.modified);
        }
 
        return true;
    }
 
    /**
     * Checks if one number is within the range of another number
     * @param x - One number
     * @param y - Other number
     * @param range - Range to check
     * @returns true if within range, false otherwise
     */
    private withinRange(x: number, y: number, range: number): boolean {
        return x >= y - range
            && x <= y + range;
    }
 
    /**
     * Verifies the checksum of this file against the checksum stored in this object
     * This is currently NOT implemented, as the checksum algorithm is unknown.
     * @param file - The read file
     * @returns True if checksum matches
     */
    /*
    private verifyChecksum(file: Buffer): boolean {
        return file !== undefined;
        Const hashes = [
            `BLAKE2b512`,
            `BLAKE2s256`,
            // `MD4`,
            `MD5`,
            `MD6`,
            `MD5-SHA1`,
            //`RIPEMD160`,
            `SHA1`,
            `SHA224`,
            `SHA256`,
            `SHA3-224`,
            `SHA3-256`,
            `SHA3-384`,
            `SHA3-512`,
            `SHA384`,
            `SHA512`,
            `SHA512-224`,
            `SHA512-256`,
            // `SHAKE128`,
            // `SHAKE256`,
            `SM3`,
            // `whirlpool`,
        ];
        const encodings = [
        //    `ascii`,
        //    `utf8`,
        //    `utf-8`,
        //    `utf16le`,
        //    `ucs2`,
        //    `ucs-2`,
            `base64`,
            `base64url`,
            //    `latin1`,
            //    `binary`,
            // `hex`,
        ];
        const key = Buffer.from(this.wrappingKey);// , `base64`);
        hashes.forEach(hash => {
            encodings.forEach(encoding => {
                if (Buffer.isEncoding(encoding)) {
                    try {
                        const hmacChecksum = crypto.createHmac(hash, key)
                            .update(file)
                            .digest()
                            .toString(encoding);
 
                        const checksum = crypto.createHash(hash)
                            .update(file)
                            .digest()
                            .toString(encoding);
 
                        if (checksum.includes(this.fileChecksum)) {
                            console.log(`MATCH:     ${checksum} - ${hash}/${encoding}`);
                        } else {
                            console.log(`NO match:  ${checksum} - ${hash}/${encoding}`);
                        }
 
                        if (hmacChecksum.includes(this.fileChecksum)) {
                            console.log(`MATCH:     ${hmacChecksum} - ${hash}/${encoding}`);
                        } else {
                            console.log(`NO match:  ${hmacChecksum} - ${hash}/${encoding}`);
                        }
                    } catch (error) {
                        console.log(`Problem with ${encoding} + ${hash}: ${error.message}`);
                    }
                }
            });
        });
    } */
 
    /**
     *
     * @returns A display name for this instance
     */
    getDisplayName(): string {
        return this.fileChecksum;
    }
}