This repository was archived by the owner on Feb 4, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathindex.ts
132 lines (108 loc) · 3.5 KB
/
index.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import { Provider } from '../../../uploads/provider'
import process from 'process'
import path from 'path'
import fs from 'fs'
import crypto from 'crypto'
import config from '../../../config/server'
import { FastifyInstance } from 'fastify'
import fastifyStatic from 'fastify-static'
import contentDisposition from 'content-disposition'
interface LocalProviderOptions {
uploadDirectory?: string;
endpoint?: string;
}
interface Upload {
filePath: string;
name: string;
}
interface RequestQuerystring {
key: string;
}
export default class LocalProvider implements Provider {
private uploadDirectory: string
private endpoint: string
private uploadMap: Map<string, Upload>
constructor (options: LocalProviderOptions, app: FastifyInstance) {
if (options.uploadDirectory === undefined) {
options.uploadDirectory = path.join(process.cwd(), 'uploads')
}
fs.mkdirSync(options.uploadDirectory, { recursive: true })
this.uploadDirectory = path.resolve(options.uploadDirectory)
this.endpoint = options.endpoint || '/uploads'
this.uploadMap = new Map<string, Upload>()
const uploads = fs.readdirSync(this.uploadDirectory)
for (const hash of uploads) {
for (const name of fs.readdirSync(path.join(this.uploadDirectory, hash))) {
this.uploadMap.set(this.getKey(hash, name), {
filePath: path.join(this.uploadDirectory, hash, name),
name
})
}
}
void app.register(async (fastify) => {
void fastify.register(fastifyStatic, {
root: this.uploadDirectory,
serve: false
})
// Fastify bug #2466
// eslint-disable-next-line @typescript-eslint/no-misused-promises
fastify.setNotFoundHandler(async (req, res) => {
void res.status(404)
return 'Not found'
})
fastify.get<{
Querystring: RequestQuerystring
}>('/', {
schema: {
querystring: {
type: 'object',
properties: {
key: {
type: 'string'
}
},
required: ['key']
}
}
}, async (request, reply) => {
const key = request.query.key.toString()
const upload = this.uploadMap.get(key)
if (upload != null) {
void reply.header('Cache-Control', 'public, max-age=31557600, immutable')
void reply.header('Content-Disposition', contentDisposition(upload.name))
void reply.sendFile(path.relative(this.uploadDirectory, upload.filePath))
} else {
reply.callNotFound()
}
})
}, {
prefix: this.endpoint
})
}
private getKey (hash: string, name: string): string {
return `${hash}/${name}`
}
private getUrlPath (key: string): string {
return `${this.endpoint}?key=${encodeURIComponent(key)}`
}
async upload (data: Buffer, name: string): Promise<string> {
const hash = crypto.createHash('sha256')
.update(data)
.digest('hex')
const key = this.getKey(hash, name)
const urlPath = this.getUrlPath(key)
const filePath = path.join(this.uploadDirectory, hash, name)
this.uploadMap.set(key, {
filePath,
name
})
await fs.promises.mkdir(path.join(this.uploadDirectory, hash))
await fs.promises.writeFile(filePath, data)
return (config.origin || '') + urlPath
}
async getUrl (sha256: string, name: string): Promise<string|null> {
const key = this.getKey(sha256, name)
if (!this.uploadMap.has(key)) return null
return this.getUrlPath(key)
}
}