]>
Commit | Line | Data |
---|---|---|
9f10b292 C |
1 | 'use strict' |
2 | ||
f0f5567b C |
3 | const config = require('config') |
4 | const crypto = require('crypto') | |
5 | const express = require('express') | |
6 | const multer = require('multer') | |
7 | ||
8 | const logger = require('../../../helpers/logger') | |
9 | const friends = require('../../../lib/friends') | |
10 | const middleware = require('../../../middlewares') | |
11 | const cacheMiddleware = middleware.cache | |
12 | const reqValidator = middleware.reqValidators.videos | |
13 | const Videos = require('../../../models/videos') // model | |
14 | const videos = require('../../../lib/videos') | |
15 | const webtorrent = require('../../../lib/webtorrent') | |
16 | ||
17 | const router = express.Router() | |
18 | const uploads = config.get('storage.uploads') | |
9f10b292 C |
19 | |
20 | // multer configuration | |
f0f5567b | 21 | const storage = multer.diskStorage({ |
9f10b292 C |
22 | destination: function (req, file, cb) { |
23 | cb(null, uploads) | |
24 | }, | |
25 | ||
26 | filename: function (req, file, cb) { | |
f0f5567b | 27 | let extension = '' |
9f10b292 C |
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) { | |
f0f5567b | 32 | const fieldname = err ? undefined : raw.toString('hex') |
9f10b292 C |
33 | cb(null, fieldname + '.' + extension) |
34 | }) | |
35 | } | |
36 | }) | |
37 | ||
8c9c1942 | 38 | const reqFiles = multer({ storage: storage }).fields([{ name: 'videofile', maxCount: 1 }]) |
8c308c2b | 39 | |
9f10b292 C |
40 | router.get('/', cacheMiddleware.cache(false), listVideos) |
41 | router.post('/', reqFiles, reqValidator.videosAdd, cacheMiddleware.cache(false), addVideo) | |
42 | router.get('/:id', reqValidator.videosGet, cacheMiddleware.cache(false), getVideos) | |
43 | router.delete('/:id', reqValidator.videosRemove, cacheMiddleware.cache(false), removeVideo) | |
44 | router.get('/search/:name', reqValidator.videosSearch, cacheMiddleware.cache(false), searchVideos) | |
8c308c2b | 45 | |
9f10b292 | 46 | // --------------------------------------------------------------------------- |
c45f7f84 | 47 | |
9f10b292 | 48 | module.exports = router |
c45f7f84 | 49 | |
9f10b292 | 50 | // --------------------------------------------------------------------------- |
c45f7f84 | 51 | |
9f10b292 | 52 | function addVideo (req, res, next) { |
8c9c1942 | 53 | const video_file = req.files.videofile[0] |
f0f5567b | 54 | const video_infos = req.body |
9f10b292 C |
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 | } | |
8c308c2b | 61 | |
f0f5567b | 62 | const video_data = { |
9f10b292 C |
63 | name: video_infos.name, |
64 | namePath: video_file.filename, | |
65 | description: video_infos.description, | |
66 | magnetUri: torrent.magnetURI | |
67 | } | |
c173e565 | 68 | |
9f10b292 | 69 | Videos.add(video_data, function (err) { |
c173e565 | 70 | if (err) { |
9f10b292 C |
71 | // TODO unseed the video |
72 | logger.error('Cannot insert this video in the database.') | |
c173e565 C |
73 | return next(err) |
74 | } | |
75 | ||
9f10b292 C |
76 | // Now we'll add the video's meta data to our friends |
77 | friends.addVideoToFriends(video_data) | |
c173e565 | 78 | |
98b01bac C |
79 | // TODO : include Location of the new video -> 201 |
80 | res.type('json').status(204).end() | |
8c308c2b | 81 | }) |
9f10b292 C |
82 | }) |
83 | } | |
8c308c2b | 84 | |
9f10b292 | 85 | function getVideos (req, res, next) { |
2df82d42 | 86 | Videos.get(req.params.id, function (err, video_obj) { |
9f10b292 | 87 | if (err) return next(err) |
8c308c2b | 88 | |
2df82d42 C |
89 | const state = videos.getVideoState(video_obj) |
90 | if (state.exist === false) { | |
91 | return res.type('json').status(204).end() | |
9f10b292 | 92 | } |
8c308c2b | 93 | |
2df82d42 | 94 | res.json(getFormatedVideo(video_obj)) |
9f10b292 C |
95 | }) |
96 | } | |
8c308c2b | 97 | |
9f10b292 C |
98 | function listVideos (req, res, next) { |
99 | Videos.list(function (err, videos_list) { | |
100 | if (err) return next(err) | |
c45f7f84 | 101 | |
2df82d42 | 102 | res.json(getFormatedVideos(videos_list)) |
9f10b292 C |
103 | }) |
104 | } | |
c45f7f84 | 105 | |
9f10b292 | 106 | function removeVideo (req, res, next) { |
f0f5567b | 107 | const video_id = req.params.id |
9f10b292 C |
108 | Videos.get(video_id, function (err, video) { |
109 | if (err) return next(err) | |
8c308c2b | 110 | |
9f10b292 C |
111 | removeTorrent(video.magnetUri, function () { |
112 | Videos.removeOwned(req.params.id, function (err) { | |
113 | if (err) return next(err) | |
c173e565 | 114 | |
f0f5567b | 115 | const params = { |
9f10b292 C |
116 | name: video.name, |
117 | magnetUri: video.magnetUri | |
118 | } | |
c173e565 | 119 | |
9f10b292 | 120 | friends.removeVideoToFriends(params) |
dc8bc31b | 121 | res.type('json').status(204).end() |
c173e565 | 122 | }) |
8c308c2b | 123 | }) |
9f10b292 C |
124 | }) |
125 | } | |
8c308c2b | 126 | |
9f10b292 C |
127 | function searchVideos (req, res, next) { |
128 | Videos.search(req.params.name, function (err, videos_list) { | |
129 | if (err) return next(err) | |
8c308c2b | 130 | |
2df82d42 | 131 | res.json(getFormatedVideos(videos_list)) |
9f10b292 C |
132 | }) |
133 | } | |
c173e565 | 134 | |
9f10b292 | 135 | // --------------------------------------------------------------------------- |
c173e565 | 136 | |
2df82d42 C |
137 | function getFormatedVideo (video_obj) { |
138 | const formated_video = { | |
139 | id: video_obj._id, | |
140 | name: video_obj.name, | |
141 | description: video_obj.description, | |
142 | podUrl: video_obj.podUrl, | |
143 | isLocal: videos.getVideoState(video_obj).owned, | |
144 | magnetUri: video_obj.magnetUri | |
145 | } | |
146 | ||
147 | return formated_video | |
148 | } | |
149 | ||
150 | function getFormatedVideos (videos_obj) { | |
151 | const formated_videos = [] | |
152 | ||
153 | videos_obj.forEach(function (video_obj) { | |
154 | formated_videos.push(getFormatedVideo(video_obj)) | |
155 | }) | |
156 | ||
157 | return formated_videos | |
158 | } | |
159 | ||
9f10b292 C |
160 | // Maybe the torrent is not seeded, but we catch the error to don't stop the removing process |
161 | function removeTorrent (magnetUri, callback) { | |
162 | try { | |
163 | webtorrent.remove(magnetUri, callback) | |
164 | } catch (err) { | |
165 | logger.warn('Cannot remove the torrent from WebTorrent', { err: err }) | |
166 | return callback(null) | |
c173e565 | 167 | } |
9f10b292 | 168 | } |