]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/lazy-static.ts
Merge branch 'release/3.1.0' into develop
[github/Chocobozzz/PeerTube.git] / server / controllers / lazy-static.ts
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 { actorImagePathUnsafeCache, pushActorImageProcessInQueue } from '../lib/actor-image'
8 import { VideosCaptionCache, VideosPreviewCache } from '../lib/files-cache'
9 import { asyncMiddleware } from '../middlewares'
10 import { ActorImageModel } from '../models/account/actor-image'
11
12 const lazyStaticRouter = express.Router()
13
14 lazyStaticRouter.use(cors())
15
16 lazyStaticRouter.use(
17 LAZY_STATIC_PATHS.AVATARS + ':filename',
18 asyncMiddleware(getActorImage)
19 )
20
21 lazyStaticRouter.use(
22 LAZY_STATIC_PATHS.BANNERS + ':filename',
23 asyncMiddleware(getActorImage)
24 )
25
26 lazyStaticRouter.use(
27 LAZY_STATIC_PATHS.PREVIEWS + ':filename',
28 asyncMiddleware(getPreview)
29 )
30
31 lazyStaticRouter.use(
32 LAZY_STATIC_PATHS.VIDEO_CAPTIONS + ':filename',
33 asyncMiddleware(getVideoCaption)
34 )
35
36 lazyStaticRouter.use(
37 LAZY_STATIC_PATHS.TORRENTS + ':filename',
38 asyncMiddleware(getTorrent)
39 )
40
41 // ---------------------------------------------------------------------------
42
43 export {
44 lazyStaticRouter,
45 getPreview,
46 getVideoCaption
47 }
48
49 // ---------------------------------------------------------------------------
50
51 async function getActorImage (req: express.Request, res: express.Response) {
52 const filename = req.params.filename
53
54 if (actorImagePathUnsafeCache.has(filename)) {
55 return res.sendFile(actorImagePathUnsafeCache.get(filename), { maxAge: STATIC_MAX_AGE.SERVER })
56 }
57
58 const image = await ActorImageModel.loadByName(filename)
59 if (!image) return res.sendStatus(HttpStatusCode.NOT_FOUND_404)
60
61 if (image.onDisk === false) {
62 if (!image.fileUrl) return res.sendStatus(HttpStatusCode.NOT_FOUND_404)
63
64 logger.info('Lazy serve remote actor image %s.', image.fileUrl)
65
66 try {
67 await pushActorImageProcessInQueue({ filename: image.filename, fileUrl: image.fileUrl, type: image.type })
68 } catch (err) {
69 logger.warn('Cannot process remote actor image %s.', image.fileUrl, { err })
70 return res.sendStatus(HttpStatusCode.NOT_FOUND_404)
71 }
72
73 image.onDisk = true
74 image.save()
75 .catch(err => logger.error('Cannot save new actor image disk state.', { err }))
76 }
77
78 const path = image.getPath()
79
80 actorImagePathUnsafeCache.set(filename, path)
81 return res.sendFile(path, { maxAge: STATIC_MAX_AGE.LAZY_SERVER })
82 }
83
84 async function getPreview (req: express.Request, res: express.Response) {
85 const result = await VideosPreviewCache.Instance.getFilePath(req.params.filename)
86 if (!result) return res.sendStatus(HttpStatusCode.NOT_FOUND_404)
87
88 return res.sendFile(result.path, { maxAge: STATIC_MAX_AGE.LAZY_SERVER })
89 }
90
91 async function getVideoCaption (req: express.Request, res: express.Response) {
92 const result = await VideosCaptionCache.Instance.getFilePath(req.params.filename)
93 if (!result) return res.sendStatus(HttpStatusCode.NOT_FOUND_404)
94
95 return res.sendFile(result.path, { maxAge: STATIC_MAX_AGE.LAZY_SERVER })
96 }
97
98 async function getTorrent (req: express.Request, res: express.Response) {
99 const result = await VideosTorrentCache.Instance.getFilePath(req.params.filename)
100 if (!result) return res.sendStatus(HttpStatusCode.NOT_FOUND_404)
101
102 // Torrents still use the old naming convention (video uuid + .torrent)
103 return res.sendFile(result.path, { maxAge: STATIC_MAX_AGE.SERVER })
104 }