]>
Commit | Line | Data |
---|---|---|
4d4e5cd4 | 1 | import * as safeBuffer from 'safe-buffer' |
65fcc311 | 2 | const Buffer = safeBuffer.Buffer |
4d4e5cd4 | 3 | import * as ffmpeg from 'fluent-ffmpeg' |
4d4e5cd4 | 4 | import * as magnetUtil from 'magnet-uri' |
93e1258c | 5 | import { map } from 'lodash' |
4d4e5cd4 | 6 | import * as parseTorrent from 'parse-torrent' |
65fcc311 | 7 | import { join } from 'path' |
e02643f3 | 8 | import * as Sequelize from 'sequelize' |
6fcd19ba | 9 | import * as Promise from 'bluebird' |
65fcc311 | 10 | |
6fcd19ba | 11 | import { TagInstance } from './tag-interface' |
65fcc311 C |
12 | import { |
13 | logger, | |
14 | isVideoNameValid, | |
15 | isVideoCategoryValid, | |
16 | isVideoLicenceValid, | |
17 | isVideoLanguageValid, | |
18 | isVideoNSFWValid, | |
19 | isVideoDescriptionValid, | |
6fcd19ba C |
20 | isVideoDurationValid, |
21 | readFileBufferPromise, | |
22 | unlinkPromise, | |
23 | renamePromise, | |
24 | writeFilePromise, | |
40298b02 C |
25 | createTorrentPromise, |
26 | statPromise | |
74889a71 | 27 | } from '../../helpers' |
65fcc311 | 28 | import { |
65fcc311 C |
29 | CONFIG, |
30 | REMOTE_SCHEME, | |
31 | STATIC_PATHS, | |
32 | VIDEO_CATEGORIES, | |
33 | VIDEO_LICENCES, | |
34 | VIDEO_LANGUAGES, | |
93e1258c C |
35 | THUMBNAILS_SIZE, |
36 | VIDEO_FILE_RESOLUTIONS | |
74889a71 | 37 | } from '../../initializers' |
93e1258c | 38 | import { removeVideoToFriends } from '../../lib' |
40298b02 C |
39 | import { VideoResolution } from '../../../shared' |
40 | import { VideoFileInstance, VideoFileModel } from './video-file-interface' | |
aaf61f38 | 41 | |
74889a71 | 42 | import { addMethodsToModel, getSort } from '../utils' |
e02643f3 | 43 | import { |
e02643f3 C |
44 | VideoInstance, |
45 | VideoAttributes, | |
46 | ||
47 | VideoMethods | |
48 | } from './video-interface' | |
49 | ||
50 | let Video: Sequelize.Model<VideoInstance, VideoAttributes> | |
40298b02 | 51 | let getOriginalFile: VideoMethods.GetOriginalFile |
e02643f3 C |
52 | let generateMagnetUri: VideoMethods.GenerateMagnetUri |
53 | let getVideoFilename: VideoMethods.GetVideoFilename | |
54 | let getThumbnailName: VideoMethods.GetThumbnailName | |
55 | let getPreviewName: VideoMethods.GetPreviewName | |
93e1258c | 56 | let getTorrentFileName: VideoMethods.GetTorrentFileName |
e02643f3 | 57 | let isOwned: VideoMethods.IsOwned |
0aef76c4 | 58 | let toFormattedJSON: VideoMethods.ToFormattedJSON |
e02643f3 C |
59 | let toAddRemoteJSON: VideoMethods.ToAddRemoteJSON |
60 | let toUpdateRemoteJSON: VideoMethods.ToUpdateRemoteJSON | |
40298b02 C |
61 | let optimizeOriginalVideofile: VideoMethods.OptimizeOriginalVideofile |
62 | let transcodeOriginalVideofile: VideoMethods.TranscodeOriginalVideofile | |
93e1258c C |
63 | let createPreview: VideoMethods.CreatePreview |
64 | let createThumbnail: VideoMethods.CreateThumbnail | |
65 | let getVideoFilePath: VideoMethods.GetVideoFilePath | |
66 | let createTorrentAndSetInfoHash: VideoMethods.CreateTorrentAndSetInfoHash | |
40298b02 | 67 | let getOriginalFileHeight: VideoMethods.GetOriginalFileHeight |
e02643f3 C |
68 | |
69 | let generateThumbnailFromData: VideoMethods.GenerateThumbnailFromData | |
70 | let getDurationFromFile: VideoMethods.GetDurationFromFile | |
71 | let list: VideoMethods.List | |
72 | let listForApi: VideoMethods.ListForApi | |
0a6658fd | 73 | let loadByHostAndUUID: VideoMethods.LoadByHostAndUUID |
e02643f3 C |
74 | let listOwnedAndPopulateAuthorAndTags: VideoMethods.ListOwnedAndPopulateAuthorAndTags |
75 | let listOwnedByAuthor: VideoMethods.ListOwnedByAuthor | |
76 | let load: VideoMethods.Load | |
0a6658fd | 77 | let loadByUUID: VideoMethods.LoadByUUID |
e02643f3 C |
78 | let loadAndPopulateAuthor: VideoMethods.LoadAndPopulateAuthor |
79 | let loadAndPopulateAuthorAndPodAndTags: VideoMethods.LoadAndPopulateAuthorAndPodAndTags | |
0a6658fd | 80 | let loadByUUIDAndPopulateAuthorAndPodAndTags: VideoMethods.LoadByUUIDAndPopulateAuthorAndPodAndTags |
e02643f3 | 81 | let searchAndPopulateAuthorAndPodAndTags: VideoMethods.SearchAndPopulateAuthorAndPodAndTags |
93e1258c C |
82 | let removeThumbnail: VideoMethods.RemoveThumbnail |
83 | let removePreview: VideoMethods.RemovePreview | |
84 | let removeFile: VideoMethods.RemoveFile | |
85 | let removeTorrent: VideoMethods.RemoveTorrent | |
e02643f3 | 86 | |
127944aa C |
87 | export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) { |
88 | Video = sequelize.define<VideoInstance, VideoAttributes>('Video', | |
feb4bdfd | 89 | { |
0a6658fd | 90 | uuid: { |
feb4bdfd C |
91 | type: DataTypes.UUID, |
92 | defaultValue: DataTypes.UUIDV4, | |
0a6658fd | 93 | allowNull: false, |
67bf9b96 C |
94 | validate: { |
95 | isUUID: 4 | |
96 | } | |
aaf61f38 | 97 | }, |
feb4bdfd | 98 | name: { |
67bf9b96 C |
99 | type: DataTypes.STRING, |
100 | allowNull: false, | |
101 | validate: { | |
075f16ca | 102 | nameValid: value => { |
65fcc311 | 103 | const res = isVideoNameValid(value) |
67bf9b96 C |
104 | if (res === false) throw new Error('Video name is not valid.') |
105 | } | |
106 | } | |
6a94a109 | 107 | }, |
6e07c3de C |
108 | category: { |
109 | type: DataTypes.INTEGER, | |
110 | allowNull: false, | |
111 | validate: { | |
075f16ca | 112 | categoryValid: value => { |
65fcc311 | 113 | const res = isVideoCategoryValid(value) |
6e07c3de C |
114 | if (res === false) throw new Error('Video category is not valid.') |
115 | } | |
116 | } | |
117 | }, | |
6f0c39e2 C |
118 | licence: { |
119 | type: DataTypes.INTEGER, | |
120 | allowNull: false, | |
3092476e | 121 | defaultValue: null, |
6f0c39e2 | 122 | validate: { |
075f16ca | 123 | licenceValid: value => { |
65fcc311 | 124 | const res = isVideoLicenceValid(value) |
6f0c39e2 C |
125 | if (res === false) throw new Error('Video licence is not valid.') |
126 | } | |
127 | } | |
128 | }, | |
3092476e C |
129 | language: { |
130 | type: DataTypes.INTEGER, | |
131 | allowNull: true, | |
132 | validate: { | |
075f16ca | 133 | languageValid: value => { |
65fcc311 | 134 | const res = isVideoLanguageValid(value) |
3092476e C |
135 | if (res === false) throw new Error('Video language is not valid.') |
136 | } | |
137 | } | |
138 | }, | |
31b59b47 C |
139 | nsfw: { |
140 | type: DataTypes.BOOLEAN, | |
141 | allowNull: false, | |
142 | validate: { | |
075f16ca | 143 | nsfwValid: value => { |
65fcc311 | 144 | const res = isVideoNSFWValid(value) |
31b59b47 C |
145 | if (res === false) throw new Error('Video nsfw attribute is not valid.') |
146 | } | |
147 | } | |
148 | }, | |
feb4bdfd | 149 | description: { |
67bf9b96 C |
150 | type: DataTypes.STRING, |
151 | allowNull: false, | |
152 | validate: { | |
075f16ca | 153 | descriptionValid: value => { |
65fcc311 | 154 | const res = isVideoDescriptionValid(value) |
67bf9b96 C |
155 | if (res === false) throw new Error('Video description is not valid.') |
156 | } | |
157 | } | |
feb4bdfd | 158 | }, |
feb4bdfd | 159 | duration: { |
67bf9b96 C |
160 | type: DataTypes.INTEGER, |
161 | allowNull: false, | |
162 | validate: { | |
075f16ca | 163 | durationValid: value => { |
65fcc311 | 164 | const res = isVideoDurationValid(value) |
67bf9b96 C |
165 | if (res === false) throw new Error('Video duration is not valid.') |
166 | } | |
167 | } | |
9e167724 C |
168 | }, |
169 | views: { | |
170 | type: DataTypes.INTEGER, | |
171 | allowNull: false, | |
172 | defaultValue: 0, | |
173 | validate: { | |
174 | min: 0, | |
175 | isInt: true | |
176 | } | |
d38b8281 C |
177 | }, |
178 | likes: { | |
179 | type: DataTypes.INTEGER, | |
180 | allowNull: false, | |
181 | defaultValue: 0, | |
182 | validate: { | |
183 | min: 0, | |
184 | isInt: true | |
185 | } | |
186 | }, | |
187 | dislikes: { | |
188 | type: DataTypes.INTEGER, | |
189 | allowNull: false, | |
190 | defaultValue: 0, | |
191 | validate: { | |
192 | min: 0, | |
193 | isInt: true | |
194 | } | |
0a6658fd C |
195 | }, |
196 | remote: { | |
197 | type: DataTypes.BOOLEAN, | |
198 | allowNull: false, | |
199 | defaultValue: false | |
aaf61f38 | 200 | } |
feb4bdfd C |
201 | }, |
202 | { | |
319d072e C |
203 | indexes: [ |
204 | { | |
205 | fields: [ 'authorId' ] | |
206 | }, | |
319d072e C |
207 | { |
208 | fields: [ 'name' ] | |
209 | }, | |
210 | { | |
211 | fields: [ 'createdAt' ] | |
212 | }, | |
213 | { | |
214 | fields: [ 'duration' ] | |
215 | }, | |
9e167724 C |
216 | { |
217 | fields: [ 'views' ] | |
d38b8281 C |
218 | }, |
219 | { | |
220 | fields: [ 'likes' ] | |
0a6658fd C |
221 | }, |
222 | { | |
223 | fields: [ 'uuid' ] | |
319d072e C |
224 | } |
225 | ], | |
feb4bdfd | 226 | hooks: { |
feb4bdfd C |
227 | afterDestroy |
228 | } | |
229 | } | |
230 | ) | |
aaf61f38 | 231 | |
e02643f3 C |
232 | const classMethods = [ |
233 | associate, | |
234 | ||
235 | generateThumbnailFromData, | |
236 | getDurationFromFile, | |
237 | list, | |
238 | listForApi, | |
239 | listOwnedAndPopulateAuthorAndTags, | |
240 | listOwnedByAuthor, | |
241 | load, | |
e02643f3 C |
242 | loadAndPopulateAuthor, |
243 | loadAndPopulateAuthorAndPodAndTags, | |
93e1258c C |
244 | loadByHostAndUUID, |
245 | loadByUUID, | |
0a6658fd | 246 | loadByUUIDAndPopulateAuthorAndPodAndTags, |
93e1258c | 247 | searchAndPopulateAuthorAndPodAndTags |
e02643f3 C |
248 | ] |
249 | const instanceMethods = [ | |
93e1258c C |
250 | createPreview, |
251 | createThumbnail, | |
252 | createTorrentAndSetInfoHash, | |
e02643f3 | 253 | generateMagnetUri, |
e02643f3 | 254 | getPreviewName, |
93e1258c C |
255 | getThumbnailName, |
256 | getTorrentFileName, | |
257 | getVideoFilename, | |
258 | getVideoFilePath, | |
40298b02 | 259 | getOriginalFile, |
e02643f3 | 260 | isOwned, |
93e1258c C |
261 | removeFile, |
262 | removePreview, | |
263 | removeThumbnail, | |
264 | removeTorrent, | |
e02643f3 | 265 | toAddRemoteJSON, |
0aef76c4 | 266 | toFormattedJSON, |
e02643f3 | 267 | toUpdateRemoteJSON, |
40298b02 C |
268 | optimizeOriginalVideofile, |
269 | transcodeOriginalVideofile, | |
270 | getOriginalFileHeight | |
e02643f3 C |
271 | ] |
272 | addMethodsToModel(Video, classMethods, instanceMethods) | |
273 | ||
feb4bdfd C |
274 | return Video |
275 | } | |
aaf61f38 | 276 | |
aaf61f38 C |
277 | // ------------------------------ METHODS ------------------------------ |
278 | ||
feb4bdfd | 279 | function associate (models) { |
e02643f3 | 280 | Video.belongsTo(models.Author, { |
feb4bdfd C |
281 | foreignKey: { |
282 | name: 'authorId', | |
283 | allowNull: false | |
284 | }, | |
285 | onDelete: 'cascade' | |
286 | }) | |
7920c273 | 287 | |
e02643f3 | 288 | Video.belongsToMany(models.Tag, { |
7920c273 C |
289 | foreignKey: 'videoId', |
290 | through: models.VideoTag, | |
291 | onDelete: 'cascade' | |
292 | }) | |
55fa55a9 | 293 | |
e02643f3 | 294 | Video.hasMany(models.VideoAbuse, { |
55fa55a9 C |
295 | foreignKey: { |
296 | name: 'videoId', | |
297 | allowNull: false | |
298 | }, | |
299 | onDelete: 'cascade' | |
300 | }) | |
93e1258c C |
301 | |
302 | Video.hasMany(models.VideoFile, { | |
303 | foreignKey: { | |
304 | name: 'videoId', | |
305 | allowNull: false | |
306 | }, | |
307 | onDelete: 'cascade' | |
308 | }) | |
feb4bdfd C |
309 | } |
310 | ||
91f6f169 | 311 | function afterDestroy (video: VideoInstance, options: { transaction: Sequelize.Transaction }) { |
93e1258c | 312 | const tasks = [] |
f285faa0 | 313 | |
93e1258c C |
314 | tasks.push( |
315 | video.removeThumbnail() | |
316 | ) | |
f285faa0 | 317 | |
93e1258c C |
318 | if (video.isOwned()) { |
319 | const removeVideoToFriendsParams = { | |
320 | uuid: video.uuid | |
321 | } | |
f285faa0 | 322 | |
93e1258c C |
323 | tasks.push( |
324 | video.removePreview(), | |
91f6f169 | 325 | removeVideoToFriends(removeVideoToFriendsParams, options.transaction) |
93e1258c C |
326 | ) |
327 | ||
91f6f169 | 328 | // Remove physical files and torrents |
93e1258c C |
329 | video.VideoFiles.forEach(file => { |
330 | video.removeFile(file), | |
331 | video.removeTorrent(file) | |
332 | }) | |
f285faa0 C |
333 | } |
334 | ||
93e1258c | 335 | return Promise.all(tasks) |
558d7c23 C |
336 | } |
337 | ||
40298b02 C |
338 | getOriginalFile = function (this: VideoInstance) { |
339 | if (Array.isArray(this.VideoFiles) === false) return undefined | |
340 | ||
341 | return this.VideoFiles.find(file => file.resolution === VideoResolution.ORIGINAL) | |
342 | } | |
343 | ||
93e1258c | 344 | getVideoFilename = function (this: VideoInstance, videoFile: VideoFileInstance) { |
40298b02 | 345 | return this.uuid + '-' + VIDEO_FILE_RESOLUTIONS[videoFile.resolution] + videoFile.extname |
f285faa0 C |
346 | } |
347 | ||
70c065d6 | 348 | getThumbnailName = function (this: VideoInstance) { |
f285faa0 | 349 | // We always have a copy of the thumbnail |
0a6658fd C |
350 | const extension = '.jpg' |
351 | return this.uuid + extension | |
558d7c23 C |
352 | } |
353 | ||
70c065d6 | 354 | getPreviewName = function (this: VideoInstance) { |
f285faa0 | 355 | const extension = '.jpg' |
0a6658fd | 356 | return this.uuid + extension |
f285faa0 C |
357 | } |
358 | ||
93e1258c | 359 | getTorrentFileName = function (this: VideoInstance, videoFile: VideoFileInstance) { |
f285faa0 | 360 | const extension = '.torrent' |
40298b02 | 361 | return this.uuid + '-' + VIDEO_FILE_RESOLUTIONS[videoFile.resolution] + extension |
558d7c23 C |
362 | } |
363 | ||
70c065d6 | 364 | isOwned = function (this: VideoInstance) { |
0a6658fd | 365 | return this.remote === false |
aaf61f38 C |
366 | } |
367 | ||
93e1258c C |
368 | createPreview = function (this: VideoInstance, videoFile: VideoFileInstance) { |
369 | return generateImage(this, this.getVideoFilePath(videoFile), CONFIG.STORAGE.PREVIEWS_DIR, this.getPreviewName(), null) | |
370 | } | |
371 | ||
372 | createThumbnail = function (this: VideoInstance, videoFile: VideoFileInstance) { | |
373 | return generateImage(this, this.getVideoFilePath(videoFile), CONFIG.STORAGE.THUMBNAILS_DIR, this.getThumbnailName(), THUMBNAILS_SIZE) | |
374 | } | |
375 | ||
376 | getVideoFilePath = function (this: VideoInstance, videoFile: VideoFileInstance) { | |
377 | return join(CONFIG.STORAGE.VIDEOS_DIR, this.getVideoFilename(videoFile)) | |
378 | } | |
379 | ||
380 | createTorrentAndSetInfoHash = function (this: VideoInstance, videoFile: VideoFileInstance) { | |
381 | const options = { | |
382 | announceList: [ | |
383 | [ CONFIG.WEBSERVER.WS + '://' + CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT + '/tracker/socket' ] | |
384 | ], | |
385 | urlList: [ | |
386 | CONFIG.WEBSERVER.URL + STATIC_PATHS.WEBSEED + this.getVideoFilename(videoFile) | |
387 | ] | |
388 | } | |
389 | ||
390 | return createTorrentPromise(this.getVideoFilePath(videoFile), options) | |
391 | .then(torrent => { | |
392 | const filePath = join(CONFIG.STORAGE.TORRENTS_DIR, this.getTorrentFileName(videoFile)) | |
fdbda9e3 C |
393 | logger.info('Creating torrent %s.', filePath) |
394 | ||
93e1258c C |
395 | return writeFilePromise(filePath, torrent).then(() => torrent) |
396 | }) | |
397 | .then(torrent => { | |
398 | const parsedTorrent = parseTorrent(torrent) | |
399 | ||
400 | videoFile.infoHash = parsedTorrent.infoHash | |
401 | }) | |
402 | } | |
403 | ||
404 | generateMagnetUri = function (this: VideoInstance, videoFile: VideoFileInstance) { | |
405 | let baseUrlHttp | |
406 | let baseUrlWs | |
407 | ||
408 | if (this.isOwned()) { | |
409 | baseUrlHttp = CONFIG.WEBSERVER.URL | |
410 | baseUrlWs = CONFIG.WEBSERVER.WS + '://' + CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT | |
411 | } else { | |
412 | baseUrlHttp = REMOTE_SCHEME.HTTP + '://' + this.Author.Pod.host | |
413 | baseUrlWs = REMOTE_SCHEME.WS + '://' + this.Author.Pod.host | |
414 | } | |
415 | ||
416 | const xs = baseUrlHttp + STATIC_PATHS.TORRENTS + this.getTorrentFileName(videoFile) | |
417 | const announce = [ baseUrlWs + '/tracker/socket' ] | |
418 | const urlList = [ baseUrlHttp + STATIC_PATHS.WEBSEED + this.getVideoFilename(videoFile) ] | |
419 | ||
420 | const magnetHash = { | |
421 | xs, | |
422 | announce, | |
423 | urlList, | |
424 | infoHash: videoFile.infoHash, | |
425 | name: this.name | |
426 | } | |
427 | ||
428 | return magnetUtil.encode(magnetHash) | |
429 | } | |
430 | ||
0aef76c4 | 431 | toFormattedJSON = function (this: VideoInstance) { |
feb4bdfd C |
432 | let podHost |
433 | ||
434 | if (this.Author.Pod) { | |
435 | podHost = this.Author.Pod.host | |
436 | } else { | |
437 | // It means it's our video | |
65fcc311 | 438 | podHost = CONFIG.WEBSERVER.HOST |
feb4bdfd C |
439 | } |
440 | ||
6e07c3de | 441 | // Maybe our pod is not up to date and there are new categories since our version |
65fcc311 | 442 | let categoryLabel = VIDEO_CATEGORIES[this.category] |
6e07c3de C |
443 | if (!categoryLabel) categoryLabel = 'Misc' |
444 | ||
6f0c39e2 | 445 | // Maybe our pod is not up to date and there are new licences since our version |
65fcc311 | 446 | let licenceLabel = VIDEO_LICENCES[this.licence] |
6f0c39e2 C |
447 | if (!licenceLabel) licenceLabel = 'Unknown' |
448 | ||
3092476e | 449 | // Language is an optional attribute |
65fcc311 | 450 | let languageLabel = VIDEO_LANGUAGES[this.language] |
3092476e C |
451 | if (!languageLabel) languageLabel = 'Unknown' |
452 | ||
aaf61f38 | 453 | const json = { |
feb4bdfd | 454 | id: this.id, |
0a6658fd | 455 | uuid: this.uuid, |
aaf61f38 | 456 | name: this.name, |
6e07c3de C |
457 | category: this.category, |
458 | categoryLabel, | |
6f0c39e2 C |
459 | licence: this.licence, |
460 | licenceLabel, | |
3092476e C |
461 | language: this.language, |
462 | languageLabel, | |
31b59b47 | 463 | nsfw: this.nsfw, |
aaf61f38 | 464 | description: this.description, |
feb4bdfd | 465 | podHost, |
aaf61f38 | 466 | isLocal: this.isOwned(), |
feb4bdfd | 467 | author: this.Author.name, |
aaf61f38 | 468 | duration: this.duration, |
9e167724 | 469 | views: this.views, |
d38b8281 C |
470 | likes: this.likes, |
471 | dislikes: this.dislikes, | |
6fcd19ba | 472 | tags: map<TagInstance, string>(this.Tags, 'name'), |
65fcc311 | 473 | thumbnailPath: join(STATIC_PATHS.THUMBNAILS, this.getThumbnailName()), |
f981dae8 | 474 | previewPath: join(STATIC_PATHS.PREVIEWS, this.getPreviewName()), |
79066fdf | 475 | createdAt: this.createdAt, |
93e1258c C |
476 | updatedAt: this.updatedAt, |
477 | files: [] | |
aaf61f38 C |
478 | } |
479 | ||
93e1258c C |
480 | this.VideoFiles.forEach(videoFile => { |
481 | let resolutionLabel = VIDEO_FILE_RESOLUTIONS[videoFile.resolution] | |
482 | if (!resolutionLabel) resolutionLabel = 'Unknown' | |
483 | ||
484 | const videoFileJson = { | |
485 | resolution: videoFile.resolution, | |
486 | resolutionLabel, | |
487 | magnetUri: this.generateMagnetUri(videoFile), | |
488 | size: videoFile.size | |
489 | } | |
490 | ||
491 | json.files.push(videoFileJson) | |
492 | }) | |
493 | ||
aaf61f38 C |
494 | return json |
495 | } | |
496 | ||
6fcd19ba | 497 | toAddRemoteJSON = function (this: VideoInstance) { |
4d324488 | 498 | // Get thumbnail data to send to the other pod |
65fcc311 | 499 | const thumbnailPath = join(CONFIG.STORAGE.THUMBNAILS_DIR, this.getThumbnailName()) |
aaf61f38 | 500 | |
6fcd19ba | 501 | return readFileBufferPromise(thumbnailPath).then(thumbnailData => { |
aaf61f38 | 502 | const remoteVideo = { |
0a6658fd | 503 | uuid: this.uuid, |
e02643f3 C |
504 | name: this.name, |
505 | category: this.category, | |
506 | licence: this.licence, | |
507 | language: this.language, | |
508 | nsfw: this.nsfw, | |
509 | description: this.description, | |
e02643f3 C |
510 | author: this.Author.name, |
511 | duration: this.duration, | |
4d324488 | 512 | thumbnailData: thumbnailData.toString('binary'), |
6fcd19ba | 513 | tags: map<TagInstance, string>(this.Tags, 'name'), |
e02643f3 C |
514 | createdAt: this.createdAt, |
515 | updatedAt: this.updatedAt, | |
e02643f3 C |
516 | views: this.views, |
517 | likes: this.likes, | |
93e1258c C |
518 | dislikes: this.dislikes, |
519 | files: [] | |
aaf61f38 C |
520 | } |
521 | ||
93e1258c C |
522 | this.VideoFiles.forEach(videoFile => { |
523 | remoteVideo.files.push({ | |
524 | infoHash: videoFile.infoHash, | |
525 | resolution: videoFile.resolution, | |
526 | extname: videoFile.extname, | |
527 | size: videoFile.size | |
528 | }) | |
529 | }) | |
530 | ||
6fcd19ba | 531 | return remoteVideo |
aaf61f38 C |
532 | }) |
533 | } | |
534 | ||
70c065d6 | 535 | toUpdateRemoteJSON = function (this: VideoInstance) { |
7b1f49de | 536 | const json = { |
0a6658fd | 537 | uuid: this.uuid, |
7b1f49de | 538 | name: this.name, |
6e07c3de | 539 | category: this.category, |
6f0c39e2 | 540 | licence: this.licence, |
3092476e | 541 | language: this.language, |
31b59b47 | 542 | nsfw: this.nsfw, |
7b1f49de | 543 | description: this.description, |
7b1f49de C |
544 | author: this.Author.name, |
545 | duration: this.duration, | |
6fcd19ba | 546 | tags: map<TagInstance, string>(this.Tags, 'name'), |
7b1f49de | 547 | createdAt: this.createdAt, |
79066fdf | 548 | updatedAt: this.updatedAt, |
d38b8281 C |
549 | views: this.views, |
550 | likes: this.likes, | |
93e1258c C |
551 | dislikes: this.dislikes, |
552 | files: [] | |
7b1f49de C |
553 | } |
554 | ||
93e1258c C |
555 | this.VideoFiles.forEach(videoFile => { |
556 | json.files.push({ | |
557 | infoHash: videoFile.infoHash, | |
558 | resolution: videoFile.resolution, | |
559 | extname: videoFile.extname, | |
560 | size: videoFile.size | |
561 | }) | |
562 | }) | |
563 | ||
7b1f49de C |
564 | return json |
565 | } | |
566 | ||
40298b02 | 567 | optimizeOriginalVideofile = function (this: VideoInstance) { |
65fcc311 | 568 | const videosDirectory = CONFIG.STORAGE.VIDEOS_DIR |
227d02fe | 569 | const newExtname = '.mp4' |
40298b02 | 570 | const inputVideoFile = this.getOriginalFile() |
93e1258c C |
571 | const videoInputPath = join(videosDirectory, this.getVideoFilename(inputVideoFile)) |
572 | const videoOutputPath = join(videosDirectory, this.id + '-transcoded' + newExtname) | |
227d02fe | 573 | |
6fcd19ba C |
574 | return new Promise<void>((res, rej) => { |
575 | ffmpeg(videoInputPath) | |
576 | .output(videoOutputPath) | |
577 | .videoCodec('libx264') | |
578 | .outputOption('-threads ' + CONFIG.TRANSCODING.THREADS) | |
579 | .outputOption('-movflags faststart') | |
580 | .on('error', rej) | |
581 | .on('end', () => { | |
582 | ||
583 | return unlinkPromise(videoInputPath) | |
584 | .then(() => { | |
585 | // Important to do this before getVideoFilename() to take in account the new file extension | |
93e1258c | 586 | inputVideoFile.set('extname', newExtname) |
6fcd19ba | 587 | |
93e1258c | 588 | return renamePromise(videoOutputPath, this.getVideoFilePath(inputVideoFile)) |
227d02fe | 589 | }) |
40298b02 C |
590 | .then(() => { |
591 | return statPromise(this.getVideoFilePath(inputVideoFile)) | |
592 | }) | |
593 | .then(stats => { | |
594 | return inputVideoFile.set('size', stats.size) | |
595 | }) | |
6fcd19ba | 596 | .then(() => { |
93e1258c | 597 | return this.createTorrentAndSetInfoHash(inputVideoFile) |
6fcd19ba C |
598 | }) |
599 | .then(() => { | |
93e1258c | 600 | return inputVideoFile.save() |
6fcd19ba C |
601 | }) |
602 | .then(() => { | |
603 | return res() | |
604 | }) | |
605 | .catch(err => { | |
b0f9f39e | 606 | // Auto destruction... |
93e1258c | 607 | this.destroy().catch(err => logger.error('Cannot destruct video after transcoding failure.', err)) |
227d02fe | 608 | |
6fcd19ba C |
609 | return rej(err) |
610 | }) | |
227d02fe | 611 | }) |
6fcd19ba C |
612 | .run() |
613 | }) | |
227d02fe C |
614 | } |
615 | ||
40298b02 C |
616 | transcodeOriginalVideofile = function (this: VideoInstance, resolution: VideoResolution) { |
617 | const videosDirectory = CONFIG.STORAGE.VIDEOS_DIR | |
618 | const extname = '.mp4' | |
619 | ||
620 | // We are sure it's x264 in mp4 because optimizeOriginalVideofile was already executed | |
621 | const videoInputPath = join(videosDirectory, this.getVideoFilename(this.getOriginalFile())) | |
622 | ||
623 | const newVideoFile = (Video['sequelize'].models.VideoFile as VideoFileModel).build({ | |
624 | resolution, | |
625 | extname, | |
626 | size: 0, | |
627 | videoId: this.id | |
628 | }) | |
629 | const videoOutputPath = join(videosDirectory, this.getVideoFilename(newVideoFile)) | |
ecb455b6 | 630 | const resolutionOption = `${resolution}x?` // '720x?' for example |
40298b02 C |
631 | |
632 | return new Promise<void>((res, rej) => { | |
633 | ffmpeg(videoInputPath) | |
634 | .output(videoOutputPath) | |
635 | .videoCodec('libx264') | |
ecb455b6 | 636 | .size(resolutionOption) |
40298b02 C |
637 | .outputOption('-threads ' + CONFIG.TRANSCODING.THREADS) |
638 | .outputOption('-movflags faststart') | |
639 | .on('error', rej) | |
640 | .on('end', () => { | |
641 | return statPromise(videoOutputPath) | |
642 | .then(stats => { | |
643 | newVideoFile.set('size', stats.size) | |
644 | ||
645 | return undefined | |
646 | }) | |
647 | .then(() => { | |
648 | return this.createTorrentAndSetInfoHash(newVideoFile) | |
649 | }) | |
650 | .then(() => { | |
651 | return newVideoFile.save() | |
652 | }) | |
653 | .then(() => { | |
654 | return this.VideoFiles.push(newVideoFile) | |
655 | }) | |
656 | .then(() => { | |
657 | return res() | |
658 | }) | |
659 | .catch(rej) | |
660 | }) | |
661 | .run() | |
662 | }) | |
663 | } | |
664 | ||
665 | getOriginalFileHeight = function (this: VideoInstance) { | |
666 | const originalFilePath = this.getVideoFilePath(this.getOriginalFile()) | |
667 | ||
668 | return new Promise<number>((res, rej) => { | |
669 | ffmpeg.ffprobe(originalFilePath, (err, metadata) => { | |
670 | if (err) return rej(err) | |
671 | ||
672 | const videoStream = metadata.streams.find(s => s.codec_type === 'video') | |
673 | return res(videoStream.height) | |
674 | }) | |
675 | }) | |
676 | } | |
677 | ||
93e1258c C |
678 | removeThumbnail = function (this: VideoInstance) { |
679 | const thumbnailPath = join(CONFIG.STORAGE.THUMBNAILS_DIR, this.getThumbnailName()) | |
680 | return unlinkPromise(thumbnailPath) | |
681 | } | |
682 | ||
683 | removePreview = function (this: VideoInstance) { | |
684 | // Same name than video thumbnail | |
685 | return unlinkPromise(CONFIG.STORAGE.PREVIEWS_DIR + this.getPreviewName()) | |
686 | } | |
687 | ||
688 | removeFile = function (this: VideoInstance, videoFile: VideoFileInstance) { | |
689 | const filePath = join(CONFIG.STORAGE.VIDEOS_DIR, this.getVideoFilename(videoFile)) | |
690 | return unlinkPromise(filePath) | |
691 | } | |
692 | ||
693 | removeTorrent = function (this: VideoInstance, videoFile: VideoFileInstance) { | |
b0f9f39e C |
694 | const torrentPath = join(CONFIG.STORAGE.TORRENTS_DIR, this.getTorrentFileName(videoFile)) |
695 | return unlinkPromise(torrentPath) | |
93e1258c C |
696 | } |
697 | ||
aaf61f38 C |
698 | // ------------------------------ STATICS ------------------------------ |
699 | ||
6fcd19ba | 700 | generateThumbnailFromData = function (video: VideoInstance, thumbnailData: string) { |
c77fa067 C |
701 | // Creating the thumbnail for a remote video |
702 | ||
703 | const thumbnailName = video.getThumbnailName() | |
65fcc311 | 704 | const thumbnailPath = join(CONFIG.STORAGE.THUMBNAILS_DIR, thumbnailName) |
6fcd19ba C |
705 | return writeFilePromise(thumbnailPath, Buffer.from(thumbnailData, 'binary')).then(() => { |
706 | return thumbnailName | |
c77fa067 C |
707 | }) |
708 | } | |
709 | ||
6fcd19ba C |
710 | getDurationFromFile = function (videoPath: string) { |
711 | return new Promise<number>((res, rej) => { | |
075f16ca | 712 | ffmpeg.ffprobe(videoPath, (err, metadata) => { |
6fcd19ba | 713 | if (err) return rej(err) |
aaf61f38 | 714 | |
6fcd19ba C |
715 | return res(Math.floor(metadata.format.duration)) |
716 | }) | |
aaf61f38 C |
717 | }) |
718 | } | |
719 | ||
6fcd19ba | 720 | list = function () { |
93e1258c C |
721 | const query = { |
722 | include: [ Video['sequelize'].models.VideoFile ] | |
723 | } | |
724 | ||
725 | return Video.findAll(query) | |
b769007f C |
726 | } |
727 | ||
6fcd19ba | 728 | listForApi = function (start: number, count: number, sort: string) { |
556ddc31 | 729 | // Exclude blacklisted videos from the list |
feb4bdfd | 730 | const query = { |
e02643f3 | 731 | distinct: true, |
feb4bdfd C |
732 | offset: start, |
733 | limit: count, | |
e02643f3 | 734 | order: [ getSort(sort), [ Video['sequelize'].models.Tag, 'name', 'ASC' ] ], |
feb4bdfd C |
735 | include: [ |
736 | { | |
e02643f3 C |
737 | model: Video['sequelize'].models.Author, |
738 | include: [ { model: Video['sequelize'].models.Pod, required: false } ] | |
7920c273 | 739 | }, |
93e1258c C |
740 | Video['sequelize'].models.Tag, |
741 | Video['sequelize'].models.VideoFile | |
198b205c | 742 | ], |
e02643f3 | 743 | where: createBaseVideosWhere() |
feb4bdfd C |
744 | } |
745 | ||
6fcd19ba C |
746 | return Video.findAndCountAll(query).then(({ rows, count }) => { |
747 | return { | |
748 | data: rows, | |
749 | total: count | |
750 | } | |
feb4bdfd | 751 | }) |
aaf61f38 C |
752 | } |
753 | ||
0a6658fd | 754 | loadByHostAndUUID = function (fromHost: string, uuid: string) { |
feb4bdfd C |
755 | const query = { |
756 | where: { | |
0a6658fd | 757 | uuid |
feb4bdfd C |
758 | }, |
759 | include: [ | |
93e1258c C |
760 | { |
761 | model: Video['sequelize'].models.VideoFile | |
762 | }, | |
feb4bdfd | 763 | { |
e02643f3 | 764 | model: Video['sequelize'].models.Author, |
feb4bdfd C |
765 | include: [ |
766 | { | |
e02643f3 | 767 | model: Video['sequelize'].models.Pod, |
7920c273 | 768 | required: true, |
feb4bdfd C |
769 | where: { |
770 | host: fromHost | |
771 | } | |
772 | } | |
773 | ] | |
774 | } | |
775 | ] | |
776 | } | |
aaf61f38 | 777 | |
6fcd19ba | 778 | return Video.findOne(query) |
aaf61f38 C |
779 | } |
780 | ||
6fcd19ba | 781 | listOwnedAndPopulateAuthorAndTags = function () { |
feb4bdfd C |
782 | const query = { |
783 | where: { | |
0a6658fd | 784 | remote: false |
feb4bdfd | 785 | }, |
93e1258c C |
786 | include: [ |
787 | Video['sequelize'].models.VideoFile, | |
788 | Video['sequelize'].models.Author, | |
789 | Video['sequelize'].models.Tag | |
790 | ] | |
feb4bdfd C |
791 | } |
792 | ||
6fcd19ba | 793 | return Video.findAll(query) |
aaf61f38 C |
794 | } |
795 | ||
6fcd19ba | 796 | listOwnedByAuthor = function (author: string) { |
feb4bdfd C |
797 | const query = { |
798 | where: { | |
0a6658fd | 799 | remote: false |
feb4bdfd C |
800 | }, |
801 | include: [ | |
93e1258c C |
802 | { |
803 | model: Video['sequelize'].models.VideoFile | |
804 | }, | |
feb4bdfd | 805 | { |
e02643f3 | 806 | model: Video['sequelize'].models.Author, |
feb4bdfd C |
807 | where: { |
808 | name: author | |
809 | } | |
810 | } | |
811 | ] | |
812 | } | |
9bd26629 | 813 | |
6fcd19ba | 814 | return Video.findAll(query) |
aaf61f38 C |
815 | } |
816 | ||
0a6658fd | 817 | load = function (id: number) { |
6fcd19ba | 818 | return Video.findById(id) |
feb4bdfd C |
819 | } |
820 | ||
0a6658fd C |
821 | loadByUUID = function (uuid: string) { |
822 | const query = { | |
823 | where: { | |
824 | uuid | |
93e1258c C |
825 | }, |
826 | include: [ Video['sequelize'].models.VideoFile ] | |
0a6658fd C |
827 | } |
828 | return Video.findOne(query) | |
829 | } | |
830 | ||
831 | loadAndPopulateAuthor = function (id: number) { | |
feb4bdfd | 832 | const options = { |
93e1258c | 833 | include: [ Video['sequelize'].models.VideoFile, Video['sequelize'].models.Author ] |
feb4bdfd C |
834 | } |
835 | ||
6fcd19ba | 836 | return Video.findById(id, options) |
feb4bdfd C |
837 | } |
838 | ||
0a6658fd | 839 | loadAndPopulateAuthorAndPodAndTags = function (id: number) { |
feb4bdfd C |
840 | const options = { |
841 | include: [ | |
842 | { | |
e02643f3 C |
843 | model: Video['sequelize'].models.Author, |
844 | include: [ { model: Video['sequelize'].models.Pod, required: false } ] | |
7920c273 | 845 | }, |
93e1258c C |
846 | Video['sequelize'].models.Tag, |
847 | Video['sequelize'].models.VideoFile | |
feb4bdfd C |
848 | ] |
849 | } | |
850 | ||
6fcd19ba | 851 | return Video.findById(id, options) |
aaf61f38 C |
852 | } |
853 | ||
0a6658fd C |
854 | loadByUUIDAndPopulateAuthorAndPodAndTags = function (uuid: string) { |
855 | const options = { | |
856 | where: { | |
857 | uuid | |
858 | }, | |
859 | include: [ | |
860 | { | |
861 | model: Video['sequelize'].models.Author, | |
862 | include: [ { model: Video['sequelize'].models.Pod, required: false } ] | |
863 | }, | |
93e1258c C |
864 | Video['sequelize'].models.Tag, |
865 | Video['sequelize'].models.VideoFile | |
0a6658fd C |
866 | ] |
867 | } | |
868 | ||
869 | return Video.findOne(options) | |
870 | } | |
871 | ||
6fcd19ba | 872 | searchAndPopulateAuthorAndPodAndTags = function (value: string, field: string, start: number, count: number, sort: string) { |
e6d4b0ff | 873 | const podInclude: Sequelize.IncludeOptions = { |
e02643f3 | 874 | model: Video['sequelize'].models.Pod, |
7920c273 | 875 | required: false |
feb4bdfd | 876 | } |
7920c273 | 877 | |
e6d4b0ff | 878 | const authorInclude: Sequelize.IncludeOptions = { |
e02643f3 | 879 | model: Video['sequelize'].models.Author, |
feb4bdfd C |
880 | include: [ |
881 | podInclude | |
882 | ] | |
883 | } | |
884 | ||
e6d4b0ff | 885 | const tagInclude: Sequelize.IncludeOptions = { |
e02643f3 | 886 | model: Video['sequelize'].models.Tag |
7920c273 C |
887 | } |
888 | ||
93e1258c C |
889 | const videoFileInclude: Sequelize.IncludeOptions = { |
890 | model: Video['sequelize'].models.VideoFile | |
891 | } | |
892 | ||
556ddc31 | 893 | const query: Sequelize.FindOptions<VideoAttributes> = { |
e02643f3 C |
894 | distinct: true, |
895 | where: createBaseVideosWhere(), | |
feb4bdfd C |
896 | offset: start, |
897 | limit: count, | |
e02643f3 | 898 | order: [ getSort(sort), [ Video['sequelize'].models.Tag, 'name', 'ASC' ] ] |
feb4bdfd C |
899 | } |
900 | ||
aaf61f38 | 901 | // Make an exact search with the magnet |
55723d16 | 902 | if (field === 'magnetUri') { |
93e1258c C |
903 | videoFileInclude.where = { |
904 | infoHash: magnetUtil.decode(value).infoHash | |
905 | } | |
55723d16 | 906 | } else if (field === 'tags') { |
e02643f3 | 907 | const escapedValue = Video['sequelize'].escape('%' + value + '%') |
e6d4b0ff | 908 | query.where['id'].$in = Video['sequelize'].literal( |
6fcd19ba C |
909 | `(SELECT "VideoTags"."videoId" |
910 | FROM "Tags" | |
911 | INNER JOIN "VideoTags" ON "Tags"."id" = "VideoTags"."tagId" | |
18c8e945 | 912 | WHERE name ILIKE ${escapedValue} |
6fcd19ba | 913 | )` |
198b205c | 914 | ) |
7920c273 C |
915 | } else if (field === 'host') { |
916 | // FIXME: Include our pod? (not stored in the database) | |
917 | podInclude.where = { | |
918 | host: { | |
18c8e945 | 919 | $iLike: '%' + value + '%' |
feb4bdfd | 920 | } |
feb4bdfd | 921 | } |
7920c273 | 922 | podInclude.required = true |
feb4bdfd | 923 | } else if (field === 'author') { |
7920c273 C |
924 | authorInclude.where = { |
925 | name: { | |
18c8e945 | 926 | $iLike: '%' + value + '%' |
feb4bdfd C |
927 | } |
928 | } | |
7920c273 C |
929 | |
930 | // authorInclude.or = true | |
aaf61f38 | 931 | } else { |
feb4bdfd | 932 | query.where[field] = { |
18c8e945 | 933 | $iLike: '%' + value + '%' |
feb4bdfd | 934 | } |
aaf61f38 C |
935 | } |
936 | ||
7920c273 | 937 | query.include = [ |
93e1258c | 938 | authorInclude, tagInclude, videoFileInclude |
7920c273 C |
939 | ] |
940 | ||
6fcd19ba C |
941 | return Video.findAndCountAll(query).then(({ rows, count }) => { |
942 | return { | |
943 | data: rows, | |
944 | total: count | |
945 | } | |
feb4bdfd | 946 | }) |
aaf61f38 C |
947 | } |
948 | ||
aaf61f38 C |
949 | // --------------------------------------------------------------------------- |
950 | ||
15d4ee04 C |
951 | function createBaseVideosWhere () { |
952 | return { | |
953 | id: { | |
e02643f3 | 954 | $notIn: Video['sequelize'].literal( |
15d4ee04 C |
955 | '(SELECT "BlacklistedVideos"."videoId" FROM "BlacklistedVideos")' |
956 | ) | |
957 | } | |
958 | } | |
959 | } | |
960 | ||
6fcd19ba | 961 | function generateImage (video: VideoInstance, videoPath: string, folder: string, imageName: string, size: string) { |
e6d4b0ff | 962 | const options = { |
f285faa0 | 963 | filename: imageName, |
558d7c23 C |
964 | count: 1, |
965 | folder | |
966 | } | |
aaf61f38 | 967 | |
69818c93 | 968 | if (size) { |
e6d4b0ff | 969 | options['size'] = size |
558d7c23 C |
970 | } |
971 | ||
6fcd19ba C |
972 | return new Promise<string>((res, rej) => { |
973 | ffmpeg(videoPath) | |
974 | .on('error', rej) | |
075f16ca | 975 | .on('end', () => res(imageName)) |
6fcd19ba C |
976 | .thumbnail(options) |
977 | }) | |
aaf61f38 | 978 | } |