From aaf61f3810e6d57c5130af959bd2860df32775e7 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 24 Jun 2016 17:42:51 +0200 Subject: Video model refractoring -> use mongoose api --- server/lib/friends.js | 21 ++--- server/lib/requestsScheduler.js | 18 ++-- server/lib/videos.js | 199 ---------------------------------------- 3 files changed, 20 insertions(+), 218 deletions(-) delete mode 100644 server/lib/videos.js (limited to 'server/lib') diff --git a/server/lib/friends.js b/server/lib/friends.js index d81a603ad..91cd69f86 100644 --- a/server/lib/friends.js +++ b/server/lib/friends.js @@ -3,6 +3,7 @@ const async = require('async') const config = require('config') const fs = require('fs') +const mongoose = require('mongoose') const request = require('request') const constants = require('../initializers/constants') @@ -11,12 +12,11 @@ const peertubeCrypto = require('../helpers/peertubeCrypto') const Pods = require('../models/pods') const requestsScheduler = require('../lib/requestsScheduler') const requests = require('../helpers/requests') -const videos = require('../lib/videos') -const Videos = require('../models/videos') const http = config.get('webserver.https') ? 'https' : 'http' const host = config.get('webserver.host') const port = config.get('webserver.port') +const Video = mongoose.model('Video') const pods = { addVideoToFriends: addVideoToFriends, @@ -117,18 +117,13 @@ function quitFriends (callback) { function listRemoteVideos (callbackAsync) { logger.info('Broke friends, so sad :(') - Videos.listFromRemotes(callbackAsync) + Video.listRemotes(callbackAsync) }, function removeTheRemoteVideos (videosList, callbackAsync) { - videos.removeRemoteVideos(videosList, function (err) { - if (err) { - logger.error('Cannot remove remote videos.', { error: err }) - return callbackAsync(err) - } - - return callbackAsync(null) - }) + async.each(videosList, function (video, callbackEach) { + video.remove(callbackEach) + }, callbackAsync) } ], function (err) { // Don't forget to re activate the scheduler, even if there was an error @@ -146,14 +141,14 @@ function removeVideoToFriends (video) { } function sendOwnedVideosToPod (podId) { - Videos.listOwned(function (err, videosList) { + Video.listOwned(function (err, videosList) { if (err) { logger.error('Cannot get the list of videos we own.') return } videosList.forEach(function (video) { - videos.convertVideoToRemote(video, function (err, remoteVideo) { + video.toRemoteJSON(function (err, remoteVideo) { if (err) { logger.error('Cannot convert video to remote.', { error: err }) // Don't break the process diff --git a/server/lib/requestsScheduler.js b/server/lib/requestsScheduler.js index ac75e5b93..b192d8299 100644 --- a/server/lib/requestsScheduler.js +++ b/server/lib/requestsScheduler.js @@ -2,14 +2,15 @@ const async = require('async') const map = require('lodash/map') +const mongoose = require('mongoose') const constants = require('../initializers/constants') const logger = require('../helpers/logger') const Pods = require('../models/pods') const Requests = require('../models/requests') const requests = require('../helpers/requests') -const videos = require('../lib/videos') -const Videos = require('../models/videos') + +const Video = mongoose.model('Video') let timer = null @@ -210,7 +211,7 @@ function removeBadPods () { const urls = map(pods, 'url') const ids = map(pods, '_id') - Videos.listFromUrls(urls, function (err, videosList) { + Video.listByUrls(urls, function (err, videosList) { if (err) { logger.error('Cannot list videos urls.', { error: err, urls: urls }) return callback(null, ids, []) @@ -224,9 +225,14 @@ function removeBadPods () { // We don't have to remove pods, skip if (typeof podIds === 'function') return podIds(null) - // Remove the remote videos - videos.removeRemoteVideos(videosList, function (err) { - if (err) logger.error('Cannot remove remote videos.', { error: err }) + async.each(videosList, function (video, callbackEach) { + video.remove(callbackEach) + }, function (err) { + if (err) { + // Don't stop the process + logger.error('Error while removing videos of bad pods.', { error: err }) + return + } return callback(null, podIds) }) diff --git a/server/lib/videos.js b/server/lib/videos.js deleted file mode 100644 index a74c77dc4..000000000 --- a/server/lib/videos.js +++ /dev/null @@ -1,199 +0,0 @@ -'use strict' - -const async = require('async') -const config = require('config') -const ffmpeg = require('fluent-ffmpeg') -const fs = require('fs') -const map = require('lodash/map') -const pathUtils = require('path') - -const constants = require('../initializers/constants') -const logger = require('../helpers/logger') -const utils = require('../helpers/utils') -const Videos = require('../models/videos') -const webtorrent = require('../lib/webtorrent') - -const uploadDir = pathUtils.join(__dirname, '..', '..', config.get('storage.uploads')) -const thumbnailsDir = pathUtils.join(__dirname, '..', '..', config.get('storage.thumbnails')) - -const videos = { - convertVideoToRemote: convertVideoToRemote, - createRemoteVideos: createRemoteVideos, - getVideoDuration: getVideoDuration, - getVideoState: getVideoState, - createVideoThumbnail: createVideoThumbnail, - removeVideosDataFromDisk: removeVideosDataFromDisk, - removeRemoteVideos: removeRemoteVideos, - seed: seed, - seedAllExisting: seedAllExisting -} - -function convertVideoToRemote (video, callback) { - fs.readFile(thumbnailsDir + video.thumbnail, function (err, thumbnailData) { - if (err) { - logger.error('Cannot read the thumbnail of the video') - return callback(err) - } - - const remoteVideo = { - name: video.name, - description: video.description, - magnetUri: video.magnetUri, - author: video.author, - duration: video.duration, - thumbnailBase64: new Buffer(thumbnailData).toString('base64'), - tags: video.tags, - createdDate: video.createdDate, - podUrl: video.podUrl - } - - return callback(null, remoteVideo) - }) -} - -function createRemoteVideos (videos, callback) { - // Create the remote videos from the new pod - createRemoteVideoObjects(videos, function (err, remoteVideos) { - if (err) return callback(err) - - Videos.addRemotes(remoteVideos, callback) - }) -} - -function getVideoDuration (videoPath, callback) { - ffmpeg.ffprobe(videoPath, function (err, metadata) { - if (err) return callback(err) - - return callback(null, Math.floor(metadata.format.duration)) - }) -} - -function getVideoState (video) { - const exist = (video !== null) - let owned = false - if (exist === true) { - owned = (video.namePath !== null) - } - - return { exist: exist, owned: owned } -} - -function createVideoThumbnail (videoPath, callback) { - const filename = pathUtils.basename(videoPath) + '.jpg' - ffmpeg(videoPath) - .on('error', callback) - .on('end', function () { - callback(null, filename) - }) - .thumbnail({ - count: 1, - folder: thumbnailsDir, - size: constants.THUMBNAILS_SIZE, - filename: filename - }) -} - -// Remove video datas from disk (video file, thumbnail...) -function removeVideosDataFromDisk (videos, callback) { - async.each(videos, function (video, callbackEach) { - fs.unlink(thumbnailsDir + video.thumbnail, function (err) { - if (err) logger.error('Cannot remove the video thumbnail') - - if (getVideoState(video).owned === true) { - fs.unlink(uploadDir + video.namePath, function (err) { - if (err) { - logger.error('Cannot remove this video file.') - return callbackEach(err) - } - - callbackEach(null) - }) - } else { - callbackEach(null) - } - }) - }, callback) -} - -function removeRemoteVideos (videos, callback) { - Videos.removeByIds(map(videos, '_id'), function (err) { - if (err) return callback(err) - - removeVideosDataFromDisk(videos, callback) - }) -} - -function seed (path, callback) { - logger.info('Seeding %s...', path) - - webtorrent.seed(path, function (torrent) { - logger.info('%s seeded (%s).', path, torrent.magnetURI) - - return callback(null, torrent) - }) -} - -function seedAllExisting (callback) { - Videos.listOwned(function (err, videosList) { - if (err) { - logger.error('Cannot get list of the videos to seed.') - return callback(err) - } - - async.each(videosList, function (video, callbackEach) { - seed(uploadDir + video.namePath, function (err) { - if (err) { - logger.error('Cannot seed this video.') - return callback(err) - } - - callbackEach(null) - }) - }, callback) - }) -} - -// --------------------------------------------------------------------------- - -module.exports = videos - -// --------------------------------------------------------------------------- - -function createRemoteVideoObjects (videos, callback) { - const remoteVideos = [] - - async.each(videos, function (video, callbackEach) { - // Creating the thumbnail for this remote video - utils.generateRandomString(16, function (err, randomString) { - if (err) return callbackEach(err) - - const thumbnailName = randomString + '.jpg' - createThumbnailFromBase64(thumbnailName, video.thumbnailBase64, function (err) { - if (err) return callbackEach(err) - - const params = { - name: video.name, - description: video.description, - magnetUri: video.magnetUri, - podUrl: video.podUrl, - duration: video.duration, - thumbnail: thumbnailName, - tags: video.tags, - author: video.author - } - remoteVideos.push(params) - - callbackEach(null) - }) - }) - }, - function (err) { - if (err) return callback(err) - - callback(null, remoteVideos) - }) -} - -function createThumbnailFromBase64 (thumbnailName, data, callback) { - fs.writeFile(thumbnailsDir + thumbnailName, data, { encoding: 'base64' }, callback) -} -- cgit v1.2.3