From cbe2f7c34822b1bd3b1f8c691f79f0c29cf21f07 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 10 May 2016 21:19:24 +0200 Subject: Refractoring and add thumbnails support (without tests) --- server/models/videos.js | 126 ++++++++++++------------------------------------ 1 file changed, 32 insertions(+), 94 deletions(-) (limited to 'server/models/videos.js') diff --git a/server/models/videos.js b/server/models/videos.js index e02158be7..eedb6eb58 100644 --- a/server/models/videos.js +++ b/server/models/videos.js @@ -1,18 +1,13 @@ 'use strict' -const async = require('async') const config = require('config') -const dz = require('dezalgo') -const fs = require('fs') const mongoose = require('mongoose') -const path = require('path') const logger = require('../helpers/logger') const http = config.get('webserver.https') === true ? 'https' : 'http' const host = config.get('webserver.host') const port = config.get('webserver.port') -const uploadDir = path.join(__dirname, '..', '..', config.get('storage.uploads')) // --------------------------------------------------------------------------- @@ -23,7 +18,8 @@ const videosSchema = mongoose.Schema({ magnetUri: String, podUrl: String, author: String, - duration: Number + duration: Number, + thumbnail: String }) const VideosDB = mongoose.model('videos', videosSchema) @@ -34,11 +30,13 @@ const Videos = { addRemotes: addRemotes, get: get, list: list, + listFromUrl: listFromUrl, + listFromUrls: listFromUrls, + listFromUrlAndMagnets: listFromUrlAndMagnets, + listFromRemotes: listFromRemotes, listOwned: listOwned, removeOwned: removeOwned, - removeAllRemotes: removeAllRemotes, - removeAllRemotesOf: removeAllRemotesOf, - removeRemotesOfByMagnetUris: removeRemotesOfByMagnetUris, + removeByIds: removeByIds, search: search } @@ -58,38 +56,13 @@ function add (video, callback) { }) } -// TODO: avoid doublons function addRemotes (videos, callback) { - if (!callback) callback = function () {} - - const to_add = [] - - async.each(videos, function (video, callback_each) { - callback_each = dz(callback_each) - logger.debug('Add remote video from pod: %s', video.podUrl) - - const params = { - name: video.name, - namePath: null, - description: video.description, - magnetUri: video.magnetUri, - podUrl: video.podUrl, - duration: video.duration - } - - to_add.push(params) - - callback_each() - }, function () { - VideosDB.create(to_add, function (err, videos) { - if (err) { - logger.error('Cannot insert this remote video.') - return callback(err) - } - - return callback(null, videos) - }) + videos.forEach(function (video) { + // Ensure they are remote videos + video.namePath = null }) + + VideosDB.create(videos, callback) } function get (id, callback) { @@ -114,6 +87,22 @@ function list (callback) { }) } +function listFromUrl (fromUrl, callback) { + VideosDB.find({ podUrl: fromUrl }, callback) +} + +function listFromUrls (fromUrls, callback) { + VideosDB.find({ podUrl: { $in: fromUrls } }, callback) +} + +function listFromUrlAndMagnets (fromUrl, magnets, callback) { + VideosDB.find({ podUrl: fromUrl, magnetUri: { $in: magnets } }, callback) +} + +function listFromRemotes (callback) { + VideosDB.find({ namePath: null }, callback) +} + function listOwned (callback) { // If namePath is not null this is *our* video VideosDB.find({ namePath: { $ne: null } }, function (err, videos_list) { @@ -126,65 +115,14 @@ function listOwned (callback) { }) } +// Return the video in the callback function removeOwned (id, callback) { - VideosDB.findByIdAndRemove(id, function (err, video) { - if (err) { - logger.error('Cannot remove the torrent.') - return callback(err) - } - - fs.unlink(uploadDir + video.namePath, function (err) { - if (err) { - logger.error('Cannot remove this video file.') - return callback(err) - } - - callback(null) - }) - }) -} - -function removeAllRemotes (callback) { - VideosDB.remove({ namePath: null }, callback) -} - -function removeAllRemotesOf (fromUrl, callback) { - VideosDB.remove({ podUrl: fromUrl }, callback) + VideosDB.findByIdAndRemove(id, callback) } // Use the magnet Uri because the _id field is not the same on different servers -function removeRemotesOfByMagnetUris (fromUrl, magnetUris, callback) { - if (callback === undefined) callback = function () {} - - VideosDB.find({ magnetUri: { $in: magnetUris } }, function (err, videos) { - if (err || !videos) { - logger.error('Cannot find the torrent URI of these remote videos.') - return callback(err) - } - - const to_remove = [] - async.each(videos, function (video, callback_async) { - callback_async = dz(callback_async) - - if (video.podUrl !== fromUrl) { - logger.error('The pod %s has not the rights on the video of %s.', fromUrl, video.podUrl) - } else { - to_remove.push(video._id) - } - - callback_async() - }, function () { - VideosDB.remove({ _id: { $in: to_remove } }, function (err) { - if (err) { - logger.error('Cannot remove the remote videos.') - return callback(err) - } - - logger.info('Removed remote videos from %s.', fromUrl) - callback(null) - }) - }) - }) +function removeByIds (ids, callback) { + VideosDB.remove({ _id: { $in: ids } }, callback) } function search (name, callback) { -- cgit v1.2.3