]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/static.ts
Correctly handle video import errors
[github/Chocobozzz/PeerTube.git] / server / controllers / static.ts
CommitLineData
4d4e5cd4 1import * as cors from 'cors'
50d6de9c 2import * as express from 'express'
3f6d68d9 3import { CONFIG, STATIC_DOWNLOAD_PATHS, STATIC_MAX_AGE, STATIC_PATHS, ROUTE_CACHE_LIFETIME } from '../initializers'
50d6de9c 4import { VideosPreviewCache } from '../lib/cache'
98d3324d 5import { cacheRoute } from '../middlewares/cache'
02756fbd
C
6import { asyncMiddleware, videosGetValidator } from '../middlewares'
7import { VideoModel } from '../models/video/video'
40e87e9e 8import { VideosCaptionCache } from '../lib/cache/videos-caption-cache'
3f6d68d9
RK
9import { UserModel } from '../models/account/user'
10import { VideoCommentModel } from '../models/video/video-comment'
11import { HttpNodeinfoDiasporaSoftwareNsSchema20 } from '../models/nodeinfo'
65fcc311 12
3f6d68d9 13const packageJSON = require('../../../package.json')
65fcc311
C
14const staticRouter = express.Router()
15
62945f06
C
16staticRouter.use(cors())
17
65fcc311 18/*
60862425 19 Cors is very important to let other servers access torrent and video files
65fcc311
C
20*/
21
22const torrentsPhysicalPath = CONFIG.STORAGE.TORRENTS_DIR
23staticRouter.use(
24 STATIC_PATHS.TORRENTS,
25 cors(),
49379960 26 express.static(torrentsPhysicalPath, { maxAge: 0 }) // Don't cache because we could regenerate the torrent file
65fcc311 27)
02756fbd
C
28staticRouter.use(
29 STATIC_DOWNLOAD_PATHS.TORRENTS + ':id-:resolution([0-9]+).torrent',
30 asyncMiddleware(videosGetValidator),
31 asyncMiddleware(downloadTorrent)
32)
65fcc311
C
33
34// Videos path for webseeding
35const videosPhysicalPath = CONFIG.STORAGE.VIDEOS_DIR
36staticRouter.use(
37 STATIC_PATHS.WEBSEED,
38 cors(),
57a81ff6 39 express.static(videosPhysicalPath)
65fcc311 40)
02756fbd
C
41staticRouter.use(
42 STATIC_DOWNLOAD_PATHS.VIDEOS + ':id-:resolution([0-9]+).:extension',
43 asyncMiddleware(videosGetValidator),
44 asyncMiddleware(downloadVideoFile)
45)
65fcc311
C
46
47// Thumbnails path for express
48const thumbnailsPhysicalPath = CONFIG.STORAGE.THUMBNAILS_DIR
49staticRouter.use(
50 STATIC_PATHS.THUMBNAILS,
a8bf1d82 51 express.static(thumbnailsPhysicalPath, { maxAge: STATIC_MAX_AGE, fallthrough: false }) // 404 if the file does not exist
65fcc311
C
52)
53
c5911fd3
C
54const avatarsPhysicalPath = CONFIG.STORAGE.AVATARS_DIR
55staticRouter.use(
56 STATIC_PATHS.AVATARS,
a8bf1d82 57 express.static(avatarsPhysicalPath, { maxAge: STATIC_MAX_AGE, fallthrough: false }) // 404 if the file does not exist
c5911fd3
C
58)
59
40e87e9e 60// We don't have video previews, fetch them from the origin instance
65fcc311 61staticRouter.use(
f981dae8 62 STATIC_PATHS.PREVIEWS + ':uuid.jpg',
eb080476 63 asyncMiddleware(getPreview)
65fcc311
C
64)
65
40e87e9e
C
66// We don't have video captions, fetch them from the origin instance
67staticRouter.use(
68 STATIC_PATHS.VIDEO_CAPTIONS + ':videoId-:captionLanguage([a-z]+).vtt',
69 asyncMiddleware(getVideoCaption)
70)
71
ac235c37 72// robots.txt service
3f6d68d9 73staticRouter.get('/robots.txt',
98d3324d 74 asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.ROBOTS)),
3f6d68d9
RK
75 (_, res: express.Response) => {
76 res.type('text/plain')
77 return res.send(CONFIG.INSTANCE.ROBOTS)
78 }
79)
80
81// nodeinfo service
82staticRouter.use('/.well-known/nodeinfo',
98d3324d 83 asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.NODEINFO)),
3f6d68d9
RK
84 (_, res: express.Response) => {
85 return res.json({
86 links: [
87 {
88 rel: 'http://nodeinfo.diaspora.software/ns/schema/2.0',
89 href: CONFIG.WEBSERVER.URL + '/nodeinfo/2.0.json'
90 }
91 ]
92 })
93 }
94)
95staticRouter.use('/nodeinfo/:version.json',
98d3324d 96 // asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.NODEINFO)),
3f6d68d9
RK
97 asyncMiddleware(generateNodeinfo)
98)
ac235c37 99
65fcc311
C
100// ---------------------------------------------------------------------------
101
102export {
103 staticRouter
104}
f981dae8
C
105
106// ---------------------------------------------------------------------------
107
eb080476 108async function getPreview (req: express.Request, res: express.Response, next: express.NextFunction) {
40e87e9e
C
109 const path = await VideosPreviewCache.Instance.getFilePath(req.params.uuid)
110 if (!path) return res.sendStatus(404)
111
112 return res.sendFile(path, { maxAge: STATIC_MAX_AGE })
113}
114
115async function getVideoCaption (req: express.Request, res: express.Response) {
116 const path = await VideosCaptionCache.Instance.getFilePath({
117 videoId: req.params.videoId,
118 language: req.params.captionLanguage
119 })
eb080476 120 if (!path) return res.sendStatus(404)
f981dae8 121
eb080476 122 return res.sendFile(path, { maxAge: STATIC_MAX_AGE })
f981dae8 123}
02756fbd 124
3f6d68d9
RK
125async function generateNodeinfo (req: express.Request, res: express.Response, next: express.NextFunction) {
126 const { totalVideos } = await VideoModel.getStats()
127 const { totalLocalVideoComments } = await VideoCommentModel.getStats()
128 const { totalUsers } = await UserModel.getStats()
129 let json = {}
130
131 if (req.params.version && (req.params.version === '2.0')) {
132 json = {
133 version: '2.0',
134 software: {
135 name: 'peertube',
136 version: packageJSON.version
137 },
138 protocols: [
139 'activitypub'
140 ],
141 services: {
142 inbound: [],
143 outbound: [
144 'atom1.0',
145 'rss2.0'
146 ]
147 },
148 openRegistrations: CONFIG.SIGNUP.ENABLED,
149 usage: {
150 users: {
151 total: totalUsers
152 },
153 localPosts: totalVideos,
154 localComments: totalLocalVideoComments
155 },
156 metadata: {
157 taxonomy: {
158 postsName: 'Videos'
159 },
160 nodeName: CONFIG.INSTANCE.NAME,
161 nodeDescription: CONFIG.INSTANCE.SHORT_DESCRIPTION
162 }
163 } as HttpNodeinfoDiasporaSoftwareNsSchema20
98d3324d 164 res.contentType('application/json; profile="http://nodeinfo.diaspora.software/ns/schema/2.0#"')
3f6d68d9
RK
165 } else {
166 json = { error: 'Nodeinfo schema version not handled' }
167 res.status(404)
168 }
169
98d3324d 170 return res.send(json).end()
3f6d68d9
RK
171}
172
02756fbd 173async function downloadTorrent (req: express.Request, res: express.Response, next: express.NextFunction) {
9118bca3 174 const { video, videoFile } = getVideoAndFile(req, res)
02756fbd
C
175 if (!videoFile) return res.status(404).end()
176
177 return res.download(video.getTorrentFilePath(videoFile), `${video.name}-${videoFile.resolution}p.torrent`)
178}
179
180async function downloadVideoFile (req: express.Request, res: express.Response, next: express.NextFunction) {
9118bca3 181 const { video, videoFile } = getVideoAndFile(req, res)
02756fbd
C
182 if (!videoFile) return res.status(404).end()
183
184 return res.download(video.getVideoFilePath(videoFile), `${video.name}-${videoFile.resolution}p${videoFile.extname}`)
185}
186
9118bca3 187function getVideoAndFile (req: express.Request, res: express.Response) {
02756fbd
C
188 const resolution = parseInt(req.params.resolution, 10)
189 const video: VideoModel = res.locals.video
190
191 const videoFile = video.VideoFiles.find(f => f.resolution === resolution)
192
193 return { video, videoFile }
194}