-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathnpdl.ts
55 lines (44 loc) · 1.32 KB
/
npdl.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 express from 'express';
import subdomain from 'express-subdomain';
import { getTaskFile } from '@/database';
import { getCDNFileStream } from '@/util';
import { Stream } from 'node:stream';
import { LOG_ERROR } from '@/logger';
const npdl = express.Router();
npdl.get([
'/p01/nsa/:appID/:taskID/:fileName',
'/p01/nsa/:appID/:taskID/:languageCode/:fileName',
'/p01/nsa/:appID/:taskID/:countryCode/:languageCode/:fileName'
], async (request: express.Request<{
appID: string;
taskID: string;
countryCode?: string;
languageCode?: string;
fileName: string;
}, any, any, {
ap?: string;
tm?: string;
}>, response) => {
const { appID, taskID, countryCode, languageCode, fileName } = request.params;
const file = await getTaskFile(appID, taskID, fileName, countryCode, languageCode);
if (!file) {
response.sendStatus(404);
return;
}
const key = `${appID}/${taskID}/${file.hash}`;
const readStream = await getCDNFileStream(key);
if (!readStream) {
response.sendStatus(404);
return;
}
response.setHeader('Last-Modified', new Date(Number(file.updated)).toUTCString());
Stream.pipeline(readStream, response, (err) => {
if (err) {
LOG_ERROR('Error with response stream: ' + err.message);
response.end();
}
});
});
const router = express.Router();
router.use(subdomain('npdl.cdn', npdl));
export default router;