]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/lazy-static.ts
Add TMP persistent directory
[github/Chocobozzz/PeerTube.git] / server / controllers / lazy-static.ts
CommitLineData
41fb13c3
C
1import cors from 'cors'
2import express from 'express'
90a8bd30 3import { VideosTorrentCache } from '@server/lib/files-cache/videos-torrent-cache'
0f07c289 4import { MActorImage } from '@server/types/models'
c0e8b12e 5import { HttpStatusCode } from '../../shared/models/http/http-error-codes'
90a8bd30 6import { logger } from '../helpers/logger'
0f07c289 7import { ACTOR_IMAGES_SIZE, LAZY_STATIC_PATHS, STATIC_MAX_AGE } from '../initializers/constants'
557b13ae 8import { VideosCaptionCache, VideosPreviewCache } from '../lib/files-cache'
c53853ca 9import { actorImagePathUnsafeCache, downloadActorImageFromWorker } from '../lib/local-actor'
eb7b48ce 10import { asyncMiddleware, handleStaticError } from '../middlewares'
7d9ba5c0 11import { ActorImageModel } from '../models/actor/actor-image'
557b13ae
C
12
13const lazyStaticRouter = express.Router()
14
15lazyStaticRouter.use(cors())
16
17lazyStaticRouter.use(
18 LAZY_STATIC_PATHS.AVATARS + ':filename',
eb7b48ce
C
19 asyncMiddleware(getActorImage),
20 handleStaticError
f4796856
C
21)
22
23lazyStaticRouter.use(
24 LAZY_STATIC_PATHS.BANNERS + ':filename',
eb7b48ce
C
25 asyncMiddleware(getActorImage),
26 handleStaticError
557b13ae
C
27)
28
29lazyStaticRouter.use(
a8b1b404 30 LAZY_STATIC_PATHS.PREVIEWS + ':filename',
eb7b48ce
C
31 asyncMiddleware(getPreview),
32 handleStaticError
557b13ae
C
33)
34
35lazyStaticRouter.use(
6302d599 36 LAZY_STATIC_PATHS.VIDEO_CAPTIONS + ':filename',
eb7b48ce
C
37 asyncMiddleware(getVideoCaption),
38 handleStaticError
557b13ae
C
39)
40
90a8bd30
C
41lazyStaticRouter.use(
42 LAZY_STATIC_PATHS.TORRENTS + ':filename',
eb7b48ce
C
43 asyncMiddleware(getTorrent),
44 handleStaticError
90a8bd30
C
45)
46
557b13ae
C
47// ---------------------------------------------------------------------------
48
49export {
50 lazyStaticRouter,
51 getPreview,
52 getVideoCaption
53}
54
55// ---------------------------------------------------------------------------
56
79db409a 57async function getActorImage (req: express.Request, res: express.Response, next: express.NextFunction) {
557b13ae
C
58 const filename = req.params.filename
59
f4796856
C
60 if (actorImagePathUnsafeCache.has(filename)) {
61 return res.sendFile(actorImagePathUnsafeCache.get(filename), { maxAge: STATIC_MAX_AGE.SERVER })
557b13ae
C
62 }
63
f4796856 64 const image = await ActorImageModel.loadByName(filename)
76148b27 65 if (!image) return res.status(HttpStatusCode.NOT_FOUND_404).end()
26ff0279 66
f4796856 67 if (image.onDisk === false) {
76148b27 68 if (!image.fileUrl) return res.status(HttpStatusCode.NOT_FOUND_404).end()
557b13ae 69
f4796856 70 logger.info('Lazy serve remote actor image %s.', image.fileUrl)
557b13ae 71
e2600d8b 72 try {
c53853ca 73 await downloadActorImageFromWorker({
d0800f76 74 filename: image.filename,
75 fileUrl: image.fileUrl,
0f07c289 76 size: getActorImageSize(image),
d0800f76 77 type: image.type
78 })
e2600d8b 79 } catch (err) {
f4796856 80 logger.warn('Cannot process remote actor image %s.', image.fileUrl, { err })
76148b27 81 return res.status(HttpStatusCode.NOT_FOUND_404).end()
e2600d8b 82 }
557b13ae 83
f4796856
C
84 image.onDisk = true
85 image.save()
86 .catch(err => logger.error('Cannot save new actor image disk state.', { err }))
557b13ae
C
87 }
88
f4796856 89 const path = image.getPath()
557b13ae 90
f4796856 91 actorImagePathUnsafeCache.set(filename, path)
79db409a
C
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
0f07c289 100 actorImagePathUnsafeCache.delete(filename)
79db409a
C
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 })
557b13ae
C
109}
110
0f07c289
C
111function 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
557b13ae 122async function getPreview (req: express.Request, res: express.Response) {
a8b1b404 123 const result = await VideosPreviewCache.Instance.getFilePath(req.params.filename)
76148b27 124 if (!result) return res.status(HttpStatusCode.NOT_FOUND_404).end()
557b13ae 125
90a8bd30 126 return res.sendFile(result.path, { maxAge: STATIC_MAX_AGE.LAZY_SERVER })
557b13ae
C
127}
128
129async function getVideoCaption (req: express.Request, res: express.Response) {
6302d599 130 const result = await VideosCaptionCache.Instance.getFilePath(req.params.filename)
76148b27 131 if (!result) return res.status(HttpStatusCode.NOT_FOUND_404).end()
557b13ae 132
90a8bd30
C
133 return res.sendFile(result.path, { maxAge: STATIC_MAX_AGE.LAZY_SERVER })
134}
135
136async function getTorrent (req: express.Request, res: express.Response) {
137 const result = await VideosTorrentCache.Instance.getFilePath(req.params.filename)
76148b27 138 if (!result) return res.status(HttpStatusCode.NOT_FOUND_404).end()
90a8bd30 139
8efc27bf 140 // Torrents still use the old naming convention (video uuid + .torrent)
557b13ae
C
141 return res.sendFile(result.path, { maxAge: STATIC_MAX_AGE.SERVER })
142}