]>
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 magnetUtil
= require('magnet-uri')
7 const parallel
= require('async/parallel')
8 const parseTorrent
= require('parse-torrent')
9 const pathUtils
= require('path')
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')
17 // ---------------------------------------------------------------------------
19 // TODO: add indexes on searchable columns
20 const VideoSchema
= mongoose
.Schema({
24 enum: [ '.mp4', '.webm', '.ogv' ]
26 remoteId: mongoose
.Schema
.Types
.ObjectId
,
41 VideoSchema
.path('name').validate(customVideosValidators
.isVideoNameValid
)
42 VideoSchema
.path('description').validate(customVideosValidators
.isVideoDescriptionValid
)
43 VideoSchema
.path('podHost').validate(customVideosValidators
.isVideoPodHostValid
)
44 VideoSchema
.path('author').validate(customVideosValidators
.isVideoAuthorValid
)
45 VideoSchema
.path('duration').validate(customVideosValidators
.isVideoDurationValid
)
46 VideoSchema
.path('tags').validate(customVideosValidators
.isVideoTagsValid
)
48 VideoSchema
.methods
= {
59 VideoSchema
.statics
= {
62 listByHostAndRemoteId
,
71 VideoSchema
.pre('remove', function (next
) {
77 removeThumbnail(video
, callback
)
81 if (video
.isOwned()) {
84 removeFile(video
, callback
)
87 removeTorrent(video
, callback
)
90 removePreview(video
, callback
)
98 VideoSchema
.pre('save', function (next
) {
102 if (video
.isOwned()) {
103 const videoPath
= pathUtils
.join(constants
.CONFIG
.STORAGE
.VIDEOS_DIR
, video
.getVideoFilename())
104 this.podHost
= constants
.CONFIG
.WEBSERVER
.HOST
107 // TODO: refractoring
108 function (callback
) {
111 [ constants
.CONFIG
.WEBSERVER
.WS
+ '://' + constants
.CONFIG
.WEBSERVER
.HOSTNAME
+ ':' + constants
.CONFIG
.WEBSERVER
.PORT
+ '/tracker/socket' ]
114 constants
.CONFIG
.WEBSERVER
.URL
+ constants
.STATIC_PATHS
.WEBSEED
+ video
.getVideoFilename()
118 createTorrent(videoPath
, options
, function (err
, torrent
) {
119 if (err
) return callback(err
)
121 fs
.writeFile(constants
.CONFIG
.STORAGE
.TORRENTS_DIR
+ video
.getTorrentName(), torrent
, function (err
) {
122 if (err
) return callback(err
)
124 const parsedTorrent
= parseTorrent(torrent
)
125 video
.magnet
.infoHash
= parsedTorrent
.infoHash
131 function (callback
) {
132 createThumbnail(video
, videoPath
, callback
)
134 function (callback
) {
135 createPreview(video
, videoPath
, callback
)
139 parallel(tasks
, next
)
141 generateThumbnailFromBase64(video
, video
.thumbnail
, next
)
145 mongoose
.model('Video', VideoSchema
)
147 // ------------------------------ METHODS ------------------------------
149 function generateMagnetUri () {
150 let baseUrlHttp
, baseUrlWs
152 if (this.isOwned()) {
153 baseUrlHttp
= constants
.CONFIG
.WEBSERVER
.URL
154 baseUrlWs
= constants
.CONFIG
.WEBSERVER
.WS
+ '://' + constants
.CONFIG
.WEBSERVER
.HOSTNAME
+ ':' + constants
.CONFIG
.WEBSERVER
.PORT
156 baseUrlHttp
= constants
.REMOTE_SCHEME
.HTTP
+ '://' + this.podHost
157 baseUrlWs
= constants
.REMOTE_SCHEME
.WS
+ this.podHost
160 const xs
= baseUrlHttp
+ constants
.STATIC_PATHS
.TORRENTS
+ this.getTorrentName()
161 const announce
= baseUrlWs
+ '/tracker/socket'
162 const urlList
= [ baseUrlHttp
+ constants
.STATIC_PATHS
.WEBSEED
+ this.getVideoFilename() ]
168 infoHash: this.magnet
.infoHash
,
172 return magnetUtil
.encode(magnetHash
)
175 function getVideoFilename () {
176 if (this.isOwned()) return this._id
+ this.extname
178 return this.remoteId
+ this.extname
181 function getThumbnailName () {
182 // We always have a copy of the thumbnail
183 return this._id
+ '.jpg'
186 function getPreviewName () {
187 const extension
= '.jpg'
189 if (this.isOwned()) return this._id
+ extension
191 return this.remoteId
+ extension
194 function getTorrentName () {
195 const extension
= '.torrent'
197 if (this.isOwned()) return this._id
+ extension
199 return this.remoteId
+ extension
202 function isOwned () {
203 return this.remoteId
=== null
206 function toFormatedJSON () {
210 description: this.description
,
211 podHost: this.podHost
,
212 isLocal: this.isOwned(),
213 magnetUri: this.generateMagnetUri(),
215 duration: this.duration
,
217 thumbnailPath: constants
.STATIC_PATHS
.THUMBNAILS
+ '/' + this.getThumbnailName(),
218 createdDate: this.createdDate
224 function toRemoteJSON (callback
) {
227 // Convert thumbnail to base64
228 const thumbnailPath
= pathUtils
.join(constants
.CONFIG
.STORAGE
.THUMBNAILS_DIR
, this.getThumbnailName())
229 fs
.readFile(thumbnailPath
, function (err
, thumbnailData
) {
231 logger
.error('Cannot read the thumbnail of the video')
235 const remoteVideo
= {
237 description: self
.description
,
241 duration: self
.duration
,
242 thumbnailBase64: new Buffer(thumbnailData
).toString('base64'),
244 createdDate: self
.createdDate
,
245 podHost: self
.podHost
248 return callback(null, remoteVideo
)
252 // ------------------------------ STATICS ------------------------------
254 function getDurationFromFile (videoPath
, callback
) {
255 ffmpeg
.ffprobe(videoPath
, function (err
, metadata
) {
256 if (err
) return callback(err
)
258 return callback(null, Math
.floor(metadata
.format
.duration
))
262 function listForApi (start
, count
, sort
, callback
) {
264 return modelUtils
.listForApiWithCount
.call(this, query
, start
, count
, sort
, callback
)
267 function listByHostAndRemoteId (fromHost
, remoteId
, callback
) {
268 this.find({ podHost: fromHost
, remoteId: remoteId
}, callback
)
271 function listByHost (fromHost
, callback
) {
272 this.find({ podHost: fromHost
}, callback
)
275 function listOwned (callback
) {
276 // If remoteId is null this is *our* video
277 this.find({ remoteId: null }, callback
)
280 function listOwnedByAuthor (author
, callback
) {
281 this.find({ remoteId: null, author: author
}, callback
)
284 function listRemotes (callback
) {
285 this.find({ remoteId: { $ne: null } }, callback
)
288 function load (id
, callback
) {
289 this.findById(id
, callback
)
292 function search (value
, field
, start
, count
, sort
, callback
) {
294 // Make an exact search with the magnet
295 if (field
=== 'magnetUri') {
296 const infoHash
= magnetUtil
.decode(value
).infoHash
300 } else if (field
=== 'tags') {
303 query
[field
] = new RegExp(value
, 'i')
306 modelUtils
.listForApiWithCount
.call(this, query
, start
, count
, sort
, callback
)
309 // ---------------------------------------------------------------------------
311 function removeThumbnail (video
, callback
) {
312 fs
.unlink(constants
.CONFIG
.STORAGE
.THUMBNAILS_DIR
+ video
.getThumbnailName(), callback
)
315 function removeFile (video
, callback
) {
316 fs
.unlink(constants
.CONFIG
.STORAGE
.VIDEOS_DIR
+ video
.getVideoFilename(), callback
)
319 function removeTorrent (video
, callback
) {
320 fs
.unlink(constants
.CONFIG
.STORAGE
.TORRENTS_DIR
+ video
.getTorrentName(), callback
)
323 function removePreview (video
, callback
) {
324 // Same name than video thumnail
325 fs
.unlink(constants
.CONFIG
.STORAGE
.PREVIEWS_DIR
+ video
.getPreviewName(), callback
)
328 function createPreview (video
, videoPath
, callback
) {
329 generateImage(video
, videoPath
, constants
.CONFIG
.STORAGE
.PREVIEWS_DIR
, video
.getPreviewName(), callback
)
332 function createThumbnail (video
, videoPath
, callback
) {
333 generateImage(video
, videoPath
, constants
.CONFIG
.STORAGE
.THUMBNAILS_DIR
, video
.getThumbnailName(), constants
.THUMBNAILS_SIZE
, callback
)
336 function generateThumbnailFromBase64 (video
, thumbnailData
, callback
) {
337 // Creating the thumbnail for this remote video)
339 const thumbnailName
= video
.getThumbnailName()
340 const thumbnailPath
= constants
.CONFIG
.STORAGE
.THUMBNAILS_DIR
+ thumbnailName
341 fs
.writeFile(thumbnailPath
, thumbnailData
, { encoding: 'base64' }, function (err
) {
342 if (err
) return callback(err
)
344 return callback(null, thumbnailName
)
348 function generateImage (video
, videoPath
, folder
, imageName
, size
, callback
) {
362 .on('error', callback
)
363 .on('end', function () {
364 callback(null, imageName
)