X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fcontrollers%2Fapi%2Fv1%2Fvideos.js;h=c86a96a25c8a1a6db7b7a2b4adc493192561b837;hb=bc503c2a62dcf9aed6b8d90b68f0f27a7755ac01;hp=d25ca95f72c7b2c37646279df85b5c66f7057a7d;hpb=0c1cbbfe29d91c95f9c574b57adf067654b8b5b4;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/controllers/api/v1/videos.js b/server/controllers/api/v1/videos.js index d25ca95f7..c86a96a25 100644 --- a/server/controllers/api/v1/videos.js +++ b/server/controllers/api/v1/videos.js @@ -1,16 +1,19 @@ 'use strict' const config = require('config') -const crypto = require('crypto') const express = require('express') +const fs = require('fs') +const path = require('path') const multer = require('multer') +const constants = require('../../../initializers/constants') const logger = require('../../../helpers/logger') const friends = require('../../../lib/friends') const middleware = require('../../../middlewares') const oAuth2 = require('../../../middlewares/oauth2') const cacheMiddleware = middleware.cache const reqValidator = middleware.reqValidators.videos +const utils = require('../../../helpers/utils') const Videos = require('../../../models/videos') // model const videos = require('../../../lib/videos') const webtorrent = require('../../../lib/webtorrent') @@ -29,14 +32,15 @@ const storage = multer.diskStorage({ if (file.mimetype === 'video/webm') extension = 'webm' else if (file.mimetype === 'video/mp4') extension = 'mp4' else if (file.mimetype === 'video/ogg') extension = 'ogv' - crypto.pseudoRandomBytes(16, function (err, raw) { - const fieldname = err ? undefined : raw.toString('hex') + utils.generateRandomString(16, function (err, randomString) { + const fieldname = err ? undefined : randomString cb(null, fieldname + '.' + extension) }) } }) const reqFiles = multer({ storage: storage }).fields([{ name: 'videofile', maxCount: 1 }]) +const thumbnailsDir = path.join(__dirname, '..', '..', '..', '..', config.get('storage.thumbnails')) router.get('/', cacheMiddleware.cache(false), listVideos) router.post('/', oAuth2.authenticate, reqFiles, reqValidator.videosAdd, cacheMiddleware.cache(false), addVideo) @@ -51,113 +55,147 @@ module.exports = router // --------------------------------------------------------------------------- function addVideo (req, res, next) { - const video_file = req.files.videofile[0] - const video_infos = req.body + const videoFile = req.files.videofile[0] + const videoInfos = req.body - videos.seed(video_file.path, function (err, torrent) { + videos.seed(videoFile.path, function (err, torrent) { if (err) { logger.error('Cannot seed this video.') return next(err) } - const video_data = { - name: video_infos.name, - namePath: video_file.filename, - description: video_infos.description, - magnetUri: torrent.magnetURI, - author: res.locals.oauth.token.user.username - } - - Videos.add(video_data, function (err) { + videos.getVideoDuration(videoFile.path, function (err, duration) { if (err) { - // TODO unseed the video - logger.error('Cannot insert this video in the database.') + // TODO: unseed the video + logger.error('Cannot retrieve metadata of the file.') return next(err) } - // Now we'll add the video's meta data to our friends - friends.addVideoToFriends(video_data) + videos.getVideoThumbnail(videoFile.path, function (err, thumbnailName) { + if (err) { + // TODO: unseed the video + logger.error('Cannot make a thumbnail of the video file.') + return next(err) + } + + const videoData = { + name: videoInfos.name, + namePath: videoFile.filename, + description: videoInfos.description, + magnetUri: torrent.magnetURI, + author: res.locals.oauth.token.user.username, + duration: duration, + thumbnail: thumbnailName + } - // TODO : include Location of the new video -> 201 - res.type('json').status(204).end() + Videos.add(videoData, function (err) { + if (err) { + // TODO unseed the video + logger.error('Cannot insert this video in the database.') + return next(err) + } + + fs.readFile(thumbnailsDir + thumbnailName, function (err, data) { + if (err) { + // TODO: remove video? + logger.error('Cannot read the thumbnail of the video') + return next(err) + } + + // Set the image in base64 + videoData.thumbnailBase64 = new Buffer(data).toString('base64') + // Now we'll add the video's meta data to our friends + friends.addVideoToFriends(videoData) + + // TODO : include Location of the new video -> 201 + res.type('json').status(204).end() + }) + }) + }) }) }) } function getVideos (req, res, next) { - Videos.get(req.params.id, function (err, video_obj) { + Videos.get(req.params.id, function (err, videoObj) { if (err) return next(err) - const state = videos.getVideoState(video_obj) + const state = videos.getVideoState(videoObj) if (state.exist === false) { return res.type('json').status(204).end() } - res.json(getFormatedVideo(video_obj)) + res.json(getFormatedVideo(videoObj)) }) } function listVideos (req, res, next) { - Videos.list(function (err, videos_list) { + Videos.list(function (err, videosList) { if (err) return next(err) - res.json(getFormatedVideos(videos_list)) + res.json(getFormatedVideos(videosList)) }) } function removeVideo (req, res, next) { - const video_id = req.params.id - Videos.get(video_id, function (err, video) { + const videoId = req.params.id + Videos.get(videoId, function (err, video) { if (err) return next(err) removeTorrent(video.magnetUri, function () { Videos.removeOwned(req.params.id, function (err) { if (err) return next(err) - const params = { - name: video.name, - magnetUri: video.magnetUri - } + videos.removeVideosDataFromDisk([ video ], function (err) { + if (err) logger.error('Cannot remove video data from disk.', { video: video }) + + const params = { + name: video.name, + magnetUri: video.magnetUri + } - friends.removeVideoToFriends(params) - res.type('json').status(204).end() + friends.removeVideoToFriends(params) + res.type('json').status(204).end() + }) }) }) }) } function searchVideos (req, res, next) { - Videos.search(req.params.name, function (err, videos_list) { + Videos.search(req.params.name, function (err, videosList) { if (err) return next(err) - res.json(getFormatedVideos(videos_list)) + res.json(getFormatedVideos(videosList)) }) } // --------------------------------------------------------------------------- -function getFormatedVideo (video_obj) { - const formated_video = { - id: video_obj._id, - name: video_obj.name, - description: video_obj.description, - podUrl: video_obj.podUrl, - isLocal: videos.getVideoState(video_obj).owned, - magnetUri: video_obj.magnetUri, - author: video_obj.author +function getFormatedVideo (videoObj) { + const formatedVideo = { + id: videoObj._id, + name: videoObj.name, + description: videoObj.description, + podUrl: videoObj.podUrl, + isLocal: videos.getVideoState(videoObj).owned, + magnetUri: videoObj.magnetUri, + author: videoObj.author, + duration: videoObj.duration, + thumbnailPath: constants.THUMBNAILS_STATIC_PATH + '/' + videoObj.thumbnail } - return formated_video + return formatedVideo } -function getFormatedVideos (videos_obj) { - const formated_videos = [] +function getFormatedVideos (videosObj) { + const formatedVideos = [] - videos_obj.forEach(function (video_obj) { - formated_videos.push(getFormatedVideo(video_obj)) + videosObj.forEach(function (videoObj) { + formatedVideos.push(getFormatedVideo(videoObj)) }) - return formated_videos + return formatedVideos } // Maybe the torrent is not seeded, but we catch the error to don't stop the removing process