diff options
Diffstat (limited to 'server/lib')
-rw-r--r-- | server/lib/friends.js | 21 | ||||
-rw-r--r-- | server/lib/requestsScheduler.js | 18 | ||||
-rw-r--r-- | server/lib/videos.js | 199 |
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 @@ | |||
3 | const async = require('async') | 3 | const async = require('async') |
4 | const config = require('config') | 4 | const config = require('config') |
5 | const fs = require('fs') | 5 | const fs = require('fs') |
6 | const mongoose = require('mongoose') | ||
6 | const request = require('request') | 7 | const request = require('request') |
7 | 8 | ||
8 | const constants = require('../initializers/constants') | 9 | const constants = require('../initializers/constants') |
@@ -11,12 +12,11 @@ const peertubeCrypto = require('../helpers/peertubeCrypto') | |||
11 | const Pods = require('../models/pods') | 12 | const Pods = require('../models/pods') |
12 | const requestsScheduler = require('../lib/requestsScheduler') | 13 | const requestsScheduler = require('../lib/requestsScheduler') |
13 | const requests = require('../helpers/requests') | 14 | const requests = require('../helpers/requests') |
14 | const videos = require('../lib/videos') | ||
15 | const Videos = require('../models/videos') | ||
16 | 15 | ||
17 | const http = config.get('webserver.https') ? 'https' : 'http' | 16 | const http = config.get('webserver.https') ? 'https' : 'http' |
18 | const host = config.get('webserver.host') | 17 | const host = config.get('webserver.host') |
19 | const port = config.get('webserver.port') | 18 | const port = config.get('webserver.port') |
19 | const Video = mongoose.model('Video') | ||
20 | 20 | ||
21 | const pods = { | 21 | const 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 | ||
148 | function sendOwnedVideosToPod (podId) { | 143 | function 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 | ||
3 | const async = require('async') | 3 | const async = require('async') |
4 | const map = require('lodash/map') | 4 | const map = require('lodash/map') |
5 | const mongoose = require('mongoose') | ||
5 | 6 | ||
6 | const constants = require('../initializers/constants') | 7 | const constants = require('../initializers/constants') |
7 | const logger = require('../helpers/logger') | 8 | const logger = require('../helpers/logger') |
8 | const Pods = require('../models/pods') | 9 | const Pods = require('../models/pods') |
9 | const Requests = require('../models/requests') | 10 | const Requests = require('../models/requests') |
10 | const requests = require('../helpers/requests') | 11 | const requests = require('../helpers/requests') |
11 | const videos = require('../lib/videos') | 12 | |
12 | const Videos = require('../models/videos') | 13 | const Video = mongoose.model('Video') |
13 | 14 | ||
14 | let timer = null | 15 | let 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 | |||
3 | const async = require('async') | ||
4 | const config = require('config') | ||
5 | const ffmpeg = require('fluent-ffmpeg') | ||
6 | const fs = require('fs') | ||
7 | const map = require('lodash/map') | ||
8 | const pathUtils = require('path') | ||
9 | |||
10 | const constants = require('../initializers/constants') | ||
11 | const logger = require('../helpers/logger') | ||
12 | const utils = require('../helpers/utils') | ||
13 | const Videos = require('../models/videos') | ||
14 | const webtorrent = require('../lib/webtorrent') | ||
15 | |||
16 | const uploadDir = pathUtils.join(__dirname, '..', '..', config.get('storage.uploads')) | ||
17 | const thumbnailsDir = pathUtils.join(__dirname, '..', '..', config.get('storage.thumbnails')) | ||
18 | |||
19 | const 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 | |||
31 | function 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 | |||
54 | function 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 | |||
63 | function 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 | |||
71 | function 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 | |||
81 | function 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...) | ||
97 | function 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 | |||
118 | function 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 | |||
126 | function 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 | |||
136 | function 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 | |||
158 | module.exports = videos | ||
159 | |||
160 | // --------------------------------------------------------------------------- | ||
161 | |||
162 | function 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 | |||
197 | function createThumbnailFromBase64 (thumbnailName, data, callback) { | ||
198 | fs.writeFile(thumbnailsDir + thumbnailName, data, { encoding: 'base64' }, callback) | ||
199 | } | ||