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