]>
git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/models/video.js
3 const config
= require('config')
4 const createTorrent
= require('create-torrent')
5 const ffmpeg
= require('fluent-ffmpeg')
6 const fs
= require('fs')
7 const parallel
= require('async/parallel')
8 const parseTorrent
= require('parse-torrent')
9 const pathUtils
= require('path')
10 const magnet
= require('magnet-uri')
11 const mongoose
= require('mongoose')
13 const constants
= require('../initializers/constants')
14 const customVideosValidators
= require('../helpers/custom-validators').videos
15 const logger
= require('../helpers/logger')
16 const modelUtils
= require('./utils')
17 const utils
= require('../helpers/utils')
19 const http
= config
.get('webserver.https') === true ? 'https' : 'http'
20 const host
= config
.get('webserver.host')
21 const port
= config
.get('webserver.port')
22 const webseedBaseUrl
= http
+ '://' + host
+ ':' + port
+ constants
.STATIC_PATHS
.WEBSEED
24 // ---------------------------------------------------------------------------
26 // TODO: add indexes on searchable columns
27 const VideoSchema
= mongoose
.Schema({
43 VideoSchema
.path('name').validate(customVideosValidators
.isVideoNameValid
)
44 VideoSchema
.path('description').validate(customVideosValidators
.isVideoDescriptionValid
)
45 VideoSchema
.path('magnetUri').validate(customVideosValidators
.isVideoMagnetUriValid
)
46 VideoSchema
.path('podUrl').validate(customVideosValidators
.isVideoPodUrlValid
)
47 VideoSchema
.path('author').validate(customVideosValidators
.isVideoAuthorValid
)
48 VideoSchema
.path('duration').validate(customVideosValidators
.isVideoDurationValid
)
49 // The tumbnail can be the path or the data in base 64
50 // The pre save hook will convert the base 64 data in a file on disk and replace the thumbnail key by the filename
51 VideoSchema
.path('thumbnail').validate(function (value
) {
52 return customVideosValidators
.isVideoThumbnailValid(value
) || customVideosValidators
.isVideoThumbnail64Valid(value
)
54 VideoSchema
.path('tags').validate(customVideosValidators
.isVideoTagsValid
)
56 VideoSchema
.methods
= {
62 VideoSchema
.statics
= {
74 VideoSchema
.pre('remove', function (next
) {
80 removeThumbnail(video
, callback
)
84 if (video
.isOwned()) {
87 removeFile(video
, callback
)
90 removeTorrent(video
, callback
)
98 VideoSchema
.pre('save', function (next
) {
102 if (video
.isOwned()) {
103 const videoPath
= pathUtils
.join(constants
.CONFIG
.STORAGE
.UPLOAD_DIR
, video
.filename
)
104 this.podUrl
= constants
.CONFIG
.WEBSERVER
.URL
107 // TODO: refractoring
108 function (callback
) {
109 createTorrent(videoPath
, { announceList: [ [ 'ws://' + host
+ ':' + port
+ '/tracker/socket' ] ], urlList: [ webseedBaseUrl
+ video
.filename
] }, function (err
, torrent
) {
110 if (err
) return callback(err
)
112 fs
.writeFile(constants
.CONFIG
.STORAGE
.TORRENTS_DIR
+ video
.filename
+ '.torrent', torrent
, function (err
) {
113 if (err
) return callback(err
)
115 const parsedTorrent
= parseTorrent(torrent
)
116 parsedTorrent
.xs
= video
.podUrl
+ constants
.STATIC_PATHS
.TORRENTS
+ video
.filename
+ '.torrent'
117 video
.magnetUri
= magnet
.encode(parsedTorrent
)
123 function (callback
) {
124 createThumbnail(videoPath
, callback
)
128 parallel(tasks
, function (err
, results
) {
129 if (err
) return next(err
)
131 video
.thumbnail
= results
[1]
136 generateThumbnailFromBase64(video
.thumbnail
, function (err
, thumbnailName
) {
137 if (err
) return next(err
)
139 video
.thumbnail
= thumbnailName
146 mongoose
.model('Video', VideoSchema
)
148 // ------------------------------ METHODS ------------------------------
150 function isOwned () {
151 return this.filename
!== null
154 function toFormatedJSON () {
158 description: this.description
,
159 podUrl: this.podUrl
.replace(/^https?:\/\//, ''),
160 isLocal: this.isOwned(),
161 magnetUri: this.magnetUri
,
163 duration: this.duration
,
165 thumbnailPath: constants
.STATIC_PATHS
.THUMBNAILS
+ '/' + this.thumbnail
,
166 createdDate: this.createdDate
172 function toRemoteJSON (callback
) {
175 // Convert thumbnail to base64
176 fs
.readFile(pathUtils
.join(constants
.CONFIG
.STORAGE
.THUMBNAILS_DIR
, this.thumbnail
), function (err
, thumbnailData
) {
178 logger
.error('Cannot read the thumbnail of the video')
182 const remoteVideo
= {
184 description: self
.description
,
185 magnetUri: self
.magnetUri
,
188 duration: self
.duration
,
189 thumbnailBase64: new Buffer(thumbnailData
).toString('base64'),
191 createdDate: self
.createdDate
,
195 return callback(null, remoteVideo
)
199 // ------------------------------ STATICS ------------------------------
201 function getDurationFromFile (videoPath
, callback
) {
202 ffmpeg
.ffprobe(videoPath
, function (err
, metadata
) {
203 if (err
) return callback(err
)
205 return callback(null, Math
.floor(metadata
.format
.duration
))
209 function listForApi (start
, count
, sort
, callback
) {
211 return modelUtils
.listForApiWithCount
.call(this, query
, start
, count
, sort
, callback
)
214 function listByUrlAndMagnet (fromUrl
, magnetUri
, callback
) {
215 this.find({ podUrl: fromUrl
, magnetUri: magnetUri
}, callback
)
218 function listByUrls (fromUrls
, callback
) {
219 this.find({ podUrl: { $in: fromUrls
} }, callback
)
222 function listOwned (callback
) {
223 // If filename is not null this is *our* video
224 this.find({ filename: { $ne: null } }, callback
)
227 function listOwnedByAuthor (author
, callback
) {
228 this.find({ filename: { $ne: null }, author: author
}, callback
)
231 function listRemotes (callback
) {
232 this.find({ filename: null }, callback
)
235 function load (id
, callback
) {
236 this.findById(id
, callback
)
239 function search (value
, field
, start
, count
, sort
, callback
) {
241 // Make an exact search with the magnet
242 if (field
=== 'magnetUri' || field
=== 'tags') {
245 query
[field
] = new RegExp(value
)
248 modelUtils
.listForApiWithCount
.call(this, query
, start
, count
, sort
, callback
)
251 // ---------------------------------------------------------------------------
253 function removeThumbnail (video
, callback
) {
254 fs
.unlink(constants
.CONFIG
.STORAGE
.THUMBNAILS_DIR
+ video
.thumbnail
, callback
)
257 function removeFile (video
, callback
) {
258 fs
.unlink(constants
.CONFIG
.STORAGE
.UPLOAD_DIR
+ video
.filename
, callback
)
261 // Maybe the torrent is not seeded, but we catch the error to don't stop the removing process
262 function removeTorrent (video
, callback
) {
263 fs
.unlink(constants
.CONFIG
.STORAGE
.TORRENTS_DIR
+ video
.filename
+ '.torrent', callback
)
266 function createThumbnail (videoPath
, callback
) {
267 const filename
= pathUtils
.basename(videoPath
) + '.jpg'
269 .on('error', callback
)
270 .on('end', function () {
271 callback(null, filename
)
275 folder: constants
.CONFIG
.STORAGE
.THUMBNAILS_DIR
,
276 size: constants
.THUMBNAILS_SIZE
,
281 function generateThumbnailFromBase64 (data
, callback
) {
282 // Creating the thumbnail for this remote video
283 utils
.generateRandomString(16, function (err
, randomString
) {
284 if (err
) return callback(err
)
286 const thumbnailName
= randomString
+ '.jpg'
287 fs
.writeFile(constants
.CONFIG
.STORAGE
.THUMBNAILS_DIR
+ thumbnailName
, data
, { encoding: 'base64' }, function (err
) {
288 if (err
) return callback(err
)
290 return callback(null, thumbnailName
)