-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
file-type.ts
105 lines (95 loc) · 3.12 KB
/
file-type.ts
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
import {LibraryError} from "../../../app/error-types.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`,
'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`,
};
/**
* 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 Error, if the provided descriptor is unknown to the script
*/
static fromAssetType(descriptor: string, ext: string): FileType {
if (!EXT[descriptor]) {
throw new LibraryError(`Unknown filetype descriptor ${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 error, 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 LibraryError(`Unknown filetype extension: ${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;
}
}