aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2016-05-10 21:19:24 +0200
committerChocobozzz <florian.bigard@gmail.com>2016-05-10 21:19:24 +0200
commitcbe2f7c34822b1bd3b1f8c691f79f0c29cf21f07 (patch)
treece678124210db8b03b2a523e3b92a14cc403eeee /server/lib
parentf1dae018681936b556b2496b7f2d872c004cfda3 (diff)
downloadPeerTube-cbe2f7c34822b1bd3b1f8c691f79f0c29cf21f07.tar.gz
PeerTube-cbe2f7c34822b1bd3b1f8c691f79f0c29cf21f07.tar.zst
PeerTube-cbe2f7c34822b1bd3b1f8c691f79f0c29cf21f07.zip
Refractoring and add thumbnails support (without tests)
Diffstat (limited to 'server/lib')
-rw-r--r--server/lib/friends.js39
-rw-r--r--server/lib/requestsScheduler.js12
-rw-r--r--server/lib/videos.js104
3 files changed, 136 insertions, 19 deletions
diff --git a/server/lib/friends.js b/server/lib/friends.js
index 9a2c5c06e..3b8a52060 100644
--- a/server/lib/friends.js
+++ b/server/lib/friends.js
@@ -11,6 +11,7 @@ const peertubeCrypto = require('../helpers/peertubeCrypto')
11const Pods = require('../models/pods') 11const Pods = require('../models/pods')
12const requestsScheduler = require('../lib/requestsScheduler') 12const requestsScheduler = require('../lib/requestsScheduler')
13const requests = require('../helpers/requests') 13const requests = require('../helpers/requests')
14const videos = require('../lib/videos')
14const Videos = require('../models/videos') 15const Videos = require('../models/videos')
15 16
16const http = config.get('webserver.https') ? 'https' : 'http' 17const http = config.get('webserver.https') ? 'https' : 'http'
@@ -20,6 +21,7 @@ const port = config.get('webserver.port')
20const pods = { 21const pods = {
21 addVideoToFriends: addVideoToFriends, 22 addVideoToFriends: addVideoToFriends,
22 hasFriends: hasFriends, 23 hasFriends: hasFriends,
24 getMyCertificate: getMyCertificate,
23 makeFriends: makeFriends, 25 makeFriends: makeFriends,
24 quitFriends: quitFriends, 26 quitFriends: quitFriends,
25 removeVideoToFriends: removeVideoToFriends 27 removeVideoToFriends: removeVideoToFriends
@@ -42,11 +44,15 @@ function hasFriends (callback) {
42 }) 44 })
43} 45}
44 46
47function getMyCertificate (callback) {
48 fs.readFile(peertubeCrypto.getCertDir() + 'peertube.pub', 'utf8', callback)
49}
50
45function makeFriends (callback) { 51function makeFriends (callback) {
46 const pods_score = {} 52 const pods_score = {}
47 53
48 logger.info('Make friends!') 54 logger.info('Make friends!')
49 fs.readFile(peertubeCrypto.getCertDir() + 'peertube.pub', 'utf8', function (err, cert) { 55 getMyCertificate(function (err, cert) {
50 if (err) { 56 if (err) {
51 logger.error('Cannot read public cert.') 57 logger.error('Cannot read public cert.')
52 return callback(err) 58 return callback(err)
@@ -54,8 +60,8 @@ function makeFriends (callback) {
54 60
55 const urls = config.get('network.friends') 61 const urls = config.get('network.friends')
56 62
57 async.each(urls, function (url, callback) { 63 async.each(urls, function (url, callback_each) {
58 computeForeignPodsList(url, pods_score, callback) 64 computeForeignPodsList(url, pods_score, callback_each)
59 }, function (err) { 65 }, function (err) {
60 if (err) return callback(err) 66 if (err) return callback(err)
61 67
@@ -96,11 +102,18 @@ function quitFriends (callback) {
96 102
97 logger.info('Broke friends, so sad :(') 103 logger.info('Broke friends, so sad :(')
98 104
99 Videos.removeAllRemotes(function (err) { 105 Videos.listFromRemotes(function (err, videos_list) {
100 if (err) return callback(err) 106 if (err) return callback(err)
101 107
102 logger.info('Removed all remote videos.') 108 videos.removeRemoteVideos(videos_list, function (err) {
103 callback(null) 109 if (err) {
110 logger.error('Cannot remove remote videos.', { error: err })
111 return callback(err)
112 }
113
114 logger.info('Removed all remote videos.')
115 callback(null)
116 })
104 }) 117 })
105 }) 118 })
106 }) 119 })
@@ -127,16 +140,14 @@ function computeForeignPodsList (url, pods_score, callback) {
127 if (err) return callback(err) 140 if (err) return callback(err)
128 if (foreign_pods_list.length === 0) return callback() 141 if (foreign_pods_list.length === 0) return callback()
129 142
130 async.each(foreign_pods_list, function (foreign_pod, callback_each) { 143 foreign_pods_list.forEach(function (foreign_pod) {
131 const foreign_url = foreign_pod.url 144 const foreign_url = foreign_pod.url
132 145
133 if (pods_score[foreign_url]) pods_score[foreign_url]++ 146 if (pods_score[foreign_url]) pods_score[foreign_url]++
134 else pods_score[foreign_url] = 1 147 else pods_score[foreign_url] = 1
135
136 callback_each()
137 }, function () {
138 callback()
139 }) 148 })
149
150 callback()
140 }) 151 })
141} 152}
142 153
@@ -194,13 +205,15 @@ function makeRequestsToWinningPods (cert, pods_list, callback) {
194 logger.error('Error with adding %s pod.', pod.url, { error: err }) 205 logger.error('Error with adding %s pod.', pod.url, { error: err })
195 return callback_each_request() 206 return callback_each_request()
196 } 207 }
197 208 console.log('hihi')
198 Videos.addRemotes(body.videos, function (err) { 209 videos.createRemoteVideos(body.videos, function (err) {
199 if (err) { 210 if (err) {
200 logger.error('Error with adding videos of pod.', pod.url, { error: err }) 211 logger.error('Error with adding videos of pod.', pod.url, { error: err })
201 return callback_each_request() 212 return callback_each_request()
202 } 213 }
203 214
215 console.log('kik')
216
204 logger.debug('Adding remote videos from %s.', pod.url, { videos: body.videos }) 217 logger.debug('Adding remote videos from %s.', pod.url, { videos: body.videos })
205 return callback_each_request() 218 return callback_each_request()
206 }) 219 })
diff --git a/server/lib/requestsScheduler.js b/server/lib/requestsScheduler.js
index 2c5474e51..4953f6a91 100644
--- a/server/lib/requestsScheduler.js
+++ b/server/lib/requestsScheduler.js
@@ -8,6 +8,7 @@ const logger = require('../helpers/logger')
8const Pods = require('../models/pods') 8const Pods = require('../models/pods')
9const Requests = require('../models/requests') 9const Requests = require('../models/requests')
10const requests = require('../helpers/requests') 10const requests = require('../helpers/requests')
11const videos = require('../lib/videos')
11const Videos = require('../models/videos') 12const Videos = require('../models/videos')
12 13
13let timer = null 14let timer = null
@@ -99,7 +100,7 @@ function makeRequest (type, requests_to_make, callback) {
99 requests.makeMultipleRetryRequest(params, pods, callbackEachPodFinished, callbackAllPodsFinished) 100 requests.makeMultipleRetryRequest(params, pods, callbackEachPodFinished, callbackAllPodsFinished)
100 101
101 function callbackEachPodFinished (err, response, body, url, pod, callback_each_pod_finished) { 102 function callbackEachPodFinished (err, response, body, url, pod, callback_each_pod_finished) {
102 if (err || (response.statusCode !== 200 && response.statusCode !== 204)) { 103 if (err || (response.statusCode !== 200 && response.statusCode !== 201 && response.statusCode !== 204)) {
103 bad_pods.push(pod._id) 104 bad_pods.push(pod._id)
104 logger.error('Error sending secure request to %s pod.', url, { error: err || new Error('Status code not 20x') }) 105 logger.error('Error sending secure request to %s pod.', url, { error: err || new Error('Status code not 20x') })
105 } else { 106 } else {
@@ -187,12 +188,13 @@ function removeBadPods () {
187 const urls = map(pods, 'url') 188 const urls = map(pods, 'url')
188 const ids = map(pods, '_id') 189 const ids = map(pods, '_id')
189 190
190 Videos.removeAllRemotesOf(urls, function (err, r) { 191 Videos.listFromUrls(urls, function (err, videos_list) {
191 if (err) { 192 if (err) {
192 logger.error('Cannot remove videos from a pod that we removing.', { error: err }) 193 logger.error('Cannot list videos urls.', { error: err, urls: urls })
193 } else { 194 } else {
194 const videos_removed = r.result.n 195 videos.removeRemoteVideos(videos_list, function (err) {
195 logger.info('Removed %d videos.', videos_removed) 196 if (err) logger.error('Cannot remove remote videos.', { error: err })
197 })
196 } 198 }
197 199
198 Pods.removeAllByIds(ids, function (err, r) { 200 Pods.removeAllByIds(ids, function (err, r) {
diff --git a/server/lib/videos.js b/server/lib/videos.js
index 43d6c6b99..b3497743a 100644
--- a/server/lib/videos.js
+++ b/server/lib/videos.js
@@ -3,21 +3,39 @@
3const async = require('async') 3const async = require('async')
4const config = require('config') 4const config = require('config')
5const ffmpeg = require('fluent-ffmpeg') 5const ffmpeg = require('fluent-ffmpeg')
6const fs = require('fs')
7const map = require('lodash/map')
6const pathUtils = require('path') 8const pathUtils = require('path')
7const webtorrent = require('../lib/webtorrent')
8 9
10const constants = require('../initializers/constants')
9const logger = require('../helpers/logger') 11const logger = require('../helpers/logger')
12const utils = require('../helpers/utils')
10const Videos = require('../models/videos') 13const Videos = require('../models/videos')
14const webtorrent = require('../lib/webtorrent')
11 15
12const uploadDir = pathUtils.join(__dirname, '..', '..', config.get('storage.uploads')) 16const uploadDir = pathUtils.join(__dirname, '..', '..', config.get('storage.uploads'))
17const thumbnailsDir = pathUtils.join(__dirname, '..', '..', config.get('storage.thumbnails'))
13 18
14const videos = { 19const videos = {
20 createRemoteVideos: createRemoteVideos,
15 getVideoDuration: getVideoDuration, 21 getVideoDuration: getVideoDuration,
16 getVideoState: getVideoState, 22 getVideoState: getVideoState,
23 getVideoThumbnail: getVideoThumbnail,
24 removeVideosDataFromDisk: removeVideosDataFromDisk,
25 removeRemoteVideos: removeRemoteVideos,
17 seed: seed, 26 seed: seed,
18 seedAllExisting: seedAllExisting 27 seedAllExisting: seedAllExisting
19} 28}
20 29
30function createRemoteVideos (videos, callback) {
31 // Create the remote videos from the new pod
32 createRemoteVideoObjects(videos, function (err, remote_videos) {
33 if (err) return callback(err)
34
35 Videos.addRemotes(remote_videos, callback)
36 })
37}
38
21function getVideoDuration (video_path, callback) { 39function getVideoDuration (video_path, callback) {
22 ffmpeg.ffprobe(video_path, function (err, metadata) { 40 ffmpeg.ffprobe(video_path, function (err, metadata) {
23 if (err) return callback(err) 41 if (err) return callback(err)
@@ -36,6 +54,51 @@ function getVideoState (video) {
36 return { exist: exist, owned: owned } 54 return { exist: exist, owned: owned }
37} 55}
38 56
57function getVideoThumbnail (video_path, callback) {
58 const filename = pathUtils.basename(video_path) + '.jpg'
59 ffmpeg(video_path)
60 .on('error', callback)
61 .on('end', function () {
62 callback(null, filename)
63 })
64 .thumbnail({
65 count: 1,
66 folder: thumbnailsDir,
67 size: constants.THUMBNAILS_SIZE,
68 filename: filename
69 })
70}
71
72// Remove video datas from disk (video file, thumbnail...)
73function removeVideosDataFromDisk (videos, callback) {
74 async.each(videos, function (video, callback_each) {
75 fs.unlink(thumbnailsDir + video.thumbnail, function (err) {
76 if (err) logger.error('Cannot remove the video thumbnail')
77
78 if (getVideoState(video).owned === true) {
79 fs.unlink(uploadDir + video.namePath, function (err) {
80 if (err) {
81 logger.error('Cannot remove this video file.')
82 return callback_each(err)
83 }
84
85 callback_each(null)
86 })
87 } else {
88 callback_each(null)
89 }
90 })
91 }, callback)
92}
93
94function removeRemoteVideos (videos, callback) {
95 Videos.removeByIds(map(videos, '_id'), function (err) {
96 if (err) return callback(err)
97
98 removeVideosDataFromDisk(videos, callback)
99 })
100}
101
39function seed (path, callback) { 102function seed (path, callback) {
40 logger.info('Seeding %s...', path) 103 logger.info('Seeding %s...', path)
41 104
@@ -69,3 +132,42 @@ function seedAllExisting (callback) {
69// --------------------------------------------------------------------------- 132// ---------------------------------------------------------------------------
70 133
71module.exports = videos 134module.exports = videos
135
136// ---------------------------------------------------------------------------
137
138function createRemoteVideoObjects (videos, callback) {
139 const remote_videos = []
140
141 async.each(videos, function (video, callback_each) {
142 // Creating the thumbnail for this remote video
143 utils.generateRandomString(16, function (err, random_string) {
144 if (err) return callback_each(err)
145
146 const thumbnail_name = random_string + '.jpg'
147 createThumbnailFromBase64(thumbnail_name, video.thumbnail_base64, function (err) {
148 if (err) return callback_each(err)
149
150 const params = {
151 name: video.name,
152 description: video.description,
153 magnetUri: video.magnetUri,
154 podUrl: video.podUrl,
155 duration: video.duration,
156 thumbnail: thumbnail_name
157 }
158 remote_videos.push(params)
159
160 callback_each(null)
161 })
162 })
163 },
164 function (err) {
165 if (err) return callback(err)
166
167 callback(null, remote_videos)
168 })
169}
170
171function createThumbnailFromBase64 (thumbnail_name, data, callback) {
172 fs.writeFile(thumbnailsDir + thumbnail_name, data, { encoding: 'base64' }, callback)
173}