diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2016-08-19 21:34:51 +0200 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2016-08-19 21:34:51 +0200 |
commit | e861452fb26553177ad4e32bfb18b4fd8a5b1816 (patch) | |
tree | 7c0cfd464709243a452b431665f5107a973df682 | |
parent | 5c39adb7313e0696aabb4b71196ab7b0b378c359 (diff) | |
download | PeerTube-e861452fb26553177ad4e32bfb18b4fd8a5b1816.tar.gz PeerTube-e861452fb26553177ad4e32bfb18b4fd8a5b1816.tar.zst PeerTube-e861452fb26553177ad4e32bfb18b4fd8a5b1816.zip |
Server: put config in constants
-rw-r--r-- | server/controllers/api/v1/clients.js | 7 | ||||
-rw-r--r-- | server/controllers/api/v1/videos.js | 5 | ||||
-rw-r--r-- | server/helpers/logger.js | 10 | ||||
-rw-r--r-- | server/helpers/peertube-crypto.js | 29 | ||||
-rw-r--r-- | server/helpers/requests.js | 11 | ||||
-rw-r--r-- | server/initializers/constants.js | 27 | ||||
-rw-r--r-- | server/initializers/database.js | 8 | ||||
-rw-r--r-- | server/lib/friends.js | 8 | ||||
-rw-r--r-- | server/lib/webtorrent.js | 9 | ||||
-rw-r--r-- | server/models/video.js | 23 |
10 files changed, 72 insertions, 65 deletions
diff --git a/server/controllers/api/v1/clients.js b/server/controllers/api/v1/clients.js index 0d222634b..5b460db2e 100644 --- a/server/controllers/api/v1/clients.js +++ b/server/controllers/api/v1/clients.js | |||
@@ -1,9 +1,10 @@ | |||
1 | 'use strict' | 1 | 'use strict' |
2 | 2 | ||
3 | const config = require('config') | ||
4 | const express = require('express') | 3 | const express = require('express') |
5 | const mongoose = require('mongoose') | 4 | const mongoose = require('mongoose') |
6 | 5 | ||
6 | const constants = require('../../../initializers/constants') | ||
7 | |||
7 | const Client = mongoose.model('OAuthClient') | 8 | const Client = mongoose.model('OAuthClient') |
8 | 9 | ||
9 | const router = express.Router() | 10 | const router = express.Router() |
@@ -12,8 +13,8 @@ router.get('/local', getLocalClient) | |||
12 | 13 | ||
13 | // Get the client credentials for the PeerTube front end | 14 | // Get the client credentials for the PeerTube front end |
14 | function getLocalClient (req, res, next) { | 15 | function getLocalClient (req, res, next) { |
15 | const serverHost = config.get('webserver.host') | 16 | const serverHost = constants.CONFIG.WEBSERVER.HOST |
16 | const serverPort = config.get('webserver.port') | 17 | const serverPort = constants.CONFIG.WEBSERVER.PORT |
17 | let headerHostShouldBe = serverHost | 18 | let headerHostShouldBe = serverHost |
18 | if (serverPort !== 80 && serverPort !== 443) { | 19 | if (serverPort !== 80 && serverPort !== 443) { |
19 | headerHostShouldBe += ':' + serverPort | 20 | headerHostShouldBe += ':' + serverPort |
diff --git a/server/controllers/api/v1/videos.js b/server/controllers/api/v1/videos.js index 0a441f146..70d22f139 100644 --- a/server/controllers/api/v1/videos.js +++ b/server/controllers/api/v1/videos.js | |||
@@ -1,11 +1,11 @@ | |||
1 | 'use strict' | 1 | 'use strict' |
2 | 2 | ||
3 | const config = require('config') | ||
4 | const express = require('express') | 3 | const express = require('express') |
5 | const mongoose = require('mongoose') | 4 | const mongoose = require('mongoose') |
6 | const multer = require('multer') | 5 | const multer = require('multer') |
7 | const waterfall = require('async/waterfall') | 6 | const waterfall = require('async/waterfall') |
8 | 7 | ||
8 | const constants = require('../../../initializers/constants') | ||
9 | const logger = require('../../../helpers/logger') | 9 | const logger = require('../../../helpers/logger') |
10 | const friends = require('../../../lib/friends') | 10 | const friends = require('../../../lib/friends') |
11 | const middlewares = require('../../../middlewares') | 11 | const middlewares = require('../../../middlewares') |
@@ -20,13 +20,12 @@ const sort = middlewares.sort | |||
20 | const utils = require('../../../helpers/utils') | 20 | const utils = require('../../../helpers/utils') |
21 | 21 | ||
22 | const router = express.Router() | 22 | const router = express.Router() |
23 | const uploads = config.get('storage.uploads') | ||
24 | const Video = mongoose.model('Video') | 23 | const Video = mongoose.model('Video') |
25 | 24 | ||
26 | // multer configuration | 25 | // multer configuration |
27 | const storage = multer.diskStorage({ | 26 | const storage = multer.diskStorage({ |
28 | destination: function (req, file, cb) { | 27 | destination: function (req, file, cb) { |
29 | cb(null, uploads) | 28 | cb(null, constants.CONFIG.STORAGE.UPLOAD_DIR) |
30 | }, | 29 | }, |
31 | 30 | ||
32 | filename: function (req, file, cb) { | 31 | filename: function (req, file, cb) { |
diff --git a/server/helpers/logger.js b/server/helpers/logger.js index 8ae90a4b2..590ceaeb6 100644 --- a/server/helpers/logger.js +++ b/server/helpers/logger.js | |||
@@ -1,23 +1,23 @@ | |||
1 | // Thanks http://tostring.it/2014/06/23/advanced-logging-with-nodejs/ | 1 | // Thanks http://tostring.it/2014/06/23/advanced-logging-with-nodejs/ |
2 | 'use strict' | 2 | 'use strict' |
3 | 3 | ||
4 | const config = require('config') | ||
5 | const mkdirp = require('mkdirp') | 4 | const mkdirp = require('mkdirp') |
6 | const path = require('path') | 5 | const path = require('path') |
7 | const winston = require('winston') | 6 | const winston = require('winston') |
8 | winston.emitErrs = true | 7 | winston.emitErrs = true |
9 | 8 | ||
10 | const logDir = path.join(__dirname, '..', '..', config.get('storage.logs')) | 9 | const constants = require('../initializers/constants') |
11 | const label = config.get('webserver.host') + ':' + config.get('webserver.port') | 10 | |
11 | const label = constants.CONFIG.WEBSERVER.HOST + ':' + constants.CONFIG.WEBSERVER.PORT | ||
12 | 12 | ||
13 | // Create the directory if it does not exist | 13 | // Create the directory if it does not exist |
14 | mkdirp.sync(logDir) | 14 | mkdirp.sync(constants.CONFIG.STORAGE.LOG_DIR) |
15 | 15 | ||
16 | const logger = new winston.Logger({ | 16 | const logger = new winston.Logger({ |
17 | transports: [ | 17 | transports: [ |
18 | new winston.transports.File({ | 18 | new winston.transports.File({ |
19 | level: 'debug', | 19 | level: 'debug', |
20 | filename: path.join(logDir, 'all-logs.log'), | 20 | filename: path.join(constants.CONFIG.STORAGE.LOG_DIR, 'all-logs.log'), |
21 | handleExceptions: true, | 21 | handleExceptions: true, |
22 | json: true, | 22 | json: true, |
23 | maxsize: 5242880, | 23 | maxsize: 5242880, |
diff --git a/server/helpers/peertube-crypto.js b/server/helpers/peertube-crypto.js index 46dff8d03..ef130ea5c 100644 --- a/server/helpers/peertube-crypto.js +++ b/server/helpers/peertube-crypto.js | |||
@@ -1,15 +1,13 @@ | |||
1 | 'use strict' | 1 | 'use strict' |
2 | 2 | ||
3 | const config = require('config') | ||
4 | const crypto = require('crypto') | 3 | const crypto = require('crypto') |
5 | const fs = require('fs') | 4 | const fs = require('fs') |
6 | const openssl = require('openssl-wrapper') | 5 | const openssl = require('openssl-wrapper') |
7 | const path = require('path') | ||
8 | const ursa = require('ursa') | 6 | const ursa = require('ursa') |
9 | 7 | ||
8 | const constants = require('../initializers/constants') | ||
10 | const logger = require('./logger') | 9 | const logger = require('./logger') |
11 | 10 | ||
12 | const certDir = path.join(__dirname, '..', '..', config.get('storage.certs')) | ||
13 | const algorithm = 'aes-256-ctr' | 11 | const algorithm = 'aes-256-ctr' |
14 | 12 | ||
15 | const peertubeCrypto = { | 13 | const peertubeCrypto = { |
@@ -17,7 +15,6 @@ const peertubeCrypto = { | |||
17 | createCertsIfNotExist: createCertsIfNotExist, | 15 | createCertsIfNotExist: createCertsIfNotExist, |
18 | decrypt: decrypt, | 16 | decrypt: decrypt, |
19 | encrypt: encrypt, | 17 | encrypt: encrypt, |
20 | getCertDir: getCertDir, | ||
21 | sign: sign | 18 | sign: sign |
22 | } | 19 | } |
23 | 20 | ||
@@ -40,7 +37,7 @@ function createCertsIfNotExist (callback) { | |||
40 | } | 37 | } |
41 | 38 | ||
42 | function decrypt (key, data, callback) { | 39 | function decrypt (key, data, callback) { |
43 | fs.readFile(getCertDir() + 'peertube.key.pem', function (err, file) { | 40 | fs.readFile(constants.CONFIG.STORAGE.CERT_DIR + 'peertube.key.pem', function (err, file) { |
44 | if (err) return callback(err) | 41 | if (err) return callback(err) |
45 | 42 | ||
46 | const myPrivateKey = ursa.createPrivateKey(file) | 43 | const myPrivateKey = ursa.createPrivateKey(file) |
@@ -67,12 +64,8 @@ function encrypt (publicKey, data, callback) { | |||
67 | }) | 64 | }) |
68 | } | 65 | } |
69 | 66 | ||
70 | function getCertDir () { | ||
71 | return certDir | ||
72 | } | ||
73 | |||
74 | function sign (data) { | 67 | function sign (data) { |
75 | const myKey = ursa.createPrivateKey(fs.readFileSync(certDir + 'peertube.key.pem')) | 68 | const myKey = ursa.createPrivateKey(fs.readFileSync(constants.CONFIG.STORAGE.CERT_DIR + 'peertube.key.pem')) |
76 | const signature = myKey.hashAndSign('sha256', data, 'utf8', 'hex') | 69 | const signature = myKey.hashAndSign('sha256', data, 'utf8', 'hex') |
77 | 70 | ||
78 | return signature | 71 | return signature |
@@ -85,7 +78,7 @@ module.exports = peertubeCrypto | |||
85 | // --------------------------------------------------------------------------- | 78 | // --------------------------------------------------------------------------- |
86 | 79 | ||
87 | function certsExist (callback) { | 80 | function certsExist (callback) { |
88 | fs.exists(certDir + 'peertube.key.pem', function (exists) { | 81 | fs.exists(constants.CONFIG.STORAGE.CERT_DIR + 'peertube.key.pem', function (exists) { |
89 | return callback(exists) | 82 | return callback(exists) |
90 | }) | 83 | }) |
91 | } | 84 | } |
@@ -99,15 +92,25 @@ function createCerts (callback) { | |||
99 | } | 92 | } |
100 | 93 | ||
101 | logger.info('Generating a RSA key...') | 94 | logger.info('Generating a RSA key...') |
102 | openssl.exec('genrsa', { 'out': certDir + 'peertube.key.pem', '2048': false }, function (err) { | 95 | |
96 | let options = { | ||
97 | 'out': constants.CONFIG.STORAGE.CERT_DIR + 'peertube.key.pem', | ||
98 | '2048': false | ||
99 | } | ||
100 | openssl.exec('genrsa', options, function (err) { | ||
103 | if (err) { | 101 | if (err) { |
104 | logger.error('Cannot create private key on this pod.') | 102 | logger.error('Cannot create private key on this pod.') |
105 | return callback(err) | 103 | return callback(err) |
106 | } | 104 | } |
107 | logger.info('RSA key generated.') | 105 | logger.info('RSA key generated.') |
108 | 106 | ||
107 | options = { | ||
108 | 'in': constants.CONFIG.STORAGE.CERT_DIR + 'peertube.key.pem', | ||
109 | 'pubout': true, | ||
110 | 'out': constants.CONFIG.STORAGE.CERT_DIR + 'peertube.pub' | ||
111 | } | ||
109 | logger.info('Manage public key...') | 112 | logger.info('Manage public key...') |
110 | openssl.exec('rsa', { 'in': certDir + 'peertube.key.pem', 'pubout': true, 'out': certDir + 'peertube.pub' }, function (err) { | 113 | openssl.exec('rsa', options, function (err) { |
111 | if (err) { | 114 | if (err) { |
112 | logger.error('Cannot create public key on this pod.') | 115 | logger.error('Cannot create public key on this pod.') |
113 | return callback(err) | 116 | return callback(err) |
diff --git a/server/helpers/requests.js b/server/helpers/requests.js index 547230adc..f76ff3473 100644 --- a/server/helpers/requests.js +++ b/server/helpers/requests.js | |||
@@ -1,16 +1,11 @@ | |||
1 | 'use strict' | 1 | 'use strict' |
2 | 2 | ||
3 | const config = require('config') | ||
4 | const replay = require('request-replay') | 3 | const replay = require('request-replay') |
5 | const request = require('request') | 4 | const request = require('request') |
6 | 5 | ||
7 | const constants = require('../initializers/constants') | 6 | const constants = require('../initializers/constants') |
8 | const peertubeCrypto = require('./peertube-crypto') | 7 | const peertubeCrypto = require('./peertube-crypto') |
9 | 8 | ||
10 | const http = config.get('webserver.https') ? 'https' : 'http' | ||
11 | const host = config.get('webserver.host') | ||
12 | const port = config.get('webserver.port') | ||
13 | |||
14 | const requests = { | 9 | const requests = { |
15 | makeRetryRequest: makeRetryRequest, | 10 | makeRetryRequest: makeRetryRequest, |
16 | makeSecureRequest: makeSecureRequest | 11 | makeSecureRequest: makeSecureRequest |
@@ -29,8 +24,6 @@ function makeRetryRequest (params, callback) { | |||
29 | } | 24 | } |
30 | 25 | ||
31 | function makeSecureRequest (params, callback) { | 26 | function makeSecureRequest (params, callback) { |
32 | const myUrl = http + '://' + host + ':' + port | ||
33 | |||
34 | const requestParams = { | 27 | const requestParams = { |
35 | url: params.toPod.url + params.path | 28 | url: params.toPod.url + params.path |
36 | } | 29 | } |
@@ -42,8 +35,8 @@ function makeSecureRequest (params, callback) { | |||
42 | // Add signature if it is specified in the params | 35 | // Add signature if it is specified in the params |
43 | if (params.sign === true) { | 36 | if (params.sign === true) { |
44 | requestParams.json.signature = { | 37 | requestParams.json.signature = { |
45 | url: myUrl, | 38 | url: constants.CONFIG.WEBSERVER.URL, |
46 | signature: peertubeCrypto.sign(myUrl) | 39 | signature: peertubeCrypto.sign(constants.CONFIG.WEBSERVER.URL) |
47 | } | 40 | } |
48 | } | 41 | } |
49 | 42 | ||
diff --git a/server/initializers/constants.js b/server/initializers/constants.js index cd2e0cfb9..ce9f8ad6c 100644 --- a/server/initializers/constants.js +++ b/server/initializers/constants.js | |||
@@ -1,8 +1,34 @@ | |||
1 | 'use strict' | 1 | 'use strict' |
2 | 2 | ||
3 | const config = require('config') | ||
4 | const path = require('path') | ||
5 | |||
3 | // API version of our pod | 6 | // API version of our pod |
4 | const API_VERSION = 'v1' | 7 | const API_VERSION = 'v1' |
5 | 8 | ||
9 | const CONFIG = { | ||
10 | DATABASE: { | ||
11 | DBNAME: 'peertube' + config.get('database.suffix'), | ||
12 | HOST: config.get('database.host'), | ||
13 | PORT: config.get('database.port') | ||
14 | }, | ||
15 | ELECTRON: { | ||
16 | DEBUG: config.get('electron.debug') | ||
17 | }, | ||
18 | STORAGE: { | ||
19 | CERT_DIR: path.join(__dirname, '..', '..', config.get('storage.certs')), | ||
20 | LOG_DIR: path.join(__dirname, '..', '..', config.get('storage.logs')), | ||
21 | UPLOAD_DIR: path.join(__dirname, '..', '..', config.get('storage.uploads')), | ||
22 | THUMBNAILS_DIR: path.join(__dirname, '..', '..', config.get('storage.thumbnails')) | ||
23 | }, | ||
24 | WEBSERVER: { | ||
25 | SCHEME: config.get('webserver.https') === true ? 'https' : 'http', | ||
26 | HOST: config.get('webserver.host'), | ||
27 | PORT: config.get('webserver.port') | ||
28 | } | ||
29 | } | ||
30 | CONFIG.WEBSERVER.URL = CONFIG.WEBSERVER.SCHEME + '://' + CONFIG.WEBSERVER.HOST + ':' + CONFIG.WEBSERVER.PORT | ||
31 | |||
6 | const CONSTRAINTS_FIELDS = { | 32 | const CONSTRAINTS_FIELDS = { |
7 | USERS: { | 33 | USERS: { |
8 | USERNAME: { min: 3, max: 20 }, // Length | 34 | USERNAME: { min: 3, max: 20 }, // Length |
@@ -89,6 +115,7 @@ if (isTestInstance() === true) { | |||
89 | 115 | ||
90 | module.exports = { | 116 | module.exports = { |
91 | API_VERSION: API_VERSION, | 117 | API_VERSION: API_VERSION, |
118 | CONFIG: CONFIG, | ||
92 | CONSTRAINTS_FIELDS: CONSTRAINTS_FIELDS, | 119 | CONSTRAINTS_FIELDS: CONSTRAINTS_FIELDS, |
93 | FRIEND_SCORE: FRIEND_SCORE, | 120 | FRIEND_SCORE: FRIEND_SCORE, |
94 | INTERVAL: INTERVAL, | 121 | INTERVAL: INTERVAL, |
diff --git a/server/initializers/database.js b/server/initializers/database.js index 8626895ee..20dcc056e 100644 --- a/server/initializers/database.js +++ b/server/initializers/database.js | |||
@@ -1,8 +1,8 @@ | |||
1 | 'use strict' | 1 | 'use strict' |
2 | 2 | ||
3 | const config = require('config') | ||
4 | const mongoose = require('mongoose') | 3 | const mongoose = require('mongoose') |
5 | 4 | ||
5 | const constants = require('../initializers/constants') | ||
6 | const logger = require('../helpers/logger') | 6 | const logger = require('../helpers/logger') |
7 | 7 | ||
8 | // Bootstrap models | 8 | // Bootstrap models |
@@ -14,17 +14,13 @@ require('../models/video') | |||
14 | // Request model needs Video model | 14 | // Request model needs Video model |
15 | require('../models/request') | 15 | require('../models/request') |
16 | 16 | ||
17 | const dbname = 'peertube' + config.get('database.suffix') | ||
18 | const host = config.get('database.host') | ||
19 | const port = config.get('database.port') | ||
20 | |||
21 | const database = { | 17 | const database = { |
22 | connect: connect | 18 | connect: connect |
23 | } | 19 | } |
24 | 20 | ||
25 | function connect () { | 21 | function connect () { |
26 | mongoose.Promise = global.Promise | 22 | mongoose.Promise = global.Promise |
27 | mongoose.connect('mongodb://' + host + ':' + port + '/' + dbname) | 23 | mongoose.connect('mongodb://' + constants.CONFIG.DATABASE.HOST + ':' + constants.CONFIG.DATABASE.PORT + '/' + constants.CONFIG.DATABASE.DBNAME) |
28 | mongoose.connection.on('error', function () { | 24 | mongoose.connection.on('error', function () { |
29 | throw new Error('Mongodb connection error.') | 25 | throw new Error('Mongodb connection error.') |
30 | }) | 26 | }) |
diff --git a/server/lib/friends.js b/server/lib/friends.js index 6e1516b94..6a2c37fd7 100644 --- a/server/lib/friends.js +++ b/server/lib/friends.js | |||
@@ -11,12 +11,8 @@ const waterfall = require('async/waterfall') | |||
11 | 11 | ||
12 | const constants = require('../initializers/constants') | 12 | const constants = require('../initializers/constants') |
13 | const logger = require('../helpers/logger') | 13 | const logger = require('../helpers/logger') |
14 | const peertubeCrypto = require('../helpers/peertube-crypto') | ||
15 | const requests = require('../helpers/requests') | 14 | const requests = require('../helpers/requests') |
16 | 15 | ||
17 | const http = config.get('webserver.https') ? 'https' : 'http' | ||
18 | const host = config.get('webserver.host') | ||
19 | const port = config.get('webserver.port') | ||
20 | const Pod = mongoose.model('Pod') | 16 | const Pod = mongoose.model('Pod') |
21 | const Request = mongoose.model('Request') | 17 | const Request = mongoose.model('Request') |
22 | const Video = mongoose.model('Video') | 18 | const Video = mongoose.model('Video') |
@@ -45,7 +41,7 @@ function hasFriends (callback) { | |||
45 | } | 41 | } |
46 | 42 | ||
47 | function getMyCertificate (callback) { | 43 | function getMyCertificate (callback) { |
48 | fs.readFile(peertubeCrypto.getCertDir() + 'peertube.pub', 'utf8', callback) | 44 | fs.readFile(constants.CONFIG.STORAGE.CERT_DIR + 'peertube.pub', 'utf8', callback) |
49 | } | 45 | } |
50 | 46 | ||
51 | function makeFriends (callback) { | 47 | function makeFriends (callback) { |
@@ -220,7 +216,7 @@ function makeRequestsToWinningPods (cert, podsList, callback) { | |||
220 | url: pod.url + '/api/' + constants.API_VERSION + '/pods/', | 216 | url: pod.url + '/api/' + constants.API_VERSION + '/pods/', |
221 | method: 'POST', | 217 | method: 'POST', |
222 | json: { | 218 | json: { |
223 | url: http + '://' + host + ':' + port, | 219 | url: constants.CONFIG.WEBSERVER.URL, |
224 | publicKey: cert | 220 | publicKey: cert |
225 | } | 221 | } |
226 | } | 222 | } |
diff --git a/server/lib/webtorrent.js b/server/lib/webtorrent.js index bcd30139e..2090b792b 100644 --- a/server/lib/webtorrent.js +++ b/server/lib/webtorrent.js | |||
@@ -1,15 +1,14 @@ | |||
1 | 'use strict' | 1 | 'use strict' |
2 | 2 | ||
3 | const config = require('config') | ||
4 | const ipc = require('node-ipc') | 3 | const ipc = require('node-ipc') |
5 | const pathUtils = require('path') | 4 | const pathUtils = require('path') |
6 | const spawn = require('electron-spawn') | 5 | const spawn = require('electron-spawn') |
7 | 6 | ||
7 | const constants = require('../initializers/constants') | ||
8 | const logger = require('../helpers/logger') | 8 | const logger = require('../helpers/logger') |
9 | 9 | ||
10 | const electronDebug = config.get('electron.debug') | 10 | let host = constants.CONFIG.WEBSERVER.HOST |
11 | let host = config.get('webserver.host') | 11 | let port = constants.CONFIG.WEBSERVER.PORT |
12 | let port = config.get('webserver.port') | ||
13 | let nodeKey = 'webtorrentnode' + port | 12 | let nodeKey = 'webtorrentnode' + port |
14 | let processKey = 'webtorrentprocess' + port | 13 | let processKey = 'webtorrentprocess' + port |
15 | ipc.config.silent = true | 14 | ipc.config.silent = true |
@@ -59,7 +58,7 @@ function create (options, callback) { | |||
59 | 58 | ||
60 | const webtorrentProcess = spawn(pathUtils.join(__dirname, 'webtorrent-process.js'), host, port, { detached: true }) | 59 | const webtorrentProcess = spawn(pathUtils.join(__dirname, 'webtorrent-process.js'), host, port, { detached: true }) |
61 | 60 | ||
62 | if (electronDebug === true) { | 61 | if (constants.CONFIG.ELECTRON.DEBUG === true) { |
63 | webtorrentProcess.stderr.on('data', function (data) { | 62 | webtorrentProcess.stderr.on('data', function (data) { |
64 | logger.debug('Webtorrent process stderr: ', data.toString()) | 63 | logger.debug('Webtorrent process stderr: ', data.toString()) |
65 | }) | 64 | }) |
diff --git a/server/models/video.js b/server/models/video.js index 63afc2efe..0f60b6cd4 100644 --- a/server/models/video.js +++ b/server/models/video.js | |||
@@ -1,6 +1,5 @@ | |||
1 | 'use strict' | 1 | 'use strict' |
2 | 2 | ||
3 | const config = require('config') | ||
4 | const eachLimit = require('async/eachLimit') | 3 | const eachLimit = require('async/eachLimit') |
5 | const ffmpeg = require('fluent-ffmpeg') | 4 | const ffmpeg = require('fluent-ffmpeg') |
6 | const fs = require('fs') | 5 | const fs = require('fs') |
@@ -15,12 +14,6 @@ const modelUtils = require('./utils') | |||
15 | const utils = require('../helpers/utils') | 14 | const utils = require('../helpers/utils') |
16 | const webtorrent = require('../lib/webtorrent') | 15 | const webtorrent = require('../lib/webtorrent') |
17 | 16 | ||
18 | const http = config.get('webserver.https') === true ? 'https' : 'http' | ||
19 | const host = config.get('webserver.host') | ||
20 | const port = config.get('webserver.port') | ||
21 | const uploadsDir = pathUtils.join(__dirname, '..', '..', config.get('storage.uploads')) | ||
22 | const thumbnailsDir = pathUtils.join(__dirname, '..', '..', config.get('storage.thumbnails')) | ||
23 | |||
24 | // --------------------------------------------------------------------------- | 17 | // --------------------------------------------------------------------------- |
25 | 18 | ||
26 | // TODO: add indexes on searchable columns | 19 | // TODO: add indexes on searchable columns |
@@ -101,8 +94,8 @@ VideoSchema.pre('save', function (next) { | |||
101 | const tasks = [] | 94 | const tasks = [] |
102 | 95 | ||
103 | if (video.isOwned()) { | 96 | if (video.isOwned()) { |
104 | const videoPath = pathUtils.join(uploadsDir, video.filename) | 97 | const videoPath = pathUtils.join(constants.CONFIG.STORAGE.UPLOAD_DIR, video.filename) |
105 | this.podUrl = http + '://' + host + ':' + port | 98 | this.podUrl = constants.CONFIG.WEBSERVER.URL |
106 | 99 | ||
107 | tasks.push( | 100 | tasks.push( |
108 | function (callback) { | 101 | function (callback) { |
@@ -162,7 +155,7 @@ function toRemoteJSON (callback) { | |||
162 | const self = this | 155 | const self = this |
163 | 156 | ||
164 | // Convert thumbnail to base64 | 157 | // Convert thumbnail to base64 |
165 | fs.readFile(pathUtils.join(thumbnailsDir, this.thumbnail), function (err, thumbnailData) { | 158 | fs.readFile(pathUtils.join(constants.CONFIG.STORAGE.THUMBNAILS_DIR, this.thumbnail), function (err, thumbnailData) { |
166 | if (err) { | 159 | if (err) { |
167 | logger.error('Cannot read the thumbnail of the video') | 160 | logger.error('Cannot read the thumbnail of the video') |
168 | return callback(err) | 161 | return callback(err) |
@@ -242,7 +235,7 @@ function seedAllExisting (callback) { | |||
242 | if (err) return callback(err) | 235 | if (err) return callback(err) |
243 | 236 | ||
244 | eachLimit(videos, constants.SEEDS_IN_PARALLEL, function (video, callbackEach) { | 237 | eachLimit(videos, constants.SEEDS_IN_PARALLEL, function (video, callbackEach) { |
245 | const videoPath = pathUtils.join(uploadsDir, video.filename) | 238 | const videoPath = pathUtils.join(constants.CONFIG.STORAGE.UPLOAD_DIR, video.filename) |
246 | seed(videoPath, callbackEach) | 239 | seed(videoPath, callbackEach) |
247 | }, callback) | 240 | }, callback) |
248 | }) | 241 | }) |
@@ -251,11 +244,11 @@ function seedAllExisting (callback) { | |||
251 | // --------------------------------------------------------------------------- | 244 | // --------------------------------------------------------------------------- |
252 | 245 | ||
253 | function removeThumbnail (video, callback) { | 246 | function removeThumbnail (video, callback) { |
254 | fs.unlink(thumbnailsDir + video.thumbnail, callback) | 247 | fs.unlink(constants.CONFIG.STORAGE.THUMBNAILS_DIR + video.thumbnail, callback) |
255 | } | 248 | } |
256 | 249 | ||
257 | function removeFile (video, callback) { | 250 | function removeFile (video, callback) { |
258 | fs.unlink(uploadsDir + video.filename, callback) | 251 | fs.unlink(constants.CONFIG.STORAGE.UPLOAD_DIR + video.filename, callback) |
259 | } | 252 | } |
260 | 253 | ||
261 | // Maybe the torrent is not seeded, but we catch the error to don't stop the removing process | 254 | // Maybe the torrent is not seeded, but we catch the error to don't stop the removing process |
@@ -277,7 +270,7 @@ function createThumbnail (videoPath, callback) { | |||
277 | }) | 270 | }) |
278 | .thumbnail({ | 271 | .thumbnail({ |
279 | count: 1, | 272 | count: 1, |
280 | folder: thumbnailsDir, | 273 | folder: constants.CONFIG.STORAGE.THUMBNAILS_DIR, |
281 | size: constants.THUMBNAILS_SIZE, | 274 | size: constants.THUMBNAILS_SIZE, |
282 | filename: filename | 275 | filename: filename |
283 | }) | 276 | }) |
@@ -299,7 +292,7 @@ function generateThumbnailFromBase64 (data, callback) { | |||
299 | if (err) return callback(err) | 292 | if (err) return callback(err) |
300 | 293 | ||
301 | const thumbnailName = randomString + '.jpg' | 294 | const thumbnailName = randomString + '.jpg' |
302 | fs.writeFile(thumbnailsDir + thumbnailName, data, { encoding: 'base64' }, function (err) { | 295 | fs.writeFile(constants.CONFIG.STORAGE.THUMBNAILS_DIR + thumbnailName, data, { encoding: 'base64' }, function (err) { |
303 | if (err) return callback(err) | 296 | if (err) return callback(err) |
304 | 297 | ||
305 | return callback(null, thumbnailName) | 298 | return callback(null, thumbnailName) |