-
Notifications
You must be signed in to change notification settings - Fork 0
/
fileStore.ts
53 lines (51 loc) · 1.75 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
48
49
50
51
52
53
/*
* api.codeslide.net
*
* @license
* Forked from mydraft.cc by Sebastian Stehle
* Copyright (c) Do Duc Quan. All rights reserved.
*/
import * as fs from 'node:fs';
export default (() => {
const base = './tmp/'
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]]
}
}
}
}
})();