diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2016-10-02 15:39:09 +0200 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2016-10-02 15:39:09 +0200 |
commit | a6375e69668ea42e19531c6bc68dcd37f3f7cbd7 (patch) | |
tree | 03204a408d56311692c3528bedcf95d2455e94f2 /server/models/video.js | |
parent | 052937db8a8d282eccdbdf38d487ed8d85d3c0a7 (diff) | |
parent | c4403b29ad4db097af528a7f04eea07e0ed320d0 (diff) | |
download | PeerTube-a6375e69668ea42e19531c6bc68dcd37f3f7cbd7.tar.gz PeerTube-a6375e69668ea42e19531c6bc68dcd37f3f7cbd7.tar.zst PeerTube-a6375e69668ea42e19531c6bc68dcd37f3f7cbd7.zip |
Merge branch 'master' into webseed-merged
Diffstat (limited to 'server/models/video.js')
-rw-r--r-- | server/models/video.js | 85 |
1 files changed, 36 insertions, 49 deletions
diff --git a/server/models/video.js b/server/models/video.js index 14e0df6f2..7d073cffa 100644 --- a/server/models/video.js +++ b/server/models/video.js | |||
@@ -11,8 +11,9 @@ const magnet = require('magnet-uri') | |||
11 | const mongoose = require('mongoose') | 11 | const mongoose = require('mongoose') |
12 | 12 | ||
13 | const constants = require('../initializers/constants') | 13 | const constants = require('../initializers/constants') |
14 | const customValidators = require('../helpers/custom-validators') | 14 | const customVideosValidators = require('../helpers/custom-validators').videos |
15 | const logger = require('../helpers/logger') | 15 | const logger = require('../helpers/logger') |
16 | const modelUtils = require('./utils') | ||
16 | const utils = require('../helpers/utils') | 17 | const utils = require('../helpers/utils') |
17 | 18 | ||
18 | const http = config.get('webserver.https') === true ? 'https' : 'http' | 19 | const http = config.get('webserver.https') === true ? 'https' : 'http' |
@@ -42,34 +43,35 @@ const VideoSchema = mongoose.Schema({ | |||
42 | } | 43 | } |
43 | }) | 44 | }) |
44 | 45 | ||
45 | VideoSchema.path('name').validate(customValidators.isVideoNameValid) | 46 | VideoSchema.path('name').validate(customVideosValidators.isVideoNameValid) |
46 | VideoSchema.path('description').validate(customValidators.isVideoDescriptionValid) | 47 | VideoSchema.path('description').validate(customVideosValidators.isVideoDescriptionValid) |
47 | VideoSchema.path('magnetUri').validate(customValidators.isVideoMagnetUriValid) | 48 | VideoSchema.path('magnetUri').validate(customVideosValidators.isVideoMagnetUriValid) |
48 | VideoSchema.path('podUrl').validate(customValidators.isVideoPodUrlValid) | 49 | VideoSchema.path('podUrl').validate(customVideosValidators.isVideoPodUrlValid) |
49 | VideoSchema.path('author').validate(customValidators.isVideoAuthorValid) | 50 | VideoSchema.path('author').validate(customVideosValidators.isVideoAuthorValid) |
50 | VideoSchema.path('duration').validate(customValidators.isVideoDurationValid) | 51 | VideoSchema.path('duration').validate(customVideosValidators.isVideoDurationValid) |
51 | // The tumbnail can be the path or the data in base 64 | 52 | // The tumbnail can be the path or the data in base 64 |
52 | // The pre save hook will convert the base 64 data in a file on disk and replace the thumbnail key by the filename | 53 | // The pre save hook will convert the base 64 data in a file on disk and replace the thumbnail key by the filename |
53 | VideoSchema.path('thumbnail').validate(function (value) { | 54 | VideoSchema.path('thumbnail').validate(function (value) { |
54 | return customValidators.isVideoThumbnailValid(value) || customValidators.isVideoThumbnail64Valid(value) | 55 | return customVideosValidators.isVideoThumbnailValid(value) || customVideosValidators.isVideoThumbnail64Valid(value) |
55 | }) | 56 | }) |
56 | VideoSchema.path('tags').validate(customValidators.isVideoTagsValid) | 57 | VideoSchema.path('tags').validate(customVideosValidators.isVideoTagsValid) |
57 | 58 | ||
58 | VideoSchema.methods = { | 59 | VideoSchema.methods = { |
59 | isOwned: isOwned, | 60 | isOwned, |
60 | toFormatedJSON: toFormatedJSON, | 61 | toFormatedJSON, |
61 | toRemoteJSON: toRemoteJSON | 62 | toRemoteJSON |
62 | } | 63 | } |
63 | 64 | ||
64 | VideoSchema.statics = { | 65 | VideoSchema.statics = { |
65 | getDurationFromFile: getDurationFromFile, | 66 | getDurationFromFile, |
66 | list: list, | 67 | listForApi, |
67 | listByUrlAndMagnet: listByUrlAndMagnet, | 68 | listByUrlAndMagnet, |
68 | listByUrls: listByUrls, | 69 | listByUrls, |
69 | listOwned: listOwned, | 70 | listOwned, |
70 | listRemotes: listRemotes, | 71 | listOwnedByAuthor, |
71 | load: load, | 72 | listRemotes, |
72 | search: search | 73 | load, |
74 | search | ||
73 | } | 75 | } |
74 | 76 | ||
75 | VideoSchema.pre('remove', function (next) { | 77 | VideoSchema.pre('remove', function (next) { |
@@ -101,8 +103,8 @@ VideoSchema.pre('save', function (next) { | |||
101 | const tasks = [] | 103 | const tasks = [] |
102 | 104 | ||
103 | if (video.isOwned()) { | 105 | if (video.isOwned()) { |
104 | const videoPath = pathUtils.join(uploadsDir, video.filename) | 106 | const videoPath = pathUtils.join(constants.CONFIG.STORAGE.UPLOAD_DIR, video.filename) |
105 | this.podUrl = http + '://' + host + ':' + port | 107 | this.podUrl = constants.CONFIG.WEBSERVER.URL |
106 | 108 | ||
107 | tasks.push( | 109 | tasks.push( |
108 | // TODO: refractoring | 110 | // TODO: refractoring |
@@ -174,7 +176,7 @@ function toRemoteJSON (callback) { | |||
174 | const self = this | 176 | const self = this |
175 | 177 | ||
176 | // Convert thumbnail to base64 | 178 | // Convert thumbnail to base64 |
177 | fs.readFile(pathUtils.join(thumbnailsDir, this.thumbnail), function (err, thumbnailData) { | 179 | fs.readFile(pathUtils.join(constants.CONFIG.STORAGE.THUMBNAILS_DIR, this.thumbnail), function (err, thumbnailData) { |
178 | if (err) { | 180 | if (err) { |
179 | logger.error('Cannot read the thumbnail of the video') | 181 | logger.error('Cannot read the thumbnail of the video') |
180 | return callback(err) | 182 | return callback(err) |
@@ -207,9 +209,9 @@ function getDurationFromFile (videoPath, callback) { | |||
207 | }) | 209 | }) |
208 | } | 210 | } |
209 | 211 | ||
210 | function list (start, count, sort, callback) { | 212 | function listForApi (start, count, sort, callback) { |
211 | const query = {} | 213 | const query = {} |
212 | return findWithCount.call(this, query, start, count, sort, callback) | 214 | return modelUtils.listForApiWithCount.call(this, query, start, count, sort, callback) |
213 | } | 215 | } |
214 | 216 | ||
215 | function listByUrlAndMagnet (fromUrl, magnetUri, callback) { | 217 | function listByUrlAndMagnet (fromUrl, magnetUri, callback) { |
@@ -225,6 +227,10 @@ function listOwned (callback) { | |||
225 | this.find({ filename: { $ne: null } }, callback) | 227 | this.find({ filename: { $ne: null } }, callback) |
226 | } | 228 | } |
227 | 229 | ||
230 | function listOwnedByAuthor (author, callback) { | ||
231 | this.find({ filename: { $ne: null }, author: author }, callback) | ||
232 | } | ||
233 | |||
228 | function listRemotes (callback) { | 234 | function listRemotes (callback) { |
229 | this.find({ filename: null }, callback) | 235 | this.find({ filename: null }, callback) |
230 | } | 236 | } |
@@ -242,36 +248,17 @@ function search (value, field, start, count, sort, callback) { | |||
242 | query[field] = new RegExp(value) | 248 | query[field] = new RegExp(value) |
243 | } | 249 | } |
244 | 250 | ||
245 | findWithCount.call(this, query, start, count, sort, callback) | 251 | modelUtils.listForApiWithCount.call(this, query, start, count, sort, callback) |
246 | } | 252 | } |
247 | 253 | ||
248 | // --------------------------------------------------------------------------- | 254 | // --------------------------------------------------------------------------- |
249 | 255 | ||
250 | function findWithCount (query, start, count, sort, callback) { | ||
251 | const self = this | ||
252 | |||
253 | parallel([ | ||
254 | function (asyncCallback) { | ||
255 | self.find(query).skip(start).limit(count).sort(sort).exec(asyncCallback) | ||
256 | }, | ||
257 | function (asyncCallback) { | ||
258 | self.count(query, asyncCallback) | ||
259 | } | ||
260 | ], function (err, results) { | ||
261 | if (err) return callback(err) | ||
262 | |||
263 | const videos = results[0] | ||
264 | const totalVideos = results[1] | ||
265 | return callback(null, videos, totalVideos) | ||
266 | }) | ||
267 | } | ||
268 | |||
269 | function removeThumbnail (video, callback) { | 256 | function removeThumbnail (video, callback) { |
270 | fs.unlink(thumbnailsDir + video.thumbnail, callback) | 257 | fs.unlink(constants.CONFIG.STORAGE.THUMBNAILS_DIR + video.thumbnail, callback) |
271 | } | 258 | } |
272 | 259 | ||
273 | function removeFile (video, callback) { | 260 | function removeFile (video, callback) { |
274 | fs.unlink(uploadsDir + video.filename, callback) | 261 | fs.unlink(constants.CONFIG.STORAGE.UPLOAD_DIR + video.filename, callback) |
275 | } | 262 | } |
276 | 263 | ||
277 | // Maybe the torrent is not seeded, but we catch the error to don't stop the removing process | 264 | // Maybe the torrent is not seeded, but we catch the error to don't stop the removing process |
@@ -288,7 +275,7 @@ function createThumbnail (videoPath, callback) { | |||
288 | }) | 275 | }) |
289 | .thumbnail({ | 276 | .thumbnail({ |
290 | count: 1, | 277 | count: 1, |
291 | folder: thumbnailsDir, | 278 | folder: constants.CONFIG.STORAGE.THUMBNAILS_DIR, |
292 | size: constants.THUMBNAILS_SIZE, | 279 | size: constants.THUMBNAILS_SIZE, |
293 | filename: filename | 280 | filename: filename |
294 | }) | 281 | }) |
@@ -300,7 +287,7 @@ function generateThumbnailFromBase64 (data, callback) { | |||
300 | if (err) return callback(err) | 287 | if (err) return callback(err) |
301 | 288 | ||
302 | const thumbnailName = randomString + '.jpg' | 289 | const thumbnailName = randomString + '.jpg' |
303 | fs.writeFile(thumbnailsDir + thumbnailName, data, { encoding: 'base64' }, function (err) { | 290 | fs.writeFile(constants.CONFIG.STORAGE.THUMBNAILS_DIR + thumbnailName, data, { encoding: 'base64' }, function (err) { |
304 | if (err) return callback(err) | 291 | if (err) return callback(err) |
305 | 292 | ||
306 | return callback(null, thumbnailName) | 293 | return callback(null, thumbnailName) |