1
0
Fork 0
peertube/controllers/api/v1/videos.js

147 lines
4.3 KiB
JavaScript
Raw Normal View History

2015-06-09 11:41:40 -04:00
;(function () {
'use strict'
2015-12-04 15:43:26 -05:00
var config = require('config')
var crypto = require('crypto')
2016-01-31 05:23:52 -05:00
var express = require('express')
2015-12-04 15:43:26 -05:00
var multer = require('multer')
2016-02-04 15:10:33 -05:00
var logger = require('../../../helpers/logger')
var friends = require('../../../lib/friends')
2015-11-06 11:34:15 -05:00
var middleware = require('../../../middlewares')
2015-11-07 12:40:13 -05:00
var miscMiddleware = middleware.misc
var reqValidator = middleware.reqValidators.videos
2016-02-04 15:10:33 -05:00
var Videos = require('../../../models/videos') // model
var videos = require('../../../lib/videos')
var webtorrent = require('../../../lib/webTorrentNode')
2015-06-09 11:41:40 -04:00
2016-01-31 05:23:52 -05:00
var router = express.Router()
2015-12-04 15:43:26 -05:00
var uploads = config.get('storage.uploads')
2016-01-31 05:23:52 -05:00
// multer configuration
var storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, uploads)
},
2015-06-09 11:41:40 -04:00
2016-01-31 05:23:52 -05:00
filename: function (req, file, cb) {
var extension = ''
if (file.mimetype === 'video/webm') extension = 'webm'
else if (file.mimetype === 'video/mp4') extension = 'mp4'
else if (file.mimetype === 'video/ogg') extension = 'ogv'
crypto.pseudoRandomBytes(16, function (err, raw) {
var fieldname = err ? undefined : raw.toString('hex')
cb(null, fieldname + '.' + extension)
})
}
})
2015-06-09 11:41:40 -04:00
2016-01-31 05:23:52 -05:00
var reqFiles = multer({ storage: storage }).fields([{ name: 'input_video', maxCount: 1 }])
2015-06-09 11:41:40 -04:00
2016-01-31 05:23:52 -05:00
router.get('/', miscMiddleware.cache(false), listVideos)
2016-02-04 15:10:33 -05:00
router.post('/', reqFiles, reqValidator.videosAdd, miscMiddleware.cache(false), addVideo)
2016-01-31 05:23:52 -05:00
router.get('/:id', reqValidator.videosGet, miscMiddleware.cache(false), getVideos)
router.delete('/:id', reqValidator.videosRemove, miscMiddleware.cache(false), removeVideo)
router.get('/search/:name', reqValidator.videosSearch, miscMiddleware.cache(false), searchVideos)
// ---------------------------------------------------------------------------
module.exports = router
// ---------------------------------------------------------------------------
2015-06-09 11:41:40 -04:00
2016-02-04 15:10:33 -05:00
function addVideo (req, res, next) {
var video_file = req.files.input_video[0]
var video_infos = req.body
videos.seed(video_file.path, function (err, torrent) {
if (err) {
logger.error('Cannot seed this video.', { error: err })
return next(err)
}
var video_data = {
name: video_infos.name,
namePath: video_file.filename,
description: video_infos.description,
magnetUri: torrent.magnetURI
}
Videos.add(video_data, function (err) {
if (err) {
// TODO unseed the video
logger.error('Cannot insert this video in the database.', { error: err })
return next(err)
}
2015-06-09 11:41:40 -04:00
2016-02-04 15:10:33 -05:00
// Now we'll add the video's meta data to our friends
friends.addVideoToFriends(video_data)
// TODO : include Location of the new video
res.sendStatus(201)
})
2015-06-09 11:41:40 -04:00
})
}
function getVideos (req, res, next) {
2016-02-04 15:10:33 -05:00
Videos.get(req.params.id, function (err, video) {
2015-10-30 12:51:40 -04:00
if (err) return next(err)
2015-06-09 11:41:40 -04:00
if (video === null) {
return res.sendStatus(404)
}
res.json(video)
})
}
2016-01-31 05:23:52 -05:00
function listVideos (req, res, next) {
2016-02-04 15:10:33 -05:00
Videos.list(function (err, videos_list) {
2016-01-31 05:23:52 -05:00
if (err) return next(err)
res.json(videos_list)
})
}
2015-06-09 11:41:40 -04:00
function removeVideo (req, res, next) {
2016-02-04 15:10:33 -05:00
var video_id = req.params.id
Videos.get(video_id, function (err, video) {
2015-10-30 12:51:40 -04:00
if (err) return next(err)
2015-06-09 11:41:40 -04:00
2016-02-04 15:10:33 -05:00
removeTorrent(video.magnetUri, function () {
Videos.removeOwned(req.params.id, function (err) {
if (err) return next(err)
var params = {
name: video.name,
magnetUri: video.magnetUri
}
friends.removeVideoToFriends(params)
res.sendStatus(204)
})
})
2015-06-09 11:41:40 -04:00
})
}
2016-01-31 05:23:52 -05:00
function searchVideos (req, res, next) {
2016-02-04 15:10:33 -05:00
Videos.search(req.params.name, function (err, videos_list) {
2016-01-31 05:23:52 -05:00
if (err) return next(err)
2015-06-09 11:41:40 -04:00
2016-01-31 05:23:52 -05:00
res.json(videos_list)
})
}
2016-02-04 15:10:33 -05:00
// ---------------------------------------------------------------------------
// Maybe the torrent is not seeded, but we catch the error to don't stop the removing process
function removeTorrent (magnetUri, callback) {
try {
webtorrent.remove(magnetUri, callback)
} catch (err) {
logger.warn('Cannot remove the torrent from WebTorrent', { err: err })
return callback(null)
}
}
2015-06-09 11:41:40 -04:00
})()