diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2016-05-10 21:19:24 +0200 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2016-05-10 21:19:24 +0200 |
commit | cbe2f7c34822b1bd3b1f8c691f79f0c29cf21f07 (patch) | |
tree | ce678124210db8b03b2a523e3b92a14cc403eeee /server/controllers | |
parent | f1dae018681936b556b2496b7f2d872c004cfda3 (diff) | |
download | PeerTube-cbe2f7c34822b1bd3b1f8c691f79f0c29cf21f07.tar.gz PeerTube-cbe2f7c34822b1bd3b1f8c691f79f0c29cf21f07.tar.zst PeerTube-cbe2f7c34822b1bd3b1f8c691f79f0c29cf21f07.zip |
Refractoring and add thumbnails support (without tests)
Diffstat (limited to 'server/controllers')
-rw-r--r-- | server/controllers/api/v1/pods.js | 27 | ||||
-rw-r--r-- | server/controllers/api/v1/remoteVideos.js | 32 | ||||
-rw-r--r-- | server/controllers/api/v1/videos.js | 82 |
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 | ||
3 | const express = require('express') | 3 | const express = require('express') |
4 | const fs = require('fs') | ||
5 | 4 | ||
6 | const logger = require('../../../helpers/logger') | 5 | const logger = require('../../../helpers/logger') |
7 | const friends = require('../../../lib/friends') | 6 | const friends = require('../../../lib/friends') |
8 | const middleware = require('../../../middlewares') | 7 | const middleware = require('../../../middlewares') |
9 | const cacheMiddleware = middleware.cache | 8 | const cacheMiddleware = middleware.cache |
10 | const peertubeCrypto = require('../../../helpers/peertubeCrypto') | ||
11 | const Pods = require('../../../models/pods') | 9 | const Pods = require('../../../models/pods') |
12 | const reqValidator = middleware.reqValidators.pods | 10 | const reqValidator = middleware.reqValidators.pods |
13 | const secureMiddleware = middleware.secure | 11 | const secureMiddleware = middleware.secure |
14 | const secureRequest = middleware.reqValidators.remote.secureRequest | 12 | const secureRequest = middleware.reqValidators.remote.secureRequest |
13 | const videos = require('../../../lib/videos') | ||
15 | const Videos = require('../../../models/videos') | 14 | const Videos = require('../../../models/videos') |
16 | 15 | ||
17 | const router = express.Router() | 16 | const 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') | |||
7 | const secureMiddleware = middleware.secure | 7 | const secureMiddleware = middleware.secure |
8 | const cacheMiddleware = middleware.cache | 8 | const cacheMiddleware = middleware.cache |
9 | const reqValidator = middleware.reqValidators.remote | 9 | const reqValidator = middleware.reqValidators.remote |
10 | const videos = require('../../../models/videos') | 10 | const logger = require('../../../helpers/logger') |
11 | const Videos = require('../../../models/videos') | ||
12 | const videos = require('../../../lib/videos') | ||
11 | 13 | ||
12 | const router = express.Router() | 14 | const router = express.Router() |
13 | 15 | ||
@@ -34,20 +36,34 @@ module.exports = router | |||
34 | // --------------------------------------------------------------------------- | 36 | // --------------------------------------------------------------------------- |
35 | 37 | ||
36 | function addRemoteVideos (req, res, next) { | 38 | function 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 | ||
44 | function removeRemoteVideo (req, res, next) { | 50 | function 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 | ||
3 | const config = require('config') | 3 | const config = require('config') |
4 | const crypto = require('crypto') | ||
5 | const express = require('express') | 4 | const express = require('express') |
5 | const fs = require('fs') | ||
6 | const path = require('path') | ||
6 | const multer = require('multer') | 7 | const multer = require('multer') |
7 | 8 | ||
9 | const constants = require('../../../initializers/constants') | ||
8 | const logger = require('../../../helpers/logger') | 10 | const logger = require('../../../helpers/logger') |
9 | const friends = require('../../../lib/friends') | 11 | const friends = require('../../../lib/friends') |
10 | const middleware = require('../../../middlewares') | 12 | const middleware = require('../../../middlewares') |
11 | const oAuth2 = require('../../../middlewares/oauth2') | 13 | const oAuth2 = require('../../../middlewares/oauth2') |
12 | const cacheMiddleware = middleware.cache | 14 | const cacheMiddleware = middleware.cache |
13 | const reqValidator = middleware.reqValidators.videos | 15 | const reqValidator = middleware.reqValidators.videos |
16 | const utils = require('../../../helpers/utils') | ||
14 | const Videos = require('../../../models/videos') // model | 17 | const Videos = require('../../../models/videos') // model |
15 | const videos = require('../../../lib/videos') | 18 | const videos = require('../../../lib/videos') |
16 | const webtorrent = require('../../../lib/webtorrent') | 19 | const 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 | ||
39 | const reqFiles = multer({ storage: storage }).fields([{ name: 'videofile', maxCount: 1 }]) | 42 | const reqFiles = multer({ storage: storage }).fields([{ name: 'videofile', maxCount: 1 }]) |
43 | const thumbnailsDir = path.join(__dirname, '..', '..', '..', '..', config.get('storage.thumbnails')) | ||
40 | 44 | ||
41 | router.get('/', cacheMiddleware.cache(false), listVideos) | 45 | router.get('/', cacheMiddleware.cache(false), listVideos) |
42 | router.post('/', oAuth2.authenticate, reqFiles, reqValidator.videosAdd, cacheMiddleware.cache(false), addVideo) | 46 | router.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 |