Server: paths refractoring

This commit is contained in:
Chocobozzz 2017-01-17 21:42:47 +01:00
parent 1e4b0080ff
commit 15103f11ec
8 changed files with 61 additions and 38 deletions

View File

@ -5,6 +5,7 @@ const waterfall = require('async/waterfall')
const db = require('../../initializers/database') const db = require('../../initializers/database')
const logger = require('../../helpers/logger') const logger = require('../../helpers/logger')
const peertubeCrypto = require('../../helpers/peertube-crypto')
const utils = require('../../helpers/utils') const utils = require('../../helpers/utils')
const friends = require('../../lib/friends') const friends = require('../../lib/friends')
const middlewares = require('../../middlewares') const middlewares = require('../../middlewares')
@ -67,7 +68,7 @@ function addPods (req, res, next) {
}, },
function fetchMyCertificate (callback) { function fetchMyCertificate (callback) {
friends.getMyCertificate(function (err, cert) { peertubeCrypto.getMyPublicCert(function (err, cert) {
if (err) { if (err) {
logger.error('Cannot read cert file.') logger.error('Cannot read cert file.')
return callback(err) return callback(err)

View File

@ -12,7 +12,7 @@ const db = require('../initializers/database')
const router = express.Router() const router = express.Router()
const opengraphComment = '<!-- opengraph tags -->' const opengraphComment = '<!-- opengraph tags -->'
const distPath = path.join(__dirname, '../../client/dist') const distPath = path.join(__dirname, '..', '..', 'client/dist')
const embedPath = path.join(distPath, 'standalone/videos/embed.html') const embedPath = path.join(distPath, 'standalone/videos/embed.html')
const indexPath = path.join(distPath, 'index.html') const indexPath = path.join(distPath, 'index.html')

View File

@ -4,6 +4,7 @@ const crypto = require('crypto')
const bcrypt = require('bcrypt') const bcrypt = require('bcrypt')
const fs = require('fs') const fs = require('fs')
const openssl = require('openssl-wrapper') const openssl = require('openssl-wrapper')
const pathUtils = require('path')
const constants = require('../initializers/constants') const constants = require('../initializers/constants')
const logger = require('./logger') const logger = require('./logger')
@ -13,6 +14,8 @@ const peertubeCrypto = {
comparePassword, comparePassword,
createCertsIfNotExist, createCertsIfNotExist,
cryptPassword, cryptPassword,
getMyPrivateCert,
getMyPublicCert,
sign sign
} }
@ -55,7 +58,8 @@ function sign (data) {
sign.update(dataString, 'utf8') sign.update(dataString, 'utf8')
// TODO: make async // TODO: make async
const myKey = fs.readFileSync(constants.CONFIG.STORAGE.CERT_DIR + 'peertube.key.pem') const certPath = pathUtils.join(constants.CONFIG.STORAGE.CERT_DIR, constants.PRIVATE_CERT_NAME)
const myKey = fs.readFileSync(certPath)
const signature = sign.sign(myKey, constants.SIGNATURE_ENCODING) const signature = sign.sign(myKey, constants.SIGNATURE_ENCODING)
return signature return signature
@ -91,6 +95,16 @@ function cryptPassword (password, callback) {
}) })
} }
function getMyPrivateCert (callback) {
const certPath = pathUtils.join(constants.CONFIG.STORAGE.CERT_DIR, constants.PRIVATE_CERT_NAME)
fs.readFile(certPath, 'utf8', callback)
}
function getMyPublicCert (callback) {
const certPath = pathUtils.join(constants.CONFIG.STORAGE.CERT_DIR, constants.PUBLIC_CERT_NAME)
fs.readFile(certPath, 'utf8', callback)
}
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
module.exports = peertubeCrypto module.exports = peertubeCrypto
@ -98,7 +112,8 @@ module.exports = peertubeCrypto
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
function certsExist (callback) { function certsExist (callback) {
fs.exists(constants.CONFIG.STORAGE.CERT_DIR + 'peertube.key.pem', function (exists) { const certPath = pathUtils.join(constants.CONFIG.STORAGE.CERT_DIR, constants.PRIVATE_CERT_NAME)
fs.exists(certPath, function (exists) {
return callback(exists) return callback(exists)
}) })
} }
@ -113,24 +128,27 @@ function createCerts (callback) {
logger.info('Generating a RSA key...') logger.info('Generating a RSA key...')
let options = { const privateCertPath = pathUtils.join(constants.CONFIG.STORAGE.CERT_DIR, constants.PRIVATE_CERT_NAME)
'out': constants.CONFIG.STORAGE.CERT_DIR + 'peertube.key.pem', const genRsaOptions = {
'out': privateCertPath,
'2048': false '2048': false
} }
openssl.exec('genrsa', options, function (err) { openssl.exec('genrsa', genRsaOptions, function (err) {
if (err) { if (err) {
logger.error('Cannot create private key on this pod.') logger.error('Cannot create private key on this pod.')
return callback(err) return callback(err)
} }
logger.info('RSA key generated.')
options = { logger.info('RSA key generated.')
'in': constants.CONFIG.STORAGE.CERT_DIR + 'peertube.key.pem', logger.info('Managing public key...')
const publicCertPath = pathUtils.join(constants.CONFIG.STORAGE.CERT_DIR, 'peertube.pub')
const rsaOptions = {
'in': privateCertPath,
'pubout': true, 'pubout': true,
'out': constants.CONFIG.STORAGE.CERT_DIR + 'peertube.pub' 'out': publicCertPath
} }
logger.info('Manage public key...') openssl.exec('rsa', rsaOptions, function (err) {
openssl.exec('rsa', options, function (err) {
if (err) { if (err) {
logger.error('Cannot create public key on this pod.') logger.error('Cannot create public key on this pod.')
return callback(err) return callback(err)

View File

@ -134,6 +134,8 @@ const REMOTE_SCHEME = {
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
const PRIVATE_CERT_NAME = 'peertube.key.pem'
const PUBLIC_CERT_NAME = 'peertube.pub'
const SIGNATURE_ALGORITHM = 'RSA-SHA256' const SIGNATURE_ALGORITHM = 'RSA-SHA256'
const SIGNATURE_ENCODING = 'hex' const SIGNATURE_ENCODING = 'hex'
@ -189,13 +191,15 @@ module.exports = {
PAGINATION_COUNT_DEFAULT, PAGINATION_COUNT_DEFAULT,
PODS_SCORE, PODS_SCORE,
PREVIEWS_SIZE, PREVIEWS_SIZE,
PRIVATE_CERT_NAME,
PUBLIC_CERT_NAME,
REMOTE_SCHEME, REMOTE_SCHEME,
REQUEST_ENDPOINTS,
REQUEST_ENDPOINT_ACTIONS, REQUEST_ENDPOINT_ACTIONS,
REQUEST_ENDPOINTS,
REQUESTS_IN_PARALLEL, REQUESTS_IN_PARALLEL,
REQUESTS_INTERVAL, REQUESTS_INTERVAL,
REQUESTS_LIMIT_PODS,
REQUESTS_LIMIT_PER_POD, REQUESTS_LIMIT_PER_POD,
REQUESTS_LIMIT_PODS,
RETRY_REQUESTS, RETRY_REQUESTS,
SEARCHABLE_COLUMNS, SEARCHABLE_COLUMNS,
SIGNATURE_ALGORITHM, SIGNATURE_ALGORITHM,

View File

@ -3,13 +3,13 @@
const each = require('async/each') const each = require('async/each')
const eachLimit = require('async/eachLimit') const eachLimit = require('async/eachLimit')
const eachSeries = require('async/eachSeries') const eachSeries = require('async/eachSeries')
const fs = require('fs')
const request = require('request') const request = require('request')
const waterfall = require('async/waterfall') const waterfall = require('async/waterfall')
const constants = require('../initializers/constants') const constants = require('../initializers/constants')
const db = require('../initializers/database') const db = require('../initializers/database')
const logger = require('../helpers/logger') const logger = require('../helpers/logger')
const peertubeCrypto = require('../helpers/peertube-crypto')
const requests = require('../helpers/requests') const requests = require('../helpers/requests')
const ENDPOINT_ACTIONS = constants.REQUEST_ENDPOINT_ACTIONS[constants.REQUEST_ENDPOINTS.VIDEOS] const ENDPOINT_ACTIONS = constants.REQUEST_ENDPOINT_ACTIONS[constants.REQUEST_ENDPOINTS.VIDEOS]
@ -19,7 +19,6 @@ const friends = {
updateVideoToFriends, updateVideoToFriends,
reportAbuseVideoToFriend, reportAbuseVideoToFriend,
hasFriends, hasFriends,
getMyCertificate,
makeFriends, makeFriends,
quitFriends, quitFriends,
removeVideoToFriends, removeVideoToFriends,
@ -74,15 +73,11 @@ function hasFriends (callback) {
}) })
} }
function getMyCertificate (callback) {
fs.readFile(constants.CONFIG.STORAGE.CERT_DIR + 'peertube.pub', 'utf8', callback)
}
function makeFriends (hosts, callback) { function makeFriends (hosts, callback) {
const podsScore = {} const podsScore = {}
logger.info('Make friends!') logger.info('Make friends!')
getMyCertificate(function (err, cert) { peertubeCrypto.getMyPublicCert(function (err, cert) {
if (err) { if (err) {
logger.error('Cannot read public cert.') logger.error('Cannot read public cert.')
return callback(err) return callback(err)

View File

@ -157,8 +157,7 @@ function beforeCreate (video, options, next) {
const videoPath = pathUtils.join(constants.CONFIG.STORAGE.VIDEOS_DIR, video.getVideoFilename()) const videoPath = pathUtils.join(constants.CONFIG.STORAGE.VIDEOS_DIR, video.getVideoFilename())
tasks.push( tasks.push(
// TODO: refractoring function createVideoTorrent (callback) {
function (callback) {
const options = { const options = {
announceList: [ announceList: [
[ constants.CONFIG.WEBSERVER.WS + '://' + constants.CONFIG.WEBSERVER.HOSTNAME + ':' + constants.CONFIG.WEBSERVER.PORT + '/tracker/socket' ] [ constants.CONFIG.WEBSERVER.WS + '://' + constants.CONFIG.WEBSERVER.HOSTNAME + ':' + constants.CONFIG.WEBSERVER.PORT + '/tracker/socket' ]
@ -171,7 +170,8 @@ function beforeCreate (video, options, next) {
createTorrent(videoPath, options, function (err, torrent) { createTorrent(videoPath, options, function (err, torrent) {
if (err) return callback(err) if (err) return callback(err)
fs.writeFile(constants.CONFIG.STORAGE.TORRENTS_DIR + video.getTorrentName(), torrent, function (err) { const filePath = pathUtils.join(constants.CONFIG.STORAGE.TORRENTS_DIR, video.getTorrentName())
fs.writeFile(filePath, torrent, function (err) {
if (err) return callback(err) if (err) return callback(err)
const parsedTorrent = parseTorrent(torrent) const parsedTorrent = parseTorrent(torrent)
@ -180,10 +180,12 @@ function beforeCreate (video, options, next) {
}) })
}) })
}, },
function (callback) {
function createVideoThumbnail (callback) {
createThumbnail(video, videoPath, callback) createThumbnail(video, videoPath, callback)
}, },
function (callback) {
function createVIdeoPreview (callback) {
createPreview(video, videoPath, callback) createPreview(video, videoPath, callback)
} }
) )
@ -205,19 +207,19 @@ function afterDestroy (video, options, next) {
if (video.isOwned()) { if (video.isOwned()) {
tasks.push( tasks.push(
function (callback) { function removeVideoFile (callback) {
removeFile(video, callback) removeFile(video, callback)
}, },
function (callback) { function removeVideoTorrent (callback) {
removeTorrent(video, callback) removeTorrent(video, callback)
}, },
function (callback) { function removeVideoPreview (callback) {
removePreview(video, callback) removePreview(video, callback)
}, },
function (callback) { function removeVideoToFriends (callback) {
const params = { const params = {
remoteId: video.id remoteId: video.id
} }
@ -395,7 +397,7 @@ function generateThumbnailFromData (video, thumbnailData, callback) {
// Creating the thumbnail for a remote video // Creating the thumbnail for a remote video
const thumbnailName = video.getThumbnailName() const thumbnailName = video.getThumbnailName()
const thumbnailPath = constants.CONFIG.STORAGE.THUMBNAILS_DIR + thumbnailName const thumbnailPath = pathUtils.join(constants.CONFIG.STORAGE.THUMBNAILS_DIR, thumbnailName)
fs.writeFile(thumbnailPath, Buffer.from(thumbnailData, 'binary'), function (err) { fs.writeFile(thumbnailPath, Buffer.from(thumbnailData, 'binary'), function (err) {
if (err) return callback(err) if (err) return callback(err)
@ -596,15 +598,18 @@ function searchAndPopulateAuthorAndPodAndTags (value, field, start, count, sort,
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
function removeThumbnail (video, callback) { function removeThumbnail (video, callback) {
fs.unlink(constants.CONFIG.STORAGE.THUMBNAILS_DIR + video.getThumbnailName(), callback) const thumbnailPath = pathUtils.join(constants.CONFIG.STORAGE.THUMBNAILS_DIR, video.getThumbnailName())
fs.unlink(thumbnailPath, callback)
} }
function removeFile (video, callback) { function removeFile (video, callback) {
fs.unlink(constants.CONFIG.STORAGE.VIDEOS_DIR + video.getVideoFilename(), callback) const filePath = pathUtils.join(constants.CONFIG.STORAGE.VIDEOS_DIR, video.getVideoFilename())
fs.unlink(filePath, callback)
} }
function removeTorrent (video, callback) { function removeTorrent (video, callback) {
fs.unlink(constants.CONFIG.STORAGE.TORRENTS_DIR + video.getTorrentName(), callback) const torrenPath = pathUtils.join(constants.CONFIG.STORAGE.TORRENTS_DIR, video.getTorrentName())
fs.unlink(torrenPath, callback)
} }
function removePreview (video, callback) { function removePreview (video, callback) {

View File

@ -251,12 +251,12 @@ describe('Test a single pod', function () {
videosUtils.removeVideo(server.url, server.accessToken, videoId, function (err) { videosUtils.removeVideo(server.url, server.accessToken, videoId, function (err) {
if (err) throw err if (err) throw err
fs.readdir(pathUtils.join(__dirname, '../../../test1/videos/'), function (err, files) { fs.readdir(pathUtils.join(__dirname, '..', '..', '..', 'test1/videos/'), function (err, files) {
if (err) throw err if (err) throw err
expect(files.length).to.equal(0) expect(files.length).to.equal(0)
fs.readdir(pathUtils.join(__dirname, '../../../test1/thumbnails/'), function (err, files) { fs.readdir(pathUtils.join(__dirname, '..', '..', '..', 'test1/thumbnails/'), function (err, files) {
if (err) throw err if (err) throw err
expect(files.length).to.equal(0) expect(files.length).to.equal(0)

View File

@ -81,7 +81,7 @@ function runServer (number, callback) {
detached: true detached: true
} }
server.app = fork(pathUtils.join(__dirname, '../../../server.js'), [], options) server.app = fork(pathUtils.join(__dirname, '..', '..', '..', 'server.js'), [], options)
server.app.stdout.on('data', function onStdout (data) { server.app.stdout.on('data', function onStdout (data) {
let dontContinue = false let dontContinue = false