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