]>
Commit | Line | Data |
---|---|---|
aaf61f38 C |
1 | 'use strict' |
2 | ||
4d324488 | 3 | const Buffer = require('safe-buffer').Buffer |
052937db | 4 | const createTorrent = require('create-torrent') |
aaf61f38 C |
5 | const ffmpeg = require('fluent-ffmpeg') |
6 | const fs = require('fs') | |
f285faa0 | 7 | const magnetUtil = require('magnet-uri') |
7920c273 | 8 | const map = require('lodash/map') |
1a42c9e2 | 9 | const parallel = require('async/parallel') |
052937db | 10 | const parseTorrent = require('parse-torrent') |
aaf61f38 | 11 | const pathUtils = require('path') |
67bf9b96 | 12 | const values = require('lodash/values') |
aaf61f38 C |
13 | |
14 | const constants = require('../initializers/constants') | |
aaf61f38 | 15 | const logger = require('../helpers/logger') |
98ac898a | 16 | const friends = require('../lib/friends') |
0ff21c1c | 17 | const modelUtils = require('./utils') |
67bf9b96 | 18 | const customVideosValidators = require('../helpers/custom-validators').videos |
aaf61f38 | 19 | |
aaf61f38 C |
20 | // --------------------------------------------------------------------------- |
21 | ||
feb4bdfd | 22 | module.exports = function (sequelize, DataTypes) { |
feb4bdfd C |
23 | const Video = sequelize.define('Video', |
24 | { | |
25 | id: { | |
26 | type: DataTypes.UUID, | |
27 | defaultValue: DataTypes.UUIDV4, | |
67bf9b96 C |
28 | primaryKey: true, |
29 | validate: { | |
30 | isUUID: 4 | |
31 | } | |
aaf61f38 | 32 | }, |
feb4bdfd | 33 | name: { |
67bf9b96 C |
34 | type: DataTypes.STRING, |
35 | allowNull: false, | |
36 | validate: { | |
37 | nameValid: function (value) { | |
38 | const res = customVideosValidators.isVideoNameValid(value) | |
39 | if (res === false) throw new Error('Video name is not valid.') | |
40 | } | |
41 | } | |
6a94a109 | 42 | }, |
feb4bdfd | 43 | extname: { |
67bf9b96 C |
44 | type: DataTypes.ENUM(values(constants.CONSTRAINTS_FIELDS.VIDEOS.EXTNAME)), |
45 | allowNull: false | |
feb4bdfd C |
46 | }, |
47 | remoteId: { | |
67bf9b96 C |
48 | type: DataTypes.UUID, |
49 | allowNull: true, | |
50 | validate: { | |
51 | isUUID: 4 | |
52 | } | |
feb4bdfd C |
53 | }, |
54 | description: { | |
67bf9b96 C |
55 | type: DataTypes.STRING, |
56 | allowNull: false, | |
57 | validate: { | |
58 | descriptionValid: function (value) { | |
59 | const res = customVideosValidators.isVideoDescriptionValid(value) | |
60 | if (res === false) throw new Error('Video description is not valid.') | |
61 | } | |
62 | } | |
feb4bdfd C |
63 | }, |
64 | infoHash: { | |
67bf9b96 C |
65 | type: DataTypes.STRING, |
66 | allowNull: false, | |
67 | validate: { | |
68 | infoHashValid: function (value) { | |
69 | const res = customVideosValidators.isVideoInfoHashValid(value) | |
70 | if (res === false) throw new Error('Video info hash is not valid.') | |
71 | } | |
72 | } | |
feb4bdfd C |
73 | }, |
74 | duration: { | |
67bf9b96 C |
75 | type: DataTypes.INTEGER, |
76 | allowNull: false, | |
77 | validate: { | |
78 | durationValid: function (value) { | |
79 | const res = customVideosValidators.isVideoDurationValid(value) | |
80 | if (res === false) throw new Error('Video duration is not valid.') | |
81 | } | |
82 | } | |
9e167724 C |
83 | }, |
84 | views: { | |
85 | type: DataTypes.INTEGER, | |
86 | allowNull: false, | |
87 | defaultValue: 0, | |
88 | validate: { | |
89 | min: 0, | |
90 | isInt: true | |
91 | } | |
aaf61f38 | 92 | } |
feb4bdfd C |
93 | }, |
94 | { | |
319d072e C |
95 | indexes: [ |
96 | { | |
97 | fields: [ 'authorId' ] | |
98 | }, | |
99 | { | |
100 | fields: [ 'remoteId' ] | |
101 | }, | |
102 | { | |
103 | fields: [ 'name' ] | |
104 | }, | |
105 | { | |
106 | fields: [ 'createdAt' ] | |
107 | }, | |
108 | { | |
109 | fields: [ 'duration' ] | |
110 | }, | |
111 | { | |
112 | fields: [ 'infoHash' ] | |
9e167724 C |
113 | }, |
114 | { | |
115 | fields: [ 'views' ] | |
319d072e C |
116 | } |
117 | ], | |
feb4bdfd C |
118 | classMethods: { |
119 | associate, | |
120 | ||
4d324488 | 121 | generateThumbnailFromData, |
feb4bdfd | 122 | getDurationFromFile, |
b769007f | 123 | list, |
feb4bdfd | 124 | listForApi, |
7920c273 | 125 | listOwnedAndPopulateAuthorAndTags, |
feb4bdfd C |
126 | listOwnedByAuthor, |
127 | load, | |
3d118fb5 | 128 | loadByHostAndRemoteId, |
feb4bdfd | 129 | loadAndPopulateAuthor, |
7920c273 C |
130 | loadAndPopulateAuthorAndPodAndTags, |
131 | searchAndPopulateAuthorAndPodAndTags | |
feb4bdfd C |
132 | }, |
133 | instanceMethods: { | |
134 | generateMagnetUri, | |
135 | getVideoFilename, | |
136 | getThumbnailName, | |
137 | getPreviewName, | |
138 | getTorrentName, | |
139 | isOwned, | |
140 | toFormatedJSON, | |
7b1f49de C |
141 | toAddRemoteJSON, |
142 | toUpdateRemoteJSON | |
feb4bdfd C |
143 | }, |
144 | hooks: { | |
67bf9b96 | 145 | beforeValidate, |
feb4bdfd C |
146 | beforeCreate, |
147 | afterDestroy | |
148 | } | |
149 | } | |
150 | ) | |
aaf61f38 | 151 | |
feb4bdfd C |
152 | return Video |
153 | } | |
aaf61f38 | 154 | |
67bf9b96 | 155 | function beforeValidate (video, options, next) { |
7f4e7c36 C |
156 | // Put a fake infoHash if it does not exists yet |
157 | if (video.isOwned() && !video.infoHash) { | |
67bf9b96 C |
158 | // 40 hexa length |
159 | video.infoHash = '0123456789abcdef0123456789abcdef01234567' | |
160 | } | |
161 | ||
162 | return next(null) | |
163 | } | |
feb4bdfd C |
164 | |
165 | function beforeCreate (video, options, next) { | |
aaf61f38 C |
166 | const tasks = [] |
167 | ||
168 | if (video.isOwned()) { | |
f285faa0 | 169 | const videoPath = pathUtils.join(constants.CONFIG.STORAGE.VIDEOS_DIR, video.getVideoFilename()) |
aaf61f38 C |
170 | |
171 | tasks.push( | |
15103f11 | 172 | function createVideoTorrent (callback) { |
25cad919 C |
173 | const options = { |
174 | announceList: [ | |
3737bbaf | 175 | [ constants.CONFIG.WEBSERVER.WS + '://' + constants.CONFIG.WEBSERVER.HOSTNAME + ':' + constants.CONFIG.WEBSERVER.PORT + '/tracker/socket' ] |
25cad919 C |
176 | ], |
177 | urlList: [ | |
f285faa0 | 178 | constants.CONFIG.WEBSERVER.URL + constants.STATIC_PATHS.WEBSEED + video.getVideoFilename() |
25cad919 C |
179 | ] |
180 | } | |
181 | ||
182 | createTorrent(videoPath, options, function (err, torrent) { | |
052937db C |
183 | if (err) return callback(err) |
184 | ||
15103f11 C |
185 | const filePath = pathUtils.join(constants.CONFIG.STORAGE.TORRENTS_DIR, video.getTorrentName()) |
186 | fs.writeFile(filePath, torrent, function (err) { | |
052937db C |
187 | if (err) return callback(err) |
188 | ||
189 | const parsedTorrent = parseTorrent(torrent) | |
67bf9b96 C |
190 | video.set('infoHash', parsedTorrent.infoHash) |
191 | video.validate().asCallback(callback) | |
052937db C |
192 | }) |
193 | }) | |
aaf61f38 | 194 | }, |
15103f11 C |
195 | |
196 | function createVideoThumbnail (callback) { | |
558d7c23 | 197 | createThumbnail(video, videoPath, callback) |
6a94a109 | 198 | }, |
15103f11 C |
199 | |
200 | function createVIdeoPreview (callback) { | |
558d7c23 | 201 | createPreview(video, videoPath, callback) |
aaf61f38 C |
202 | } |
203 | ) | |
204 | ||
c77fa067 | 205 | return parallel(tasks, next) |
aaf61f38 | 206 | } |
c77fa067 C |
207 | |
208 | return next() | |
feb4bdfd | 209 | } |
aaf61f38 | 210 | |
feb4bdfd C |
211 | function afterDestroy (video, options, next) { |
212 | const tasks = [] | |
213 | ||
214 | tasks.push( | |
215 | function (callback) { | |
216 | removeThumbnail(video, callback) | |
217 | } | |
218 | ) | |
219 | ||
220 | if (video.isOwned()) { | |
221 | tasks.push( | |
15103f11 | 222 | function removeVideoFile (callback) { |
feb4bdfd C |
223 | removeFile(video, callback) |
224 | }, | |
98ac898a | 225 | |
15103f11 | 226 | function removeVideoTorrent (callback) { |
feb4bdfd C |
227 | removeTorrent(video, callback) |
228 | }, | |
98ac898a | 229 | |
15103f11 | 230 | function removeVideoPreview (callback) { |
feb4bdfd | 231 | removePreview(video, callback) |
98ac898a C |
232 | }, |
233 | ||
15103f11 | 234 | function removeVideoToFriends (callback) { |
98ac898a | 235 | const params = { |
98ac898a C |
236 | remoteId: video.id |
237 | } | |
238 | ||
239 | friends.removeVideoToFriends(params) | |
240 | ||
241 | return callback() | |
feb4bdfd C |
242 | } |
243 | ) | |
244 | } | |
245 | ||
246 | parallel(tasks, next) | |
247 | } | |
aaf61f38 C |
248 | |
249 | // ------------------------------ METHODS ------------------------------ | |
250 | ||
feb4bdfd C |
251 | function associate (models) { |
252 | this.belongsTo(models.Author, { | |
253 | foreignKey: { | |
254 | name: 'authorId', | |
255 | allowNull: false | |
256 | }, | |
257 | onDelete: 'cascade' | |
258 | }) | |
7920c273 C |
259 | |
260 | this.belongsToMany(models.Tag, { | |
261 | foreignKey: 'videoId', | |
262 | through: models.VideoTag, | |
263 | onDelete: 'cascade' | |
264 | }) | |
55fa55a9 C |
265 | |
266 | this.hasMany(models.VideoAbuse, { | |
267 | foreignKey: { | |
268 | name: 'videoId', | |
269 | allowNull: false | |
270 | }, | |
271 | onDelete: 'cascade' | |
272 | }) | |
feb4bdfd C |
273 | } |
274 | ||
f285faa0 C |
275 | function generateMagnetUri () { |
276 | let baseUrlHttp, baseUrlWs | |
277 | ||
278 | if (this.isOwned()) { | |
279 | baseUrlHttp = constants.CONFIG.WEBSERVER.URL | |
280 | baseUrlWs = constants.CONFIG.WEBSERVER.WS + '://' + constants.CONFIG.WEBSERVER.HOSTNAME + ':' + constants.CONFIG.WEBSERVER.PORT | |
281 | } else { | |
feb4bdfd C |
282 | baseUrlHttp = constants.REMOTE_SCHEME.HTTP + '://' + this.Author.Pod.host |
283 | baseUrlWs = constants.REMOTE_SCHEME.WS + '://' + this.Author.Pod.host | |
f285faa0 C |
284 | } |
285 | ||
286 | const xs = baseUrlHttp + constants.STATIC_PATHS.TORRENTS + this.getTorrentName() | |
287 | const announce = baseUrlWs + '/tracker/socket' | |
288 | const urlList = [ baseUrlHttp + constants.STATIC_PATHS.WEBSEED + this.getVideoFilename() ] | |
289 | ||
290 | const magnetHash = { | |
291 | xs, | |
292 | announce, | |
293 | urlList, | |
feb4bdfd | 294 | infoHash: this.infoHash, |
f285faa0 C |
295 | name: this.name |
296 | } | |
297 | ||
298 | return magnetUtil.encode(magnetHash) | |
558d7c23 C |
299 | } |
300 | ||
f285faa0 | 301 | function getVideoFilename () { |
feb4bdfd | 302 | if (this.isOwned()) return this.id + this.extname |
f285faa0 C |
303 | |
304 | return this.remoteId + this.extname | |
305 | } | |
306 | ||
307 | function getThumbnailName () { | |
308 | // We always have a copy of the thumbnail | |
feb4bdfd | 309 | return this.id + '.jpg' |
558d7c23 C |
310 | } |
311 | ||
f285faa0 C |
312 | function getPreviewName () { |
313 | const extension = '.jpg' | |
314 | ||
feb4bdfd | 315 | if (this.isOwned()) return this.id + extension |
f285faa0 C |
316 | |
317 | return this.remoteId + extension | |
318 | } | |
319 | ||
558d7c23 | 320 | function getTorrentName () { |
f285faa0 C |
321 | const extension = '.torrent' |
322 | ||
feb4bdfd | 323 | if (this.isOwned()) return this.id + extension |
f285faa0 C |
324 | |
325 | return this.remoteId + extension | |
558d7c23 C |
326 | } |
327 | ||
aaf61f38 | 328 | function isOwned () { |
558d7c23 | 329 | return this.remoteId === null |
aaf61f38 C |
330 | } |
331 | ||
332 | function toFormatedJSON () { | |
feb4bdfd C |
333 | let podHost |
334 | ||
335 | if (this.Author.Pod) { | |
336 | podHost = this.Author.Pod.host | |
337 | } else { | |
338 | // It means it's our video | |
339 | podHost = constants.CONFIG.WEBSERVER.HOST | |
340 | } | |
341 | ||
aaf61f38 | 342 | const json = { |
feb4bdfd | 343 | id: this.id, |
aaf61f38 C |
344 | name: this.name, |
345 | description: this.description, | |
feb4bdfd | 346 | podHost, |
aaf61f38 | 347 | isLocal: this.isOwned(), |
f285faa0 | 348 | magnetUri: this.generateMagnetUri(), |
feb4bdfd | 349 | author: this.Author.name, |
aaf61f38 | 350 | duration: this.duration, |
9e167724 | 351 | views: this.views, |
7920c273 | 352 | tags: map(this.Tags, 'name'), |
91cc839a | 353 | thumbnailPath: pathUtils.join(constants.STATIC_PATHS.THUMBNAILS, this.getThumbnailName()), |
79066fdf C |
354 | createdAt: this.createdAt, |
355 | updatedAt: this.updatedAt | |
aaf61f38 C |
356 | } |
357 | ||
358 | return json | |
359 | } | |
360 | ||
7b1f49de | 361 | function toAddRemoteJSON (callback) { |
aaf61f38 C |
362 | const self = this |
363 | ||
4d324488 | 364 | // Get thumbnail data to send to the other pod |
f285faa0 | 365 | const thumbnailPath = pathUtils.join(constants.CONFIG.STORAGE.THUMBNAILS_DIR, this.getThumbnailName()) |
558d7c23 | 366 | fs.readFile(thumbnailPath, function (err, thumbnailData) { |
aaf61f38 C |
367 | if (err) { |
368 | logger.error('Cannot read the thumbnail of the video') | |
369 | return callback(err) | |
370 | } | |
371 | ||
372 | const remoteVideo = { | |
373 | name: self.name, | |
374 | description: self.description, | |
feb4bdfd C |
375 | infoHash: self.infoHash, |
376 | remoteId: self.id, | |
377 | author: self.Author.name, | |
aaf61f38 | 378 | duration: self.duration, |
4d324488 | 379 | thumbnailData: thumbnailData.toString('binary'), |
7920c273 | 380 | tags: map(self.Tags, 'name'), |
feb4bdfd | 381 | createdAt: self.createdAt, |
79066fdf | 382 | updatedAt: self.updatedAt, |
8f217302 | 383 | extname: self.extname |
aaf61f38 C |
384 | } |
385 | ||
386 | return callback(null, remoteVideo) | |
387 | }) | |
388 | } | |
389 | ||
7b1f49de C |
390 | function toUpdateRemoteJSON (callback) { |
391 | const json = { | |
392 | name: this.name, | |
393 | description: this.description, | |
394 | infoHash: this.infoHash, | |
395 | remoteId: this.id, | |
396 | author: this.Author.name, | |
397 | duration: this.duration, | |
398 | tags: map(this.Tags, 'name'), | |
399 | createdAt: this.createdAt, | |
79066fdf | 400 | updatedAt: this.updatedAt, |
7b1f49de C |
401 | extname: this.extname |
402 | } | |
403 | ||
404 | return json | |
405 | } | |
406 | ||
aaf61f38 C |
407 | // ------------------------------ STATICS ------------------------------ |
408 | ||
4d324488 | 409 | function generateThumbnailFromData (video, thumbnailData, callback) { |
c77fa067 C |
410 | // Creating the thumbnail for a remote video |
411 | ||
412 | const thumbnailName = video.getThumbnailName() | |
15103f11 | 413 | const thumbnailPath = pathUtils.join(constants.CONFIG.STORAGE.THUMBNAILS_DIR, thumbnailName) |
4d324488 | 414 | fs.writeFile(thumbnailPath, Buffer.from(thumbnailData, 'binary'), function (err) { |
c77fa067 C |
415 | if (err) return callback(err) |
416 | ||
417 | return callback(null, thumbnailName) | |
418 | }) | |
419 | } | |
420 | ||
aaf61f38 C |
421 | function getDurationFromFile (videoPath, callback) { |
422 | ffmpeg.ffprobe(videoPath, function (err, metadata) { | |
423 | if (err) return callback(err) | |
424 | ||
425 | return callback(null, Math.floor(metadata.format.duration)) | |
426 | }) | |
427 | } | |
428 | ||
b769007f | 429 | function list (callback) { |
9cc99d7b | 430 | return this.findAll().asCallback(callback) |
b769007f C |
431 | } |
432 | ||
0ff21c1c | 433 | function listForApi (start, count, sort, callback) { |
feb4bdfd C |
434 | const query = { |
435 | offset: start, | |
436 | limit: count, | |
7920c273 | 437 | distinct: true, // For the count, a video can have many tags |
178edb20 | 438 | order: [ modelUtils.getSort(sort), [ this.sequelize.models.Tag, 'name', 'ASC' ] ], |
feb4bdfd C |
439 | include: [ |
440 | { | |
441 | model: this.sequelize.models.Author, | |
7920c273 C |
442 | include: [ { model: this.sequelize.models.Pod, required: false } ] |
443 | }, | |
444 | ||
445 | this.sequelize.models.Tag | |
feb4bdfd C |
446 | ] |
447 | } | |
448 | ||
449 | return this.findAndCountAll(query).asCallback(function (err, result) { | |
450 | if (err) return callback(err) | |
451 | ||
452 | return callback(null, result.rows, result.count) | |
453 | }) | |
aaf61f38 C |
454 | } |
455 | ||
3d118fb5 | 456 | function loadByHostAndRemoteId (fromHost, remoteId, callback) { |
feb4bdfd C |
457 | const query = { |
458 | where: { | |
459 | remoteId: remoteId | |
460 | }, | |
461 | include: [ | |
462 | { | |
463 | model: this.sequelize.models.Author, | |
464 | include: [ | |
465 | { | |
466 | model: this.sequelize.models.Pod, | |
7920c273 | 467 | required: true, |
feb4bdfd C |
468 | where: { |
469 | host: fromHost | |
470 | } | |
471 | } | |
472 | ] | |
473 | } | |
474 | ] | |
475 | } | |
aaf61f38 | 476 | |
3d118fb5 | 477 | return this.findOne(query).asCallback(callback) |
aaf61f38 C |
478 | } |
479 | ||
7920c273 | 480 | function listOwnedAndPopulateAuthorAndTags (callback) { |
558d7c23 | 481 | // If remoteId is null this is *our* video |
feb4bdfd C |
482 | const query = { |
483 | where: { | |
484 | remoteId: null | |
485 | }, | |
7920c273 | 486 | include: [ this.sequelize.models.Author, this.sequelize.models.Tag ] |
feb4bdfd C |
487 | } |
488 | ||
489 | return this.findAll(query).asCallback(callback) | |
aaf61f38 C |
490 | } |
491 | ||
9bd26629 | 492 | function listOwnedByAuthor (author, callback) { |
feb4bdfd C |
493 | const query = { |
494 | where: { | |
495 | remoteId: null | |
496 | }, | |
497 | include: [ | |
498 | { | |
499 | model: this.sequelize.models.Author, | |
500 | where: { | |
501 | name: author | |
502 | } | |
503 | } | |
504 | ] | |
505 | } | |
9bd26629 | 506 | |
feb4bdfd | 507 | return this.findAll(query).asCallback(callback) |
aaf61f38 C |
508 | } |
509 | ||
510 | function load (id, callback) { | |
feb4bdfd C |
511 | return this.findById(id).asCallback(callback) |
512 | } | |
513 | ||
514 | function loadAndPopulateAuthor (id, callback) { | |
515 | const options = { | |
516 | include: [ this.sequelize.models.Author ] | |
517 | } | |
518 | ||
519 | return this.findById(id, options).asCallback(callback) | |
520 | } | |
521 | ||
7920c273 | 522 | function loadAndPopulateAuthorAndPodAndTags (id, callback) { |
feb4bdfd C |
523 | const options = { |
524 | include: [ | |
525 | { | |
526 | model: this.sequelize.models.Author, | |
7920c273 C |
527 | include: [ { model: this.sequelize.models.Pod, required: false } ] |
528 | }, | |
529 | this.sequelize.models.Tag | |
feb4bdfd C |
530 | ] |
531 | } | |
532 | ||
533 | return this.findById(id, options).asCallback(callback) | |
aaf61f38 C |
534 | } |
535 | ||
7920c273 | 536 | function searchAndPopulateAuthorAndPodAndTags (value, field, start, count, sort, callback) { |
feb4bdfd | 537 | const podInclude = { |
7920c273 C |
538 | model: this.sequelize.models.Pod, |
539 | required: false | |
feb4bdfd | 540 | } |
7920c273 | 541 | |
feb4bdfd C |
542 | const authorInclude = { |
543 | model: this.sequelize.models.Author, | |
544 | include: [ | |
545 | podInclude | |
546 | ] | |
547 | } | |
548 | ||
7920c273 C |
549 | const tagInclude = { |
550 | model: this.sequelize.models.Tag | |
551 | } | |
552 | ||
feb4bdfd C |
553 | const query = { |
554 | where: {}, | |
feb4bdfd C |
555 | offset: start, |
556 | limit: count, | |
7920c273 | 557 | distinct: true, // For the count, a video can have many tags |
178edb20 | 558 | order: [ modelUtils.getSort(sort), [ this.sequelize.models.Tag, 'name', 'ASC' ] ] |
feb4bdfd C |
559 | } |
560 | ||
aaf61f38 | 561 | // Make an exact search with the magnet |
55723d16 C |
562 | if (field === 'magnetUri') { |
563 | const infoHash = magnetUtil.decode(value).infoHash | |
feb4bdfd | 564 | query.where.infoHash = infoHash |
55723d16 | 565 | } else if (field === 'tags') { |
7920c273 C |
566 | const escapedValue = this.sequelize.escape('%' + value + '%') |
567 | query.where = { | |
568 | id: { | |
569 | $in: this.sequelize.literal( | |
570 | '(SELECT "VideoTags"."videoId" FROM "Tags" INNER JOIN "VideoTags" ON "Tags"."id" = "VideoTags"."tagId" WHERE name LIKE ' + escapedValue + ')' | |
571 | ) | |
feb4bdfd C |
572 | } |
573 | } | |
7920c273 C |
574 | } else if (field === 'host') { |
575 | // FIXME: Include our pod? (not stored in the database) | |
576 | podInclude.where = { | |
577 | host: { | |
578 | $like: '%' + value + '%' | |
feb4bdfd | 579 | } |
feb4bdfd | 580 | } |
7920c273 | 581 | podInclude.required = true |
feb4bdfd | 582 | } else if (field === 'author') { |
7920c273 C |
583 | authorInclude.where = { |
584 | name: { | |
feb4bdfd C |
585 | $like: '%' + value + '%' |
586 | } | |
587 | } | |
7920c273 C |
588 | |
589 | // authorInclude.or = true | |
aaf61f38 | 590 | } else { |
feb4bdfd C |
591 | query.where[field] = { |
592 | $like: '%' + value + '%' | |
593 | } | |
aaf61f38 C |
594 | } |
595 | ||
7920c273 C |
596 | query.include = [ |
597 | authorInclude, tagInclude | |
598 | ] | |
599 | ||
600 | if (tagInclude.where) { | |
601 | // query.include.push([ this.sequelize.models.Tag ]) | |
602 | } | |
603 | ||
feb4bdfd C |
604 | return this.findAndCountAll(query).asCallback(function (err, result) { |
605 | if (err) return callback(err) | |
606 | ||
607 | return callback(null, result.rows, result.count) | |
608 | }) | |
aaf61f38 C |
609 | } |
610 | ||
aaf61f38 C |
611 | // --------------------------------------------------------------------------- |
612 | ||
aaf61f38 | 613 | function removeThumbnail (video, callback) { |
15103f11 C |
614 | const thumbnailPath = pathUtils.join(constants.CONFIG.STORAGE.THUMBNAILS_DIR, video.getThumbnailName()) |
615 | fs.unlink(thumbnailPath, callback) | |
aaf61f38 C |
616 | } |
617 | ||
618 | function removeFile (video, callback) { | |
15103f11 C |
619 | const filePath = pathUtils.join(constants.CONFIG.STORAGE.VIDEOS_DIR, video.getVideoFilename()) |
620 | fs.unlink(filePath, callback) | |
aaf61f38 C |
621 | } |
622 | ||
aaf61f38 | 623 | function removeTorrent (video, callback) { |
15103f11 C |
624 | const torrenPath = pathUtils.join(constants.CONFIG.STORAGE.TORRENTS_DIR, video.getTorrentName()) |
625 | fs.unlink(torrenPath, callback) | |
aaf61f38 C |
626 | } |
627 | ||
6a94a109 C |
628 | function removePreview (video, callback) { |
629 | // Same name than video thumnail | |
f285faa0 | 630 | fs.unlink(constants.CONFIG.STORAGE.PREVIEWS_DIR + video.getPreviewName(), callback) |
6a94a109 C |
631 | } |
632 | ||
558d7c23 | 633 | function createPreview (video, videoPath, callback) { |
f285faa0 | 634 | generateImage(video, videoPath, constants.CONFIG.STORAGE.PREVIEWS_DIR, video.getPreviewName(), callback) |
6a94a109 C |
635 | } |
636 | ||
558d7c23 | 637 | function createThumbnail (video, videoPath, callback) { |
f285faa0 | 638 | generateImage(video, videoPath, constants.CONFIG.STORAGE.THUMBNAILS_DIR, video.getThumbnailName(), constants.THUMBNAILS_SIZE, callback) |
aaf61f38 C |
639 | } |
640 | ||
f285faa0 | 641 | function generateImage (video, videoPath, folder, imageName, size, callback) { |
558d7c23 | 642 | const options = { |
f285faa0 | 643 | filename: imageName, |
558d7c23 C |
644 | count: 1, |
645 | folder | |
646 | } | |
aaf61f38 | 647 | |
558d7c23 C |
648 | if (!callback) { |
649 | callback = size | |
650 | } else { | |
651 | options.size = size | |
652 | } | |
653 | ||
654 | ffmpeg(videoPath) | |
655 | .on('error', callback) | |
656 | .on('end', function () { | |
f285faa0 | 657 | callback(null, imageName) |
aaf61f38 | 658 | }) |
558d7c23 | 659 | .thumbnail(options) |
aaf61f38 | 660 | } |