]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/models/video.js
Server: adapt magnet uri search
[github/Chocobozzz/PeerTube.git] / server / models / video.js
CommitLineData
aaf61f38
C
1'use strict'
2
052937db 3const createTorrent = require('create-torrent')
aaf61f38
C
4const ffmpeg = require('fluent-ffmpeg')
5const fs = require('fs')
f285faa0 6const magnetUtil = require('magnet-uri')
1a42c9e2 7const parallel = require('async/parallel')
052937db 8const parseTorrent = require('parse-torrent')
aaf61f38
C
9const pathUtils = require('path')
10const mongoose = require('mongoose')
11
12const constants = require('../initializers/constants')
e4c55619 13const customVideosValidators = require('../helpers/custom-validators').videos
aaf61f38 14const logger = require('../helpers/logger')
0ff21c1c 15const modelUtils = require('./utils')
aaf61f38 16
aaf61f38
C
17// ---------------------------------------------------------------------------
18
19// TODO: add indexes on searchable columns
20const VideoSchema = mongoose.Schema({
21 name: String,
558d7c23
C
22 extname: {
23 type: String,
24 enum: [ '.mp4', '.webm', '.ogv' ]
25 },
26 remoteId: mongoose.Schema.Types.ObjectId,
aaf61f38 27 description: String,
f285faa0
C
28 magnet: {
29 infoHash: String
30 },
aaf61f38
C
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
e4c55619
C
42VideoSchema.path('name').validate(customVideosValidators.isVideoNameValid)
43VideoSchema.path('description').validate(customVideosValidators.isVideoDescriptionValid)
e4c55619
C
44VideoSchema.path('podUrl').validate(customVideosValidators.isVideoPodUrlValid)
45VideoSchema.path('author').validate(customVideosValidators.isVideoAuthorValid)
46VideoSchema.path('duration').validate(customVideosValidators.isVideoDurationValid)
aaf61f38
C
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
49VideoSchema.path('thumbnail').validate(function (value) {
e4c55619 50 return customVideosValidators.isVideoThumbnailValid(value) || customVideosValidators.isVideoThumbnail64Valid(value)
aaf61f38 51})
e4c55619 52VideoSchema.path('tags').validate(customVideosValidators.isVideoTagsValid)
aaf61f38
C
53
54VideoSchema.methods = {
f285faa0
C
55 generateMagnetUri,
56 getVideoFilename,
57 getThumbnailName,
58 getPreviewName,
558d7c23 59 getTorrentName,
c4403b29
C
60 isOwned,
61 toFormatedJSON,
62 toRemoteJSON
aaf61f38
C
63}
64
65VideoSchema.statics = {
c4403b29
C
66 getDurationFromFile,
67 listForApi,
558d7c23 68 listByUrlAndRemoteId,
80a6c9e7 69 listByUrl,
c4403b29
C
70 listOwned,
71 listOwnedByAuthor,
72 listRemotes,
73 load,
a6375e69 74 search
aaf61f38
C
75}
76
77VideoSchema.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)
6a94a109
C
94 },
95 function (callback) {
96 removePreview(video, callback)
aaf61f38
C
97 }
98 )
99 }
100
1a42c9e2 101 parallel(tasks, next)
aaf61f38
C
102})
103
104VideoSchema.pre('save', function (next) {
105 const video = this
106 const tasks = []
107
108 if (video.isOwned()) {
f285faa0
C
109 const videoPath = pathUtils.join(constants.CONFIG.STORAGE.VIDEOS_DIR, video.getVideoFilename())
110 this.podUrl = constants.CONFIG.WEBSERVER.HOSTNAME + ':' + constants.CONFIG.WEBSERVER.PORT
aaf61f38
C
111
112 tasks.push(
052937db 113 // TODO: refractoring
aaf61f38 114 function (callback) {
25cad919
C
115 const options = {
116 announceList: [
3737bbaf 117 [ constants.CONFIG.WEBSERVER.WS + '://' + constants.CONFIG.WEBSERVER.HOSTNAME + ':' + constants.CONFIG.WEBSERVER.PORT + '/tracker/socket' ]
25cad919
C
118 ],
119 urlList: [
f285faa0 120 constants.CONFIG.WEBSERVER.URL + constants.STATIC_PATHS.WEBSEED + video.getVideoFilename()
25cad919
C
121 ]
122 }
123
124 createTorrent(videoPath, options, function (err, torrent) {
052937db
C
125 if (err) return callback(err)
126
558d7c23 127 fs.writeFile(constants.CONFIG.STORAGE.TORRENTS_DIR + video.getTorrentName(), torrent, function (err) {
052937db
C
128 if (err) return callback(err)
129
130 const parsedTorrent = parseTorrent(torrent)
f285faa0 131 video.magnet.infoHash = parsedTorrent.infoHash
052937db 132
f285faa0 133 console.log(parsedTorrent)
052937db
C
134 callback(null)
135 })
136 })
aaf61f38
C
137 },
138 function (callback) {
558d7c23 139 createThumbnail(video, videoPath, callback)
6a94a109
C
140 },
141 function (callback) {
558d7c23 142 createPreview(video, videoPath, callback)
aaf61f38
C
143 }
144 )
145
558d7c23 146 parallel(tasks, next)
aaf61f38 147 } else {
558d7c23 148 generateThumbnailFromBase64(video, video.thumbnail, next)
aaf61f38
C
149 }
150})
151
152mongoose.model('Video', VideoSchema)
153
154// ------------------------------ METHODS ------------------------------
155
f285faa0
C
156function 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)
558d7c23
C
180}
181
f285faa0
C
182function getVideoFilename () {
183 if (this.isOwned()) return this._id + this.extname
184
185 return this.remoteId + this.extname
186}
187
188function getThumbnailName () {
189 // We always have a copy of the thumbnail
558d7c23
C
190 return this._id + '.jpg'
191}
192
f285faa0
C
193function getPreviewName () {
194 const extension = '.jpg'
195
196 if (this.isOwned()) return this._id + extension
197
198 return this.remoteId + extension
199}
200
558d7c23 201function getTorrentName () {
f285faa0
C
202 const extension = '.torrent'
203
204 if (this.isOwned()) return this._id + extension
205
206 return this.remoteId + extension
558d7c23
C
207}
208
aaf61f38 209function isOwned () {
558d7c23 210 return this.remoteId === null
aaf61f38
C
211}
212
213function toFormatedJSON () {
214 const json = {
215 id: this._id,
216 name: this.name,
217 description: this.description,
f285faa0 218 podUrl: this.podUrl,
aaf61f38 219 isLocal: this.isOwned(),
f285faa0 220 magnetUri: this.generateMagnetUri(),
aaf61f38
C
221 author: this.author,
222 duration: this.duration,
223 tags: this.tags,
f285faa0 224 thumbnailPath: constants.STATIC_PATHS.THUMBNAILS + '/' + this.getThumbnailName(),
aaf61f38
C
225 createdDate: this.createdDate
226 }
227
228 return json
229}
230
231function toRemoteJSON (callback) {
232 const self = this
233
234 // Convert thumbnail to base64
f285faa0 235 const thumbnailPath = pathUtils.join(constants.CONFIG.STORAGE.THUMBNAILS_DIR, this.getThumbnailName())
558d7c23 236 fs.readFile(thumbnailPath, function (err, thumbnailData) {
aaf61f38
C
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,
f285faa0 245 magnet: self.magnet,
558d7c23 246 remoteId: self._id,
aaf61f38
C
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
261function 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
0ff21c1c 269function listForApi (start, count, sort, callback) {
aaf61f38 270 const query = {}
5c39adb7 271 return modelUtils.listForApiWithCount.call(this, query, start, count, sort, callback)
aaf61f38
C
272}
273
558d7c23
C
274function listByUrlAndRemoteId (fromUrl, remoteId, callback) {
275 this.find({ podUrl: fromUrl, remoteId: remoteId }, callback)
aaf61f38
C
276}
277
80a6c9e7
C
278function listByUrl (fromUrl, callback) {
279 this.find({ podUrl: fromUrl }, callback)
aaf61f38
C
280}
281
282function listOwned (callback) {
558d7c23
C
283 // If remoteId is null this is *our* video
284 this.find({ remoteId: null }, callback)
aaf61f38
C
285}
286
9bd26629 287function listOwnedByAuthor (author, callback) {
558d7c23 288 this.find({ remoteId: null, author: author }, callback)
9bd26629
C
289}
290
aaf61f38 291function listRemotes (callback) {
558d7c23 292 this.find({ remoteId: { $ne: null } }, callback)
aaf61f38
C
293}
294
295function load (id, callback) {
296 this.findById(id, callback)
297}
298
299function search (value, field, start, count, sort, callback) {
300 const query = {}
301 // Make an exact search with the magnet
55723d16
C
302 if (field === 'magnetUri') {
303 const infoHash = magnetUtil.decode(value).infoHash
304 query.magnet = {
305 infoHash
306 }
307 } else if (field === 'tags') {
aaf61f38
C
308 query[field] = value
309 } else {
cf6412e8 310 query[field] = new RegExp(value, 'i')
aaf61f38
C
311 }
312
5c39adb7 313 modelUtils.listForApiWithCount.call(this, query, start, count, sort, callback)
aaf61f38
C
314}
315
aaf61f38
C
316// ---------------------------------------------------------------------------
317
aaf61f38 318function removeThumbnail (video, callback) {
f285faa0 319 fs.unlink(constants.CONFIG.STORAGE.THUMBNAILS_DIR + video.getThumbnailName(), callback)
aaf61f38
C
320}
321
322function removeFile (video, callback) {
f285faa0 323 fs.unlink(constants.CONFIG.STORAGE.VIDEOS_DIR + video.getVideoFilename(), callback)
aaf61f38
C
324}
325
aaf61f38 326function removeTorrent (video, callback) {
558d7c23 327 fs.unlink(constants.CONFIG.STORAGE.TORRENTS_DIR + video.getTorrentName(), callback)
aaf61f38
C
328}
329
6a94a109
C
330function removePreview (video, callback) {
331 // Same name than video thumnail
f285faa0 332 fs.unlink(constants.CONFIG.STORAGE.PREVIEWS_DIR + video.getPreviewName(), callback)
6a94a109
C
333}
334
558d7c23 335function createPreview (video, videoPath, callback) {
f285faa0 336 generateImage(video, videoPath, constants.CONFIG.STORAGE.PREVIEWS_DIR, video.getPreviewName(), callback)
6a94a109
C
337}
338
558d7c23 339function createThumbnail (video, videoPath, callback) {
f285faa0 340 generateImage(video, videoPath, constants.CONFIG.STORAGE.THUMBNAILS_DIR, video.getThumbnailName(), constants.THUMBNAILS_SIZE, callback)
aaf61f38
C
341}
342
558d7c23
C
343function generateThumbnailFromBase64 (video, thumbnailData, callback) {
344 // Creating the thumbnail for this remote video)
345
f285faa0 346 const thumbnailName = video.getThumbnailName()
558d7c23
C
347 const thumbnailPath = constants.CONFIG.STORAGE.THUMBNAILS_DIR + thumbnailName
348 fs.writeFile(thumbnailPath, thumbnailData, { encoding: 'base64' }, function (err) {
aaf61f38
C
349 if (err) return callback(err)
350
558d7c23
C
351 return callback(null, thumbnailName)
352 })
353}
354
f285faa0 355function generateImage (video, videoPath, folder, imageName, size, callback) {
558d7c23 356 const options = {
f285faa0 357 filename: imageName,
558d7c23
C
358 count: 1,
359 folder
360 }
aaf61f38 361
558d7c23
C
362 if (!callback) {
363 callback = size
364 } else {
365 options.size = size
366 }
367
368 ffmpeg(videoPath)
369 .on('error', callback)
370 .on('end', function () {
f285faa0 371 callback(null, imageName)
aaf61f38 372 })
558d7c23 373 .thumbnail(options)
aaf61f38 374}