X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fmodels%2Fvideo.js;h=a5540d127a157630b92f834d92dd4e78bcd7aac1;hb=0ff21c1c086f907612e34880f76f08bc74eeb78c;hp=98f43a06de86ed03baec25b49019a61fe0b474a5;hpb=1a42c9e2c0fb64cdbebd81b311736e752f591e0a;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/models/video.js b/server/models/video.js index 98f43a06d..a5540d127 100644 --- a/server/models/video.js +++ b/server/models/video.js @@ -1,7 +1,7 @@ 'use strict' const config = require('config') -const each = require('async/each') +const eachLimit = require('async/eachLimit') const ffmpeg = require('fluent-ffmpeg') const fs = require('fs') const parallel = require('async/parallel') @@ -9,8 +9,9 @@ const pathUtils = require('path') const mongoose = require('mongoose') const constants = require('../initializers/constants') -const customValidators = require('../helpers/custom-validators') +const customVideosValidators = require('../helpers/custom-validators').videos const logger = require('../helpers/logger') +const modelUtils = require('./utils') const utils = require('../helpers/utils') const webtorrent = require('../lib/webtorrent') @@ -39,18 +40,18 @@ const VideoSchema = mongoose.Schema({ } }) -VideoSchema.path('name').validate(customValidators.isVideoNameValid) -VideoSchema.path('description').validate(customValidators.isVideoDescriptionValid) -VideoSchema.path('magnetUri').validate(customValidators.isVideoMagnetUriValid) -VideoSchema.path('podUrl').validate(customValidators.isVideoPodUrlValid) -VideoSchema.path('author').validate(customValidators.isVideoAuthorValid) -VideoSchema.path('duration').validate(customValidators.isVideoDurationValid) +VideoSchema.path('name').validate(customVideosValidators.isVideoNameValid) +VideoSchema.path('description').validate(customVideosValidators.isVideoDescriptionValid) +VideoSchema.path('magnetUri').validate(customVideosValidators.isVideoMagnetUriValid) +VideoSchema.path('podUrl').validate(customVideosValidators.isVideoPodUrlValid) +VideoSchema.path('author').validate(customVideosValidators.isVideoAuthorValid) +VideoSchema.path('duration').validate(customVideosValidators.isVideoDurationValid) // The tumbnail can be the path or the data in base 64 // The pre save hook will convert the base 64 data in a file on disk and replace the thumbnail key by the filename VideoSchema.path('thumbnail').validate(function (value) { - return customValidators.isVideoThumbnailValid(value) || customValidators.isVideoThumbnail64Valid(value) + return customVideosValidators.isVideoThumbnailValid(value) || customVideosValidators.isVideoThumbnail64Valid(value) }) -VideoSchema.path('tags').validate(customValidators.isVideoTagsValid) +VideoSchema.path('tags').validate(customVideosValidators.isVideoTagsValid) VideoSchema.methods = { isOwned: isOwned, @@ -60,10 +61,11 @@ VideoSchema.methods = { VideoSchema.statics = { getDurationFromFile: getDurationFromFile, - list: list, + listForApi: listForApi, listByUrlAndMagnet: listByUrlAndMagnet, listByUrls: listByUrls, listOwned: listOwned, + listOwnedByAuthor: listOwnedByAuthor, listRemotes: listRemotes, load: load, search: search, @@ -193,9 +195,9 @@ function getDurationFromFile (videoPath, callback) { }) } -function list (start, count, sort, callback) { +function listForApi (start, count, sort, callback) { const query = {} - return findWithCount.call(this, query, start, count, sort, callback) + return modelUtils.findWithCount.call(this, query, start, count, sort, callback) } function listByUrlAndMagnet (fromUrl, magnetUri, callback) { @@ -211,6 +213,10 @@ function listOwned (callback) { this.find({ filename: { $ne: null } }, callback) } +function listOwnedByAuthor (author, callback) { + this.find({ filename: { $ne: null }, author: author }, callback) +} + function listRemotes (callback) { this.find({ filename: null }, callback) } @@ -228,14 +234,14 @@ function search (value, field, start, count, sort, callback) { query[field] = new RegExp(value) } - findWithCount.call(this, query, start, count, sort, callback) + modelUtils.findWithCount.call(this, query, start, count, sort, callback) } function seedAllExisting (callback) { listOwned.call(this, function (err, videos) { if (err) return callback(err) - each(videos, function (video, callbackEach) { + eachLimit(videos, constants.SEEDS_IN_PARALLEL, function (video, callbackEach) { const videoPath = pathUtils.join(uploadsDir, video.filename) seed(videoPath, callbackEach) }, callback) @@ -244,25 +250,6 @@ function seedAllExisting (callback) { // --------------------------------------------------------------------------- -function findWithCount (query, start, count, sort, callback) { - const self = this - - parallel([ - function (asyncCallback) { - self.find(query).skip(start).limit(count).sort(sort).exec(asyncCallback) - }, - function (asyncCallback) { - self.count(query, asyncCallback) - } - ], function (err, results) { - if (err) return callback(err) - - const videos = results[0] - const totalVideos = results[1] - return callback(null, videos, totalVideos) - }) -} - function removeThumbnail (video, callback) { fs.unlink(thumbnailsDir + video.thumbnail, callback) }