aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/controllers/lazy-static.ts
diff options
context:
space:
mode:
Diffstat (limited to 'server/controllers/lazy-static.ts')
-rw-r--r--server/controllers/lazy-static.ts80
1 files changed, 80 insertions, 0 deletions
diff --git a/server/controllers/lazy-static.ts b/server/controllers/lazy-static.ts
new file mode 100644
index 000000000..4285fd727
--- /dev/null
+++ b/server/controllers/lazy-static.ts
@@ -0,0 +1,80 @@
1import * as cors from 'cors'
2import * as express from 'express'
3import { LAZY_STATIC_PATHS, STATIC_MAX_AGE } from '../initializers/constants'
4import { VideosCaptionCache, VideosPreviewCache } from '../lib/files-cache'
5import { asyncMiddleware } from '../middlewares'
6import { AvatarModel } from '../models/avatar/avatar'
7import { logger } from '../helpers/logger'
8import { avatarPathUnsafeCache, pushAvatarProcessInQueue } from '../lib/avatar'
9
10const lazyStaticRouter = express.Router()
11
12lazyStaticRouter.use(cors())
13
14lazyStaticRouter.use(
15 LAZY_STATIC_PATHS.AVATARS + ':filename',
16 asyncMiddleware(getAvatar)
17)
18
19lazyStaticRouter.use(
20 LAZY_STATIC_PATHS.PREVIEWS + ':uuid.jpg',
21 asyncMiddleware(getPreview)
22)
23
24lazyStaticRouter.use(
25 LAZY_STATIC_PATHS.VIDEO_CAPTIONS + ':videoId-:captionLanguage([a-z]+).vtt',
26 asyncMiddleware(getVideoCaption)
27)
28
29// ---------------------------------------------------------------------------
30
31export {
32 lazyStaticRouter,
33 getPreview,
34 getVideoCaption
35}
36
37// ---------------------------------------------------------------------------
38
39async function getAvatar (req: express.Request, res: express.Response) {
40 const filename = req.params.filename
41
42 if (avatarPathUnsafeCache.has(filename)) {
43 return res.sendFile(avatarPathUnsafeCache.get(filename), { maxAge: STATIC_MAX_AGE.SERVER })
44 }
45
46 const avatar = await AvatarModel.loadByName(filename)
47 if (avatar.onDisk === false) {
48 if (!avatar.fileUrl) return res.sendStatus(404)
49
50 logger.info('Lazy serve remote avatar image %s.', avatar.fileUrl)
51
52 await pushAvatarProcessInQueue({ filename: avatar.filename, fileUrl: avatar.fileUrl })
53
54 avatar.onDisk = true
55 avatar.save()
56 .catch(err => logger.error('Cannot save new avatar disk state.', { err }))
57 }
58
59 const path = avatar.getPath()
60
61 avatarPathUnsafeCache.set(filename, path)
62 return res.sendFile(path, { maxAge: STATIC_MAX_AGE.SERVER })
63}
64
65async function getPreview (req: express.Request, res: express.Response) {
66 const result = await VideosPreviewCache.Instance.getFilePath(req.params.uuid)
67 if (!result) return res.sendStatus(404)
68
69 return res.sendFile(result.path, { maxAge: STATIC_MAX_AGE.SERVER })
70}
71
72async function getVideoCaption (req: express.Request, res: express.Response) {
73 const result = await VideosCaptionCache.Instance.getFilePath({
74 videoId: req.params.videoId,
75 language: req.params.captionLanguage
76 })
77 if (!result) return res.sendStatus(404)
78
79 return res.sendFile(result.path, { maxAge: STATIC_MAX_AGE.SERVER })
80}