aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/controllers/api/v1/videos.js
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2016-03-07 11:33:59 +0100
committerChocobozzz <florian.bigard@gmail.com>2016-03-07 11:33:59 +0100
commitb9a3e09ad5a7673f64556d1dba122ed4c4fac980 (patch)
tree66d4928b82af19a2372a2505822233884f3fd471 /server/controllers/api/v1/videos.js
parentb2ff5e3e686eb552c5ccd64ce67b0455972ceef0 (diff)
downloadPeerTube-b9a3e09ad5a7673f64556d1dba122ed4c4fac980.tar.gz
PeerTube-b9a3e09ad5a7673f64556d1dba122ed4c4fac980.tar.zst
PeerTube-b9a3e09ad5a7673f64556d1dba122ed4c4fac980.zip
Prepare folders structure for angular app
Diffstat (limited to 'server/controllers/api/v1/videos.js')
-rw-r--r--server/controllers/api/v1/videos.js144
1 files changed, 144 insertions, 0 deletions
diff --git a/server/controllers/api/v1/videos.js b/server/controllers/api/v1/videos.js
new file mode 100644
index 000000000..620711925
--- /dev/null
+++ b/server/controllers/api/v1/videos.js
@@ -0,0 +1,144 @@
1'use strict'
2
3var config = require('config')
4var crypto = require('crypto')
5var express = require('express')
6var multer = require('multer')
7
8var logger = require('../../../helpers/logger')
9var friends = require('../../../lib/friends')
10var middleware = require('../../../middlewares')
11var cacheMiddleware = middleware.cache
12var reqValidator = middleware.reqValidators.videos
13var Videos = require('../../../models/videos') // model
14var videos = require('../../../lib/videos')
15var webtorrent = require('../../../lib/webtorrent')
16
17var router = express.Router()
18var uploads = config.get('storage.uploads')
19
20// multer configuration
21var storage = multer.diskStorage({
22 destination: function (req, file, cb) {
23 cb(null, uploads)
24 },
25
26 filename: function (req, file, cb) {
27 var extension = ''
28 if (file.mimetype === 'video/webm') extension = 'webm'
29 else if (file.mimetype === 'video/mp4') extension = 'mp4'
30 else if (file.mimetype === 'video/ogg') extension = 'ogv'
31 crypto.pseudoRandomBytes(16, function (err, raw) {
32 var fieldname = err ? undefined : raw.toString('hex')
33 cb(null, fieldname + '.' + extension)
34 })
35 }
36})
37
38var reqFiles = multer({ storage: storage }).fields([{ name: 'input_video', maxCount: 1 }])
39
40router.get('/', cacheMiddleware.cache(false), listVideos)
41router.post('/', reqFiles, reqValidator.videosAdd, cacheMiddleware.cache(false), addVideo)
42router.get('/:id', reqValidator.videosGet, cacheMiddleware.cache(false), getVideos)
43router.delete('/:id', reqValidator.videosRemove, cacheMiddleware.cache(false), removeVideo)
44router.get('/search/:name', reqValidator.videosSearch, cacheMiddleware.cache(false), searchVideos)
45
46// ---------------------------------------------------------------------------
47
48module.exports = router
49
50// ---------------------------------------------------------------------------
51
52function addVideo (req, res, next) {
53 var video_file = req.files.input_video[0]
54 var video_infos = req.body
55
56 videos.seed(video_file.path, function (err, torrent) {
57 if (err) {
58 logger.error('Cannot seed this video.')
59 return next(err)
60 }
61
62 var video_data = {
63 name: video_infos.name,
64 namePath: video_file.filename,
65 description: video_infos.description,
66 magnetUri: torrent.magnetURI
67 }
68
69 Videos.add(video_data, function (err) {
70 if (err) {
71 // TODO unseed the video
72 logger.error('Cannot insert this video in the database.')
73 return next(err)
74 }
75
76 // Now we'll add the video's meta data to our friends
77 friends.addVideoToFriends(video_data)
78
79 // TODO : include Location of the new video
80 res.sendStatus(201)
81 })
82 })
83}
84
85function getVideos (req, res, next) {
86 Videos.get(req.params.id, function (err, video) {
87 if (err) return next(err)
88
89 if (video === null) {
90 return res.sendStatus(404)
91 }
92
93 res.json(video)
94 })
95}
96
97function listVideos (req, res, next) {
98 Videos.list(function (err, videos_list) {
99 if (err) return next(err)
100
101 res.json(videos_list)
102 })
103}
104
105function removeVideo (req, res, next) {
106 var video_id = req.params.id
107 Videos.get(video_id, function (err, video) {
108 if (err) return next(err)
109
110 removeTorrent(video.magnetUri, function () {
111 Videos.removeOwned(req.params.id, function (err) {
112 if (err) return next(err)
113
114 var params = {
115 name: video.name,
116 magnetUri: video.magnetUri
117 }
118
119 friends.removeVideoToFriends(params)
120 res.sendStatus(204)
121 })
122 })
123 })
124}
125
126function searchVideos (req, res, next) {
127 Videos.search(req.params.name, function (err, videos_list) {
128 if (err) return next(err)
129
130 res.json(videos_list)
131 })
132}
133
134// ---------------------------------------------------------------------------
135
136// Maybe the torrent is not seeded, but we catch the error to don't stop the removing process
137function removeTorrent (magnetUri, callback) {
138 try {
139 webtorrent.remove(magnetUri, callback)
140 } catch (err) {
141 logger.warn('Cannot remove the torrent from WebTorrent', { err: err })
142 return callback(null)
143 }
144}