]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/models/videos.js
Update client packages
[github/Chocobozzz/PeerTube.git] / server / models / videos.js
CommitLineData
9f10b292
C
1'use strict'
2
f0f5567b
C
3const async = require('async')
4const config = require('config')
5const dz = require('dezalgo')
6const fs = require('fs')
7const mongoose = require('mongoose')
8const path = require('path')
9f10b292 9
f0f5567b 10const logger = require('../helpers/logger')
9f10b292 11
f0f5567b
C
12const http = config.get('webserver.https') === true ? 'https' : 'http'
13const host = config.get('webserver.host')
14const port = config.get('webserver.port')
15const uploadDir = path.join(__dirname, '..', '..', config.get('storage.uploads'))
9f10b292
C
16
17// ---------------------------------------------------------------------------
18
f0f5567b 19const videosSchema = mongoose.Schema({
9f10b292
C
20 name: String,
21 namePath: String,
22 description: String,
23 magnetUri: String,
24 podUrl: String
25})
f0f5567b 26const VideosDB = mongoose.model('videos', videosSchema)
9f10b292
C
27
28// ---------------------------------------------------------------------------
29
f0f5567b 30const Videos = {
9f10b292
C
31 add: add,
32 addRemotes: addRemotes,
33 get: get,
9f10b292
C
34 list: list,
35 listOwned: listOwned,
36 removeOwned: removeOwned,
37 removeAllRemotes: removeAllRemotes,
38 removeAllRemotesOf: removeAllRemotesOf,
39 removeRemotesOfByMagnetUris: removeRemotesOfByMagnetUris,
40 search: search
41}
42
43function add (video, callback) {
44 logger.info('Adding %s video to database.', video.name)
45
f0f5567b 46 const params = video
9f10b292
C
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)
c173e565 56 })
9f10b292 57}
8c308c2b 58
9f10b292
C
59// TODO: avoid doublons
60function addRemotes (videos, callback) {
61 if (!callback) callback = function () {}
c45f7f84 62
f0f5567b 63 const to_add = []
c45f7f84 64
9f10b292
C
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)
c45f7f84 68
f0f5567b 69 const params = {
9f10b292
C
70 name: video.name,
71 namePath: null,
72 description: video.description,
73 magnetUri: video.magnetUri,
74 podUrl: video.podUrl
75 }
c45f7f84 76
9f10b292 77 to_add.push(params)
c45f7f84 78
9f10b292
C
79 callback_each()
80 }, function () {
81 VideosDB.create(to_add, function (err, videos) {
c45f7f84 82 if (err) {
9f10b292 83 logger.error('Cannot insert this remote video.')
c45f7f84
C
84 return callback(err)
85 }
86
9f10b292 87 return callback(null, videos)
c45f7f84 88 })
9f10b292
C
89 })
90}
c45f7f84 91
9f10b292
C
92function 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 }
c173e565 98
9f10b292
C
99 return callback(null, video)
100 })
101}
c173e565 102
9f10b292
C
103function 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 }
c173e565 109
9f10b292
C
110 return callback(null, videos_list)
111 })
112}
c45f7f84 113
9f10b292
C
114function 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
126function 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 }
c45f7f84 132
9f10b292 133 fs.unlink(uploadDir + video.namePath, function (err) {
c45f7f84 134 if (err) {
9f10b292 135 logger.error('Cannot remove this video file.')
c45f7f84
C
136 return callback(err)
137 }
138
9f10b292 139 callback(null)
c45f7f84 140 })
9f10b292
C
141 })
142}
143
144function removeAllRemotes (callback) {
145 VideosDB.remove({ namePath: null }, callback)
146}
147
148function removeAllRemotesOf (fromUrl, callback) {
9f10b292
C
149 VideosDB.remove({ podUrl: fromUrl }, callback)
150}
151
152// Use the magnet Uri because the _id field is not the same on different servers
153function 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
f0f5567b 162 const to_remove = []
9f10b292
C
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)
8c308c2b
C
170 }
171
9f10b292
C
172 callback_async()
173 }, function () {
174 VideosDB.remove({ _id: { $in: to_remove } }, function (err) {
c173e565 175 if (err) {
9f10b292 176 logger.error('Cannot remove the remote videos.')
c173e565
C
177 return callback(err)
178 }
8c308c2b 179
9f10b292 180 logger.info('Removed remote videos from %s.', fromUrl)
c173e565 181 callback(null)
8c308c2b
C
182 })
183 })
9f10b292
C
184 })
185}
8c308c2b 186
9f10b292
C
187function 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 }
8c308c2b 193
9f10b292
C
194 return callback(null, videos)
195 })
196}
8c308c2b 197
9f10b292 198// ---------------------------------------------------------------------------
c45f7f84 199
9f10b292 200module.exports = Videos