aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/controllers/api/v1
diff options
context:
space:
mode:
Diffstat (limited to 'server/controllers/api/v1')
-rw-r--r--server/controllers/api/v1/pods.js27
-rw-r--r--server/controllers/api/v1/remoteVideos.js32
-rw-r--r--server/controllers/api/v1/videos.js82
3 files changed, 98 insertions, 43 deletions
diff --git a/server/controllers/api/v1/pods.js b/server/controllers/api/v1/pods.js
index dbe1a7372..d08b7860d 100644
--- a/server/controllers/api/v1/pods.js
+++ b/server/controllers/api/v1/pods.js
@@ -1,17 +1,16 @@
1'use strict' 1'use strict'
2 2
3const express = require('express') 3const express = require('express')
4const fs = require('fs')
5 4
6const logger = require('../../../helpers/logger') 5const logger = require('../../../helpers/logger')
7const friends = require('../../../lib/friends') 6const friends = require('../../../lib/friends')
8const middleware = require('../../../middlewares') 7const middleware = require('../../../middlewares')
9const cacheMiddleware = middleware.cache 8const cacheMiddleware = middleware.cache
10const peertubeCrypto = require('../../../helpers/peertubeCrypto')
11const Pods = require('../../../models/pods') 9const Pods = require('../../../models/pods')
12const reqValidator = middleware.reqValidators.pods 10const reqValidator = middleware.reqValidators.pods
13const secureMiddleware = middleware.secure 11const secureMiddleware = middleware.secure
14const secureRequest = middleware.reqValidators.remote.secureRequest 12const secureRequest = middleware.reqValidators.remote.secureRequest
13const videos = require('../../../lib/videos')
15const Videos = require('../../../models/videos') 14const Videos = require('../../../models/videos')
16 15
17const router = express.Router() 16const router = express.Router()
@@ -34,9 +33,12 @@ function addPods (req, res, next) {
34 Pods.add(informations, function (err) { 33 Pods.add(informations, function (err) {
35 if (err) return next(err) 34 if (err) return next(err)
36 35
37 Videos.addRemotes(informations.videos) 36 // Create the remote videos from the new pod
37 videos.createRemoteVideos(informations.videos, function (err) {
38 if (err) logger.error('Cannot create remote videos.', { error: err })
39 })
38 40
39 fs.readFile(peertubeCrypto.getCertDir() + 'peertube.pub', 'utf8', function (err, cert) { 41 friends.getMyCertificate(function (err, cert) {
40 if (err) { 42 if (err) {
41 logger.error('Cannot read cert file.') 43 logger.error('Cannot read cert file.')
42 return next(err) 44 return next(err)
@@ -75,11 +77,20 @@ function removePods (req, res, next) {
75 Pods.remove(url, function (err) { 77 Pods.remove(url, function (err) {
76 if (err) return next(err) 78 if (err) return next(err)
77 79
78 Videos.removeAllRemotesOf(url, function (err) { 80 Videos.listFromUrl(url, function (err, videos_list) {
79 if (err) logger.error('Cannot remove all remote videos of %s.', url) 81 if (err) {
80 else logger.info('%s pod removed.', url) 82 logger.error('Cannot list videos from url.', { error: err })
83 next(err)
84 }
81 85
82 res.type('json').status(204).end() 86 videos.removeRemoteVideos(videos_list, function (err) {
87 if (err) {
88 logger.error('Cannot remove remote videos.', { error: err })
89 next(err)
90 }
91
92 res.type('json').status(204).end()
93 })
83 }) 94 })
84 }) 95 })
85} 96}
diff --git a/server/controllers/api/v1/remoteVideos.js b/server/controllers/api/v1/remoteVideos.js
index 5e9e71fdd..8ff212b7f 100644
--- a/server/controllers/api/v1/remoteVideos.js
+++ b/server/controllers/api/v1/remoteVideos.js
@@ -7,7 +7,9 @@ const middleware = require('../../../middlewares')
7const secureMiddleware = middleware.secure 7const secureMiddleware = middleware.secure
8const cacheMiddleware = middleware.cache 8const cacheMiddleware = middleware.cache
9const reqValidator = middleware.reqValidators.remote 9const reqValidator = middleware.reqValidators.remote
10const videos = require('../../../models/videos') 10const logger = require('../../../helpers/logger')
11const Videos = require('../../../models/videos')
12const videos = require('../../../lib/videos')
11 13
12const router = express.Router() 14const router = express.Router()
13 15
@@ -34,20 +36,34 @@ module.exports = router
34// --------------------------------------------------------------------------- 36// ---------------------------------------------------------------------------
35 37
36function addRemoteVideos (req, res, next) { 38function addRemoteVideos (req, res, next) {
37 videos.addRemotes(req.body.data, function (err, videos) { 39 const videos_to_create = req.body.data
38 if (err) return next(err) 40 videos.createRemoteVideos(videos_to_create, function (err, remote_videos) {
41 if (err) {
42 logger.error('Cannot create remote videos.', { error: err })
43 return next(err)
44 }
39 45
40 res.json(videos) 46 res.type('json').status(201).end()
41 }) 47 })
42} 48}
43 49
44function removeRemoteVideo (req, res, next) { 50function removeRemoteVideo (req, res, next) {
45 const url = req.body.signature.url 51 const fromUrl = req.body.signature.url
46 const magnetUris = map(req.body.data, 'magnetUri') 52 const magnetUris = map(req.body.data, 'magnetUri')
47 53
48 videos.removeRemotesOfByMagnetUris(url, magnetUris, function (err) { 54 Videos.listFromUrlAndMagnets(fromUrl, magnetUris, function (err, videos_list) {
49 if (err) return next(err) 55 if (err) {
56 logger.error('Cannot list videos from url and magnets.', { error: err })
57 return next(err)
58 }
50 59
51 res.type('json').status(204).end() 60 videos.removeRemoteVideos(videos_list, function (err) {
61 if (err) {
62 logger.error('Cannot remove remote videos.', { error: err })
63 return next(err)
64 }
65
66 res.type('json').status(204).end()
67 })
52 }) 68 })
53} 69}
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