]>
Commit | Line | Data |
---|---|---|
1 | import * as cors from 'cors' | |
2 | import * as express from 'express' | |
3 | import { VideosTorrentCache } from '@server/lib/files-cache/videos-torrent-cache' | |
4 | import { HttpStatusCode } from '../../shared/core-utils/miscs/http-error-codes' | |
5 | import { logger } from '../helpers/logger' | |
6 | import { LAZY_STATIC_PATHS, STATIC_MAX_AGE } from '../initializers/constants' | |
7 | import { avatarPathUnsafeCache, pushAvatarProcessInQueue } from '../lib/avatar' | |
8 | import { VideosCaptionCache, VideosPreviewCache } from '../lib/files-cache' | |
9 | import { asyncMiddleware } from '../middlewares' | |
10 | import { AvatarModel } from '../models/avatar/avatar' | |
11 | ||
12 | const lazyStaticRouter = express.Router() | |
13 | ||
14 | lazyStaticRouter.use(cors()) | |
15 | ||
16 | lazyStaticRouter.use( | |
17 | LAZY_STATIC_PATHS.AVATARS + ':filename', | |
18 | asyncMiddleware(getAvatar) | |
19 | ) | |
20 | ||
21 | lazyStaticRouter.use( | |
22 | LAZY_STATIC_PATHS.PREVIEWS + ':filename', | |
23 | asyncMiddleware(getPreview) | |
24 | ) | |
25 | ||
26 | lazyStaticRouter.use( | |
27 | LAZY_STATIC_PATHS.VIDEO_CAPTIONS + ':filename', | |
28 | asyncMiddleware(getVideoCaption) | |
29 | ) | |
30 | ||
31 | lazyStaticRouter.use( | |
32 | LAZY_STATIC_PATHS.TORRENTS + ':filename', | |
33 | asyncMiddleware(getTorrent) | |
34 | ) | |
35 | ||
36 | // --------------------------------------------------------------------------- | |
37 | ||
38 | export { | |
39 | lazyStaticRouter, | |
40 | getPreview, | |
41 | getVideoCaption | |
42 | } | |
43 | ||
44 | // --------------------------------------------------------------------------- | |
45 | ||
46 | async function getAvatar (req: express.Request, res: express.Response) { | |
47 | const filename = req.params.filename | |
48 | ||
49 | if (avatarPathUnsafeCache.has(filename)) { | |
50 | return res.sendFile(avatarPathUnsafeCache.get(filename), { maxAge: STATIC_MAX_AGE.SERVER }) | |
51 | } | |
52 | ||
53 | const avatar = await AvatarModel.loadByName(filename) | |
54 | if (!avatar) return res.sendStatus(HttpStatusCode.NOT_FOUND_404) | |
55 | ||
56 | if (avatar.onDisk === false) { | |
57 | if (!avatar.fileUrl) return res.sendStatus(HttpStatusCode.NOT_FOUND_404) | |
58 | ||
59 | logger.info('Lazy serve remote avatar image %s.', avatar.fileUrl) | |
60 | ||
61 | try { | |
62 | await pushAvatarProcessInQueue({ filename: avatar.filename, fileUrl: avatar.fileUrl }) | |
63 | } catch (err) { | |
64 | logger.warn('Cannot process remote avatar %s.', avatar.fileUrl, { err }) | |
65 | return res.sendStatus(HttpStatusCode.NOT_FOUND_404) | |
66 | } | |
67 | ||
68 | avatar.onDisk = true | |
69 | avatar.save() | |
70 | .catch(err => logger.error('Cannot save new avatar disk state.', { err })) | |
71 | } | |
72 | ||
73 | const path = avatar.getPath() | |
74 | ||
75 | avatarPathUnsafeCache.set(filename, path) | |
76 | return res.sendFile(path, { maxAge: STATIC_MAX_AGE.LAZY_SERVER }) | |
77 | } | |
78 | ||
79 | async function getPreview (req: express.Request, res: express.Response) { | |
80 | const result = await VideosPreviewCache.Instance.getFilePath(req.params.filename) | |
81 | if (!result) return res.sendStatus(HttpStatusCode.NOT_FOUND_404) | |
82 | ||
83 | return res.sendFile(result.path, { maxAge: STATIC_MAX_AGE.LAZY_SERVER }) | |
84 | } | |
85 | ||
86 | async function getVideoCaption (req: express.Request, res: express.Response) { | |
87 | const result = await VideosCaptionCache.Instance.getFilePath(req.params.filename) | |
88 | if (!result) return res.sendStatus(HttpStatusCode.NOT_FOUND_404) | |
89 | ||
90 | return res.sendFile(result.path, { maxAge: STATIC_MAX_AGE.LAZY_SERVER }) | |
91 | } | |
92 | ||
93 | async function getTorrent (req: express.Request, res: express.Response) { | |
94 | const result = await VideosTorrentCache.Instance.getFilePath(req.params.filename) | |
95 | if (!result) return res.sendStatus(HttpStatusCode.NOT_FOUND_404) | |
96 | ||
97 | // Torrents still use the old naming convention (video uuid + .torrent) | |
98 | return res.sendFile(result.path, { maxAge: STATIC_MAX_AGE.SERVER }) | |
99 | } |