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