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