]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame_incremental - server/controllers/static.ts
Improve update host script and add warning if AP urls are invalid
[github/Chocobozzz/PeerTube.git] / server / controllers / static.ts
... / ...
CommitLineData
1import * as cors from 'cors'
2import * as express from 'express'
3import { CONFIG, STATIC_DOWNLOAD_PATHS, STATIC_MAX_AGE, STATIC_PATHS } from '../initializers'
4import { VideosPreviewCache } from '../lib/cache'
5import { asyncMiddleware, videosGetValidator } from '../middlewares'
6import { VideoModel } from '../models/video/video'
7
8const staticRouter = express.Router()
9
10/*
11 Cors is very important to let other servers access torrent and video files
12*/
13
14const torrentsPhysicalPath = CONFIG.STORAGE.TORRENTS_DIR
15staticRouter.use(
16 STATIC_PATHS.TORRENTS,
17 cors(),
18 express.static(torrentsPhysicalPath, { maxAge: 0 }) // Don't cache because we could regenerate the torrent file
19)
20staticRouter.use(
21 STATIC_DOWNLOAD_PATHS.TORRENTS + ':id-:resolution([0-9]+).torrent',
22 asyncMiddleware(videosGetValidator),
23 asyncMiddleware(downloadTorrent)
24)
25
26// Videos path for webseeding
27const videosPhysicalPath = CONFIG.STORAGE.VIDEOS_DIR
28staticRouter.use(
29 STATIC_PATHS.WEBSEED,
30 cors(),
31 express.static(videosPhysicalPath, { maxAge: STATIC_MAX_AGE })
32)
33staticRouter.use(
34 STATIC_DOWNLOAD_PATHS.VIDEOS + ':id-:resolution([0-9]+).:extension',
35 asyncMiddleware(videosGetValidator),
36 asyncMiddleware(downloadVideoFile)
37)
38
39// Thumbnails path for express
40const thumbnailsPhysicalPath = CONFIG.STORAGE.THUMBNAILS_DIR
41staticRouter.use(
42 STATIC_PATHS.THUMBNAILS,
43 express.static(thumbnailsPhysicalPath, { maxAge: STATIC_MAX_AGE })
44)
45
46const avatarsPhysicalPath = CONFIG.STORAGE.AVATARS_DIR
47staticRouter.use(
48 STATIC_PATHS.AVATARS,
49 express.static(avatarsPhysicalPath, { maxAge: STATIC_MAX_AGE })
50)
51
52// Video previews path for express
53staticRouter.use(
54 STATIC_PATHS.PREVIEWS + ':uuid.jpg',
55 asyncMiddleware(getPreview)
56)
57
58// robots.txt service
59staticRouter.get('/robots.txt', (req: express.Request, res: express.Response) => {
60 res.type('text/plain')
61 return res.send(CONFIG.INSTANCE.ROBOTS)
62})
63
64// ---------------------------------------------------------------------------
65
66export {
67 staticRouter
68}
69
70// ---------------------------------------------------------------------------
71
72async function getPreview (req: express.Request, res: express.Response, next: express.NextFunction) {
73 const path = await VideosPreviewCache.Instance.getPreviewPath(req.params.uuid)
74 if (!path) return res.sendStatus(404)
75
76 return res.sendFile(path, { maxAge: STATIC_MAX_AGE })
77}
78
79async function downloadTorrent (req: express.Request, res: express.Response, next: express.NextFunction) {
80 const { video, videoFile } = getVideoAndFile(req, res)
81 if (!videoFile) return res.status(404).end()
82
83 return res.download(video.getTorrentFilePath(videoFile), `${video.name}-${videoFile.resolution}p.torrent`)
84}
85
86async function downloadVideoFile (req: express.Request, res: express.Response, next: express.NextFunction) {
87 const { video, videoFile } = getVideoAndFile(req, res)
88 if (!videoFile) return res.status(404).end()
89
90 return res.download(video.getVideoFilePath(videoFile), `${video.name}-${videoFile.resolution}p${videoFile.extname}`)
91}
92
93function getVideoAndFile (req: express.Request, res: express.Response) {
94 const resolution = parseInt(req.params.resolution, 10)
95 const video: VideoModel = res.locals.video
96
97 const videoFile = video.VideoFiles.find(f => f.resolution === resolution)
98
99 return { video, videoFile }
100}