diff options
Diffstat (limited to 'server/models/video.js')
-rw-r--r-- | server/models/video.js | 74 |
1 files changed, 56 insertions, 18 deletions
diff --git a/server/models/video.js b/server/models/video.js index 04478c8d7..3ebc48ad4 100644 --- a/server/models/video.js +++ b/server/models/video.js | |||
@@ -8,10 +8,12 @@ const map = require('lodash/map') | |||
8 | const parallel = require('async/parallel') | 8 | const parallel = require('async/parallel') |
9 | const parseTorrent = require('parse-torrent') | 9 | const parseTorrent = require('parse-torrent') |
10 | const pathUtils = require('path') | 10 | const pathUtils = require('path') |
11 | const values = require('lodash/values') | ||
11 | 12 | ||
12 | const constants = require('../initializers/constants') | 13 | const constants = require('../initializers/constants') |
13 | const logger = require('../helpers/logger') | 14 | const logger = require('../helpers/logger') |
14 | const modelUtils = require('./utils') | 15 | const modelUtils = require('./utils') |
16 | const customVideosValidators = require('../helpers/custom-validators').videos | ||
15 | 17 | ||
16 | // --------------------------------------------------------------------------- | 18 | // --------------------------------------------------------------------------- |
17 | 19 | ||
@@ -22,26 +24,61 @@ module.exports = function (sequelize, DataTypes) { | |||
22 | id: { | 24 | id: { |
23 | type: DataTypes.UUID, | 25 | type: DataTypes.UUID, |
24 | defaultValue: DataTypes.UUIDV4, | 26 | defaultValue: DataTypes.UUIDV4, |
25 | primaryKey: true | 27 | primaryKey: true, |
28 | validate: { | ||
29 | isUUID: 4 | ||
30 | } | ||
26 | }, | 31 | }, |
27 | name: { | 32 | name: { |
28 | type: DataTypes.STRING | 33 | type: DataTypes.STRING, |
34 | allowNull: false, | ||
35 | validate: { | ||
36 | nameValid: function (value) { | ||
37 | const res = customVideosValidators.isVideoNameValid(value) | ||
38 | if (res === false) throw new Error('Video name is not valid.') | ||
39 | } | ||
40 | } | ||
29 | }, | 41 | }, |
30 | extname: { | 42 | extname: { |
31 | // TODO: enum? | 43 | type: DataTypes.ENUM(values(constants.CONSTRAINTS_FIELDS.VIDEOS.EXTNAME)), |
32 | type: DataTypes.STRING | 44 | allowNull: false |
33 | }, | 45 | }, |
34 | remoteId: { | 46 | remoteId: { |
35 | type: DataTypes.UUID | 47 | type: DataTypes.UUID, |
48 | allowNull: true, | ||
49 | validate: { | ||
50 | isUUID: 4 | ||
51 | } | ||
36 | }, | 52 | }, |
37 | description: { | 53 | description: { |
38 | type: DataTypes.STRING | 54 | type: DataTypes.STRING, |
55 | allowNull: false, | ||
56 | validate: { | ||
57 | descriptionValid: function (value) { | ||
58 | const res = customVideosValidators.isVideoDescriptionValid(value) | ||
59 | if (res === false) throw new Error('Video description is not valid.') | ||
60 | } | ||
61 | } | ||
39 | }, | 62 | }, |
40 | infoHash: { | 63 | infoHash: { |
41 | type: DataTypes.STRING | 64 | type: DataTypes.STRING, |
65 | allowNull: false, | ||
66 | validate: { | ||
67 | infoHashValid: function (value) { | ||
68 | const res = customVideosValidators.isVideoInfoHashValid(value) | ||
69 | if (res === false) throw new Error('Video info hash is not valid.') | ||
70 | } | ||
71 | } | ||
42 | }, | 72 | }, |
43 | duration: { | 73 | duration: { |
44 | type: DataTypes.INTEGER | 74 | type: DataTypes.INTEGER, |
75 | allowNull: false, | ||
76 | validate: { | ||
77 | durationValid: function (value) { | ||
78 | const res = customVideosValidators.isVideoDurationValid(value) | ||
79 | if (res === false) throw new Error('Video duration is not valid.') | ||
80 | } | ||
81 | } | ||
45 | } | 82 | } |
46 | }, | 83 | }, |
47 | { | 84 | { |
@@ -71,6 +108,7 @@ module.exports = function (sequelize, DataTypes) { | |||
71 | toRemoteJSON | 108 | toRemoteJSON |
72 | }, | 109 | }, |
73 | hooks: { | 110 | hooks: { |
111 | beforeValidate, | ||
74 | beforeCreate, | 112 | beforeCreate, |
75 | afterDestroy | 113 | afterDestroy |
76 | } | 114 | } |
@@ -80,13 +118,14 @@ module.exports = function (sequelize, DataTypes) { | |||
80 | return Video | 118 | return Video |
81 | } | 119 | } |
82 | 120 | ||
83 | // TODO: Validation | 121 | function beforeValidate (video, options, next) { |
84 | // VideoSchema.path('name').validate(customVideosValidators.isVideoNameValid) | 122 | if (video.isOwned()) { |
85 | // VideoSchema.path('description').validate(customVideosValidators.isVideoDescriptionValid) | 123 | // 40 hexa length |
86 | // VideoSchema.path('podHost').validate(customVideosValidators.isVideoPodHostValid) | 124 | video.infoHash = '0123456789abcdef0123456789abcdef01234567' |
87 | // VideoSchema.path('author').validate(customVideosValidators.isVideoAuthorValid) | 125 | } |
88 | // VideoSchema.path('duration').validate(customVideosValidators.isVideoDurationValid) | 126 | |
89 | // VideoSchema.path('tags').validate(customVideosValidators.isVideoTagsValid) | 127 | return next(null) |
128 | } | ||
90 | 129 | ||
91 | function beforeCreate (video, options, next) { | 130 | function beforeCreate (video, options, next) { |
92 | const tasks = [] | 131 | const tasks = [] |
@@ -113,9 +152,8 @@ function beforeCreate (video, options, next) { | |||
113 | if (err) return callback(err) | 152 | if (err) return callback(err) |
114 | 153 | ||
115 | const parsedTorrent = parseTorrent(torrent) | 154 | const parsedTorrent = parseTorrent(torrent) |
116 | video.infoHash = parsedTorrent.infoHash | 155 | video.set('infoHash', parsedTorrent.infoHash) |
117 | 156 | video.validate().asCallback(callback) | |
118 | callback(null) | ||
119 | }) | 157 | }) |
120 | }) | 158 | }) |
121 | }, | 159 | }, |