aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/controllers/api/v1/videos.js
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2016-05-10 21:19:24 +0200
committerChocobozzz <florian.bigard@gmail.com>2016-05-10 21:19:24 +0200
commitcbe2f7c34822b1bd3b1f8c691f79f0c29cf21f07 (patch)
treece678124210db8b03b2a523e3b92a14cc403eeee /server/controllers/api/v1/videos.js
parentf1dae018681936b556b2496b7f2d872c004cfda3 (diff)
downloadPeerTube-cbe2f7c34822b1bd3b1f8c691f79f0c29cf21f07.tar.gz
PeerTube-cbe2f7c34822b1bd3b1f8c691f79f0c29cf21f07.tar.zst
PeerTube-cbe2f7c34822b1bd3b1f8c691f79f0c29cf21f07.zip
Refractoring and add thumbnails support (without tests)
Diffstat (limited to 'server/controllers/api/v1/videos.js')
-rw-r--r--server/controllers/api/v1/videos.js82
1 files changed, 55 insertions, 27 deletions
diff --git a/server/controllers/api/v1/videos.js b/server/controllers/api/v1/videos.js
index 7fdc50e52..98f17ac6c 100644
--- a/server/controllers/api/v1/videos.js
+++ b/server/controllers/api/v1/videos.js
@@ -1,16 +1,19 @@
1'use strict' 1'use strict'
2 2
3const config = require('config') 3const config = require('config')
4const crypto = require('crypto')
5const express = require('express') 4const express = require('express')
5const fs = require('fs')
6const path = require('path')
6const multer = require('multer') 7const multer = require('multer')
7 8
9const constants = require('../../../initializers/constants')
8const logger = require('../../../helpers/logger') 10const logger = require('../../../helpers/logger')
9const friends = require('../../../lib/friends') 11const friends = require('../../../lib/friends')
10const middleware = require('../../../middlewares') 12const middleware = require('../../../middlewares')
11const oAuth2 = require('../../../middlewares/oauth2') 13const oAuth2 = require('../../../middlewares/oauth2')
12const cacheMiddleware = middleware.cache 14const cacheMiddleware = middleware.cache
13const reqValidator = middleware.reqValidators.videos 15const reqValidator = middleware.reqValidators.videos
16const utils = require('../../../helpers/utils')
14const Videos = require('../../../models/videos') // model 17const Videos = require('../../../models/videos') // model
15const videos = require('../../../lib/videos') 18const videos = require('../../../lib/videos')
16const webtorrent = require('../../../lib/webtorrent') 19const webtorrent = require('../../../lib/webtorrent')
@@ -29,14 +32,15 @@ const storage = multer.diskStorage({
29 if (file.mimetype === 'video/webm') extension = 'webm' 32 if (file.mimetype === 'video/webm') extension = 'webm'
30 else if (file.mimetype === 'video/mp4') extension = 'mp4' 33 else if (file.mimetype === 'video/mp4') extension = 'mp4'
31 else if (file.mimetype === 'video/ogg') extension = 'ogv' 34 else if (file.mimetype === 'video/ogg') extension = 'ogv'
32 crypto.pseudoRandomBytes(16, function (err, raw) { 35 utils.generateRandomString(16, function (err, random_string) {
33 const fieldname = err ? undefined : raw.toString('hex') 36 const fieldname = err ? undefined : random_string
34 cb(null, fieldname + '.' + extension) 37 cb(null, fieldname + '.' + extension)
35 }) 38 })
36 } 39 }
37}) 40})
38 41
39const reqFiles = multer({ storage: storage }).fields([{ name: 'videofile', maxCount: 1 }]) 42const reqFiles = multer({ storage: storage }).fields([{ name: 'videofile', maxCount: 1 }])
43const thumbnailsDir = path.join(__dirname, '..', '..', '..', '..', config.get('storage.thumbnails'))
40 44
41router.get('/', cacheMiddleware.cache(false), listVideos) 45router.get('/', cacheMiddleware.cache(false), listVideos)
42router.post('/', oAuth2.authenticate, reqFiles, reqValidator.videosAdd, cacheMiddleware.cache(false), addVideo) 46router.post('/', oAuth2.authenticate, reqFiles, reqValidator.videosAdd, cacheMiddleware.cache(false), addVideo)
@@ -63,31 +67,50 @@ function addVideo (req, res, next) {
63 videos.getVideoDuration(video_file.path, function (err, duration) { 67 videos.getVideoDuration(video_file.path, function (err, duration) {
64 if (err) { 68 if (err) {
65 // TODO: unseed the video 69 // TODO: unseed the video
66 logger.error('Cannot retrieve metadata of the file') 70 logger.error('Cannot retrieve metadata of the file.')
67 return next(err) 71 return next(err)
68 } 72 }
69 73
70 const video_data = { 74 videos.getVideoThumbnail(video_file.path, function (err, thumbnail_name) {
71 name: video_infos.name,
72 namePath: video_file.filename,
73 description: video_infos.description,
74 magnetUri: torrent.magnetURI,
75 author: res.locals.oauth.token.user.username,
76 duration: duration
77 }
78
79 Videos.add(video_data, function (err) {
80 if (err) { 75 if (err) {
81 // TODO unseed the video 76 // TODO: unseed the video
82 logger.error('Cannot insert this video in the database.') 77 logger.error('Cannot make a thumbnail of the video file.')
83 return next(err) 78 return next(err)
84 } 79 }
85 80
86 // Now we'll add the video's meta data to our friends 81 const video_data = {
87 friends.addVideoToFriends(video_data) 82 name: video_infos.name,
83 namePath: video_file.filename,
84 description: video_infos.description,
85 magnetUri: torrent.magnetURI,
86 author: res.locals.oauth.token.user.username,
87 duration: duration,
88 thumbnail: thumbnail_name
89 }
88 90
89 // TODO : include Location of the new video -> 201 91 Videos.add(video_data, function (err) {
90 res.type('json').status(204).end() 92 if (err) {
93 // TODO unseed the video
94 logger.error('Cannot insert this video in the database.')
95 return next(err)
96 }
97
98 fs.readFile(thumbnailsDir + thumbnail_name, function (err, data) {
99 if (err) {
100 // TODO: remove video?
101 logger.error('Cannot read the thumbnail of the video')
102 return next(err)
103 }
104
105 // Set the image in base64
106 video_data.thumbnail_base64 = new Buffer(data).toString('base64')
107 // Now we'll add the video's meta data to our friends
108 friends.addVideoToFriends(video_data)
109
110 // TODO : include Location of the new video -> 201
111 res.type('json').status(204).end()
112 })
113 })
91 }) 114 })
92 }) 115 })
93 }) 116 })
@@ -123,13 +146,17 @@ function removeVideo (req, res, next) {
123 Videos.removeOwned(req.params.id, function (err) { 146 Videos.removeOwned(req.params.id, function (err) {
124 if (err) return next(err) 147 if (err) return next(err)
125 148
126 const params = { 149 videos.removeVideosDataFromDisk([ video ], function (err) {
127 name: video.name, 150 if (err) logger.error('Cannot remove video data from disk.', { video: video })
128 magnetUri: video.magnetUri 151
129 } 152 const params = {
153 name: video.name,
154 magnetUri: video.magnetUri
155 }
130 156
131 friends.removeVideoToFriends(params) 157 friends.removeVideoToFriends(params)
132 res.type('json').status(204).end() 158 res.type('json').status(204).end()
159 })
133 }) 160 })
134 }) 161 })
135 }) 162 })
@@ -154,7 +181,8 @@ function getFormatedVideo (video_obj) {
154 isLocal: videos.getVideoState(video_obj).owned, 181 isLocal: videos.getVideoState(video_obj).owned,
155 magnetUri: video_obj.magnetUri, 182 magnetUri: video_obj.magnetUri,
156 author: video_obj.author, 183 author: video_obj.author,
157 duration: video_obj.duration 184 duration: video_obj.duration,
185 thumbnail_path: constants.THUMBNAILS_STATIC_PATH + '/' + video_obj.thumbnail
158 } 186 }
159 187
160 return formated_video 188 return formated_video