From c173e56520b0fe4206b9ea8049b6add40bfeabcd Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Thu, 4 Feb 2016 21:10:33 +0100 Subject: Split models --- controllers/api/v1/pods.js | 56 +++++++++++++++++---------- controllers/api/v1/remoteVideos.js | 5 ++- controllers/api/v1/videos.js | 77 ++++++++++++++++++++++++++++++++------ 3 files changed, 106 insertions(+), 32 deletions(-) (limited to 'controllers') diff --git a/controllers/api/v1/pods.js b/controllers/api/v1/pods.js index b073e85af..82d8d7f08 100644 --- a/controllers/api/v1/pods.js +++ b/controllers/api/v1/pods.js @@ -2,18 +2,23 @@ 'use strict' var express = require('express') + var fs = require('fs') + var logger = require('../../../helpers/logger') + var friends = require('../../../lib/friends') var middleware = require('../../../middlewares') var miscMiddleware = middleware.misc - var pods = require('../../../models/pods') + var Pods = require('../../../models/pods') var reqValidator = middleware.reqValidators.pods var secureRequest = middleware.reqValidators.remote.secureRequest + var utils = require('../../../helpers/utils') + var Videos = require('../../../models/videos') var router = express.Router() router.get('/', miscMiddleware.cache(false), listPods) router.post('/', reqValidator.podsAdd, miscMiddleware.cache(false), addPods) - router.get('/makefriends', miscMiddleware.cache(false), makeFriends) + router.get('/makefriends', reqValidator.makeFriends, miscMiddleware.cache(false), makeFriends) router.get('/quitfriends', miscMiddleware.cache(false), quitFriends) // Post because this is a secured request router.post('/remove', secureRequest, miscMiddleware.decryptBody, removePods) @@ -25,15 +30,32 @@ // --------------------------------------------------------------------------- function addPods (req, res, next) { - pods.add(req.body.data, function (err, json) { + var informations = req.body.data + Pods.add(informations, function (err) { if (err) return next(err) - res.json(json) + Videos.addRemotes(informations.videos) + + fs.readFile(utils.getCertDir() + 'peertube.pub', 'utf8', function (err, cert) { + if (err) { + logger.error('Cannot read cert file.', { error: err }) + return next(err) + } + + Videos.listOwned(function (err, videos_list) { + if (err) { + logger.error('Cannot get the list of owned videos.', { error: err }) + return next(err) + } + + res.json({ cert: cert, videos: videos_list }) + }) + }) }) } function listPods (req, res, next) { - pods.list(function (err, pods_list) { + Pods.list(function (err, pods_list) { if (err) return next(err) res.json(pods_list) @@ -41,32 +63,28 @@ } function makeFriends (req, res, next) { - pods.hasFriends(function (err, has_friends) { + friends.makeFriends(function (err) { if (err) return next(err) - if (has_friends === true) { - // We need to quit our friends before make new ones - res.sendStatus(409) - } else { - pods.makeFriends(function (err) { - if (err) return next(err) - - res.sendStatus(204) - }) - } + res.sendStatus(204) }) } function removePods (req, res, next) { - pods.remove(req.body.signature.url, function (err) { + var url = req.body.signature.url + Pods.remove(url, function (err) { if (err) return next(err) - res.sendStatus(204) + Videos.removeAllRemotesOf(url, function (err) { + if (err) logger.error('Cannot remove all remote videos of %s.', url) + logger.info('%s pod removed.', url) + res.sendStatus(204) + }) }) } function quitFriends (req, res, next) { - pods.quitFriends(function (err) { + friends.quitFriends(function (err) { if (err) return next(err) res.sendStatus(204) diff --git a/controllers/api/v1/remoteVideos.js b/controllers/api/v1/remoteVideos.js index 2be2fc87e..d72db9836 100644 --- a/controllers/api/v1/remoteVideos.js +++ b/controllers/api/v1/remoteVideos.js @@ -42,7 +42,10 @@ } function removeRemoteVideo (req, res, next) { - videos.removeRemotes(req.body.signature.url, pluck(req.body.data, 'magnetUri'), function (err) { + var url = req.body.signature.url + var magnetUris = pluck(req.body.data, 'magnetUri') + + videos.removeRemotesOfByMagnetUris(url, magnetUris, function (err) { if (err) return next(err) res.sendStatus(204) diff --git a/controllers/api/v1/videos.js b/controllers/api/v1/videos.js index 64b05e32b..d2e7e8825 100644 --- a/controllers/api/v1/videos.js +++ b/controllers/api/v1/videos.js @@ -6,10 +6,14 @@ var express = require('express') var multer = require('multer') + var logger = require('../../../helpers/logger') + var friends = require('../../../lib/friends') var middleware = require('../../../middlewares') var miscMiddleware = middleware.misc var reqValidator = middleware.reqValidators.videos - var videos = require('../../../models/videos') + var Videos = require('../../../models/videos') // model + var videos = require('../../../lib/videos') + var webtorrent = require('../../../lib/webTorrentNode') var router = express.Router() var uploads = config.get('storage.uploads') @@ -35,7 +39,7 @@ var reqFiles = multer({ storage: storage }).fields([{ name: 'input_video', maxCount: 1 }]) router.get('/', miscMiddleware.cache(false), listVideos) - router.post('/', reqFiles, reqValidator.videosAdd, miscMiddleware.cache(false), addVideos) + router.post('/', reqFiles, reqValidator.videosAdd, miscMiddleware.cache(false), addVideo) 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) @@ -46,17 +50,41 @@ // --------------------------------------------------------------------------- - function addVideos (req, res, next) { - videos.add({ video: req.files.input_video[0], data: req.body }, function (err) { - if (err) return next(err) + 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) + } - // TODO : include Location of the new video - res.sendStatus(201) + // 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) + }) }) } function getVideos (req, res, next) { - videos.get(req.params.id, function (err, video) { + Videos.get(req.params.id, function (err, video) { if (err) return next(err) if (video === null) { @@ -68,7 +96,7 @@ } function listVideos (req, res, next) { - videos.list(function (err, videos_list) { + Videos.list(function (err, videos_list) { if (err) return next(err) res.json(videos_list) @@ -76,18 +104,43 @@ } function removeVideo (req, res, next) { - videos.remove(req.params.id, function (err) { + var video_id = req.params.id + Videos.get(video_id, function (err, video) { if (err) return next(err) - res.sendStatus(204) + 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) + }) + }) }) } function searchVideos (req, res, next) { - videos.search(req.params.name, function (err, videos_list) { + Videos.search(req.params.name, function (err, videos_list) { if (err) return next(err) res.json(videos_list) }) } + + // --------------------------------------------------------------------------- + + // 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) + } + } })() -- cgit v1.2.3