// ---------------------------------------------------------------------------
module.exports = function (sequelize, DataTypes) {
- // TODO: add indexes on searchable columns
const Video = sequelize.define('Video',
{
id: {
if (res === false) throw new Error('Video duration is not valid.')
}
}
+ },
+ views: {
+ type: DataTypes.INTEGER,
+ allowNull: false,
+ defaultValue: 0,
+ validate: {
+ min: 0,
+ isInt: true
+ }
}
},
{
},
{
fields: [ 'infoHash' ]
+ },
+ {
+ fields: [ 'views' ]
}
],
classMethods: {
getDurationFromFile,
list,
listForApi,
- listByHostAndRemoteId,
listOwnedAndPopulateAuthorAndTags,
listOwnedByAuthor,
load,
+ loadByHostAndRemoteId,
loadAndPopulateAuthor,
loadAndPopulateAuthorAndPodAndTags,
searchAndPopulateAuthorAndPodAndTags
}
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'
}
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' ]
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)
})
})
},
- function (callback) {
+
+ function createVideoThumbnail (callback) {
createThumbnail(video, videoPath, callback)
},
- function (callback) {
+
+ function createVIdeoPreview (callback) {
createPreview(video, videoPath, callback)
}
)
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
}
through: models.VideoTag,
onDelete: 'cascade'
})
+
+ this.hasMany(models.VideoAbuse, {
+ foreignKey: {
+ name: 'videoId',
+ allowNull: false
+ },
+ onDelete: 'cascade'
+ })
}
function generateMagnetUri () {
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
thumbnailData: thumbnailData.toString('binary'),
tags: map(self.Tags, 'name'),
createdAt: self.createdAt,
+ updatedAt: self.updatedAt,
extname: self.extname
}
duration: this.duration,
tags: map(this.Tags, 'name'),
createdAt: this.createdAt,
+ updatedAt: this.updatedAt,
extname: this.extname
}
// 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)
}
function list (callback) {
- return this.find().asCallback()
+ return this.findAll().asCallback(callback)
}
function listForApi (start, count, sort, callback) {
})
}
-function listByHostAndRemoteId (fromHost, remoteId, callback) {
+function loadByHostAndRemoteId (fromHost, remoteId, callback) {
const query = {
where: {
remoteId: remoteId
]
}
- return this.findAll(query).asCallback(callback)
+ return this.findOne(query).asCallback(callback)
}
function listOwnedAndPopulateAuthorAndTags (callback) {
// ---------------------------------------------------------------------------
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) {