-
Notifications
You must be signed in to change notification settings - Fork 2
/
filestorage.ts
111 lines (97 loc) · 3.74 KB
/
filestorage.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
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
import { createStorage, defineDriver } from "unstorage";
import { existsSync, watch } from "fs";
import { readFile, writeFile, rename, unlink, readdir, lstat, mkdir } from "fs/promises";
import { join, resolve, sep } from "pathe";
import type { WatchEventType } from "node:fs";
const customDriver = defineDriver((options?: { destination: string }) => {
if (!options || !options.destination) {
options = { destination: './' }
}
const location = (destination: string) => {
const basePath = join("public", "uploads", options!.destination, destination).replace(/:/g, sep)
return resolve(basePath)
}
return {
name: "custom-driver",
options,
hasItem(key, _opts) {
return existsSync(location(key))
},
async getItem(key, _opts) {
return await readFile(location(key))
},
async setItem(key, value: File | string, _opts) {
const fileLocation = location(key)
if (!existsSync(fileLocation)) {
const folder = fileLocation.split(sep).slice(0, -1).join(sep)
await mkdir(folder, { recursive: true })
}
if (value instanceof File) {
await rename(value.filepath, fileLocation)
return fileLocation
} else {
await writeFile(fileLocation, value)
return fileLocation
}
},
async setItemRaw(key, value, _opts) {
const folder = location(key).split(sep).slice(0, -1).join(sep)
if(!existsSync(folder)){
await mkdir(folder, { recursive: true })
}
return await rename(value.filepath, location(key))
},
async removeItem(key, _opts) {
return await unlink(location(key))
},
async getKeys(base, _opts) {
async function construct(location: string, sb: string) {
const stats = await lstat(location).catch(e => null)
if (stats?.isDirectory()) {
const next = await readdir(location)
return construct(next[0], join(sb, location))
}
return join(sb, location)
}
const files = await readdir(join(options!.destination, "public", "uploads")).catch(e => {
console.error(e)
return []
})
const keys: string[] = []
for (const file of files) {
construct(file, join(options!.destination, "public", "uploads")).then(value => {
keys.push(value)
})
}
return keys
},
async clear(base, _opts) {
async function deleteFiles(directory: string) {
if (!existsSync(directory)) return
const files = await readdir(directory)
for (const file of files) {
const stats = await lstat(file).catch(e => null)
if (stats?.isDirectory()) {
deleteFiles(file)
}
unlink(file)
}
}
return await deleteFiles(join(options!.destination, "public", "uploads"))
},
async dispose() {
},
async watch(callback: (event: WatchEventType, filename: string | null) => void) {
const watcher = watch(join(options!.destination, "public", "uploads"), { recursive: true }, (event, filename) => {
callback(event, filename)
})
return () => {
watcher.close()
}
},
};
});
const imageStorage = createStorage({
driver: customDriver(undefined)
})
export default imageStorage;