forked from p2r3/convert
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFormatHandler.ts
More file actions
137 lines (126 loc) · 4.01 KB
/
FormatHandler.ts
File metadata and controls
137 lines (126 loc) · 4.01 KB
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
/**
* Definition of file format. Contains format defined constants like mime type and names
*/
export interface IFormatDefinition {
/** Format description (long name) for displaying to the user. */
name: string;
/** Short, "formal" name for displaying to the user. */
format: string;
/** File extension. */
extension: string;
/** MIME type. */
mime: string;
}
export interface FileFormat extends IFormatDefinition {
/** Whether conversion **from** this format is supported. */
from: boolean;
/** Whether conversion **to** this format is supported. */
to: boolean;
/** Format identifier for the handler's internal reference. */
internal: string;
/** Category for grouping formats. */
category?: Array<string> | string
/** Whether the format is lossless (if applicable). */
lossless?: boolean;
}
/**
* Class containing format definition and method used to produce FileFormat
* that can be supported by handlers.
*/
export class FormatDefinition implements IFormatDefinition {
public readonly name: string;
public readonly format: string;
public readonly extension: string;
public readonly mime: string;
public readonly category?: string[] | string;
constructor (
name: string,
format: string,
extension: string,
mime: string,
category?: string[] | string
) {
this.name = name
this.format = format
this.extension = extension
this.mime = mime
this.category = category
}
/**
* Returns `FileFormat` object that uses this format definition
* and specified options
* @param ref Format identifier for the handler's internal reference.
* @param from Whether conversion **from** this format is supported.
* @param to Whether conversion **to** this format is supported.
* @param lossless (Optional) Whether the format is lossless in this context. Defaults to `false`.
* @returns
*/
supported(ref: string, from: boolean, to: boolean, lossless?: boolean): FileFormat {
return {
...this,
internal: ref,
from: from,
to: to,
lossless: lossless
}
}
}
export interface FileData {
/** File name with extension. */
name: string;
/**
* File contents in bytes.
*
* **Please note:** _handlers_ are responsible for ensuring the lifetime
* and consistency of this buffer. If you're not sure that your handler
* won't modify it, wrap it in `new Uint8Array()`.
*/
readonly bytes: Uint8Array;
}
/**
* Establishes a common interface for converting between file formats.
* Often a "wrapper" for existing tools.
*/
export interface FormatHandler {
/** Name of the tool being wrapped (e.g. "FFmpeg"). */
name: string;
/** List of supported input/output {@link FileFormat}s. */
supportedFormats?: FileFormat[];
/** Whether the handler supports input of any type.
* Conversion using this handler will be performed only if no other direct conversion is found.
*/
supportAnyInput?: boolean;
/**
* Whether the handler is ready for use. Should be set in {@link init}.
* If true, {@link doConvert} is expected to work.
*/
ready: boolean;
/**
* Initializes the handler if necessary.
* Should set {@link ready} to true.
*/
init: () => Promise<void>;
/**
* Performs the actual file conversion.
* @param inputFiles Array of {@link FileData} entries, one per input file.
* @param inputFormat Input {@link FileFormat}, the same for all inputs.
* @param outputFormat Output {@link FileFormat}, the same for all outputs.
* @param args Optional arguments as a string array.
* Can be used to perform recursion with different settings.
* @returns Array of {@link FileData} entries, one per generated output file.
*/
doConvert: (
inputFiles: FileData[],
inputFormat: FileFormat,
outputFormat: FileFormat,
args?: string[]
) => Promise<FileData[]>;
}
export class ConvertPathNode {
public handler: FormatHandler;
public format: FileFormat;
constructor(handler: FormatHandler, format: FileFormat) {
this.handler = handler;
this.format = format;
}
}