]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/models/video.js
Server: fix thumbnail in remote videos
[github/Chocobozzz/PeerTube.git] / server / models / video.js
index 19136ba2544fc96897e51f4fdcd0f2ab3f2918fd..527652230c66311eaced4c8fc197f112cd84ef7d 100644 (file)
@@ -28,10 +28,9 @@ const VideoSchema = mongoose.Schema({
   magnet: {
     infoHash: String
   },
-  podUrl: String,
+  podHost: String,
   author: String,
   duration: Number,
-  thumbnail: String,
   tags: [ String ],
   createdDate: {
     type: Date,
@@ -41,14 +40,9 @@ const VideoSchema = mongoose.Schema({
 
 VideoSchema.path('name').validate(customVideosValidators.isVideoNameValid)
 VideoSchema.path('description').validate(customVideosValidators.isVideoDescriptionValid)
-VideoSchema.path('podUrl').validate(customVideosValidators.isVideoPodUrlValid)
+VideoSchema.path('podHost').validate(customVideosValidators.isVideoPodHostValid)
 VideoSchema.path('author').validate(customVideosValidators.isVideoAuthorValid)
 VideoSchema.path('duration').validate(customVideosValidators.isVideoDurationValid)
-// The tumbnail can be the path or the data in base 64
-// The pre save hook will convert the base 64 data in a file on disk and replace the thumbnail key by the filename
-VideoSchema.path('thumbnail').validate(function (value) {
-  return customVideosValidators.isVideoThumbnailValid(value) || customVideosValidators.isVideoThumbnail64Valid(value)
-})
 VideoSchema.path('tags').validate(customVideosValidators.isVideoTagsValid)
 
 VideoSchema.methods = {
@@ -63,10 +57,11 @@ VideoSchema.methods = {
 }
 
 VideoSchema.statics = {
+  generateThumbnailFromBase64,
   getDurationFromFile,
   listForApi,
-  listByUrlAndRemoteId,
-  listByUrl,
+  listByHostAndRemoteId,
+  listByHost,
   listOwned,
   listOwnedByAuthor,
   listRemotes,
@@ -107,7 +102,7 @@ VideoSchema.pre('save', function (next) {
 
   if (video.isOwned()) {
     const videoPath = pathUtils.join(constants.CONFIG.STORAGE.VIDEOS_DIR, video.getVideoFilename())
-    this.podUrl = constants.CONFIG.WEBSERVER.HOSTNAME + ':' + constants.CONFIG.WEBSERVER.PORT
+    this.podHost = constants.CONFIG.WEBSERVER.HOST
 
     tasks.push(
       // TODO: refractoring
@@ -130,7 +125,6 @@ VideoSchema.pre('save', function (next) {
             const parsedTorrent = parseTorrent(torrent)
             video.magnet.infoHash = parsedTorrent.infoHash
 
-            console.log(parsedTorrent)
             callback(null)
           })
         })
@@ -143,10 +137,10 @@ VideoSchema.pre('save', function (next) {
       }
     )
 
-    parallel(tasks, next)
-  } else {
-    generateThumbnailFromBase64(video, video.thumbnail, next)
+    return parallel(tasks, next)
   }
+
+  return next()
 })
 
 mongoose.model('Video', VideoSchema)
@@ -160,8 +154,8 @@ function generateMagnetUri () {
     baseUrlHttp = constants.CONFIG.WEBSERVER.URL
     baseUrlWs = constants.CONFIG.WEBSERVER.WS + '://' + constants.CONFIG.WEBSERVER.HOSTNAME + ':' + constants.CONFIG.WEBSERVER.PORT
   } else {
-    baseUrlHttp = constants.REMOTE_SCHEME.HTTP + this.podUrl
-    baseUrlWs = constants.REMOTE_SCHEME.WS + this.podUrl
+    baseUrlHttp = constants.REMOTE_SCHEME.HTTP + '://' + this.podHost
+    baseUrlWs = constants.REMOTE_SCHEME.WS + this.podHost
   }
 
   const xs = baseUrlHttp + constants.STATIC_PATHS.TORRENTS + this.getTorrentName()
@@ -215,7 +209,7 @@ function toFormatedJSON () {
     id: this._id,
     name: this.name,
     description: this.description,
-    podUrl: this.podUrl,
+    podHost: this.podHost,
     isLocal: this.isOwned(),
     magnetUri: this.generateMagnetUri(),
     author: this.author,
@@ -249,7 +243,7 @@ function toRemoteJSON (callback) {
       thumbnailBase64: new Buffer(thumbnailData).toString('base64'),
       tags: self.tags,
       createdDate: self.createdDate,
-      podUrl: self.podUrl
+      podHost: self.podHost
     }
 
     return callback(null, remoteVideo)
@@ -258,6 +252,18 @@ function toRemoteJSON (callback) {
 
 // ------------------------------ STATICS ------------------------------
 
+function generateThumbnailFromBase64 (video, thumbnailData, callback) {
+  // Creating the thumbnail for a remote video
+
+  const thumbnailName = video.getThumbnailName()
+  const thumbnailPath = constants.CONFIG.STORAGE.THUMBNAILS_DIR + thumbnailName
+  fs.writeFile(thumbnailPath, thumbnailData, { encoding: 'base64' }, function (err) {
+    if (err) return callback(err)
+
+    return callback(null, thumbnailName)
+  })
+}
+
 function getDurationFromFile (videoPath, callback) {
   ffmpeg.ffprobe(videoPath, function (err, metadata) {
     if (err) return callback(err)
@@ -271,12 +277,12 @@ function listForApi (start, count, sort, callback) {
   return modelUtils.listForApiWithCount.call(this, query, start, count, sort, callback)
 }
 
-function listByUrlAndRemoteId (fromUrl, remoteId, callback) {
-  this.find({ podUrl: fromUrl, remoteId: remoteId }, callback)
+function listByHostAndRemoteId (fromHost, remoteId, callback) {
+  this.find({ podHost: fromHost, remoteId: remoteId }, callback)
 }
 
-function listByUrl (fromUrl, callback) {
-  this.find({ podUrl: fromUrl }, callback)
+function listByHost (fromHost, callback) {
+  this.find({ podHost: fromHost }, callback)
 }
 
 function listOwned (callback) {
@@ -299,7 +305,12 @@ function load (id, callback) {
 function search (value, field, start, count, sort, callback) {
   const query = {}
   // Make an exact search with the magnet
-  if (field === 'magnetUri' || field === 'tags') {
+  if (field === 'magnetUri') {
+    const infoHash = magnetUtil.decode(value).infoHash
+    query.magnet = {
+      infoHash
+    }
+  } else if (field === 'tags') {
     query[field] = value
   } else {
     query[field] = new RegExp(value, 'i')
@@ -335,18 +346,6 @@ function createThumbnail (video, videoPath, callback) {
   generateImage(video, videoPath, constants.CONFIG.STORAGE.THUMBNAILS_DIR, video.getThumbnailName(), constants.THUMBNAILS_SIZE, callback)
 }
 
-function generateThumbnailFromBase64 (video, thumbnailData, callback) {
-  // Creating the thumbnail for this remote video)
-
-  const thumbnailName = video.getThumbnailName()
-  const thumbnailPath = constants.CONFIG.STORAGE.THUMBNAILS_DIR + thumbnailName
-  fs.writeFile(thumbnailPath, thumbnailData, { encoding: 'base64' }, function (err) {
-    if (err) return callback(err)
-
-    return callback(null, thumbnailName)
-  })
-}
-
 function generateImage (video, videoPath, folder, imageName, size, callback) {
   const options = {
     filename: imageName,