aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/videos.js
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/videos.js
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/videos.js')
-rw-r--r--server/lib/videos.js104
1 files changed, 103 insertions, 1 deletions
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}