]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/models/videos.js
Video duration support (server)
[github/Chocobozzz/PeerTube.git] / server / models / videos.js
1 'use strict'
2
3 const async = require('async')
4 const config = require('config')
5 const dz = require('dezalgo')
6 const fs = require('fs')
7 const mongoose = require('mongoose')
8 const path = require('path')
9
10 const logger = require('../helpers/logger')
11
12 const http = config.get('webserver.https') === true ? 'https' : 'http'
13 const host = config.get('webserver.host')
14 const port = config.get('webserver.port')
15 const uploadDir = path.join(__dirname, '..', '..', config.get('storage.uploads'))
16
17 // ---------------------------------------------------------------------------
18
19 const videosSchema = mongoose.Schema({
20 name: String,
21 namePath: String,
22 description: String,
23 magnetUri: String,
24 podUrl: String,
25 author: String,
26 duration: Number
27 })
28 const VideosDB = mongoose.model('videos', videosSchema)
29
30 // ---------------------------------------------------------------------------
31
32 const Videos = {
33 add: add,
34 addRemotes: addRemotes,
35 get: get,
36 list: list,
37 listOwned: listOwned,
38 removeOwned: removeOwned,
39 removeAllRemotes: removeAllRemotes,
40 removeAllRemotesOf: removeAllRemotesOf,
41 removeRemotesOfByMagnetUris: removeRemotesOfByMagnetUris,
42 search: search
43 }
44
45 function add (video, callback) {
46 logger.info('Adding %s video to database.', video.name)
47
48 const params = video
49 params.podUrl = http + '://' + host + ':' + port
50
51 VideosDB.create(params, function (err, video) {
52 if (err) {
53 logger.error('Cannot insert this video into database.')
54 return callback(err)
55 }
56
57 callback(null)
58 })
59 }
60
61 // TODO: avoid doublons
62 function addRemotes (videos, callback) {
63 if (!callback) callback = function () {}
64
65 const to_add = []
66
67 async.each(videos, function (video, callback_each) {
68 callback_each = dz(callback_each)
69 logger.debug('Add remote video from pod: %s', video.podUrl)
70
71 const params = {
72 name: video.name,
73 namePath: null,
74 description: video.description,
75 magnetUri: video.magnetUri,
76 podUrl: video.podUrl,
77 duration: video.duration
78 }
79
80 to_add.push(params)
81
82 callback_each()
83 }, function () {
84 VideosDB.create(to_add, function (err, videos) {
85 if (err) {
86 logger.error('Cannot insert this remote video.')
87 return callback(err)
88 }
89
90 return callback(null, videos)
91 })
92 })
93 }
94
95 function get (id, callback) {
96 VideosDB.findById(id, function (err, video) {
97 if (err) {
98 logger.error('Cannot get this video.')
99 return callback(err)
100 }
101
102 return callback(null, video)
103 })
104 }
105
106 function list (callback) {
107 VideosDB.find(function (err, videos_list) {
108 if (err) {
109 logger.error('Cannot get the list of the videos.')
110 return callback(err)
111 }
112
113 return callback(null, videos_list)
114 })
115 }
116
117 function listOwned (callback) {
118 // If namePath is not null this is *our* video
119 VideosDB.find({ namePath: { $ne: null } }, function (err, videos_list) {
120 if (err) {
121 logger.error('Cannot get the list of owned videos.')
122 return callback(err)
123 }
124
125 return callback(null, videos_list)
126 })
127 }
128
129 function removeOwned (id, callback) {
130 VideosDB.findByIdAndRemove(id, function (err, video) {
131 if (err) {
132 logger.error('Cannot remove the torrent.')
133 return callback(err)
134 }
135
136 fs.unlink(uploadDir + video.namePath, function (err) {
137 if (err) {
138 logger.error('Cannot remove this video file.')
139 return callback(err)
140 }
141
142 callback(null)
143 })
144 })
145 }
146
147 function removeAllRemotes (callback) {
148 VideosDB.remove({ namePath: null }, callback)
149 }
150
151 function removeAllRemotesOf (fromUrl, callback) {
152 VideosDB.remove({ podUrl: fromUrl }, callback)
153 }
154
155 // Use the magnet Uri because the _id field is not the same on different servers
156 function removeRemotesOfByMagnetUris (fromUrl, magnetUris, callback) {
157 if (callback === undefined) callback = function () {}
158
159 VideosDB.find({ magnetUri: { $in: magnetUris } }, function (err, videos) {
160 if (err || !videos) {
161 logger.error('Cannot find the torrent URI of these remote videos.')
162 return callback(err)
163 }
164
165 const to_remove = []
166 async.each(videos, function (video, callback_async) {
167 callback_async = dz(callback_async)
168
169 if (video.podUrl !== fromUrl) {
170 logger.error('The pod %s has not the rights on the video of %s.', fromUrl, video.podUrl)
171 } else {
172 to_remove.push(video._id)
173 }
174
175 callback_async()
176 }, function () {
177 VideosDB.remove({ _id: { $in: to_remove } }, function (err) {
178 if (err) {
179 logger.error('Cannot remove the remote videos.')
180 return callback(err)
181 }
182
183 logger.info('Removed remote videos from %s.', fromUrl)
184 callback(null)
185 })
186 })
187 })
188 }
189
190 function search (name, callback) {
191 VideosDB.find({ name: new RegExp(name) }, function (err, videos) {
192 if (err) {
193 logger.error('Cannot search the videos.')
194 return callback(err)
195 }
196
197 return callback(null, videos)
198 })
199 }
200
201 // ---------------------------------------------------------------------------
202
203 module.exports = Videos