ニジカ投稿局 https://tv.nizika.tv
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

302 lines
8.8 KiB

  1. import { forceNumber } from '@peertube/peertube-core-utils'
  2. import { FileStorage, HttpStatusCode, VideoStreamingPlaylistType } from '@peertube/peertube-models'
  3. import { logger } from '@server/helpers/logger.js'
  4. import { VideoTorrentsSimpleFileCache } from '@server/lib/files-cache/index.js'
  5. import {
  6. generateHLSFilePresignedUrl,
  7. generateOriginalFilePresignedUrl,
  8. generateUserExportPresignedUrl,
  9. generateWebVideoPresignedUrl
  10. } from '@server/lib/object-storage/index.js'
  11. import { getFSUserExportFilePath } from '@server/lib/paths.js'
  12. import { Hooks } from '@server/lib/plugins/hooks.js'
  13. import { VideoPathManager } from '@server/lib/video-path-manager.js'
  14. import {
  15. MStreamingPlaylist,
  16. MStreamingPlaylistVideo,
  17. MUserExport,
  18. MVideo,
  19. MVideoFile,
  20. MVideoFullLight
  21. } from '@server/types/models/index.js'
  22. import { MVideoSource } from '@server/types/models/video/video-source.js'
  23. import cors from 'cors'
  24. import express from 'express'
  25. import { STATIC_DOWNLOAD_PATHS } from '../initializers/constants.js'
  26. import {
  27. asyncMiddleware, optionalAuthenticate,
  28. originalVideoFileDownloadValidator,
  29. userExportDownloadValidator,
  30. videosDownloadValidator
  31. } from '../middlewares/index.js'
  32. const downloadRouter = express.Router()
  33. downloadRouter.use(cors())
  34. downloadRouter.use(
  35. STATIC_DOWNLOAD_PATHS.TORRENTS + ':filename',
  36. asyncMiddleware(downloadTorrent)
  37. )
  38. downloadRouter.use(
  39. STATIC_DOWNLOAD_PATHS.VIDEOS + ':id-:resolution([0-9]+).:extension',
  40. optionalAuthenticate,
  41. asyncMiddleware(videosDownloadValidator),
  42. asyncMiddleware(downloadWebVideoFile)
  43. )
  44. downloadRouter.use(
  45. STATIC_DOWNLOAD_PATHS.HLS_VIDEOS + ':id-:resolution([0-9]+)-fragmented.:extension',
  46. optionalAuthenticate,
  47. asyncMiddleware(videosDownloadValidator),
  48. asyncMiddleware(downloadHLSVideoFile)
  49. )
  50. downloadRouter.use(
  51. STATIC_DOWNLOAD_PATHS.USER_EXPORTS + ':filename',
  52. asyncMiddleware(userExportDownloadValidator), // Include JWT token authentication
  53. asyncMiddleware(downloadUserExport)
  54. )
  55. downloadRouter.use(
  56. STATIC_DOWNLOAD_PATHS.ORIGINAL_VIDEO_FILE + ':filename',
  57. optionalAuthenticate,
  58. asyncMiddleware(originalVideoFileDownloadValidator),
  59. asyncMiddleware(downloadOriginalFile)
  60. )
  61. // ---------------------------------------------------------------------------
  62. export {
  63. downloadRouter
  64. }
  65. // ---------------------------------------------------------------------------
  66. async function downloadTorrent (req: express.Request, res: express.Response) {
  67. const result = await VideoTorrentsSimpleFileCache.Instance.getFilePath(req.params.filename)
  68. if (!result) {
  69. return res.fail({
  70. status: HttpStatusCode.NOT_FOUND_404,
  71. message: 'Torrent file not found'
  72. })
  73. }
  74. const allowParameters = {
  75. req,
  76. res,
  77. torrentPath: result.path,
  78. downloadName: result.downloadName
  79. }
  80. const allowedResult = await Hooks.wrapFun(
  81. isTorrentDownloadAllowed,
  82. allowParameters,
  83. 'filter:api.download.torrent.allowed.result'
  84. )
  85. if (!checkAllowResult(res, allowParameters, allowedResult)) return
  86. return res.download(result.path, result.downloadName)
  87. }
  88. async function downloadWebVideoFile (req: express.Request, res: express.Response) {
  89. const video = res.locals.videoAll
  90. const videoFile = getVideoFile(req, video.VideoFiles)
  91. if (!videoFile) {
  92. return res.fail({
  93. status: HttpStatusCode.NOT_FOUND_404,
  94. message: 'Video file not found'
  95. })
  96. }
  97. const allowParameters = {
  98. req,
  99. res,
  100. video,
  101. videoFile
  102. }
  103. const allowedResult = await Hooks.wrapFun(
  104. isVideoDownloadAllowed,
  105. allowParameters,
  106. 'filter:api.download.video.allowed.result'
  107. )
  108. if (!checkAllowResult(res, allowParameters, allowedResult)) return
  109. // Express uses basename on filename parameter
  110. const videoName = video.name.replace(/[/\\]/g, '_')
  111. const downloadFilename = `${videoName}-${videoFile.resolution}p${videoFile.extname}`
  112. if (videoFile.storage === FileStorage.OBJECT_STORAGE) {
  113. return redirectVideoDownloadToObjectStorage({ res, video, file: videoFile, downloadFilename })
  114. }
  115. await VideoPathManager.Instance.makeAvailableVideoFile(videoFile.withVideoOrPlaylist(video), path => {
  116. return res.download(path, downloadFilename)
  117. })
  118. }
  119. async function downloadHLSVideoFile (req: express.Request, res: express.Response) {
  120. const video = res.locals.videoAll
  121. const streamingPlaylist = getHLSPlaylist(video)
  122. if (!streamingPlaylist) return res.sendStatus(HttpStatusCode.NOT_FOUND_404)
  123. const videoFile = getVideoFile(req, streamingPlaylist.VideoFiles)
  124. if (!videoFile) {
  125. return res.fail({
  126. status: HttpStatusCode.NOT_FOUND_404,
  127. message: 'Video file not found'
  128. })
  129. }
  130. const allowParameters = {
  131. req,
  132. res,
  133. video,
  134. streamingPlaylist,
  135. videoFile
  136. }
  137. const allowedResult = await Hooks.wrapFun(
  138. isVideoDownloadAllowed,
  139. allowParameters,
  140. 'filter:api.download.video.allowed.result'
  141. )
  142. if (!checkAllowResult(res, allowParameters, allowedResult)) return
  143. const videoName = video.name.replace(/\//g, '_')
  144. const downloadFilename = `${videoName}-${videoFile.resolution}p-${streamingPlaylist.getStringType()}${videoFile.extname}`
  145. if (videoFile.storage === FileStorage.OBJECT_STORAGE) {
  146. return redirectVideoDownloadToObjectStorage({ res, video, streamingPlaylist, file: videoFile, downloadFilename })
  147. }
  148. await VideoPathManager.Instance.makeAvailableVideoFile(videoFile.withVideoOrPlaylist(streamingPlaylist), path => {
  149. return res.download(path, downloadFilename)
  150. })
  151. }
  152. function downloadUserExport (req: express.Request, res: express.Response) {
  153. const userExport = res.locals.userExport
  154. const downloadFilename = userExport.filename
  155. if (userExport.storage === FileStorage.OBJECT_STORAGE) {
  156. return redirectUserExportToObjectStorage({ res, userExport, downloadFilename })
  157. }
  158. res.download(getFSUserExportFilePath(userExport), downloadFilename)
  159. return Promise.resolve()
  160. }
  161. function downloadOriginalFile (req: express.Request, res: express.Response) {
  162. const videoSource = res.locals.videoSource
  163. const downloadFilename = videoSource.inputFilename
  164. if (videoSource.storage === FileStorage.OBJECT_STORAGE) {
  165. return redirectOriginalFileToObjectStorage({ res, videoSource, downloadFilename })
  166. }
  167. res.download(VideoPathManager.Instance.getFSOriginalVideoFilePath(videoSource.keptOriginalFilename), downloadFilename)
  168. return Promise.resolve()
  169. }
  170. // ---------------------------------------------------------------------------
  171. function getVideoFile (req: express.Request, files: MVideoFile[]) {
  172. const resolution = forceNumber(req.params.resolution)
  173. return files.find(f => f.resolution === resolution)
  174. }
  175. function getHLSPlaylist (video: MVideoFullLight) {
  176. const playlist = video.VideoStreamingPlaylists.find(p => p.type === VideoStreamingPlaylistType.HLS)
  177. if (!playlist) return undefined
  178. return Object.assign(playlist, { Video: video })
  179. }
  180. type AllowedResult = {
  181. allowed: boolean
  182. errorMessage?: string
  183. }
  184. function isTorrentDownloadAllowed (_object: {
  185. torrentPath: string
  186. }): AllowedResult {
  187. return { allowed: true }
  188. }
  189. function isVideoDownloadAllowed (_object: {
  190. video: MVideo
  191. videoFile: MVideoFile
  192. streamingPlaylist?: MStreamingPlaylist
  193. }): AllowedResult {
  194. return { allowed: true }
  195. }
  196. function checkAllowResult (res: express.Response, allowParameters: any, result?: AllowedResult) {
  197. if (!result || result.allowed !== true) {
  198. logger.info('Download is not allowed.', { result, allowParameters })
  199. res.fail({
  200. status: HttpStatusCode.FORBIDDEN_403,
  201. message: result?.errorMessage || 'Refused download'
  202. })
  203. return false
  204. }
  205. return true
  206. }
  207. async function redirectVideoDownloadToObjectStorage (options: {
  208. res: express.Response
  209. video: MVideo
  210. file: MVideoFile
  211. streamingPlaylist?: MStreamingPlaylistVideo
  212. downloadFilename: string
  213. }) {
  214. const { res, video, streamingPlaylist, file, downloadFilename } = options
  215. const url = streamingPlaylist
  216. ? await generateHLSFilePresignedUrl({ streamingPlaylist, file, downloadFilename })
  217. : await generateWebVideoPresignedUrl({ file, downloadFilename })
  218. logger.debug('Generating pre-signed URL %s for video %s', url, video.uuid)
  219. return res.redirect(url)
  220. }
  221. async function redirectUserExportToObjectStorage (options: {
  222. res: express.Response
  223. downloadFilename: string
  224. userExport: MUserExport
  225. }) {
  226. const { res, downloadFilename, userExport } = options
  227. const url = await generateUserExportPresignedUrl({ userExport, downloadFilename })
  228. logger.debug('Generating pre-signed URL %s for user export %s', url, userExport.filename)
  229. return res.redirect(url)
  230. }
  231. async function redirectOriginalFileToObjectStorage (options: {
  232. res: express.Response
  233. downloadFilename: string
  234. videoSource: MVideoSource
  235. }) {
  236. const { res, downloadFilename, videoSource } = options
  237. const url = await generateOriginalFilePresignedUrl({ videoSource, downloadFilename })
  238. logger.debug('Generating pre-signed URL %s for original video file %s', url, videoSource.keptOriginalFilename)
  239. return res.redirect(url)
  240. }