aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2016-06-24 17:42:51 +0200
committerChocobozzz <florian.bigard@gmail.com>2016-06-24 17:42:51 +0200
commitaaf61f3810e6d57c5130af959bd2860df32775e7 (patch)
tree3871b20359aa56995a4b8974ae5cb6b911dd73f5 /server/lib
parent07d932038745e773359fa87bac6be70523f593ee (diff)
downloadPeerTube-aaf61f3810e6d57c5130af959bd2860df32775e7.tar.gz
PeerTube-aaf61f3810e6d57c5130af959bd2860df32775e7.tar.zst
PeerTube-aaf61f3810e6d57c5130af959bd2860df32775e7.zip
Video model refractoring -> use mongoose api
Diffstat (limited to 'server/lib')
-rw-r--r--server/lib/friends.js21
-rw-r--r--server/lib/requestsScheduler.js18
-rw-r--r--server/lib/videos.js199
3 files changed, 20 insertions, 218 deletions
diff --git a/server/lib/friends.js b/server/lib/friends.js
index d81a603ad..91cd69f86 100644
--- a/server/lib/friends.js
+++ b/server/lib/friends.js
@@ -3,6 +3,7 @@
3const async = require('async') 3const async = require('async')
4const config = require('config') 4const config = require('config')
5const fs = require('fs') 5const fs = require('fs')
6const mongoose = require('mongoose')
6const request = require('request') 7const request = require('request')
7 8
8const constants = require('../initializers/constants') 9const constants = require('../initializers/constants')
@@ -11,12 +12,11 @@ const peertubeCrypto = require('../helpers/peertubeCrypto')
11const Pods = require('../models/pods') 12const Pods = require('../models/pods')
12const requestsScheduler = require('../lib/requestsScheduler') 13const requestsScheduler = require('../lib/requestsScheduler')
13const requests = require('../helpers/requests') 14const requests = require('../helpers/requests')
14const videos = require('../lib/videos')
15const Videos = require('../models/videos')
16 15
17const http = config.get('webserver.https') ? 'https' : 'http' 16const http = config.get('webserver.https') ? 'https' : 'http'
18const host = config.get('webserver.host') 17const host = config.get('webserver.host')
19const port = config.get('webserver.port') 18const port = config.get('webserver.port')
19const Video = mongoose.model('Video')
20 20
21const pods = { 21const pods = {
22 addVideoToFriends: addVideoToFriends, 22 addVideoToFriends: addVideoToFriends,
@@ -117,18 +117,13 @@ function quitFriends (callback) {
117 function listRemoteVideos (callbackAsync) { 117 function listRemoteVideos (callbackAsync) {
118 logger.info('Broke friends, so sad :(') 118 logger.info('Broke friends, so sad :(')
119 119
120 Videos.listFromRemotes(callbackAsync) 120 Video.listRemotes(callbackAsync)
121 }, 121 },
122 122
123 function removeTheRemoteVideos (videosList, callbackAsync) { 123 function removeTheRemoteVideos (videosList, callbackAsync) {
124 videos.removeRemoteVideos(videosList, function (err) { 124 async.each(videosList, function (video, callbackEach) {
125 if (err) { 125 video.remove(callbackEach)
126 logger.error('Cannot remove remote videos.', { error: err }) 126 }, callbackAsync)
127 return callbackAsync(err)
128 }
129
130 return callbackAsync(null)
131 })
132 } 127 }
133 ], function (err) { 128 ], function (err) {
134 // Don't forget to re activate the scheduler, even if there was an error 129 // Don't forget to re activate the scheduler, even if there was an error
@@ -146,14 +141,14 @@ function removeVideoToFriends (video) {
146} 141}
147 142
148function sendOwnedVideosToPod (podId) { 143function sendOwnedVideosToPod (podId) {
149 Videos.listOwned(function (err, videosList) { 144 Video.listOwned(function (err, videosList) {
150 if (err) { 145 if (err) {
151 logger.error('Cannot get the list of videos we own.') 146 logger.error('Cannot get the list of videos we own.')
152 return 147 return
153 } 148 }
154 149
155 videosList.forEach(function (video) { 150 videosList.forEach(function (video) {
156 videos.convertVideoToRemote(video, function (err, remoteVideo) { 151 video.toRemoteJSON(function (err, remoteVideo) {
157 if (err) { 152 if (err) {
158 logger.error('Cannot convert video to remote.', { error: err }) 153 logger.error('Cannot convert video to remote.', { error: err })
159 // Don't break the process 154 // Don't break the process
diff --git a/server/lib/requestsScheduler.js b/server/lib/requestsScheduler.js
index ac75e5b93..b192d8299 100644
--- a/server/lib/requestsScheduler.js
+++ b/server/lib/requestsScheduler.js
@@ -2,14 +2,15 @@
2 2
3const async = require('async') 3const async = require('async')
4const map = require('lodash/map') 4const map = require('lodash/map')
5const mongoose = require('mongoose')
5 6
6const constants = require('../initializers/constants') 7const constants = require('../initializers/constants')
7const logger = require('../helpers/logger') 8const logger = require('../helpers/logger')
8const Pods = require('../models/pods') 9const Pods = require('../models/pods')
9const Requests = require('../models/requests') 10const Requests = require('../models/requests')
10const requests = require('../helpers/requests') 11const requests = require('../helpers/requests')
11const videos = require('../lib/videos') 12
12const Videos = require('../models/videos') 13const Video = mongoose.model('Video')
13 14
14let timer = null 15let timer = null
15 16
@@ -210,7 +211,7 @@ function removeBadPods () {
210 const urls = map(pods, 'url') 211 const urls = map(pods, 'url')
211 const ids = map(pods, '_id') 212 const ids = map(pods, '_id')
212 213
213 Videos.listFromUrls(urls, function (err, videosList) { 214 Video.listByUrls(urls, function (err, videosList) {
214 if (err) { 215 if (err) {
215 logger.error('Cannot list videos urls.', { error: err, urls: urls }) 216 logger.error('Cannot list videos urls.', { error: err, urls: urls })
216 return callback(null, ids, []) 217 return callback(null, ids, [])
@@ -224,9 +225,14 @@ function removeBadPods () {
224 // We don't have to remove pods, skip 225 // We don't have to remove pods, skip
225 if (typeof podIds === 'function') return podIds(null) 226 if (typeof podIds === 'function') return podIds(null)
226 227
227 // Remove the remote videos 228 async.each(videosList, function (video, callbackEach) {
228 videos.removeRemoteVideos(videosList, function (err) { 229 video.remove(callbackEach)
229 if (err) logger.error('Cannot remove remote videos.', { error: err }) 230 }, function (err) {
231 if (err) {
232 // Don't stop the process
233 logger.error('Error while removing videos of bad pods.', { error: err })
234 return
235 }
230 236
231 return callback(null, podIds) 237 return callback(null, podIds)
232 }) 238 })
diff --git a/server/lib/videos.js b/server/lib/videos.js
deleted file mode 100644
index a74c77dc4..000000000
--- a/server/lib/videos.js
+++ /dev/null
@@ -1,199 +0,0 @@
1'use strict'
2
3const async = require('async')
4const config = require('config')
5const ffmpeg = require('fluent-ffmpeg')
6const fs = require('fs')
7const map = require('lodash/map')
8const pathUtils = require('path')
9
10const constants = require('../initializers/constants')
11const logger = require('../helpers/logger')
12const utils = require('../helpers/utils')
13const Videos = require('../models/videos')
14const webtorrent = require('../lib/webtorrent')
15
16const uploadDir = pathUtils.join(__dirname, '..', '..', config.get('storage.uploads'))
17const thumbnailsDir = pathUtils.join(__dirname, '..', '..', config.get('storage.thumbnails'))
18
19const videos = {
20 convertVideoToRemote: convertVideoToRemote,
21 createRemoteVideos: createRemoteVideos,
22 getVideoDuration: getVideoDuration,
23 getVideoState: getVideoState,
24 createVideoThumbnail: createVideoThumbnail,
25 removeVideosDataFromDisk: removeVideosDataFromDisk,
26 removeRemoteVideos: removeRemoteVideos,
27 seed: seed,
28 seedAllExisting: seedAllExisting
29}
30
31function convertVideoToRemote (video, callback) {
32 fs.readFile(thumbnailsDir + video.thumbnail, function (err, thumbnailData) {
33 if (err) {
34 logger.error('Cannot read the thumbnail of the video')
35 return callback(err)
36 }
37
38 const remoteVideo = {
39 name: video.name,
40 description: video.description,
41 magnetUri: video.magnetUri,
42 author: video.author,
43 duration: video.duration,
44 thumbnailBase64: new Buffer(thumbnailData).toString('base64'),
45 tags: video.tags,
46 createdDate: video.createdDate,
47 podUrl: video.podUrl
48 }
49
50 return callback(null, remoteVideo)
51 })
52}
53
54function createRemoteVideos (videos, callback) {
55 // Create the remote videos from the new pod
56 createRemoteVideoObjects(videos, function (err, remoteVideos) {
57 if (err) return callback(err)
58
59 Videos.addRemotes(remoteVideos, callback)
60 })
61}
62
63function getVideoDuration (videoPath, callback) {
64 ffmpeg.ffprobe(videoPath, function (err, metadata) {
65 if (err) return callback(err)
66
67 return callback(null, Math.floor(metadata.format.duration))
68 })
69}
70
71function getVideoState (video) {
72 const exist = (video !== null)
73 let owned = false
74 if (exist === true) {
75 owned = (video.namePath !== null)
76 }
77
78 return { exist: exist, owned: owned }
79}
80
81function createVideoThumbnail (videoPath, callback) {
82 const filename = pathUtils.basename(videoPath) + '.jpg'
83 ffmpeg(videoPath)
84 .on('error', callback)
85 .on('end', function () {
86 callback(null, filename)
87 })
88 .thumbnail({
89 count: 1,
90 folder: thumbnailsDir,
91 size: constants.THUMBNAILS_SIZE,
92 filename: filename
93 })
94}
95
96// Remove video datas from disk (video file, thumbnail...)
97function removeVideosDataFromDisk (videos, callback) {
98 async.each(videos, function (video, callbackEach) {
99 fs.unlink(thumbnailsDir + video.thumbnail, function (err) {
100 if (err) logger.error('Cannot remove the video thumbnail')
101
102 if (getVideoState(video).owned === true) {
103 fs.unlink(uploadDir + video.namePath, function (err) {
104 if (err) {
105 logger.error('Cannot remove this video file.')
106 return callbackEach(err)
107 }
108
109 callbackEach(null)
110 })
111 } else {
112 callbackEach(null)
113 }
114 })
115 }, callback)
116}
117
118function removeRemoteVideos (videos, callback) {
119 Videos.removeByIds(map(videos, '_id'), function (err) {
120 if (err) return callback(err)
121
122 removeVideosDataFromDisk(videos, callback)
123 })
124}
125
126function seed (path, callback) {
127 logger.info('Seeding %s...', path)
128
129 webtorrent.seed(path, function (torrent) {
130 logger.info('%s seeded (%s).', path, torrent.magnetURI)
131
132 return callback(null, torrent)
133 })
134}
135
136function seedAllExisting (callback) {
137 Videos.listOwned(function (err, videosList) {
138 if (err) {
139 logger.error('Cannot get list of the videos to seed.')
140 return callback(err)
141 }
142
143 async.each(videosList, function (video, callbackEach) {
144 seed(uploadDir + video.namePath, function (err) {
145 if (err) {
146 logger.error('Cannot seed this video.')
147 return callback(err)
148 }
149
150 callbackEach(null)
151 })
152 }, callback)
153 })
154}
155
156// ---------------------------------------------------------------------------
157
158module.exports = videos
159
160// ---------------------------------------------------------------------------
161
162function createRemoteVideoObjects (videos, callback) {
163 const remoteVideos = []
164
165 async.each(videos, function (video, callbackEach) {
166 // Creating the thumbnail for this remote video
167 utils.generateRandomString(16, function (err, randomString) {
168 if (err) return callbackEach(err)
169
170 const thumbnailName = randomString + '.jpg'
171 createThumbnailFromBase64(thumbnailName, video.thumbnailBase64, function (err) {
172 if (err) return callbackEach(err)
173
174 const params = {
175 name: video.name,
176 description: video.description,
177 magnetUri: video.magnetUri,
178 podUrl: video.podUrl,
179 duration: video.duration,
180 thumbnail: thumbnailName,
181 tags: video.tags,
182 author: video.author
183 }
184 remoteVideos.push(params)
185
186 callbackEach(null)
187 })
188 })
189 },
190 function (err) {
191 if (err) return callback(err)
192
193 callback(null, remoteVideos)
194 })
195}
196
197function createThumbnailFromBase64 (thumbnailName, data, callback) {
198 fs.writeFile(thumbnailsDir + thumbnailName, data, { encoding: 'base64' }, callback)
199}