All files / lib/photos-library/model file-type.ts

100% Statements 125/125
100% Branches 12/12
100% Functions 6/6
100% Lines 125/125

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 1251x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 420x 420x 420x 420x 420x 420x 420x 420x 420x 420x 420x 420x 420x 420x 420x 420x 420x 420x 420x 420x 420x 207x 1x 1x 1x 1x 1x 206x 206x 206x 420x 420x 420x 420x 420x 420x 420x 420x 224x 25x 25x 224x 224x 224x 10x 10x 10x 10x 214x 214x 214x 420x 420x 420x 420x 420x 420x 392x 392x 420x 420x 420x 420x 420x 420x 420x 14x 14x 420x
import {iCPSError} from "../../../app/error/error.js";
import {LIBRARY_ERR} from "../../../app/error/error-codes.js";
 
/**
 * Mapping of backend provided filetype description (key) and actual file extension (value)
 * Incomplete list, due to lack of documentation
 */
const EXT = {
    'public.png': `png`,
    'public.mpeg-4': `mp4`,
    'public.jpeg': `jpeg`,
    'com.apple.quicktime-movie': `mov`,
    'public.heic': `heic`,
    'public.image': `HEIC`,
    'com.sony.arw-raw-image': `arw`,
    'org.webmproject.webp': `webp`,
    'com.compuserve.gif': `gif`,
    'com.adobe.raw-image': `dng`,
    'public.tiff': `tiff`,
    'public.jpeg-2000': `jp2`,
    'com.truevision.tga-image': `tga`,
    'com.sgi.sgi-image': `sgi`,
    'com.adobe.photoshop-image': `psd`,
    'public.pbm': `pbm`,
    'public.heif': `heif`,
    'com.microsoft.bmp': `bmp`,
    'public.mpeg': `mpg`,
    'com.apple.m4v-video': `m4v`,
    'public.3gpp': `3gp`,
    'public.mpeg-2-video': `m2v`,
    'com.fuji.raw-image': `raf`,
    'com.canon.cr2-raw-image': `cr2`,
    'com.panasonic.rw2-raw-image': `rw2`,
    'com.nikon.nrw-raw-image': `nrw`,
    'com.pentax.raw-image': `pef`,
    'com.nikon.raw-image': `nef`,
    'com.olympus.raw-image': `orf`,
    'public.avi': `avi`,
    'com.adobe.pdf': `pdf`,
    'public.avchd-mpeg-2-transport-stream': `mts`,
    'com.adobe.illustrator.ai-image': `ai`,
    'com.canon.cr3-raw-image': `cr3`,
    'com.olympus.or-raw-image': `orf`,
    'public.item': `largeThumbnail`,
    'public.mpo-image': `mpo`,
    'com.dji.mimo.pano.jpeg': `jpg`,
    'public.avif': `avif`,
    'com.apple.m4a-audio': `m4a`,
    'com.canon.crw-raw-image': `crw`,
    'public.mp3': `mp3`,
};
 
/**
 * This class represents the type of file a given asset has
 */
export class FileType {
    /**
     * The descriptor, as provided by the backend
     */
    descriptor: string;
 
    /**
     * Constructs a new FileType
     * @param descriptor - The descriptor as provided by the backend
     */
    private constructor(descriptor: string) {
        this.descriptor = descriptor;
    }
 
    /**
     * Creates the file type from the backend
     * @param descriptor - The descriptor as provided by the backend
     * @param ext - The extension as provided by the encoded filename
     * @returns The newly created FileType
     * @throws An iCPSError, if the provided descriptor is unknown to the script
     */
    static fromAssetType(descriptor: string, ext: string): FileType {
        if (!EXT[descriptor]) {
            throw new iCPSError(LIBRARY_ERR.UNKNOWN_FILETYPE_DESCRIPTOR)
                .addContext(`extension`, ext)
                .addContext(`descriptor`, descriptor)
                .addMessage(`${descriptor} (with potential extension ${ext})`);
        }
 
        return new FileType(descriptor);
    }
 
    /**
     * Creates a file type from a file extension
     * @param ext - The file extension of the file
     * @returns The newly created FileType
     * @throws An iCPSError, if the provided extension is unknown to the script
     */
    static fromExtension(ext: string): FileType {
        if (ext.startsWith(`.`)) {
            ext = ext.substring(1);
        }
 
        const descriptor = Object.keys(EXT).find(key => EXT[key] === ext);
        if (!descriptor) {
            throw new iCPSError(LIBRARY_ERR.UNKNOWN_FILETYPE_EXTENSION)
                .addContext(`extension`, ext)
                .addMessage(ext);
        }
 
        return new FileType(descriptor);
    }
 
    /**
     *
     * @returns The FileType as a file extension
     */
    getExtension(): string {
        return `.` + EXT[this.descriptor];
    }
 
    /**
     * Compares the given FileType to this instance
     * @param fileType - A FileType to compare this instance to
     * @returns True, if the given FileType is equal to this instance
     */
    equal(fileType: FileType) {
        return fileType && this.descriptor === fileType.descriptor;
    }
}