mirror of
https://github.com/Chocobozzz/PeerTube.git
synced 2025-10-05 10:49:28 +02:00
Video model refractoring -> use mongoose api
This commit is contained in:
parent
07d9320387
commit
aaf61f3810
15 changed files with 412 additions and 534 deletions
|
@ -2,15 +2,15 @@
|
|||
|
||||
const async = require('async')
|
||||
const express = require('express')
|
||||
const mongoose = require('mongoose')
|
||||
|
||||
const middlewares = require('../../../middlewares')
|
||||
const secureMiddleware = middlewares.secure
|
||||
const reqValidator = middlewares.reqValidators.remote
|
||||
const logger = require('../../../helpers/logger')
|
||||
const Videos = require('../../../models/videos')
|
||||
const videos = require('../../../lib/videos')
|
||||
|
||||
const router = express.Router()
|
||||
const Video = mongoose.model('Video')
|
||||
|
||||
router.post('/videos',
|
||||
reqValidator.signature,
|
||||
|
@ -33,48 +33,39 @@ function remoteVideos (req, res, next) {
|
|||
// We need to process in the same order to keep consistency
|
||||
// TODO: optimization
|
||||
async.eachSeries(requests, function (request, callbackEach) {
|
||||
const video = request.data
|
||||
const videoData = request.data
|
||||
|
||||
if (request.type === 'add') {
|
||||
addRemoteVideo(video, callbackEach)
|
||||
addRemoteVideo(videoData, callbackEach)
|
||||
} else if (request.type === 'remove') {
|
||||
removeRemoteVideo(video, fromUrl, callbackEach)
|
||||
removeRemoteVideo(videoData, fromUrl, callbackEach)
|
||||
}
|
||||
}, function (err) {
|
||||
if (err) logger.error('Error managing remote videos.', { error: err })
|
||||
})
|
||||
|
||||
// We don't need to keep the other pod waiting
|
||||
return res.type('json').status(204).end()
|
||||
}
|
||||
|
||||
function addRemoteVideo (videoToCreate, callback) {
|
||||
videos.createRemoteVideos([ videoToCreate ], function (err, remoteVideos) {
|
||||
if (err) {
|
||||
logger.error('Cannot create remote videos.', { error: err })
|
||||
// Don't break the process
|
||||
}
|
||||
function addRemoteVideo (videoToCreateData, callback) {
|
||||
// Mongoose pre hook will automatically create the thumbnail on disk
|
||||
videoToCreateData.thumbnail = videoToCreateData.thumbnailBase64
|
||||
|
||||
return callback()
|
||||
})
|
||||
const video = new Video(videoToCreateData)
|
||||
video.save(callback)
|
||||
}
|
||||
|
||||
function removeRemoteVideo (videoToRemove, fromUrl, callback) {
|
||||
const magnetUris = [ videoToRemove.magnetUri ]
|
||||
|
||||
function removeRemoteVideo (videoToRemoveData, fromUrl, callback) {
|
||||
// We need the list because we have to remove some other stuffs (thumbnail etc)
|
||||
Videos.listFromUrlAndMagnets(fromUrl, magnetUris, function (err, videosList) {
|
||||
Video.listByUrlAndMagnet(fromUrl, videoToRemoveData.magnetUri, function (err, videosList) {
|
||||
if (err) {
|
||||
logger.error('Cannot list videos from url and magnets.', { error: err })
|
||||
// Don't break the process
|
||||
return callback()
|
||||
return callback(err)
|
||||
}
|
||||
|
||||
videos.removeRemoteVideos(videosList, function (err) {
|
||||
if (err) {
|
||||
logger.error('Cannot remove remote videos.', { error: err })
|
||||
// Don't break the process
|
||||
}
|
||||
|
||||
return callback()
|
||||
})
|
||||
async.each(videosList, function (video, callbackEach) {
|
||||
video.remove(callbackEach)
|
||||
}, callback)
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue