]>
git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/models/video.js
3 const createTorrent
= require('create-torrent')
4 const ffmpeg
= require('fluent-ffmpeg')
5 const fs
= require('fs')
6 const parallel
= require('async/parallel')
7 const parseTorrent
= require('parse-torrent')
8 const pathUtils
= require('path')
9 const magnet
= require('magnet-uri')
10 const mongoose
= require('mongoose')
12 const constants
= require('../initializers/constants')
13 const customVideosValidators
= require('../helpers/custom-validators').videos
14 const logger
= require('../helpers/logger')
15 const modelUtils
= require('./utils')
16 const utils
= require('../helpers/utils')
18 // ---------------------------------------------------------------------------
20 // TODO: add indexes on searchable columns
21 const VideoSchema
= mongoose
.Schema({
37 VideoSchema
.path('name').validate(customVideosValidators
.isVideoNameValid
)
38 VideoSchema
.path('description').validate(customVideosValidators
.isVideoDescriptionValid
)
39 VideoSchema
.path('magnetUri').validate(customVideosValidators
.isVideoMagnetUriValid
)
40 VideoSchema
.path('podUrl').validate(customVideosValidators
.isVideoPodUrlValid
)
41 VideoSchema
.path('author').validate(customVideosValidators
.isVideoAuthorValid
)
42 VideoSchema
.path('duration').validate(customVideosValidators
.isVideoDurationValid
)
43 // The tumbnail can be the path or the data in base 64
44 // The pre save hook will convert the base 64 data in a file on disk and replace the thumbnail key by the filename
45 VideoSchema
.path('thumbnail').validate(function (value
) {
46 return customVideosValidators
.isVideoThumbnailValid(value
) || customVideosValidators
.isVideoThumbnail64Valid(value
)
48 VideoSchema
.path('tags').validate(customVideosValidators
.isVideoTagsValid
)
50 VideoSchema
.methods
= {
56 VideoSchema
.statics
= {
68 VideoSchema
.pre('remove', function (next
) {
74 removeThumbnail(video
, callback
)
78 if (video
.isOwned()) {
81 removeFile(video
, callback
)
84 removeTorrent(video
, callback
)
92 VideoSchema
.pre('save', function (next
) {
96 if (video
.isOwned()) {
97 const videoPath
= pathUtils
.join(constants
.CONFIG
.STORAGE
.UPLOAD_DIR
, video
.filename
)
98 this.podUrl
= constants
.CONFIG
.WEBSERVER
.URL
101 // TODO: refractoring
102 function (callback
) {
105 [ constants
.CONFIG
.WEBSERVER
.WS
+ '://' + constants
.CONFIG
.WEBSERVER
.HOST
+ ':' + constants
.CONFIG
.WEBSERVER
.PORT
+ '/tracker/socket' ]
108 constants
.CONFIG
.WEBSERVER
.URL
+ constants
.STATIC_PATHS
.WEBSEED
+ video
.filename
112 createTorrent(videoPath
, options
, function (err
, torrent
) {
113 if (err
) return callback(err
)
115 fs
.writeFile(constants
.CONFIG
.STORAGE
.TORRENTS_DIR
+ video
.filename
+ '.torrent', torrent
, function (err
) {
116 if (err
) return callback(err
)
118 const parsedTorrent
= parseTorrent(torrent
)
119 parsedTorrent
.xs
= video
.podUrl
+ constants
.STATIC_PATHS
.TORRENTS
+ video
.filename
+ '.torrent'
120 video
.magnetUri
= magnet
.encode(parsedTorrent
)
126 function (callback
) {
127 createThumbnail(videoPath
, callback
)
131 parallel(tasks
, function (err
, results
) {
132 if (err
) return next(err
)
134 video
.thumbnail
= results
[1]
139 generateThumbnailFromBase64(video
.thumbnail
, function (err
, thumbnailName
) {
140 if (err
) return next(err
)
142 video
.thumbnail
= thumbnailName
149 mongoose
.model('Video', VideoSchema
)
151 // ------------------------------ METHODS ------------------------------
153 function isOwned () {
154 return this.filename
!== null
157 function toFormatedJSON () {
161 description: this.description
,
162 podUrl: this.podUrl
.replace(/^https?:\/\//, ''),
163 isLocal: this.isOwned(),
164 magnetUri: this.magnetUri
,
166 duration: this.duration
,
168 thumbnailPath: constants
.STATIC_PATHS
.THUMBNAILS
+ '/' + this.thumbnail
,
169 createdDate: this.createdDate
175 function toRemoteJSON (callback
) {
178 // Convert thumbnail to base64
179 fs
.readFile(pathUtils
.join(constants
.CONFIG
.STORAGE
.THUMBNAILS_DIR
, this.thumbnail
), function (err
, thumbnailData
) {
181 logger
.error('Cannot read the thumbnail of the video')
185 const remoteVideo
= {
187 description: self
.description
,
188 magnetUri: self
.magnetUri
,
191 duration: self
.duration
,
192 thumbnailBase64: new Buffer(thumbnailData
).toString('base64'),
194 createdDate: self
.createdDate
,
198 return callback(null, remoteVideo
)
202 // ------------------------------ STATICS ------------------------------
204 function getDurationFromFile (videoPath
, callback
) {
205 ffmpeg
.ffprobe(videoPath
, function (err
, metadata
) {
206 if (err
) return callback(err
)
208 return callback(null, Math
.floor(metadata
.format
.duration
))
212 function listForApi (start
, count
, sort
, callback
) {
214 return modelUtils
.listForApiWithCount
.call(this, query
, start
, count
, sort
, callback
)
217 function listByUrlAndMagnet (fromUrl
, magnetUri
, callback
) {
218 this.find({ podUrl: fromUrl
, magnetUri: magnetUri
}, callback
)
221 function listByUrl (fromUrl
, callback
) {
222 this.find({ podUrl: fromUrl
}, callback
)
225 function listOwned (callback
) {
226 // If filename is not null this is *our* video
227 this.find({ filename: { $ne: null } }, callback
)
230 function listOwnedByAuthor (author
, callback
) {
231 this.find({ filename: { $ne: null }, author: author
}, callback
)
234 function listRemotes (callback
) {
235 this.find({ filename: null }, callback
)
238 function load (id
, callback
) {
239 this.findById(id
, callback
)
242 function search (value
, field
, start
, count
, sort
, callback
) {
244 // Make an exact search with the magnet
245 if (field
=== 'magnetUri' || field
=== 'tags') {
248 query
[field
] = new RegExp(value
)
251 modelUtils
.listForApiWithCount
.call(this, query
, start
, count
, sort
, callback
)
254 // ---------------------------------------------------------------------------
256 function removeThumbnail (video
, callback
) {
257 fs
.unlink(constants
.CONFIG
.STORAGE
.THUMBNAILS_DIR
+ video
.thumbnail
, callback
)
260 function removeFile (video
, callback
) {
261 fs
.unlink(constants
.CONFIG
.STORAGE
.UPLOAD_DIR
+ video
.filename
, callback
)
264 // Maybe the torrent is not seeded, but we catch the error to don't stop the removing process
265 function removeTorrent (video
, callback
) {
266 fs
.unlink(constants
.CONFIG
.STORAGE
.TORRENTS_DIR
+ video
.filename
+ '.torrent', callback
)
269 function createThumbnail (videoPath
, callback
) {
270 const filename
= pathUtils
.basename(videoPath
) + '.jpg'
272 .on('error', callback
)
273 .on('end', function () {
274 callback(null, filename
)
278 folder: constants
.CONFIG
.STORAGE
.THUMBNAILS_DIR
,
279 size: constants
.THUMBNAILS_SIZE
,
284 function generateThumbnailFromBase64 (data
, callback
) {
285 // Creating the thumbnail for this remote video
286 utils
.generateRandomString(16, function (err
, randomString
) {
287 if (err
) return callback(err
)
289 const thumbnailName
= randomString
+ '.jpg'
290 fs
.writeFile(constants
.CONFIG
.STORAGE
.THUMBNAILS_DIR
+ thumbnailName
, data
, { encoding: 'base64' }, function (err
) {
291 if (err
) return callback(err
)
293 return callback(null, thumbnailName
)