Server: use _id for image and video files. Add remoteId field
This commit is contained in:
parent
6a94a109b4
commit
558d7c2385
|
@ -64,7 +64,7 @@ function addRemoteVideo (videoToCreateData, callback) {
|
||||||
|
|
||||||
function removeRemoteVideo (videoToRemoveData, fromUrl, callback) {
|
function removeRemoteVideo (videoToRemoveData, fromUrl, callback) {
|
||||||
// We need the list because we have to remove some other stuffs (thumbnail etc)
|
// We need the list because we have to remove some other stuffs (thumbnail etc)
|
||||||
Video.listByUrlAndMagnet(fromUrl, videoToRemoveData.magnetUri, function (err, videosList) {
|
Video.listByUrlAndRemoteId(fromUrl, videoToRemoveData.remoteId, function (err, videosList) {
|
||||||
if (err) {
|
if (err) {
|
||||||
logger.error('Cannot list videos from url and magnets.', { error: err })
|
logger.error('Cannot list videos from url and magnets.', { error: err })
|
||||||
return callback(err)
|
return callback(err)
|
||||||
|
|
|
@ -1,8 +1,10 @@
|
||||||
'use strict'
|
'use strict'
|
||||||
|
|
||||||
const express = require('express')
|
const express = require('express')
|
||||||
|
const fs = require('fs')
|
||||||
const mongoose = require('mongoose')
|
const mongoose = require('mongoose')
|
||||||
const multer = require('multer')
|
const multer = require('multer')
|
||||||
|
const path = require('path')
|
||||||
const waterfall = require('async/waterfall')
|
const waterfall = require('async/waterfall')
|
||||||
|
|
||||||
const constants = require('../../initializers/constants')
|
const constants = require('../../initializers/constants')
|
||||||
|
@ -85,11 +87,14 @@ function addVideo (req, res, next) {
|
||||||
const videoInfos = req.body
|
const videoInfos = req.body
|
||||||
|
|
||||||
waterfall([
|
waterfall([
|
||||||
|
function createVideoObject (callback) {
|
||||||
|
const id = mongoose.Types.ObjectId()
|
||||||
|
|
||||||
function insertIntoDB (callback) {
|
|
||||||
const videoData = {
|
const videoData = {
|
||||||
|
_id: id,
|
||||||
name: videoInfos.name,
|
name: videoInfos.name,
|
||||||
filename: videoFile.filename,
|
remoteId: null,
|
||||||
|
extname: path.extname(videoFile.filename),
|
||||||
description: videoInfos.description,
|
description: videoInfos.description,
|
||||||
author: res.locals.oauth.token.user.username,
|
author: res.locals.oauth.token.user.username,
|
||||||
duration: videoFile.duration,
|
duration: videoFile.duration,
|
||||||
|
@ -97,7 +102,23 @@ function addVideo (req, res, next) {
|
||||||
}
|
}
|
||||||
|
|
||||||
const video = new Video(videoData)
|
const video = new Video(videoData)
|
||||||
video.save(function (err, video) {
|
|
||||||
|
return callback(null, video)
|
||||||
|
},
|
||||||
|
|
||||||
|
// Set the videoname the same as the MongoDB id
|
||||||
|
function renameVideoFile (video, callback) {
|
||||||
|
const videoDir = constants.CONFIG.STORAGE.VIDEOS_DIR
|
||||||
|
const source = path.join(videoDir, videoFile.filename)
|
||||||
|
const destination = path.join(videoDir, video.getFilename())
|
||||||
|
|
||||||
|
fs.rename(source, destination, function (err) {
|
||||||
|
return callback(err, video)
|
||||||
|
})
|
||||||
|
},
|
||||||
|
|
||||||
|
function insertIntoDB (video, callback) {
|
||||||
|
video.save(function (err, video, videoFile) {
|
||||||
// Assert there are only one argument sent to the next function (video)
|
// Assert there are only one argument sent to the next function (video)
|
||||||
return callback(err, video)
|
return callback(err, video)
|
||||||
})
|
})
|
||||||
|
@ -164,7 +185,7 @@ function removeVideo (req, res, next) {
|
||||||
function sendInformationToFriends (video, callback) {
|
function sendInformationToFriends (video, callback) {
|
||||||
const params = {
|
const params = {
|
||||||
name: video.name,
|
name: video.name,
|
||||||
magnetUri: video.magnetUri
|
remoteId: video._id
|
||||||
}
|
}
|
||||||
|
|
||||||
friends.removeVideoToFriends(params)
|
friends.removeVideoToFriends(params)
|
||||||
|
|
|
@ -35,12 +35,13 @@ function isEachRemoteVideosValid (requests) {
|
||||||
isVideoNameValid(video.name) &&
|
isVideoNameValid(video.name) &&
|
||||||
isVideoPodUrlValid(video.podUrl) &&
|
isVideoPodUrlValid(video.podUrl) &&
|
||||||
isVideoTagsValid(video.tags) &&
|
isVideoTagsValid(video.tags) &&
|
||||||
isVideoThumbnail64Valid(video.thumbnailBase64)
|
isVideoThumbnail64Valid(video.thumbnailBase64) &&
|
||||||
|
isVideoRemoteIdValid(video.remoteId)
|
||||||
) ||
|
) ||
|
||||||
(
|
(
|
||||||
isRequestTypeRemoveValid(request.type) &&
|
isRequestTypeRemoveValid(request.type) &&
|
||||||
isVideoNameValid(video.name) &&
|
isVideoNameValid(video.name) &&
|
||||||
isVideoMagnetUriValid(video.magnetUri)
|
isVideoRemoteIdValid(video.remoteId)
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -92,6 +93,10 @@ function isVideoThumbnail64Valid (value) {
|
||||||
validator.isByteLength(value, VIDEOS_CONSTRAINTS_FIELDS.THUMBNAIL64)
|
validator.isByteLength(value, VIDEOS_CONSTRAINTS_FIELDS.THUMBNAIL64)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function isVideoRemoteIdValid (value) {
|
||||||
|
return validator.isMongoId(value)
|
||||||
|
}
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
module.exports = videosValidators
|
module.exports = videosValidators
|
||||||
|
|
|
@ -13,14 +13,17 @@ const constants = require('../initializers/constants')
|
||||||
const customVideosValidators = require('../helpers/custom-validators').videos
|
const customVideosValidators = require('../helpers/custom-validators').videos
|
||||||
const logger = require('../helpers/logger')
|
const logger = require('../helpers/logger')
|
||||||
const modelUtils = require('./utils')
|
const modelUtils = require('./utils')
|
||||||
const utils = require('../helpers/utils')
|
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
// TODO: add indexes on searchable columns
|
// TODO: add indexes on searchable columns
|
||||||
const VideoSchema = mongoose.Schema({
|
const VideoSchema = mongoose.Schema({
|
||||||
name: String,
|
name: String,
|
||||||
filename: String,
|
extname: {
|
||||||
|
type: String,
|
||||||
|
enum: [ '.mp4', '.webm', '.ogv' ]
|
||||||
|
},
|
||||||
|
remoteId: mongoose.Schema.Types.ObjectId,
|
||||||
description: String,
|
description: String,
|
||||||
magnetUri: String,
|
magnetUri: String,
|
||||||
podUrl: String,
|
podUrl: String,
|
||||||
|
@ -48,6 +51,9 @@ VideoSchema.path('thumbnail').validate(function (value) {
|
||||||
VideoSchema.path('tags').validate(customVideosValidators.isVideoTagsValid)
|
VideoSchema.path('tags').validate(customVideosValidators.isVideoTagsValid)
|
||||||
|
|
||||||
VideoSchema.methods = {
|
VideoSchema.methods = {
|
||||||
|
getFilename,
|
||||||
|
getJPEGName,
|
||||||
|
getTorrentName,
|
||||||
isOwned,
|
isOwned,
|
||||||
toFormatedJSON,
|
toFormatedJSON,
|
||||||
toRemoteJSON
|
toRemoteJSON
|
||||||
|
@ -56,7 +62,7 @@ VideoSchema.methods = {
|
||||||
VideoSchema.statics = {
|
VideoSchema.statics = {
|
||||||
getDurationFromFile,
|
getDurationFromFile,
|
||||||
listForApi,
|
listForApi,
|
||||||
listByUrlAndMagnet,
|
listByUrlAndRemoteId,
|
||||||
listByUrl,
|
listByUrl,
|
||||||
listOwned,
|
listOwned,
|
||||||
listOwnedByAuthor,
|
listOwnedByAuthor,
|
||||||
|
@ -97,7 +103,7 @@ VideoSchema.pre('save', function (next) {
|
||||||
const tasks = []
|
const tasks = []
|
||||||
|
|
||||||
if (video.isOwned()) {
|
if (video.isOwned()) {
|
||||||
const videoPath = pathUtils.join(constants.CONFIG.STORAGE.VIDEOS_DIR, video.filename)
|
const videoPath = pathUtils.join(constants.CONFIG.STORAGE.VIDEOS_DIR, video.getFilename())
|
||||||
this.podUrl = constants.CONFIG.WEBSERVER.URL
|
this.podUrl = constants.CONFIG.WEBSERVER.URL
|
||||||
|
|
||||||
tasks.push(
|
tasks.push(
|
||||||
|
@ -108,18 +114,18 @@ VideoSchema.pre('save', function (next) {
|
||||||
[ constants.CONFIG.WEBSERVER.WS + '://' + constants.CONFIG.WEBSERVER.HOSTNAME + ':' + constants.CONFIG.WEBSERVER.PORT + '/tracker/socket' ]
|
[ constants.CONFIG.WEBSERVER.WS + '://' + constants.CONFIG.WEBSERVER.HOSTNAME + ':' + constants.CONFIG.WEBSERVER.PORT + '/tracker/socket' ]
|
||||||
],
|
],
|
||||||
urlList: [
|
urlList: [
|
||||||
constants.CONFIG.WEBSERVER.URL + constants.STATIC_PATHS.WEBSEED + video.filename
|
constants.CONFIG.WEBSERVER.URL + constants.STATIC_PATHS.WEBSEED + video.getFilename()
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
||||||
createTorrent(videoPath, options, function (err, torrent) {
|
createTorrent(videoPath, options, function (err, torrent) {
|
||||||
if (err) return callback(err)
|
if (err) return callback(err)
|
||||||
|
|
||||||
fs.writeFile(constants.CONFIG.STORAGE.TORRENTS_DIR + video.filename + '.torrent', torrent, function (err) {
|
fs.writeFile(constants.CONFIG.STORAGE.TORRENTS_DIR + video.getTorrentName(), torrent, function (err) {
|
||||||
if (err) return callback(err)
|
if (err) return callback(err)
|
||||||
|
|
||||||
const parsedTorrent = parseTorrent(torrent)
|
const parsedTorrent = parseTorrent(torrent)
|
||||||
parsedTorrent.xs = video.podUrl + constants.STATIC_PATHS.TORRENTS + video.filename + '.torrent'
|
parsedTorrent.xs = video.podUrl + constants.STATIC_PATHS.TORRENTS + video.getTorrentName()
|
||||||
video.magnetUri = magnet.encode(parsedTorrent)
|
video.magnetUri = magnet.encode(parsedTorrent)
|
||||||
|
|
||||||
callback(null)
|
callback(null)
|
||||||
|
@ -127,28 +133,16 @@ VideoSchema.pre('save', function (next) {
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
function (callback) {
|
function (callback) {
|
||||||
createThumbnail(videoPath, callback)
|
createThumbnail(video, videoPath, callback)
|
||||||
},
|
},
|
||||||
function (callback) {
|
function (callback) {
|
||||||
createPreview(videoPath, callback)
|
createPreview(video, videoPath, callback)
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
parallel(tasks, function (err, results) {
|
parallel(tasks, next)
|
||||||
if (err) return next(err)
|
|
||||||
|
|
||||||
video.thumbnail = results[1]
|
|
||||||
|
|
||||||
return next()
|
|
||||||
})
|
|
||||||
} else {
|
} else {
|
||||||
generateThumbnailFromBase64(video.thumbnail, function (err, thumbnailName) {
|
generateThumbnailFromBase64(video, video.thumbnail, next)
|
||||||
if (err) return next(err)
|
|
||||||
|
|
||||||
video.thumbnail = thumbnailName
|
|
||||||
|
|
||||||
return next()
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -156,8 +150,20 @@ mongoose.model('Video', VideoSchema)
|
||||||
|
|
||||||
// ------------------------------ METHODS ------------------------------
|
// ------------------------------ METHODS ------------------------------
|
||||||
|
|
||||||
|
function getFilename () {
|
||||||
|
return this._id + this.extname
|
||||||
|
}
|
||||||
|
|
||||||
|
function getJPEGName () {
|
||||||
|
return this._id + '.jpg'
|
||||||
|
}
|
||||||
|
|
||||||
|
function getTorrentName () {
|
||||||
|
return this._id + '.torrent'
|
||||||
|
}
|
||||||
|
|
||||||
function isOwned () {
|
function isOwned () {
|
||||||
return this.filename !== null
|
return this.remoteId === null
|
||||||
}
|
}
|
||||||
|
|
||||||
function toFormatedJSON () {
|
function toFormatedJSON () {
|
||||||
|
@ -171,7 +177,7 @@ function toFormatedJSON () {
|
||||||
author: this.author,
|
author: this.author,
|
||||||
duration: this.duration,
|
duration: this.duration,
|
||||||
tags: this.tags,
|
tags: this.tags,
|
||||||
thumbnailPath: constants.STATIC_PATHS.THUMBNAILS + '/' + this.thumbnail,
|
thumbnailPath: constants.STATIC_PATHS.THUMBNAILS + '/' + this.getJPEGName(),
|
||||||
createdDate: this.createdDate
|
createdDate: this.createdDate
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -182,7 +188,8 @@ function toRemoteJSON (callback) {
|
||||||
const self = this
|
const self = this
|
||||||
|
|
||||||
// Convert thumbnail to base64
|
// Convert thumbnail to base64
|
||||||
fs.readFile(pathUtils.join(constants.CONFIG.STORAGE.THUMBNAILS_DIR, this.thumbnail), function (err, thumbnailData) {
|
const thumbnailPath = pathUtils.join(constants.CONFIG.STORAGE.THUMBNAILS_DIR, this.getJPEGName())
|
||||||
|
fs.readFile(thumbnailPath, function (err, thumbnailData) {
|
||||||
if (err) {
|
if (err) {
|
||||||
logger.error('Cannot read the thumbnail of the video')
|
logger.error('Cannot read the thumbnail of the video')
|
||||||
return callback(err)
|
return callback(err)
|
||||||
|
@ -192,7 +199,7 @@ function toRemoteJSON (callback) {
|
||||||
name: self.name,
|
name: self.name,
|
||||||
description: self.description,
|
description: self.description,
|
||||||
magnetUri: self.magnetUri,
|
magnetUri: self.magnetUri,
|
||||||
filename: null,
|
remoteId: self._id,
|
||||||
author: self.author,
|
author: self.author,
|
||||||
duration: self.duration,
|
duration: self.duration,
|
||||||
thumbnailBase64: new Buffer(thumbnailData).toString('base64'),
|
thumbnailBase64: new Buffer(thumbnailData).toString('base64'),
|
||||||
|
@ -220,8 +227,8 @@ function listForApi (start, count, sort, callback) {
|
||||||
return modelUtils.listForApiWithCount.call(this, query, start, count, sort, callback)
|
return modelUtils.listForApiWithCount.call(this, query, start, count, sort, callback)
|
||||||
}
|
}
|
||||||
|
|
||||||
function listByUrlAndMagnet (fromUrl, magnetUri, callback) {
|
function listByUrlAndRemoteId (fromUrl, remoteId, callback) {
|
||||||
this.find({ podUrl: fromUrl, magnetUri: magnetUri }, callback)
|
this.find({ podUrl: fromUrl, remoteId: remoteId }, callback)
|
||||||
}
|
}
|
||||||
|
|
||||||
function listByUrl (fromUrl, callback) {
|
function listByUrl (fromUrl, callback) {
|
||||||
|
@ -229,16 +236,16 @@ function listByUrl (fromUrl, callback) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function listOwned (callback) {
|
function listOwned (callback) {
|
||||||
// If filename is not null this is *our* video
|
// If remoteId is null this is *our* video
|
||||||
this.find({ filename: { $ne: null } }, callback)
|
this.find({ remoteId: null }, callback)
|
||||||
}
|
}
|
||||||
|
|
||||||
function listOwnedByAuthor (author, callback) {
|
function listOwnedByAuthor (author, callback) {
|
||||||
this.find({ filename: { $ne: null }, author: author }, callback)
|
this.find({ remoteId: null, author: author }, callback)
|
||||||
}
|
}
|
||||||
|
|
||||||
function listRemotes (callback) {
|
function listRemotes (callback) {
|
||||||
this.find({ filename: null }, callback)
|
this.find({ remoteId: { $ne: null } }, callback)
|
||||||
}
|
}
|
||||||
|
|
||||||
function load (id, callback) {
|
function load (id, callback) {
|
||||||
|
@ -260,62 +267,61 @@ function search (value, field, start, count, sort, callback) {
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
function removeThumbnail (video, callback) {
|
function removeThumbnail (video, callback) {
|
||||||
fs.unlink(constants.CONFIG.STORAGE.THUMBNAILS_DIR + video.thumbnail, callback)
|
fs.unlink(constants.CONFIG.STORAGE.THUMBNAILS_DIR + video.getJPEGName(), callback)
|
||||||
}
|
}
|
||||||
|
|
||||||
function removeFile (video, callback) {
|
function removeFile (video, callback) {
|
||||||
fs.unlink(constants.CONFIG.STORAGE.VIDEOS_DIR + video.filename, callback)
|
fs.unlink(constants.CONFIG.STORAGE.VIDEOS_DIR + video.getFilename(), callback)
|
||||||
}
|
}
|
||||||
|
|
||||||
function removeTorrent (video, callback) {
|
function removeTorrent (video, callback) {
|
||||||
fs.unlink(constants.CONFIG.STORAGE.TORRENTS_DIR + video.filename + '.torrent', callback)
|
fs.unlink(constants.CONFIG.STORAGE.TORRENTS_DIR + video.getTorrentName(), callback)
|
||||||
}
|
}
|
||||||
|
|
||||||
function removePreview (video, callback) {
|
function removePreview (video, callback) {
|
||||||
// Same name than video thumnail
|
// Same name than video thumnail
|
||||||
// TODO: refractoring
|
// TODO: refractoring
|
||||||
fs.unlink(constants.CONFIG.STORAGE.PREVIEWS_DIR + video.thumbnail, callback)
|
fs.unlink(constants.CONFIG.STORAGE.PREVIEWS_DIR + video.getJPEGName(), callback)
|
||||||
}
|
}
|
||||||
|
|
||||||
function createPreview (videoPath, callback) {
|
function createPreview (video, videoPath, callback) {
|
||||||
const filename = pathUtils.basename(videoPath) + '.jpg'
|
generateImage(video, videoPath, constants.CONFIG.STORAGE.PREVIEWS_DIR, callback)
|
||||||
ffmpeg(videoPath)
|
|
||||||
.on('error', callback)
|
|
||||||
.on('end', function () {
|
|
||||||
callback(null, filename)
|
|
||||||
})
|
|
||||||
.thumbnail({
|
|
||||||
count: 1,
|
|
||||||
folder: constants.CONFIG.STORAGE.PREVIEWS_DIR,
|
|
||||||
filename: filename
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function createThumbnail (videoPath, callback) {
|
function createThumbnail (video, videoPath, callback) {
|
||||||
const filename = pathUtils.basename(videoPath) + '.jpg'
|
generateImage(video, videoPath, constants.CONFIG.STORAGE.THUMBNAILS_DIR, constants.THUMBNAILS_SIZE, callback)
|
||||||
ffmpeg(videoPath)
|
|
||||||
.on('error', callback)
|
|
||||||
.on('end', function () {
|
|
||||||
callback(null, filename)
|
|
||||||
})
|
|
||||||
.thumbnail({
|
|
||||||
count: 1,
|
|
||||||
folder: constants.CONFIG.STORAGE.THUMBNAILS_DIR,
|
|
||||||
size: constants.THUMBNAILS_SIZE,
|
|
||||||
filename: filename
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function generateThumbnailFromBase64 (data, callback) {
|
function generateThumbnailFromBase64 (video, thumbnailData, callback) {
|
||||||
// Creating the thumbnail for this remote video
|
// Creating the thumbnail for this remote video)
|
||||||
utils.generateRandomString(16, function (err, randomString) {
|
|
||||||
|
const thumbnailName = video.getJPEGName()
|
||||||
|
const thumbnailPath = constants.CONFIG.STORAGE.THUMBNAILS_DIR + thumbnailName
|
||||||
|
fs.writeFile(thumbnailPath, thumbnailData, { encoding: 'base64' }, function (err) {
|
||||||
if (err) return callback(err)
|
if (err) return callback(err)
|
||||||
|
|
||||||
const thumbnailName = randomString + '.jpg'
|
return callback(null, thumbnailName)
|
||||||
fs.writeFile(constants.CONFIG.STORAGE.THUMBNAILS_DIR + thumbnailName, data, { encoding: 'base64' }, function (err) {
|
|
||||||
if (err) return callback(err)
|
|
||||||
|
|
||||||
return callback(null, thumbnailName)
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function generateImage (video, videoPath, folder, size, callback) {
|
||||||
|
const filename = video.getJPEGName()
|
||||||
|
const options = {
|
||||||
|
filename,
|
||||||
|
count: 1,
|
||||||
|
folder
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!callback) {
|
||||||
|
callback = size
|
||||||
|
} else {
|
||||||
|
options.size = size
|
||||||
|
}
|
||||||
|
|
||||||
|
ffmpeg(videoPath)
|
||||||
|
.on('error', callback)
|
||||||
|
.on('end', function () {
|
||||||
|
callback(null, filename)
|
||||||
|
})
|
||||||
|
.thumbnail(options)
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue