Server: generate magnet uri on the fly
[oweals/peertube.git] / server / models / video.js
1 'use strict'
2
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')
11
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
17 // ---------------------------------------------------------------------------
18
19 // TODO: add indexes on searchable columns
20 const VideoSchema = mongoose.Schema({
21   name: String,
22   extname: {
23     type: String,
24     enum: [ '.mp4', '.webm', '.ogv' ]
25   },
26   remoteId: mongoose.Schema.Types.ObjectId,
27   description: String,
28   magnet: {
29     infoHash: String
30   },
31   podUrl: String,
32   author: String,
33   duration: Number,
34   thumbnail: String,
35   tags: [ String ],
36   createdDate: {
37     type: Date,
38     default: Date.now
39   }
40 })
41
42 VideoSchema.path('name').validate(customVideosValidators.isVideoNameValid)
43 VideoSchema.path('description').validate(customVideosValidators.isVideoDescriptionValid)
44 VideoSchema.path('podUrl').validate(customVideosValidators.isVideoPodUrlValid)
45 VideoSchema.path('author').validate(customVideosValidators.isVideoAuthorValid)
46 VideoSchema.path('duration').validate(customVideosValidators.isVideoDurationValid)
47 // The tumbnail can be the path or the data in base 64
48 // The pre save hook will convert the base 64 data in a file on disk and replace the thumbnail key by the filename
49 VideoSchema.path('thumbnail').validate(function (value) {
50   return customVideosValidators.isVideoThumbnailValid(value) || customVideosValidators.isVideoThumbnail64Valid(value)
51 })
52 VideoSchema.path('tags').validate(customVideosValidators.isVideoTagsValid)
53
54 VideoSchema.methods = {
55   generateMagnetUri,
56   getVideoFilename,
57   getThumbnailName,
58   getPreviewName,
59   getTorrentName,
60   isOwned,
61   toFormatedJSON,
62   toRemoteJSON
63 }
64
65 VideoSchema.statics = {
66   getDurationFromFile,
67   listForApi,
68   listByUrlAndRemoteId,
69   listByUrl,
70   listOwned,
71   listOwnedByAuthor,
72   listRemotes,
73   load,
74   search
75 }
76
77 VideoSchema.pre('remove', function (next) {
78   const video = this
79   const tasks = []
80
81   tasks.push(
82     function (callback) {
83       removeThumbnail(video, callback)
84     }
85   )
86
87   if (video.isOwned()) {
88     tasks.push(
89       function (callback) {
90         removeFile(video, callback)
91       },
92       function (callback) {
93         removeTorrent(video, callback)
94       },
95       function (callback) {
96         removePreview(video, callback)
97       }
98     )
99   }
100
101   parallel(tasks, next)
102 })
103
104 VideoSchema.pre('save', function (next) {
105   const video = this
106   const tasks = []
107
108   if (video.isOwned()) {
109     const videoPath = pathUtils.join(constants.CONFIG.STORAGE.VIDEOS_DIR, video.getVideoFilename())
110     this.podUrl = constants.CONFIG.WEBSERVER.HOSTNAME + ':' + constants.CONFIG.WEBSERVER.PORT
111
112     tasks.push(
113       // TODO: refractoring
114       function (callback) {
115         const options = {
116           announceList: [
117             [ constants.CONFIG.WEBSERVER.WS + '://' + constants.CONFIG.WEBSERVER.HOSTNAME + ':' + constants.CONFIG.WEBSERVER.PORT + '/tracker/socket' ]
118           ],
119           urlList: [
120             constants.CONFIG.WEBSERVER.URL + constants.STATIC_PATHS.WEBSEED + video.getVideoFilename()
121           ]
122         }
123
124         createTorrent(videoPath, options, function (err, torrent) {
125           if (err) return callback(err)
126
127           fs.writeFile(constants.CONFIG.STORAGE.TORRENTS_DIR + video.getTorrentName(), torrent, function (err) {
128             if (err) return callback(err)
129
130             const parsedTorrent = parseTorrent(torrent)
131             video.magnet.infoHash = parsedTorrent.infoHash
132
133             console.log(parsedTorrent)
134             callback(null)
135           })
136         })
137       },
138       function (callback) {
139         createThumbnail(video, videoPath, callback)
140       },
141       function (callback) {
142         createPreview(video, videoPath, callback)
143       }
144     )
145
146     parallel(tasks, next)
147   } else {
148     generateThumbnailFromBase64(video, video.thumbnail, next)
149   }
150 })
151
152 mongoose.model('Video', VideoSchema)
153
154 // ------------------------------ METHODS ------------------------------
155
156 function generateMagnetUri () {
157   let baseUrlHttp, baseUrlWs
158
159   if (this.isOwned()) {
160     baseUrlHttp = constants.CONFIG.WEBSERVER.URL
161     baseUrlWs = constants.CONFIG.WEBSERVER.WS + '://' + constants.CONFIG.WEBSERVER.HOSTNAME + ':' + constants.CONFIG.WEBSERVER.PORT
162   } else {
163     baseUrlHttp = constants.REMOTE_SCHEME.HTTP + this.podUrl
164     baseUrlWs = constants.REMOTE_SCHEME.WS + this.podUrl
165   }
166
167   const xs = baseUrlHttp + constants.STATIC_PATHS.TORRENTS + this.getTorrentName()
168   const announce = baseUrlWs + '/tracker/socket'
169   const urlList = [ baseUrlHttp + constants.STATIC_PATHS.WEBSEED + this.getVideoFilename() ]
170
171   const magnetHash = {
172     xs,
173     announce,
174     urlList,
175     infoHash: this.magnet.infoHash,
176     name: this.name
177   }
178
179   return magnetUtil.encode(magnetHash)
180 }
181
182 function getVideoFilename () {
183   if (this.isOwned()) return this._id + this.extname
184
185   return this.remoteId + this.extname
186 }
187
188 function getThumbnailName () {
189   // We always have a copy of the thumbnail
190   return this._id + '.jpg'
191 }
192
193 function getPreviewName () {
194   const extension = '.jpg'
195
196   if (this.isOwned()) return this._id + extension
197
198   return this.remoteId + extension
199 }
200
201 function getTorrentName () {
202   const extension = '.torrent'
203
204   if (this.isOwned()) return this._id + extension
205
206   return this.remoteId + extension
207 }
208
209 function isOwned () {
210   return this.remoteId === null
211 }
212
213 function toFormatedJSON () {
214   const json = {
215     id: this._id,
216     name: this.name,
217     description: this.description,
218     podUrl: this.podUrl,
219     isLocal: this.isOwned(),
220     magnetUri: this.generateMagnetUri(),
221     author: this.author,
222     duration: this.duration,
223     tags: this.tags,
224     thumbnailPath: constants.STATIC_PATHS.THUMBNAILS + '/' + this.getThumbnailName(),
225     createdDate: this.createdDate
226   }
227
228   return json
229 }
230
231 function toRemoteJSON (callback) {
232   const self = this
233
234   // Convert thumbnail to base64
235   const thumbnailPath = pathUtils.join(constants.CONFIG.STORAGE.THUMBNAILS_DIR, this.getThumbnailName())
236   fs.readFile(thumbnailPath, function (err, thumbnailData) {
237     if (err) {
238       logger.error('Cannot read the thumbnail of the video')
239       return callback(err)
240     }
241
242     const remoteVideo = {
243       name: self.name,
244       description: self.description,
245       magnet: self.magnet,
246       remoteId: self._id,
247       author: self.author,
248       duration: self.duration,
249       thumbnailBase64: new Buffer(thumbnailData).toString('base64'),
250       tags: self.tags,
251       createdDate: self.createdDate,
252       podUrl: self.podUrl
253     }
254
255     return callback(null, remoteVideo)
256   })
257 }
258
259 // ------------------------------ STATICS ------------------------------
260
261 function getDurationFromFile (videoPath, callback) {
262   ffmpeg.ffprobe(videoPath, function (err, metadata) {
263     if (err) return callback(err)
264
265     return callback(null, Math.floor(metadata.format.duration))
266   })
267 }
268
269 function listForApi (start, count, sort, callback) {
270   const query = {}
271   return modelUtils.listForApiWithCount.call(this, query, start, count, sort, callback)
272 }
273
274 function listByUrlAndRemoteId (fromUrl, remoteId, callback) {
275   this.find({ podUrl: fromUrl, remoteId: remoteId }, callback)
276 }
277
278 function listByUrl (fromUrl, callback) {
279   this.find({ podUrl: fromUrl }, callback)
280 }
281
282 function listOwned (callback) {
283   // If remoteId is null this is *our* video
284   this.find({ remoteId: null }, callback)
285 }
286
287 function listOwnedByAuthor (author, callback) {
288   this.find({ remoteId: null, author: author }, callback)
289 }
290
291 function listRemotes (callback) {
292   this.find({ remoteId: { $ne: null } }, callback)
293 }
294
295 function load (id, callback) {
296   this.findById(id, callback)
297 }
298
299 function search (value, field, start, count, sort, callback) {
300   const query = {}
301   // Make an exact search with the magnet
302   if (field === 'magnetUri' || field === 'tags') {
303     query[field] = value
304   } else {
305     query[field] = new RegExp(value, 'i')
306   }
307
308   modelUtils.listForApiWithCount.call(this, query, start, count, sort, callback)
309 }
310
311 // ---------------------------------------------------------------------------
312
313 function removeThumbnail (video, callback) {
314   fs.unlink(constants.CONFIG.STORAGE.THUMBNAILS_DIR + video.getThumbnailName(), callback)
315 }
316
317 function removeFile (video, callback) {
318   fs.unlink(constants.CONFIG.STORAGE.VIDEOS_DIR + video.getVideoFilename(), callback)
319 }
320
321 function removeTorrent (video, callback) {
322   fs.unlink(constants.CONFIG.STORAGE.TORRENTS_DIR + video.getTorrentName(), callback)
323 }
324
325 function removePreview (video, callback) {
326   // Same name than video thumnail
327   fs.unlink(constants.CONFIG.STORAGE.PREVIEWS_DIR + video.getPreviewName(), callback)
328 }
329
330 function createPreview (video, videoPath, callback) {
331   generateImage(video, videoPath, constants.CONFIG.STORAGE.PREVIEWS_DIR, video.getPreviewName(), callback)
332 }
333
334 function createThumbnail (video, videoPath, callback) {
335   generateImage(video, videoPath, constants.CONFIG.STORAGE.THUMBNAILS_DIR, video.getThumbnailName(), constants.THUMBNAILS_SIZE, callback)
336 }
337
338 function generateThumbnailFromBase64 (video, thumbnailData, callback) {
339   // Creating the thumbnail for this remote video)
340
341   const thumbnailName = video.getThumbnailName()
342   const thumbnailPath = constants.CONFIG.STORAGE.THUMBNAILS_DIR + thumbnailName
343   fs.writeFile(thumbnailPath, thumbnailData, { encoding: 'base64' }, function (err) {
344     if (err) return callback(err)
345
346     return callback(null, thumbnailName)
347   })
348 }
349
350 function generateImage (video, videoPath, folder, imageName, size, callback) {
351   const options = {
352     filename: imageName,
353     count: 1,
354     folder
355   }
356
357   if (!callback) {
358     callback = size
359   } else {
360     options.size = size
361   }
362
363   ffmpeg(videoPath)
364     .on('error', callback)
365     .on('end', function () {
366       callback(null, imageName)
367     })
368     .thumbnail(options)
369 }