aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/controllers/api/videos/files.ts
blob: 6d9c0b843a90c769a01de7367b0ee1b1db2b3357 (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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
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 { removeAllWebTorrentFiles, removeHLSFile, removeHLSPlaylist, removeWebTorrentFile } from '@server/lib/video-file'
import { VideoFileModel } from '@server/models/video/video-file'
import { HttpStatusCode, UserRight } from '@shared/models'
import {
  asyncMiddleware,
  authenticate,
  ensureUserHasRight,
  videoFileMetadataGetValidator,
  videoFilesDeleteHLSFileValidator,
  videoFilesDeleteHLSValidator,
  videoFilesDeleteWebTorrentFileValidator,
  videoFilesDeleteWebTorrentValidator,
  videosGetValidator
} from '../../../middlewares'
import { updatePlaylistAfterFileChange } from '@server/lib/hls'

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

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

filesRouter.delete('/:id/hls',
  authenticate,
  ensureUserHasRight(UserRight.MANAGE_VIDEO_FILES),
  asyncMiddleware(videoFilesDeleteHLSValidator),
  asyncMiddleware(removeHLSPlaylistController)
)
filesRouter.delete('/:id/hls/:videoFileId',
  authenticate,
  ensureUserHasRight(UserRight.MANAGE_VIDEO_FILES),
  asyncMiddleware(videoFilesDeleteHLSFileValidator),
  asyncMiddleware(removeHLSFileController)
)

filesRouter.delete('/:id/webtorrent',
  authenticate,
  ensureUserHasRight(UserRight.MANAGE_VIDEO_FILES),
  asyncMiddleware(videoFilesDeleteWebTorrentValidator),
  asyncMiddleware(removeAllWebTorrentFilesController)
)
filesRouter.delete('/:id/webtorrent/:videoFileId',
  authenticate,
  ensureUserHasRight(UserRight.MANAGE_VIDEO_FILES),
  asyncMiddleware(videoFilesDeleteWebTorrentFileValidator),
  asyncMiddleware(removeWebTorrentFileController)
)

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

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 removeHLSPlaylistController (req: express.Request, res: express.Response) {
  const video = res.locals.videoAll

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

  await federateVideoIfNeeded(video, false, undefined)

  return res.sendStatus(HttpStatusCode.NO_CONTENT_204)
}

async function removeHLSFileController (req: express.Request, res: express.Response) {
  const video = res.locals.videoAll
  const videoFileId = +req.params.videoFileId

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

  const playlist = await removeHLSFile(video, videoFileId)
  if (playlist) await updatePlaylistAfterFileChange(video, playlist)

  await federateVideoIfNeeded(video, false, undefined)

  return res.sendStatus(HttpStatusCode.NO_CONTENT_204)
}

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

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

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

  await removeAllWebTorrentFiles(video)
  await federateVideoIfNeeded(video, false, undefined)

  return res.sendStatus(HttpStatusCode.NO_CONTENT_204)
}

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

  const videoFileId = +req.params.videoFileId
  logger.info('Deleting WebTorrent file %d of %s.', videoFileId, video.url, lTags(video.uuid))

  await removeWebTorrentFile(video, videoFileId)
  await federateVideoIfNeeded(video, false, undefined)

  return res.sendStatus(HttpStatusCode.NO_CONTENT_204)
}