mirror of https://github.com/Chocobozzz/PeerTube
Add upload/import/go live video attributes hooks
parent
8cf43a6524
commit
d17d743051
|
@ -38,6 +38,7 @@ import { asyncMiddleware, asyncRetryTransactionMiddleware, authenticate, videoIm
|
||||||
import { VideoModel } from '../../../models/video/video'
|
import { VideoModel } from '../../../models/video/video'
|
||||||
import { VideoCaptionModel } from '../../../models/video/video-caption'
|
import { VideoCaptionModel } from '../../../models/video/video-caption'
|
||||||
import { VideoImportModel } from '../../../models/video/video-import'
|
import { VideoImportModel } from '../../../models/video/video-import'
|
||||||
|
import { Hooks } from '@server/lib/plugins/hooks'
|
||||||
|
|
||||||
const auditLogger = auditLoggerFactory('video-imports')
|
const auditLogger = auditLoggerFactory('video-imports')
|
||||||
const videoImportsRouter = express.Router()
|
const videoImportsRouter = express.Router()
|
||||||
|
@ -94,7 +95,7 @@ async function addTorrentImport (req: express.Request, res: express.Response, to
|
||||||
videoName = result.name
|
videoName = result.name
|
||||||
}
|
}
|
||||||
|
|
||||||
const video = buildVideo(res.locals.videoChannel.id, body, { name: videoName })
|
const video = await buildVideo(res.locals.videoChannel.id, body, { name: videoName })
|
||||||
|
|
||||||
const thumbnailModel = await processThumbnail(req, video)
|
const thumbnailModel = await processThumbnail(req, video)
|
||||||
const previewModel = await processPreview(req, video)
|
const previewModel = await processPreview(req, video)
|
||||||
|
@ -151,7 +152,7 @@ async function addYoutubeDLImport (req: express.Request, res: express.Response)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
const video = buildVideo(res.locals.videoChannel.id, body, youtubeDLInfo)
|
const video = await buildVideo(res.locals.videoChannel.id, body, youtubeDLInfo)
|
||||||
|
|
||||||
// Process video thumbnail from request.files
|
// Process video thumbnail from request.files
|
||||||
let thumbnailModel = await processThumbnail(req, video)
|
let thumbnailModel = await processThumbnail(req, video)
|
||||||
|
@ -210,8 +211,8 @@ async function addYoutubeDLImport (req: express.Request, res: express.Response)
|
||||||
return res.json(videoImport.toFormattedJSON()).end()
|
return res.json(videoImport.toFormattedJSON()).end()
|
||||||
}
|
}
|
||||||
|
|
||||||
function buildVideo (channelId: number, body: VideoImportCreate, importData: YoutubeDLInfo): MVideoThumbnail {
|
async function buildVideo (channelId: number, body: VideoImportCreate, importData: YoutubeDLInfo): Promise<MVideoThumbnail> {
|
||||||
const videoData = {
|
let videoData = {
|
||||||
name: body.name || importData.name || 'Unknown name',
|
name: body.name || importData.name || 'Unknown name',
|
||||||
remote: false,
|
remote: false,
|
||||||
category: body.category || importData.category,
|
category: body.category || importData.category,
|
||||||
|
@ -231,6 +232,14 @@ function buildVideo (channelId: number, body: VideoImportCreate, importData: You
|
||||||
? new Date(body.originallyPublishedAt)
|
? new Date(body.originallyPublishedAt)
|
||||||
: importData.originallyPublishedAt
|
: importData.originallyPublishedAt
|
||||||
}
|
}
|
||||||
|
|
||||||
|
videoData = await Hooks.wrapObject(
|
||||||
|
videoData,
|
||||||
|
body.targetUrl
|
||||||
|
? 'filter:api.video.import-url.video-attribute.result'
|
||||||
|
: 'filter:api.video.import-torrent.video-attribute.result'
|
||||||
|
)
|
||||||
|
|
||||||
const video = new VideoModel(videoData)
|
const video = new VideoModel(videoData)
|
||||||
video.url = getLocalVideoActivityPubUrl(video)
|
video.url = getLocalVideoActivityPubUrl(video)
|
||||||
|
|
||||||
|
|
|
@ -83,7 +83,9 @@ async function addLiveVideo (req: express.Request, res: express.Response) {
|
||||||
const videoInfo: LiveVideoCreate = req.body
|
const videoInfo: LiveVideoCreate = req.body
|
||||||
|
|
||||||
// Prepare data so we don't block the transaction
|
// Prepare data so we don't block the transaction
|
||||||
const videoData = buildLocalVideoFromReq(videoInfo, res.locals.videoChannel.id)
|
let videoData = buildLocalVideoFromReq(videoInfo, res.locals.videoChannel.id)
|
||||||
|
videoData = await Hooks.wrapObject(videoData, 'filter:api.video.live.video-attribute.result')
|
||||||
|
|
||||||
videoData.isLive = true
|
videoData.isLive = true
|
||||||
videoData.state = VideoState.WAITING_FOR_LIVE
|
videoData.state = VideoState.WAITING_FOR_LIVE
|
||||||
videoData.duration = 0
|
videoData.duration = 0
|
||||||
|
|
|
@ -153,7 +153,8 @@ async function addVideo (options: {
|
||||||
const videoChannel = res.locals.videoChannel
|
const videoChannel = res.locals.videoChannel
|
||||||
const user = res.locals.oauth.token.User
|
const user = res.locals.oauth.token.User
|
||||||
|
|
||||||
const videoData = buildLocalVideoFromReq(videoInfo, videoChannel.id)
|
let videoData = buildLocalVideoFromReq(videoInfo, videoChannel.id)
|
||||||
|
videoData = await Hooks.wrapObject(videoData, 'filter:api.video.upload.video-attribute.result')
|
||||||
|
|
||||||
videoData.state = buildNextVideoState()
|
videoData.state = buildNextVideoState()
|
||||||
videoData.duration = videoPhysicalFile.duration // duration was added by a previous middleware
|
videoData.duration = videoPhysicalFile.duration // duration was added by a previous middleware
|
||||||
|
|
|
@ -240,6 +240,21 @@ async function register ({ registerHook, registerSetting, settingsManager, stora
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// Upload/import/live attributes
|
||||||
|
for (const target of [
|
||||||
|
'filter:api.video.upload.video-attribute.result',
|
||||||
|
'filter:api.video.import-url.video-attribute.result',
|
||||||
|
'filter:api.video.import-torrent.video-attribute.result',
|
||||||
|
'filter:api.video.live.video-attribute.result'
|
||||||
|
]) {
|
||||||
|
registerHook({
|
||||||
|
target,
|
||||||
|
handler: (result) => {
|
||||||
|
return { ...result, description: result.description + ' - ' + target }
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
const filterHooks = [
|
const filterHooks = [
|
||||||
'filter:api.search.videos.local.list.params',
|
'filter:api.search.videos.local.list.params',
|
||||||
|
|
|
@ -537,6 +537,65 @@ describe('Test plugin filter hooks', function () {
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
describe('Upload/import/live attributes filters', function () {
|
||||||
|
|
||||||
|
before(async function () {
|
||||||
|
await servers[0].config.enableLive({ transcoding: false, allowReplay: false })
|
||||||
|
await servers[0].config.enableImports()
|
||||||
|
await servers[0].config.disableTranscoding()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('Should run filter:api.video.upload.video-attribute.result', async function () {
|
||||||
|
for (const mode of [ 'legacy' as 'legacy', 'resumable' as 'resumable' ]) {
|
||||||
|
const { id } = await servers[0].videos.upload({ attributes: { name: 'video', description: 'upload' }, mode })
|
||||||
|
|
||||||
|
const video = await servers[0].videos.get({ id })
|
||||||
|
expect(video.description).to.equal('upload - filter:api.video.upload.video-attribute.result')
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
it('Should run filter:api.video.import-url.video-attribute.result', async function () {
|
||||||
|
const attributes = {
|
||||||
|
name: 'video',
|
||||||
|
description: 'import url',
|
||||||
|
channelId: servers[0].store.channel.id,
|
||||||
|
targetUrl: FIXTURE_URLS.goodVideo,
|
||||||
|
privacy: VideoPrivacy.PUBLIC
|
||||||
|
}
|
||||||
|
const { video: { id } } = await servers[0].imports.importVideo({ attributes })
|
||||||
|
|
||||||
|
const video = await servers[0].videos.get({ id })
|
||||||
|
expect(video.description).to.equal('import url - filter:api.video.import-url.video-attribute.result')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('Should run filter:api.video.import-torrent.video-attribute.result', async function () {
|
||||||
|
const attributes = {
|
||||||
|
name: 'video',
|
||||||
|
description: 'import torrent',
|
||||||
|
channelId: servers[0].store.channel.id,
|
||||||
|
magnetUri: FIXTURE_URLS.magnet,
|
||||||
|
privacy: VideoPrivacy.PUBLIC
|
||||||
|
}
|
||||||
|
const { video: { id } } = await servers[0].imports.importVideo({ attributes })
|
||||||
|
|
||||||
|
const video = await servers[0].videos.get({ id })
|
||||||
|
expect(video.description).to.equal('import torrent - filter:api.video.import-torrent.video-attribute.result')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('Should run filter:api.video.live.video-attribute.result', async function () {
|
||||||
|
const fields = {
|
||||||
|
name: 'live',
|
||||||
|
description: 'live',
|
||||||
|
channelId: servers[0].store.channel.id,
|
||||||
|
privacy: VideoPrivacy.PUBLIC
|
||||||
|
}
|
||||||
|
const { id } = await servers[0].live.create({ fields })
|
||||||
|
|
||||||
|
const video = await servers[0].videos.get({ id })
|
||||||
|
expect(video.description).to.equal('live - filter:api.video.live.video-attribute.result')
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
describe('Stats filters', function () {
|
describe('Stats filters', function () {
|
||||||
|
|
||||||
it('Should run filter:api.server.stats.get.result', async function () {
|
it('Should run filter:api.server.stats.get.result', async function () {
|
||||||
|
|
|
@ -53,6 +53,12 @@ export const serverFilterHookObject = {
|
||||||
'filter:api.video-thread.create.accept.result': true,
|
'filter:api.video-thread.create.accept.result': true,
|
||||||
'filter:api.video-comment-reply.create.accept.result': true,
|
'filter:api.video-comment-reply.create.accept.result': true,
|
||||||
|
|
||||||
|
// Filter attributes when creating video object
|
||||||
|
'filter:api.video.upload.video-attribute.result': true,
|
||||||
|
'filter:api.video.import-url.video-attribute.result': true,
|
||||||
|
'filter:api.video.import-torrent.video-attribute.result': true,
|
||||||
|
'filter:api.video.live.video-attribute.result': true,
|
||||||
|
|
||||||
// Filter params/result used to list threads of a specific video
|
// Filter params/result used to list threads of a specific video
|
||||||
// (used by the video watch page)
|
// (used by the video watch page)
|
||||||
'filter:api.video-threads.list.params': true,
|
'filter:api.video-threads.list.params': true,
|
||||||
|
|
Loading…
Reference in New Issue