-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.ts
55 lines (43 loc) · 1.41 KB
/
file.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
import { readdirSync, promises } from 'fs';
import { join, resolve } from 'path';
import { getType } from 'mime';
import { fromBuffer } from 'file-type';
export function findFile(name: string | RegExp, path = '.') {
name = readdirSync(path).find(file => file.match(name));
if (name) return join(path, name);
}
/**
* Find files upward
*/
export function* findUp(from = './') {
from = resolve(from);
while (true) {
const path = join(from, '../');
if (path === from) break;
const file = readdirSync((from = path));
while (file[0]) yield join(path, file.shift());
}
}
export async function toDataURI(path: string) {
const file = await promises.readFile(path);
const type = await fromBuffer(file);
return `data:${type?.mime || getType(path)};base64,${file.toString(
'base64'
)}`;
}
const DataURI_pattern = /^data:(.+?\/(.+?))?(;base64)?,(\S+)/;
/**
* @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URIs
* @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_Types
*/
export async function blobFrom(DataURI: string) {
const [MIME, extension, base64, raw] = (
DataURI_pattern.exec(DataURI) || []
).slice(1) as string[];
const data = Buffer.from(raw, base64 ? 'base64' : 'utf-8');
return {
MIME: MIME || (await fromBuffer(data))?.mime,
extension,
data
};
}