diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2016-05-10 21:19:24 +0200 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2016-05-10 21:19:24 +0200 |
commit | cbe2f7c34822b1bd3b1f8c691f79f0c29cf21f07 (patch) | |
tree | ce678124210db8b03b2a523e3b92a14cc403eeee /server/lib/videos.js | |
parent | f1dae018681936b556b2496b7f2d872c004cfda3 (diff) | |
download | PeerTube-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.js | 104 |
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 @@ | |||
3 | const async = require('async') | 3 | const async = require('async') |
4 | const config = require('config') | 4 | const config = require('config') |
5 | const ffmpeg = require('fluent-ffmpeg') | 5 | const ffmpeg = require('fluent-ffmpeg') |
6 | const fs = require('fs') | ||
7 | const map = require('lodash/map') | ||
6 | const pathUtils = require('path') | 8 | const pathUtils = require('path') |
7 | const webtorrent = require('../lib/webtorrent') | ||
8 | 9 | ||
10 | const constants = require('../initializers/constants') | ||
9 | const logger = require('../helpers/logger') | 11 | const logger = require('../helpers/logger') |
12 | const utils = require('../helpers/utils') | ||
10 | const Videos = require('../models/videos') | 13 | const Videos = require('../models/videos') |
14 | const webtorrent = require('../lib/webtorrent') | ||
11 | 15 | ||
12 | const uploadDir = pathUtils.join(__dirname, '..', '..', config.get('storage.uploads')) | 16 | const uploadDir = pathUtils.join(__dirname, '..', '..', config.get('storage.uploads')) |
17 | const thumbnailsDir = pathUtils.join(__dirname, '..', '..', config.get('storage.thumbnails')) | ||
13 | 18 | ||
14 | const videos = { | 19 | const 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 | ||
30 | function 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 | |||
21 | function getVideoDuration (video_path, callback) { | 39 | function 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 | ||
57 | function 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...) | ||
73 | function 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 | |||
94 | function 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 | |||
39 | function seed (path, callback) { | 102 | function 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 | ||
71 | module.exports = videos | 134 | module.exports = videos |
135 | |||
136 | // --------------------------------------------------------------------------- | ||
137 | |||
138 | function 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 | |||
171 | function createThumbnailFromBase64 (thumbnail_name, data, callback) { | ||
172 | fs.writeFile(thumbnailsDir + thumbnail_name, data, { encoding: 'base64' }, callback) | ||
173 | } | ||