]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - 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
4d4e5cd4 1import * as cors from 'cors'
50d6de9c 2import * as express from 'express'
02756fbd 3import { CONFIG, STATIC_DOWNLOAD_PATHS, STATIC_MAX_AGE, STATIC_PATHS } from '../initializers'
50d6de9c 4import { VideosPreviewCache } from '../lib/cache'
02756fbd
C
5import { asyncMiddleware, videosGetValidator } from '../middlewares'
6import { VideoModel } from '../models/video/video'
65fcc311
C
7
8const staticRouter = express.Router()
9
10/*
60862425 11 Cors is very important to let other servers access torrent and video files
65fcc311
C
12*/
13
14const torrentsPhysicalPath = CONFIG.STORAGE.TORRENTS_DIR
15staticRouter.use(
16 STATIC_PATHS.TORRENTS,
17 cors(),
49379960 18 express.static(torrentsPhysicalPath, { maxAge: 0 }) // Don't cache because we could regenerate the torrent file
65fcc311 19)
02756fbd
C
20staticRouter.use(
21 STATIC_DOWNLOAD_PATHS.TORRENTS + ':id-:resolution([0-9]+).torrent',
22 asyncMiddleware(videosGetValidator),
23 asyncMiddleware(downloadTorrent)
24)
65fcc311
C
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)
02756fbd
C
33staticRouter.use(
34 STATIC_DOWNLOAD_PATHS.VIDEOS + ':id-:resolution([0-9]+).:extension',
35 asyncMiddleware(videosGetValidator),
36 asyncMiddleware(downloadVideoFile)
37)
65fcc311
C
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
c5911fd3
C
46const avatarsPhysicalPath = CONFIG.STORAGE.AVATARS_DIR
47staticRouter.use(
48 STATIC_PATHS.AVATARS,
49 express.static(avatarsPhysicalPath, { maxAge: STATIC_MAX_AGE })
50)
51
65fcc311 52// Video previews path for express
65fcc311 53staticRouter.use(
f981dae8 54 STATIC_PATHS.PREVIEWS + ':uuid.jpg',
eb080476 55 asyncMiddleware(getPreview)
65fcc311
C
56)
57
ac235c37
RK
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
65fcc311
C
64// ---------------------------------------------------------------------------
65
66export {
67 staticRouter
68}
f981dae8
C
69
70// ---------------------------------------------------------------------------
71
eb080476
C
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)
f981dae8 75
eb080476 76 return res.sendFile(path, { maxAge: STATIC_MAX_AGE })
f981dae8 77}
02756fbd
C
78
79async function downloadTorrent (req: express.Request, res: express.Response, next: express.NextFunction) {
9118bca3 80 const { video, videoFile } = getVideoAndFile(req, res)
02756fbd
C
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) {
9118bca3 87 const { video, videoFile } = getVideoAndFile(req, res)
02756fbd
C
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
9118bca3 93function getVideoAndFile (req: express.Request, res: express.Response) {
02756fbd
C
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}