-
Notifications
You must be signed in to change notification settings - Fork 2
/
fileStore.ts
47 lines (45 loc) · 1.67 KB
/
fileStore.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
import EventEmitter from 'node:events';
import * as fs from 'node:fs';
export default (() => {
const base = './localFileStore/'
const metaPath = `${base}_meta.json`
if (fs.existsSync(base) === false) {
fs.mkdirSync(base)
}
if (fs.existsSync(metaPath) === false) {
fs.writeFileSync(metaPath, JSON.stringify({}))
}
return {
file: (filePath: string) => {
const fullPath = `${base}${filePath}`
return {
exists: () => {
return [fs.existsSync(fullPath)]
},
download: async () => {
return [new Uint8Array(fs.readFileSync(fullPath))]
},
createWriteStream: () => {
return fs.createWriteStream(fullPath)
},
createReadStream: () => {
return fs.createReadStream(fullPath)
},
save: (fileContent: string, meta?: undefined | any) => {
if (typeof meta === 'object') {
const rawMeta = fs.readFileSync(metaPath);
const _meta = JSON.parse(rawMeta.toString())
_meta[fullPath] = meta.metadata
fs.writeFileSync(metaPath, JSON.stringify(_meta))
}
fs.writeFileSync(fullPath, fileContent)
},
getMetadata: () => {
const rawMeta = fs.readFileSync(metaPath);
const _meta = JSON.parse(rawMeta.toString())
return [_meta[fullPath]]
}
}
}
}
})();