]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/lazy-static.ts
Merge branch 'release/4.2.0' into develop
[github/Chocobozzz/PeerTube.git] / server / controllers / lazy-static.ts
1 import cors from 'cors'
2 import express from 'express'
3 import { VideosTorrentCache } from '@server/lib/files-cache/videos-torrent-cache'
4 import { MActorImage } from '@server/types/models'
5 import { HttpStatusCode } from '../../shared/models/http/http-error-codes'
6 import { logger } from '../helpers/logger'
7 import { ACTOR_IMAGES_SIZE, LAZY_STATIC_PATHS, STATIC_MAX_AGE } from '../initializers/constants'
8 import { VideosCaptionCache, VideosPreviewCache } from '../lib/files-cache'
9 import { actorImagePathUnsafeCache, pushActorImageProcessInQueue } from '../lib/local-actor'
10 import { asyncMiddleware } from '../middlewares'
11 import { ActorImageModel } from '../models/actor/actor-image'
12
13 const lazyStaticRouter = express.Router()
14
15 lazyStaticRouter.use(cors())
16
17 lazyStaticRouter.use(
18 LAZY_STATIC_PATHS.AVATARS + ':filename',
19 asyncMiddleware(getActorImage)
20 )
21
22 lazyStaticRouter.use(
23 LAZY_STATIC_PATHS.BANNERS + ':filename',
24 asyncMiddleware(getActorImage)
25 )
26
27 lazyStaticRouter.use(
28 LAZY_STATIC_PATHS.PREVIEWS + ':filename',
29 asyncMiddleware(getPreview)
30 )
31
32 lazyStaticRouter.use(
33 LAZY_STATIC_PATHS.VIDEO_CAPTIONS + ':filename',
34 asyncMiddleware(getVideoCaption)
35 )
36
37 lazyStaticRouter.use(
38 LAZY_STATIC_PATHS.TORRENTS + ':filename',
39 asyncMiddleware(getTorrent)
40 )
41
42 // ---------------------------------------------------------------------------
43
44 export {
45 lazyStaticRouter,
46 getPreview,
47 getVideoCaption
48 }
49
50 // ---------------------------------------------------------------------------
51
52 async function getActorImage (req: express.Request, res: express.Response, next: express.NextFunction) {
53 const filename = req.params.filename
54
55 if (actorImagePathUnsafeCache.has(filename)) {
56 return res.sendFile(actorImagePathUnsafeCache.get(filename), { maxAge: STATIC_MAX_AGE.SERVER })
57 }
58
59 const image = await ActorImageModel.loadByName(filename)
60 if (!image) return res.status(HttpStatusCode.NOT_FOUND_404).end()
61
62 if (image.onDisk === false) {
63 if (!image.fileUrl) return res.status(HttpStatusCode.NOT_FOUND_404).end()
64
65 logger.info('Lazy serve remote actor image %s.', image.fileUrl)
66
67 try {
68 await pushActorImageProcessInQueue({
69 filename: image.filename,
70 fileUrl: image.fileUrl,
71 size: getActorImageSize(image),
72 type: image.type
73 })
74 } catch (err) {
75 logger.warn('Cannot process remote actor image %s.', image.fileUrl, { err })
76 return res.status(HttpStatusCode.NOT_FOUND_404).end()
77 }
78
79 image.onDisk = true
80 image.save()
81 .catch(err => logger.error('Cannot save new actor image disk state.', { err }))
82 }
83
84 const path = image.getPath()
85
86 actorImagePathUnsafeCache.set(filename, path)
87
88 return res.sendFile(path, { maxAge: STATIC_MAX_AGE.LAZY_SERVER }, (err: any) => {
89 if (!err) return
90
91 // It seems this actor image is not on the disk anymore
92 if (err.status === HttpStatusCode.NOT_FOUND_404 && !image.isOwned()) {
93 logger.error('Cannot lazy serve actor image %s.', filename, { err })
94
95 actorImagePathUnsafeCache.delete(filename)
96
97 image.onDisk = false
98 image.save()
99 .catch(err => logger.error('Cannot save new actor image disk state.', { err }))
100 }
101
102 return next(err)
103 })
104 }
105
106 function getActorImageSize (image: MActorImage): { width: number, height: number } {
107 if (image.width && image.height) {
108 return {
109 height: image.height,
110 width: image.width
111 }
112 }
113
114 return ACTOR_IMAGES_SIZE[image.type][0]
115 }
116
117 async function getPreview (req: express.Request, res: express.Response) {
118 const result = await VideosPreviewCache.Instance.getFilePath(req.params.filename)
119 if (!result) return res.status(HttpStatusCode.NOT_FOUND_404).end()
120
121 return res.sendFile(result.path, { maxAge: STATIC_MAX_AGE.LAZY_SERVER })
122 }
123
124 async function getVideoCaption (req: express.Request, res: express.Response) {
125 const result = await VideosCaptionCache.Instance.getFilePath(req.params.filename)
126 if (!result) return res.status(HttpStatusCode.NOT_FOUND_404).end()
127
128 return res.sendFile(result.path, { maxAge: STATIC_MAX_AGE.LAZY_SERVER })
129 }
130
131 async function getTorrent (req: express.Request, res: express.Response) {
132 const result = await VideosTorrentCache.Instance.getFilePath(req.params.filename)
133 if (!result) return res.status(HttpStatusCode.NOT_FOUND_404).end()
134
135 // Torrents still use the old naming convention (video uuid + .torrent)
136 return res.sendFile(result.path, { maxAge: STATIC_MAX_AGE.SERVER })
137 }