mirror of
https://github.com/Chocobozzz/PeerTube.git
synced 2025-10-03 17:59:37 +02:00
Refractoring and add thumbnails support (without tests)
This commit is contained in:
parent
f1dae01868
commit
cbe2f7c348
19 changed files with 312 additions and 162 deletions
|
@ -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, random_string) {
|
||||
const fieldname = err ? undefined : random_string
|
||||
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)
|
||||
|
@ -63,31 +67,50 @@ function addVideo (req, res, next) {
|
|||
videos.getVideoDuration(video_file.path, function (err, duration) {
|
||||
if (err) {
|
||||
// TODO: unseed the video
|
||||
logger.error('Cannot retrieve metadata of the file')
|
||||
logger.error('Cannot retrieve metadata of the file.')
|
||||
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,
|
||||
duration: duration
|
||||
}
|
||||
|
||||
Videos.add(video_data, function (err) {
|
||||
videos.getVideoThumbnail(video_file.path, function (err, thumbnail_name) {
|
||||
if (err) {
|
||||
// TODO unseed the video
|
||||
logger.error('Cannot insert this video in the database.')
|
||||
// TODO: unseed the video
|
||||
logger.error('Cannot make a thumbnail of the video file.')
|
||||
return next(err)
|
||||
}
|
||||
|
||||
// Now we'll add the video's meta data to our friends
|
||||
friends.addVideoToFriends(video_data)
|
||||
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,
|
||||
duration: duration,
|
||||
thumbnail: thumbnail_name
|
||||
}
|
||||
|
||||
// TODO : include Location of the new video -> 201
|
||||
res.type('json').status(204).end()
|
||||
Videos.add(video_data, function (err) {
|
||||
if (err) {
|
||||
// TODO unseed the video
|
||||
logger.error('Cannot insert this video in the database.')
|
||||
return next(err)
|
||||
}
|
||||
|
||||
fs.readFile(thumbnailsDir + thumbnail_name, 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
|
||||
video_data.thumbnail_base64 = new Buffer(data).toString('base64')
|
||||
// Now we'll add the video's meta data to our friends
|
||||
friends.addVideoToFriends(video_data)
|
||||
|
||||
// TODO : include Location of the new video -> 201
|
||||
res.type('json').status(204).end()
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
@ -123,13 +146,17 @@ function removeVideo (req, res, next) {
|
|||
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 })
|
||||
|
||||
friends.removeVideoToFriends(params)
|
||||
res.type('json').status(204).end()
|
||||
const params = {
|
||||
name: video.name,
|
||||
magnetUri: video.magnetUri
|
||||
}
|
||||
|
||||
friends.removeVideoToFriends(params)
|
||||
res.type('json').status(204).end()
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
@ -154,7 +181,8 @@ function getFormatedVideo (video_obj) {
|
|||
isLocal: videos.getVideoState(video_obj).owned,
|
||||
magnetUri: video_obj.magnetUri,
|
||||
author: video_obj.author,
|
||||
duration: video_obj.duration
|
||||
duration: video_obj.duration,
|
||||
thumbnail_path: constants.THUMBNAILS_STATIC_PATH + '/' + video_obj.thumbnail
|
||||
}
|
||||
|
||||
return formated_video
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue