diff options
Diffstat (limited to 'server/controllers')
-rw-r--r-- | server/controllers/index.ts | 1 | ||||
-rw-r--r-- | server/controllers/lazy-static.ts | 80 | ||||
-rw-r--r-- | server/controllers/static.ts | 24 |
3 files changed, 85 insertions, 20 deletions
diff --git a/server/controllers/index.ts b/server/controllers/index.ts index 8b3501712..0d64b33bb 100644 --- a/server/controllers/index.ts +++ b/server/controllers/index.ts | |||
@@ -4,6 +4,7 @@ export * from './client' | |||
4 | export * from './feeds' | 4 | export * from './feeds' |
5 | export * from './services' | 5 | export * from './services' |
6 | export * from './static' | 6 | export * from './static' |
7 | export * from './lazy-static' | ||
7 | export * from './webfinger' | 8 | export * from './webfinger' |
8 | export * from './tracker' | 9 | export * from './tracker' |
9 | export * from './bots' | 10 | export * from './bots' |
diff --git a/server/controllers/lazy-static.ts b/server/controllers/lazy-static.ts new file mode 100644 index 000000000..4285fd727 --- /dev/null +++ b/server/controllers/lazy-static.ts | |||
@@ -0,0 +1,80 @@ | |||
1 | import * as cors from 'cors' | ||
2 | import * as express from 'express' | ||
3 | import { LAZY_STATIC_PATHS, STATIC_MAX_AGE } from '../initializers/constants' | ||
4 | import { VideosCaptionCache, VideosPreviewCache } from '../lib/files-cache' | ||
5 | import { asyncMiddleware } from '../middlewares' | ||
6 | import { AvatarModel } from '../models/avatar/avatar' | ||
7 | import { logger } from '../helpers/logger' | ||
8 | import { avatarPathUnsafeCache, pushAvatarProcessInQueue } from '../lib/avatar' | ||
9 | |||
10 | const lazyStaticRouter = express.Router() | ||
11 | |||
12 | lazyStaticRouter.use(cors()) | ||
13 | |||
14 | lazyStaticRouter.use( | ||
15 | LAZY_STATIC_PATHS.AVATARS + ':filename', | ||
16 | asyncMiddleware(getAvatar) | ||
17 | ) | ||
18 | |||
19 | lazyStaticRouter.use( | ||
20 | LAZY_STATIC_PATHS.PREVIEWS + ':uuid.jpg', | ||
21 | asyncMiddleware(getPreview) | ||
22 | ) | ||
23 | |||
24 | lazyStaticRouter.use( | ||
25 | LAZY_STATIC_PATHS.VIDEO_CAPTIONS + ':videoId-:captionLanguage([a-z]+).vtt', | ||
26 | asyncMiddleware(getVideoCaption) | ||
27 | ) | ||
28 | |||
29 | // --------------------------------------------------------------------------- | ||
30 | |||
31 | export { | ||
32 | lazyStaticRouter, | ||
33 | getPreview, | ||
34 | getVideoCaption | ||
35 | } | ||
36 | |||
37 | // --------------------------------------------------------------------------- | ||
38 | |||
39 | async function getAvatar (req: express.Request, res: express.Response) { | ||
40 | const filename = req.params.filename | ||
41 | |||
42 | if (avatarPathUnsafeCache.has(filename)) { | ||
43 | return res.sendFile(avatarPathUnsafeCache.get(filename), { maxAge: STATIC_MAX_AGE.SERVER }) | ||
44 | } | ||
45 | |||
46 | const avatar = await AvatarModel.loadByName(filename) | ||
47 | if (avatar.onDisk === false) { | ||
48 | if (!avatar.fileUrl) return res.sendStatus(404) | ||
49 | |||
50 | logger.info('Lazy serve remote avatar image %s.', avatar.fileUrl) | ||
51 | |||
52 | await pushAvatarProcessInQueue({ filename: avatar.filename, fileUrl: avatar.fileUrl }) | ||
53 | |||
54 | avatar.onDisk = true | ||
55 | avatar.save() | ||
56 | .catch(err => logger.error('Cannot save new avatar disk state.', { err })) | ||
57 | } | ||
58 | |||
59 | const path = avatar.getPath() | ||
60 | |||
61 | avatarPathUnsafeCache.set(filename, path) | ||
62 | return res.sendFile(path, { maxAge: STATIC_MAX_AGE.SERVER }) | ||
63 | } | ||
64 | |||
65 | async function getPreview (req: express.Request, res: express.Response) { | ||
66 | const result = await VideosPreviewCache.Instance.getFilePath(req.params.uuid) | ||
67 | if (!result) return res.sendStatus(404) | ||
68 | |||
69 | return res.sendFile(result.path, { maxAge: STATIC_MAX_AGE.SERVER }) | ||
70 | } | ||
71 | |||
72 | async function getVideoCaption (req: express.Request, res: express.Response) { | ||
73 | const result = await VideosCaptionCache.Instance.getFilePath({ | ||
74 | videoId: req.params.videoId, | ||
75 | language: req.params.captionLanguage | ||
76 | }) | ||
77 | if (!result) return res.sendStatus(404) | ||
78 | |||
79 | return res.sendFile(result.path, { maxAge: STATIC_MAX_AGE.SERVER }) | ||
80 | } | ||
diff --git a/server/controllers/static.ts b/server/controllers/static.ts index 110d25031..8979ef5f3 100644 --- a/server/controllers/static.ts +++ b/server/controllers/static.ts | |||
@@ -9,7 +9,6 @@ import { | |||
9 | STATIC_PATHS, | 9 | STATIC_PATHS, |
10 | WEBSERVER | 10 | WEBSERVER |
11 | } from '../initializers/constants' | 11 | } from '../initializers/constants' |
12 | import { VideosCaptionCache, VideosPreviewCache } from '../lib/files-cache' | ||
13 | import { cacheRoute } from '../middlewares/cache' | 12 | import { cacheRoute } from '../middlewares/cache' |
14 | import { asyncMiddleware, videosGetValidator } from '../middlewares' | 13 | import { asyncMiddleware, videosGetValidator } from '../middlewares' |
15 | import { VideoModel } from '../models/video/video' | 14 | import { VideoModel } from '../models/video/video' |
@@ -19,6 +18,7 @@ import { HttpNodeinfoDiasporaSoftwareNsSchema20 } from '../../shared/models/node | |||
19 | import { join } from 'path' | 18 | import { join } from 'path' |
20 | import { root } from '../helpers/core-utils' | 19 | import { root } from '../helpers/core-utils' |
21 | import { CONFIG } from '../initializers/config' | 20 | import { CONFIG } from '../initializers/config' |
21 | import { getPreview, getVideoCaption } from './lazy-static' | ||
22 | 22 | ||
23 | const staticRouter = express.Router() | 23 | const staticRouter = express.Router() |
24 | 24 | ||
@@ -72,19 +72,20 @@ staticRouter.use( | |||
72 | express.static(thumbnailsPhysicalPath, { maxAge: STATIC_MAX_AGE.SERVER, fallthrough: false }) // 404 if the file does not exist | 72 | express.static(thumbnailsPhysicalPath, { maxAge: STATIC_MAX_AGE.SERVER, fallthrough: false }) // 404 if the file does not exist |
73 | ) | 73 | ) |
74 | 74 | ||
75 | // DEPRECATED: use lazy-static route instead | ||
75 | const avatarsPhysicalPath = CONFIG.STORAGE.AVATARS_DIR | 76 | const avatarsPhysicalPath = CONFIG.STORAGE.AVATARS_DIR |
76 | staticRouter.use( | 77 | staticRouter.use( |
77 | STATIC_PATHS.AVATARS, | 78 | STATIC_PATHS.AVATARS, |
78 | express.static(avatarsPhysicalPath, { maxAge: STATIC_MAX_AGE.SERVER, fallthrough: false }) // 404 if the file does not exist | 79 | express.static(avatarsPhysicalPath, { maxAge: STATIC_MAX_AGE.SERVER, fallthrough: false }) // 404 if the file does not exist |
79 | ) | 80 | ) |
80 | 81 | ||
81 | // We don't have video previews, fetch them from the origin instance | 82 | // DEPRECATED: use lazy-static route instead |
82 | staticRouter.use( | 83 | staticRouter.use( |
83 | STATIC_PATHS.PREVIEWS + ':uuid.jpg', | 84 | STATIC_PATHS.PREVIEWS + ':uuid.jpg', |
84 | asyncMiddleware(getPreview) | 85 | asyncMiddleware(getPreview) |
85 | ) | 86 | ) |
86 | 87 | ||
87 | // We don't have video captions, fetch them from the origin instance | 88 | // DEPRECATED: use lazy-static route instead |
88 | staticRouter.use( | 89 | staticRouter.use( |
89 | STATIC_PATHS.VIDEO_CAPTIONS + ':videoId-:captionLanguage([a-z]+).vtt', | 90 | STATIC_PATHS.VIDEO_CAPTIONS + ':videoId-:captionLanguage([a-z]+).vtt', |
90 | asyncMiddleware(getVideoCaption) | 91 | asyncMiddleware(getVideoCaption) |
@@ -177,23 +178,6 @@ export { | |||
177 | 178 | ||
178 | // --------------------------------------------------------------------------- | 179 | // --------------------------------------------------------------------------- |
179 | 180 | ||
180 | async function getPreview (req: express.Request, res: express.Response) { | ||
181 | const result = await VideosPreviewCache.Instance.getFilePath(req.params.uuid) | ||
182 | if (!result) return res.sendStatus(404) | ||
183 | |||
184 | return res.sendFile(result.path, { maxAge: STATIC_MAX_AGE }) | ||
185 | } | ||
186 | |||
187 | async function getVideoCaption (req: express.Request, res: express.Response) { | ||
188 | const result = await VideosCaptionCache.Instance.getFilePath({ | ||
189 | videoId: req.params.videoId, | ||
190 | language: req.params.captionLanguage | ||
191 | }) | ||
192 | if (!result) return res.sendStatus(404) | ||
193 | |||
194 | return res.sendFile(result.path, { maxAge: STATIC_MAX_AGE }) | ||
195 | } | ||
196 | |||
197 | async function generateNodeinfo (req: express.Request, res: express.Response) { | 181 | async function generateNodeinfo (req: express.Request, res: express.Response) { |
198 | const { totalVideos } = await VideoModel.getStats() | 182 | const { totalVideos } = await VideoModel.getStats() |
199 | const { totalLocalVideoComments } = await VideoCommentModel.getStats() | 183 | const { totalLocalVideoComments } = await VideoCommentModel.getStats() |