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