]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/models/video.js
Server: make a basic "quick and dirty update" for videos
[github/Chocobozzz/PeerTube.git] / server / models / video.js
index 0e84e89867f722d88e0c6d236986b4faa6646d15..daa273845f5610de2b90f85df4a405a2cda2cb59 100644 (file)
@@ -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
@@ -127,7 +138,8 @@ module.exports = function (sequelize, DataTypes) {
         getTorrentName,
         isOwned,
         toFormatedJSON,
-        toRemoteJSON
+        toAddRemoteJSON,
+        toUpdateRemoteJSON
       },
       hooks: {
         beforeValidate,
@@ -141,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'
   }
@@ -156,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' ]
@@ -170,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)
@@ -179,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)
       }
     )
@@ -204,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
         }
 
@@ -248,6 +262,14 @@ function associate (models) {
     through: models.VideoTag,
     onDelete: 'cascade'
   })
+
+  this.hasMany(models.VideoAbuse, {
+    foreignKey: {
+      name: 'videoId',
+      allowNull: false
+    },
+    onDelete: 'cascade'
+  })
 }
 
 function generateMagnetUri () {
@@ -326,15 +348,17 @@ 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
 }
 
-function toRemoteJSON (callback) {
+function toAddRemoteJSON (callback) {
   const self = this
 
   // Get thumbnail data to send to the other pod
@@ -355,6 +379,7 @@ function toRemoteJSON (callback) {
       thumbnailData: thumbnailData.toString('binary'),
       tags: map(self.Tags, 'name'),
       createdAt: self.createdAt,
+      updatedAt: self.updatedAt,
       extname: self.extname
     }
 
@@ -362,13 +387,30 @@ function toRemoteJSON (callback) {
   })
 }
 
+function toUpdateRemoteJSON (callback) {
+  const json = {
+    name: this.name,
+    description: this.description,
+    infoHash: this.infoHash,
+    remoteId: this.id,
+    author: this.Author.name,
+    duration: this.duration,
+    tags: map(this.Tags, 'name'),
+    createdAt: this.createdAt,
+    updatedAt: this.updatedAt,
+    extname: this.extname
+  }
+
+  return json
+}
+
 // ------------------------------ STATICS ------------------------------
 
 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)
 
@@ -385,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) {
@@ -411,7 +453,7 @@ function listForApi (start, count, sort, callback) {
   })
 }
 
-function listByHostAndRemoteId (fromHost, remoteId, callback) {
+function loadByHostAndRemoteId (fromHost, remoteId, callback) {
   const query = {
     where: {
       remoteId: remoteId
@@ -432,7 +474,7 @@ function listByHostAndRemoteId (fromHost, remoteId, callback) {
     ]
   }
 
-  return this.findAll(query).asCallback(callback)
+  return this.findOne(query).asCallback(callback)
 }
 
 function listOwnedAndPopulateAuthorAndTags (callback) {
@@ -569,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) {