X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fmodels%2Fvideo.js;h=daa273845f5610de2b90f85df4a405a2cda2cb59;hb=9e167724f7e933f41d9ea2e1c31772bf4c560a28;hp=14fbe2f719ab2ca79dafdf28b3d2235217ce52e7;hpb=7b1f49de22c40ae121ddb3c399b2540ba56fd414;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/models/video.js b/server/models/video.js index 14fbe2f71..daa273845 100644 --- a/server/models/video.js +++ b/server/models/video.js @@ -20,7 +20,6 @@ const customVideosValidators = require('../helpers/custom-validators').videos // --------------------------------------------------------------------------- module.exports = function (sequelize, DataTypes) { - // TODO: add indexes on searchable columns const Video = sequelize.define('Video', { id: { @@ -81,6 +80,15 @@ module.exports = function (sequelize, DataTypes) { if (res === false) throw new Error('Video duration is not valid.') } } + }, + views: { + type: DataTypes.INTEGER, + allowNull: false, + defaultValue: 0, + validate: { + min: 0, + isInt: true + } } }, { @@ -102,6 +110,9 @@ module.exports = function (sequelize, DataTypes) { }, { fields: [ 'infoHash' ] + }, + { + fields: [ 'views' ] } ], classMethods: { @@ -111,10 +122,10 @@ module.exports = function (sequelize, DataTypes) { getDurationFromFile, list, listForApi, - listByHostAndRemoteId, listOwnedAndPopulateAuthorAndTags, listOwnedByAuthor, load, + loadByHostAndRemoteId, loadAndPopulateAuthor, loadAndPopulateAuthorAndPodAndTags, searchAndPopulateAuthorAndPodAndTags @@ -142,7 +153,8 @@ module.exports = function (sequelize, DataTypes) { } function beforeValidate (video, options, next) { - if (video.isOwned()) { + // Put a fake infoHash if it does not exists yet + if (video.isOwned() && !video.infoHash) { // 40 hexa length video.infoHash = '0123456789abcdef0123456789abcdef01234567' } @@ -157,8 +169,7 @@ function beforeCreate (video, options, next) { const videoPath = pathUtils.join(constants.CONFIG.STORAGE.VIDEOS_DIR, video.getVideoFilename()) tasks.push( - // TODO: refractoring - function (callback) { + function createVideoTorrent (callback) { const options = { announceList: [ [ constants.CONFIG.WEBSERVER.WS + '://' + constants.CONFIG.WEBSERVER.HOSTNAME + ':' + constants.CONFIG.WEBSERVER.PORT + '/tracker/socket' ] @@ -171,7 +182,8 @@ function beforeCreate (video, options, next) { createTorrent(videoPath, options, function (err, torrent) { if (err) return callback(err) - fs.writeFile(constants.CONFIG.STORAGE.TORRENTS_DIR + video.getTorrentName(), torrent, function (err) { + const filePath = pathUtils.join(constants.CONFIG.STORAGE.TORRENTS_DIR, video.getTorrentName()) + fs.writeFile(filePath, torrent, function (err) { if (err) return callback(err) const parsedTorrent = parseTorrent(torrent) @@ -180,10 +192,12 @@ function beforeCreate (video, options, next) { }) }) }, - function (callback) { + + function createVideoThumbnail (callback) { createThumbnail(video, videoPath, callback) }, - function (callback) { + + function createVIdeoPreview (callback) { createPreview(video, videoPath, callback) } ) @@ -205,21 +219,20 @@ function afterDestroy (video, options, next) { if (video.isOwned()) { tasks.push( - function (callback) { + function removeVideoFile (callback) { removeFile(video, callback) }, - function (callback) { + function removeVideoTorrent (callback) { removeTorrent(video, callback) }, - function (callback) { + function removeVideoPreview (callback) { removePreview(video, callback) }, - function (callback) { + function removeVideoToFriends (callback) { const params = { - name: video.name, remoteId: video.id } @@ -249,6 +262,14 @@ function associate (models) { through: models.VideoTag, onDelete: 'cascade' }) + + this.hasMany(models.VideoAbuse, { + foreignKey: { + name: 'videoId', + allowNull: false + }, + onDelete: 'cascade' + }) } function generateMagnetUri () { @@ -327,9 +348,11 @@ function toFormatedJSON () { magnetUri: this.generateMagnetUri(), author: this.Author.name, duration: this.duration, + views: this.views, tags: map(this.Tags, 'name'), - thumbnailPath: constants.STATIC_PATHS.THUMBNAILS + '/' + this.getThumbnailName(), - createdAt: this.createdAt + thumbnailPath: pathUtils.join(constants.STATIC_PATHS.THUMBNAILS, this.getThumbnailName()), + createdAt: this.createdAt, + updatedAt: this.updatedAt } return json @@ -356,6 +379,7 @@ function toAddRemoteJSON (callback) { thumbnailData: thumbnailData.toString('binary'), tags: map(self.Tags, 'name'), createdAt: self.createdAt, + updatedAt: self.updatedAt, extname: self.extname } @@ -373,6 +397,7 @@ function toUpdateRemoteJSON (callback) { duration: this.duration, tags: map(this.Tags, 'name'), createdAt: this.createdAt, + updatedAt: this.updatedAt, extname: this.extname } @@ -385,7 +410,7 @@ function generateThumbnailFromData (video, thumbnailData, callback) { // Creating the thumbnail for a remote video const thumbnailName = video.getThumbnailName() - const thumbnailPath = constants.CONFIG.STORAGE.THUMBNAILS_DIR + thumbnailName + const thumbnailPath = pathUtils.join(constants.CONFIG.STORAGE.THUMBNAILS_DIR, thumbnailName) fs.writeFile(thumbnailPath, Buffer.from(thumbnailData, 'binary'), function (err) { if (err) return callback(err) @@ -402,7 +427,7 @@ function getDurationFromFile (videoPath, callback) { } function list (callback) { - return this.find().asCallback() + return this.findAll().asCallback(callback) } function listForApi (start, count, sort, callback) { @@ -428,7 +453,7 @@ function listForApi (start, count, sort, callback) { }) } -function listByHostAndRemoteId (fromHost, remoteId, callback) { +function loadByHostAndRemoteId (fromHost, remoteId, callback) { const query = { where: { remoteId: remoteId @@ -449,7 +474,7 @@ function listByHostAndRemoteId (fromHost, remoteId, callback) { ] } - return this.findAll(query).asCallback(callback) + return this.findOne(query).asCallback(callback) } function listOwnedAndPopulateAuthorAndTags (callback) { @@ -586,15 +611,18 @@ function searchAndPopulateAuthorAndPodAndTags (value, field, start, count, sort, // --------------------------------------------------------------------------- function removeThumbnail (video, callback) { - fs.unlink(constants.CONFIG.STORAGE.THUMBNAILS_DIR + video.getThumbnailName(), callback) + const thumbnailPath = pathUtils.join(constants.CONFIG.STORAGE.THUMBNAILS_DIR, video.getThumbnailName()) + fs.unlink(thumbnailPath, callback) } function removeFile (video, callback) { - fs.unlink(constants.CONFIG.STORAGE.VIDEOS_DIR + video.getVideoFilename(), callback) + const filePath = pathUtils.join(constants.CONFIG.STORAGE.VIDEOS_DIR, video.getVideoFilename()) + fs.unlink(filePath, callback) } function removeTorrent (video, callback) { - fs.unlink(constants.CONFIG.STORAGE.TORRENTS_DIR + video.getTorrentName(), callback) + const torrenPath = pathUtils.join(constants.CONFIG.STORAGE.TORRENTS_DIR, video.getTorrentName()) + fs.unlink(torrenPath, callback) } function removePreview (video, callback) {