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