aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/controllers/api/videos/files.ts
blob: 2fe4b5a3f8bf1208ff30ad151f15b889f1fd3436 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import express from 'express'
import toInt from 'validator/lib/toInt'
import { logger, loggerTagsFactory } from '@server/helpers/logger'
import { federateVideoIfNeeded } from '@server/lib/activitypub/videos'
import { VideoFileModel } from '@server/models/video/video-file'
import { HttpStatusCode } from '@shared/models'
import {
  asyncMiddleware,
  authenticate,
  videoFileMetadataGetValidator,
  videoFilesDeleteHLSValidator,
  videoFilesDeleteWebTorrentValidator
} from '../../../middlewares'

const lTags = loggerTagsFactory('api', 'video')
const filesRouter = express.Router()

filesRouter.get('/:id/metadata/:videoFileId',
  asyncMiddleware(videoFileMetadataGetValidator),
  asyncMiddleware(getVideoFileMetadata)
)

filesRouter.delete('/:id/hls',
  authenticate,
  asyncMiddleware(videoFilesDeleteHLSValidator),
  asyncMiddleware(removeHLSPlaylist)
)

filesRouter.delete('/:id/webtorrent',
  authenticate,
  asyncMiddleware(videoFilesDeleteWebTorrentValidator),
  asyncMiddleware(removeWebTorrentFiles)
)

// ---------------------------------------------------------------------------

export {
  filesRouter
}

// ---------------------------------------------------------------------------

async function getVideoFileMetadata (req: express.Request, res: express.Response) {
  const videoFile = await VideoFileModel.loadWithMetadata(toInt(req.params.videoFileId))

  return res.json(videoFile.metadata)
}

async function removeHLSPlaylist (req: express.Request, res: express.Response) {
  const video = res.locals.videoAll

  logger.info('Deleting HLS playlist of %s.', video.url, lTags(video.uuid))

  const hls = video.getHLSPlaylist()
  await video.removeStreamingPlaylistFiles(hls)
  await hls.destroy()

  video.VideoStreamingPlaylists = video.VideoStreamingPlaylists.filter(p => p.id !== hls.id)

  await federateVideoIfNeeded(video, false, undefined)

  return res.sendStatus(HttpStatusCode.NO_CONTENT_204)
}

async function removeWebTorrentFiles (req: express.Request, res: express.Response) {
  const video = res.locals.videoAll

  logger.info('Deleting WebTorrent files of %s.', video.url, lTags(video.uuid))

  for (const file of video.VideoFiles) {
    await video.removeWebTorrentFileAndTorrent(file)
    await file.destroy()
  }

  video.VideoFiles = []
  await federateVideoIfNeeded(video, false, undefined)

  return res.sendStatus(HttpStatusCode.NO_CONTENT_204)
}