]>
git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/videos.js
a74c77dc4c98b8bdf37cb80c7d67a4b03275030f
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')
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')
16 const uploadDir
= pathUtils
.join(__dirname
, '..', '..', config
.get('storage.uploads'))
17 const thumbnailsDir
= pathUtils
.join(__dirname
, '..', '..', config
.get('storage.thumbnails'))
20 convertVideoToRemote: convertVideoToRemote
,
21 createRemoteVideos: createRemoteVideos
,
22 getVideoDuration: getVideoDuration
,
23 getVideoState: getVideoState
,
24 createVideoThumbnail: createVideoThumbnail
,
25 removeVideosDataFromDisk: removeVideosDataFromDisk
,
26 removeRemoteVideos: removeRemoteVideos
,
28 seedAllExisting: seedAllExisting
31 function convertVideoToRemote (video
, callback
) {
32 fs
.readFile(thumbnailsDir
+ video
.thumbnail
, function (err
, thumbnailData
) {
34 logger
.error('Cannot read the thumbnail of the video')
40 description: video
.description
,
41 magnetUri: video
.magnetUri
,
43 duration: video
.duration
,
44 thumbnailBase64: new Buffer(thumbnailData
).toString('base64'),
46 createdDate: video
.createdDate
,
50 return callback(null, remoteVideo
)
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
)
59 Videos
.addRemotes(remoteVideos
, callback
)
63 function getVideoDuration (videoPath
, callback
) {
64 ffmpeg
.ffprobe(videoPath
, function (err
, metadata
) {
65 if (err
) return callback(err
)
67 return callback(null, Math
.floor(metadata
.format
.duration
))
71 function getVideoState (video
) {
72 const exist
= (video
!== null)
75 owned
= (video
.namePath
!== null)
78 return { exist: exist
, owned: owned
}
81 function createVideoThumbnail (videoPath
, callback
) {
82 const filename
= pathUtils
.basename(videoPath
) + '.jpg'
84 .on('error', callback
)
85 .on('end', function () {
86 callback(null, filename
)
90 folder: thumbnailsDir
,
91 size: constants
.THUMBNAILS_SIZE
,
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')
102 if (getVideoState(video
).owned
=== true) {
103 fs
.unlink(uploadDir
+ video
.namePath
, function (err
) {
105 logger
.error('Cannot remove this video file.')
106 return callbackEach(err
)
118 function removeRemoteVideos (videos
, callback
) {
119 Videos
.removeByIds(map(videos
, '_id'), function (err
) {
120 if (err
) return callback(err
)
122 removeVideosDataFromDisk(videos
, callback
)
126 function seed (path
, callback
) {
127 logger
.info('Seeding %s...', path
)
129 webtorrent
.seed(path
, function (torrent
) {
130 logger
.info('%s seeded (%s).', path
, torrent
.magnetURI
)
132 return callback(null, torrent
)
136 function seedAllExisting (callback
) {
137 Videos
.listOwned(function (err
, videosList
) {
139 logger
.error('Cannot get list of the videos to seed.')
143 async
.each(videosList
, function (video
, callbackEach
) {
144 seed(uploadDir
+ video
.namePath
, function (err
) {
146 logger
.error('Cannot seed this video.')
156 // ---------------------------------------------------------------------------
158 module
.exports
= videos
160 // ---------------------------------------------------------------------------
162 function createRemoteVideoObjects (videos
, callback
) {
163 const remoteVideos
= []
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
)
170 const thumbnailName
= randomString
+ '.jpg'
171 createThumbnailFromBase64(thumbnailName
, video
.thumbnailBase64
, function (err
) {
172 if (err
) return callbackEach(err
)
176 description: video
.description
,
177 magnetUri: video
.magnetUri
,
178 podUrl: video
.podUrl
,
179 duration: video
.duration
,
180 thumbnail: thumbnailName
,
184 remoteVideos
.push(params
)
191 if (err
) return callback(err
)
193 callback(null, remoteVideos
)
197 function createThumbnailFromBase64 (thumbnailName
, data
, callback
) {
198 fs
.writeFile(thumbnailsDir
+ thumbnailName
, data
, { encoding: 'base64' }, callback
)