X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fmodels%2Fvideo.js;h=330067cdfa6e892776692f1787fd70efe22f2ea4;hb=437cf8b531652a4b101ec279dea7661e8dfb8cda;hp=0da2cb8ab5f5a9056236bfa785d662788ca62997;hpb=41b5da1d8cb41f5c49f0e0a01a54106c9a5925dd;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/models/video.js b/server/models/video.js index 0da2cb8ab..330067cdf 100644 --- a/server/models/video.js +++ b/server/models/video.js @@ -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 + extname: self.extname } 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) { @@ -340,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,