aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/controllers
diff options
context:
space:
mode:
Diffstat (limited to 'server/controllers')
-rw-r--r--server/controllers/index.ts1
-rw-r--r--server/controllers/lazy-static.ts80
-rw-r--r--server/controllers/static.ts24
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'
4export * from './feeds' 4export * from './feeds'
5export * from './services' 5export * from './services'
6export * from './static' 6export * from './static'
7export * from './lazy-static'
7export * from './webfinger' 8export * from './webfinger'
8export * from './tracker' 9export * from './tracker'
9export * from './bots' 10export * 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 @@
1import * as cors from 'cors'
2import * as express from 'express'
3import { LAZY_STATIC_PATHS, STATIC_MAX_AGE } from '../initializers/constants'
4import { VideosCaptionCache, VideosPreviewCache } from '../lib/files-cache'
5import { asyncMiddleware } from '../middlewares'
6import { AvatarModel } from '../models/avatar/avatar'
7import { logger } from '../helpers/logger'
8import { avatarPathUnsafeCache, pushAvatarProcessInQueue } from '../lib/avatar'
9
10const lazyStaticRouter = express.Router()
11
12lazyStaticRouter.use(cors())
13
14lazyStaticRouter.use(
15 LAZY_STATIC_PATHS.AVATARS + ':filename',
16 asyncMiddleware(getAvatar)
17)
18
19lazyStaticRouter.use(
20 LAZY_STATIC_PATHS.PREVIEWS + ':uuid.jpg',
21 asyncMiddleware(getPreview)
22)
23
24lazyStaticRouter.use(
25 LAZY_STATIC_PATHS.VIDEO_CAPTIONS + ':videoId-:captionLanguage([a-z]+).vtt',
26 asyncMiddleware(getVideoCaption)
27)
28
29// ---------------------------------------------------------------------------
30
31export {
32 lazyStaticRouter,
33 getPreview,
34 getVideoCaption
35}
36
37// ---------------------------------------------------------------------------
38
39async 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
65async 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
72async 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'
12import { VideosCaptionCache, VideosPreviewCache } from '../lib/files-cache'
13import { cacheRoute } from '../middlewares/cache' 12import { cacheRoute } from '../middlewares/cache'
14import { asyncMiddleware, videosGetValidator } from '../middlewares' 13import { asyncMiddleware, videosGetValidator } from '../middlewares'
15import { VideoModel } from '../models/video/video' 14import { VideoModel } from '../models/video/video'
@@ -19,6 +18,7 @@ import { HttpNodeinfoDiasporaSoftwareNsSchema20 } from '../../shared/models/node
19import { join } from 'path' 18import { join } from 'path'
20import { root } from '../helpers/core-utils' 19import { root } from '../helpers/core-utils'
21import { CONFIG } from '../initializers/config' 20import { CONFIG } from '../initializers/config'
21import { getPreview, getVideoCaption } from './lazy-static'
22 22
23const staticRouter = express.Router() 23const 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
75const avatarsPhysicalPath = CONFIG.STORAGE.AVATARS_DIR 76const avatarsPhysicalPath = CONFIG.STORAGE.AVATARS_DIR
76staticRouter.use( 77staticRouter.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
82staticRouter.use( 83staticRouter.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
88staticRouter.use( 89staticRouter.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
180async 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
187async 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
197async function generateNodeinfo (req: express.Request, res: express.Response) { 181async 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()