'use strict'
-const config = require('config')
const express = require('express')
const mongoose = require('mongoose')
+const constants = require('../../../initializers/constants')
+
const Client = mongoose.model('OAuthClient')
const router = express.Router()
// Get the client credentials for the PeerTube front end
function getLocalClient (req, res, next) {
- const serverHost = config.get('webserver.host')
- const serverPort = config.get('webserver.port')
+ const serverHost = constants.CONFIG.WEBSERVER.HOST
+ const serverPort = constants.CONFIG.WEBSERVER.PORT
let headerHostShouldBe = serverHost
if (serverPort !== 80 && serverPort !== 443) {
headerHostShouldBe += ':' + serverPort
'use strict'
-const config = require('config')
const express = require('express')
const mongoose = require('mongoose')
const multer = require('multer')
const waterfall = require('async/waterfall')
+const constants = require('../../../initializers/constants')
const logger = require('../../../helpers/logger')
const friends = require('../../../lib/friends')
const middlewares = require('../../../middlewares')
const utils = require('../../../helpers/utils')
const router = express.Router()
-const uploads = config.get('storage.uploads')
const Video = mongoose.model('Video')
// multer configuration
const storage = multer.diskStorage({
destination: function (req, file, cb) {
- cb(null, uploads)
+ cb(null, constants.CONFIG.STORAGE.UPLOAD_DIR)
},
filename: function (req, file, cb) {
// Thanks http://tostring.it/2014/06/23/advanced-logging-with-nodejs/
'use strict'
-const config = require('config')
const mkdirp = require('mkdirp')
const path = require('path')
const winston = require('winston')
winston.emitErrs = true
-const logDir = path.join(__dirname, '..', '..', config.get('storage.logs'))
-const label = config.get('webserver.host') + ':' + config.get('webserver.port')
+const constants = require('../initializers/constants')
+
+const label = constants.CONFIG.WEBSERVER.HOST + ':' + constants.CONFIG.WEBSERVER.PORT
// Create the directory if it does not exist
-mkdirp.sync(logDir)
+mkdirp.sync(constants.CONFIG.STORAGE.LOG_DIR)
const logger = new winston.Logger({
transports: [
new winston.transports.File({
level: 'debug',
- filename: path.join(logDir, 'all-logs.log'),
+ filename: path.join(constants.CONFIG.STORAGE.LOG_DIR, 'all-logs.log'),
handleExceptions: true,
json: true,
maxsize: 5242880,
'use strict'
-const config = require('config')
const crypto = require('crypto')
const fs = require('fs')
const openssl = require('openssl-wrapper')
-const path = require('path')
const ursa = require('ursa')
+const constants = require('../initializers/constants')
const logger = require('./logger')
-const certDir = path.join(__dirname, '..', '..', config.get('storage.certs'))
const algorithm = 'aes-256-ctr'
const peertubeCrypto = {
createCertsIfNotExist: createCertsIfNotExist,
decrypt: decrypt,
encrypt: encrypt,
- getCertDir: getCertDir,
sign: sign
}
}
function decrypt (key, data, callback) {
- fs.readFile(getCertDir() + 'peertube.key.pem', function (err, file) {
+ fs.readFile(constants.CONFIG.STORAGE.CERT_DIR + 'peertube.key.pem', function (err, file) {
if (err) return callback(err)
const myPrivateKey = ursa.createPrivateKey(file)
})
}
-function getCertDir () {
- return certDir
-}
-
function sign (data) {
- const myKey = ursa.createPrivateKey(fs.readFileSync(certDir + 'peertube.key.pem'))
+ const myKey = ursa.createPrivateKey(fs.readFileSync(constants.CONFIG.STORAGE.CERT_DIR + 'peertube.key.pem'))
const signature = myKey.hashAndSign('sha256', data, 'utf8', 'hex')
return signature
// ---------------------------------------------------------------------------
function certsExist (callback) {
- fs.exists(certDir + 'peertube.key.pem', function (exists) {
+ fs.exists(constants.CONFIG.STORAGE.CERT_DIR + 'peertube.key.pem', function (exists) {
return callback(exists)
})
}
}
logger.info('Generating a RSA key...')
- openssl.exec('genrsa', { 'out': certDir + 'peertube.key.pem', '2048': false }, function (err) {
+
+ let options = {
+ 'out': constants.CONFIG.STORAGE.CERT_DIR + 'peertube.key.pem',
+ '2048': false
+ }
+ openssl.exec('genrsa', options, function (err) {
if (err) {
logger.error('Cannot create private key on this pod.')
return callback(err)
}
logger.info('RSA key generated.')
+ options = {
+ 'in': constants.CONFIG.STORAGE.CERT_DIR + 'peertube.key.pem',
+ 'pubout': true,
+ 'out': constants.CONFIG.STORAGE.CERT_DIR + 'peertube.pub'
+ }
logger.info('Manage public key...')
- openssl.exec('rsa', { 'in': certDir + 'peertube.key.pem', 'pubout': true, 'out': certDir + 'peertube.pub' }, function (err) {
+ openssl.exec('rsa', options, function (err) {
if (err) {
logger.error('Cannot create public key on this pod.')
return callback(err)
'use strict'
-const config = require('config')
const replay = require('request-replay')
const request = require('request')
const constants = require('../initializers/constants')
const peertubeCrypto = require('./peertube-crypto')
-const http = config.get('webserver.https') ? 'https' : 'http'
-const host = config.get('webserver.host')
-const port = config.get('webserver.port')
-
const requests = {
makeRetryRequest: makeRetryRequest,
makeSecureRequest: makeSecureRequest
}
function makeSecureRequest (params, callback) {
- const myUrl = http + '://' + host + ':' + port
-
const requestParams = {
url: params.toPod.url + params.path
}
// Add signature if it is specified in the params
if (params.sign === true) {
requestParams.json.signature = {
- url: myUrl,
- signature: peertubeCrypto.sign(myUrl)
+ url: constants.CONFIG.WEBSERVER.URL,
+ signature: peertubeCrypto.sign(constants.CONFIG.WEBSERVER.URL)
}
}
'use strict'
+const config = require('config')
+const path = require('path')
+
// API version of our pod
const API_VERSION = 'v1'
+const CONFIG = {
+ DATABASE: {
+ DBNAME: 'peertube' + config.get('database.suffix'),
+ HOST: config.get('database.host'),
+ PORT: config.get('database.port')
+ },
+ ELECTRON: {
+ DEBUG: config.get('electron.debug')
+ },
+ STORAGE: {
+ CERT_DIR: path.join(__dirname, '..', '..', config.get('storage.certs')),
+ LOG_DIR: path.join(__dirname, '..', '..', config.get('storage.logs')),
+ UPLOAD_DIR: path.join(__dirname, '..', '..', config.get('storage.uploads')),
+ THUMBNAILS_DIR: path.join(__dirname, '..', '..', config.get('storage.thumbnails'))
+ },
+ WEBSERVER: {
+ SCHEME: config.get('webserver.https') === true ? 'https' : 'http',
+ HOST: config.get('webserver.host'),
+ PORT: config.get('webserver.port')
+ }
+}
+CONFIG.WEBSERVER.URL = CONFIG.WEBSERVER.SCHEME + '://' + CONFIG.WEBSERVER.HOST + ':' + CONFIG.WEBSERVER.PORT
+
const CONSTRAINTS_FIELDS = {
USERS: {
USERNAME: { min: 3, max: 20 }, // Length
module.exports = {
API_VERSION: API_VERSION,
+ CONFIG: CONFIG,
CONSTRAINTS_FIELDS: CONSTRAINTS_FIELDS,
FRIEND_SCORE: FRIEND_SCORE,
INTERVAL: INTERVAL,
'use strict'
-const config = require('config')
const mongoose = require('mongoose')
+const constants = require('../initializers/constants')
const logger = require('../helpers/logger')
// Bootstrap models
// Request model needs Video model
require('../models/request')
-const dbname = 'peertube' + config.get('database.suffix')
-const host = config.get('database.host')
-const port = config.get('database.port')
-
const database = {
connect: connect
}
function connect () {
mongoose.Promise = global.Promise
- mongoose.connect('mongodb://' + host + ':' + port + '/' + dbname)
+ mongoose.connect('mongodb://' + constants.CONFIG.DATABASE.HOST + ':' + constants.CONFIG.DATABASE.PORT + '/' + constants.CONFIG.DATABASE.DBNAME)
mongoose.connection.on('error', function () {
throw new Error('Mongodb connection error.')
})
const constants = require('../initializers/constants')
const logger = require('../helpers/logger')
-const peertubeCrypto = require('../helpers/peertube-crypto')
const requests = require('../helpers/requests')
-const http = config.get('webserver.https') ? 'https' : 'http'
-const host = config.get('webserver.host')
-const port = config.get('webserver.port')
const Pod = mongoose.model('Pod')
const Request = mongoose.model('Request')
const Video = mongoose.model('Video')
}
function getMyCertificate (callback) {
- fs.readFile(peertubeCrypto.getCertDir() + 'peertube.pub', 'utf8', callback)
+ fs.readFile(constants.CONFIG.STORAGE.CERT_DIR + 'peertube.pub', 'utf8', callback)
}
function makeFriends (callback) {
url: pod.url + '/api/' + constants.API_VERSION + '/pods/',
method: 'POST',
json: {
- url: http + '://' + host + ':' + port,
+ url: constants.CONFIG.WEBSERVER.URL,
publicKey: cert
}
}
'use strict'
-const config = require('config')
const ipc = require('node-ipc')
const pathUtils = require('path')
const spawn = require('electron-spawn')
+const constants = require('../initializers/constants')
const logger = require('../helpers/logger')
-const electronDebug = config.get('electron.debug')
-let host = config.get('webserver.host')
-let port = config.get('webserver.port')
+let host = constants.CONFIG.WEBSERVER.HOST
+let port = constants.CONFIG.WEBSERVER.PORT
let nodeKey = 'webtorrentnode' + port
let processKey = 'webtorrentprocess' + port
ipc.config.silent = true
const webtorrentProcess = spawn(pathUtils.join(__dirname, 'webtorrent-process.js'), host, port, { detached: true })
- if (electronDebug === true) {
+ if (constants.CONFIG.ELECTRON.DEBUG === true) {
webtorrentProcess.stderr.on('data', function (data) {
logger.debug('Webtorrent process stderr: ', data.toString())
})
'use strict'
-const config = require('config')
const eachLimit = require('async/eachLimit')
const ffmpeg = require('fluent-ffmpeg')
const fs = require('fs')
const utils = require('../helpers/utils')
const webtorrent = require('../lib/webtorrent')
-const http = config.get('webserver.https') === true ? 'https' : 'http'
-const host = config.get('webserver.host')
-const port = config.get('webserver.port')
-const uploadsDir = pathUtils.join(__dirname, '..', '..', config.get('storage.uploads'))
-const thumbnailsDir = pathUtils.join(__dirname, '..', '..', config.get('storage.thumbnails'))
-
// ---------------------------------------------------------------------------
// TODO: add indexes on searchable columns
const tasks = []
if (video.isOwned()) {
- const videoPath = pathUtils.join(uploadsDir, video.filename)
- this.podUrl = http + '://' + host + ':' + port
+ const videoPath = pathUtils.join(constants.CONFIG.STORAGE.UPLOAD_DIR, video.filename)
+ this.podUrl = constants.CONFIG.WEBSERVER.URL
tasks.push(
function (callback) {
const self = this
// Convert thumbnail to base64
- fs.readFile(pathUtils.join(thumbnailsDir, this.thumbnail), function (err, thumbnailData) {
+ fs.readFile(pathUtils.join(constants.CONFIG.STORAGE.THUMBNAILS_DIR, this.thumbnail), function (err, thumbnailData) {
if (err) {
logger.error('Cannot read the thumbnail of the video')
return callback(err)
if (err) return callback(err)
eachLimit(videos, constants.SEEDS_IN_PARALLEL, function (video, callbackEach) {
- const videoPath = pathUtils.join(uploadsDir, video.filename)
+ const videoPath = pathUtils.join(constants.CONFIG.STORAGE.UPLOAD_DIR, video.filename)
seed(videoPath, callbackEach)
}, callback)
})
// ---------------------------------------------------------------------------
function removeThumbnail (video, callback) {
- fs.unlink(thumbnailsDir + video.thumbnail, callback)
+ fs.unlink(constants.CONFIG.STORAGE.THUMBNAILS_DIR + video.thumbnail, callback)
}
function removeFile (video, callback) {
- fs.unlink(uploadsDir + video.filename, callback)
+ fs.unlink(constants.CONFIG.STORAGE.UPLOAD_DIR + video.filename, callback)
}
// Maybe the torrent is not seeded, but we catch the error to don't stop the removing process
})
.thumbnail({
count: 1,
- folder: thumbnailsDir,
+ folder: constants.CONFIG.STORAGE.THUMBNAILS_DIR,
size: constants.THUMBNAILS_SIZE,
filename: filename
})
if (err) return callback(err)
const thumbnailName = randomString + '.jpg'
- fs.writeFile(thumbnailsDir + thumbnailName, data, { encoding: 'base64' }, function (err) {
+ fs.writeFile(constants.CONFIG.STORAGE.THUMBNAILS_DIR + thumbnailName, data, { encoding: 'base64' }, function (err) {
if (err) return callback(err)
return callback(null, thumbnailName)