]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/models/videos.js
9521e63e314ca35a08fd60d41ed8876e932b94d2
[github/Chocobozzz/PeerTube.git] / server / models / videos.js
1 'use strict'
2
3 const async = require('async')
4 const config = require('config')
5 const mongoose = require('mongoose')
6
7 const logger = require('../helpers/logger')
8
9 const http = config.get('webserver.https') === true ? 'https' : 'http'
10 const host = config.get('webserver.host')
11 const port = config.get('webserver.port')
12
13 // ---------------------------------------------------------------------------
14
15 const videosSchema = mongoose.Schema({
16 name: String,
17 namePath: String,
18 description: String,
19 magnetUri: String,
20 podUrl: String,
21 author: String,
22 duration: Number,
23 thumbnail: String,
24 createdDate: {
25 type: Date,
26 default: Date.now
27 }
28 })
29 const VideosDB = mongoose.model('videos', videosSchema)
30
31 // ---------------------------------------------------------------------------
32
33 const Videos = {
34 add: add,
35 addRemotes: addRemotes,
36 get: get,
37 list: list,
38 listFromUrl: listFromUrl,
39 listFromUrls: listFromUrls,
40 listFromUrlAndMagnets: listFromUrlAndMagnets,
41 listFromRemotes: listFromRemotes,
42 listOwned: listOwned,
43 removeOwned: removeOwned,
44 removeByIds: removeByIds,
45 search: search
46 }
47
48 function add (video, callback) {
49 logger.info('Adding %s video to database.', video.name)
50
51 const params = video
52 params.podUrl = http + '://' + host + ':' + port
53
54 VideosDB.create(params, function (err, insertedVideo) {
55 if (err) {
56 logger.error('Cannot insert this video into database.')
57 return callback(err)
58 }
59
60 callback(null, insertedVideo)
61 })
62 }
63
64 function addRemotes (videos, callback) {
65 videos.forEach(function (video) {
66 // Ensure they are remote videos
67 video.namePath = null
68 })
69
70 VideosDB.create(videos, callback)
71 }
72
73 function get (id, callback) {
74 VideosDB.findById(id, function (err, video) {
75 if (err) {
76 logger.error('Cannot get this video.')
77 return callback(err)
78 }
79
80 return callback(null, video)
81 })
82 }
83
84 function list (start, count, sort, callback) {
85 const query = {}
86 return findWithCount(query, start, count, sort, callback)
87 }
88
89 function listFromUrl (fromUrl, callback) {
90 VideosDB.find({ podUrl: fromUrl }, callback)
91 }
92
93 function listFromUrls (fromUrls, callback) {
94 VideosDB.find({ podUrl: { $in: fromUrls } }, callback)
95 }
96
97 function listFromUrlAndMagnets (fromUrl, magnets, callback) {
98 VideosDB.find({ podUrl: fromUrl, magnetUri: { $in: magnets } }, callback)
99 }
100
101 function listFromRemotes (callback) {
102 VideosDB.find({ namePath: null }, callback)
103 }
104
105 function listOwned (callback) {
106 // If namePath is not null this is *our* video
107 VideosDB.find({ namePath: { $ne: null } }, function (err, videosList) {
108 if (err) {
109 logger.error('Cannot get the list of owned videos.')
110 return callback(err)
111 }
112
113 return callback(null, videosList)
114 })
115 }
116
117 // Return the video in the callback
118 function removeOwned (id, callback) {
119 VideosDB.findByIdAndRemove(id, callback)
120 }
121
122 // Use the magnet Uri because the _id field is not the same on different servers
123 function removeByIds (ids, callback) {
124 VideosDB.remove({ _id: { $in: ids } }, callback)
125 }
126
127 function search (name, start, count, sort, callback) {
128 const query = { name: new RegExp(name) }
129 findWithCount(query, start, count, sort, callback)
130 }
131
132 // ---------------------------------------------------------------------------
133
134 module.exports = Videos
135
136 // ---------------------------------------------------------------------------
137
138 function findWithCount (query, start, count, sort, callback) {
139 async.parallel([
140 function (asyncCallback) {
141 VideosDB.find(query).skip(start).limit(start + count).sort(sort).exec(asyncCallback)
142 },
143 function (asyncCallback) {
144 VideosDB.count(query, asyncCallback)
145 }
146 ], function (err, results) {
147 if (err) return callback(err)
148
149 const videos = results[0]
150 const totalVideos = results[1]
151 return callback(null, videos, totalVideos)
152 })
153 }