-
Notifications
You must be signed in to change notification settings - Fork 196
Expand file tree
/
Copy pathinputFile.ts.twig
More file actions
110 lines (94 loc) · 3.24 KB
/
inputFile.ts.twig
File metadata and controls
110 lines (94 loc) · 3.24 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
import { File } from "node-fetch-native-with-agent";
import { basename } from "path";
import { realpathSync } from "fs";
import { promises as fs } from "fs";
type BlobLike = {
size: number;
slice: (start: number, end: number) => BlobLike;
arrayBuffer: () => Promise<ArrayBuffer>;
};
type InputFileSource =
| { type: 'path'; path: string }
| { type: 'buffer'; data: Buffer }
| { type: 'blob'; data: BlobLike };
export class InputFile {
private source: InputFileSource;
filename: string;
private constructor(source: InputFileSource, filename: string) {
this.source = source;
this.filename = filename;
}
static fromBuffer(parts: BlobLike | Buffer | Uint8Array | ArrayBuffer | string, name: string): InputFile {
if (parts && typeof (parts as BlobLike).arrayBuffer === 'function') {
return new InputFile({ type: 'blob', data: parts as BlobLike }, name);
}
if (Buffer.isBuffer(parts)) {
return new InputFile({ type: 'buffer', data: parts }, name);
}
if (typeof parts === 'string') {
return new InputFile({ type: 'buffer', data: Buffer.from(parts) }, name);
}
if (parts instanceof ArrayBuffer) {
return new InputFile({ type: 'buffer', data: Buffer.from(parts) }, name);
}
if (ArrayBuffer.isView(parts)) {
return new InputFile({
type: 'buffer',
data: Buffer.from(parts.buffer, parts.byteOffset, parts.byteLength),
}, name);
}
throw new Error('Unsupported input type for InputFile.fromBuffer');
}
static fromPath(path: string, name?: string): InputFile {
const realPath = realpathSync(path);
return new InputFile({ type: 'path', path: realPath }, name ?? basename(realPath));
}
static fromPlainText(content: string, name: string): InputFile {
return new InputFile({ type: 'buffer', data: Buffer.from(content) }, name);
}
async size(): Promise<number> {
switch (this.source.type) {
case 'path':
return (await fs.stat(this.source.path)).size;
case 'buffer':
return this.source.data.length;
case 'blob':
return this.source.data.size;
}
}
async slice(start: number, end: number): Promise<Buffer> {
const length = end - start;
switch (this.source.type) {
case 'path': {
const handle = await fs.open(this.source.path, 'r');
try {
const buffer = Buffer.alloc(length);
const result = await handle.read(buffer, 0, length, start);
return result.bytesRead === buffer.length ? buffer : buffer.subarray(0, result.bytesRead);
} finally {
await handle.close();
}
}
case 'buffer':
return this.source.data.subarray(start, end);
case 'blob': {
const arrayBuffer = await this.source.data.slice(start, end).arrayBuffer();
return Buffer.from(arrayBuffer);
}
}
}
async toFile(): Promise<File> {
const buffer = await this.toBuffer();
return new File([buffer], this.filename);
}
private async toBuffer(): Promise<Buffer> {
switch (this.source.type) {
case 'path':
return await fs.readFile(this.source.path);
case 'buffer':
return this.source.data;
case 'blob':
return Buffer.from(await this.source.data.arrayBuffer());
}
}
}