From 15103f11ec54989a0dee7fc33daa458ecb116fe4 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 17 Jan 2017 21:42:47 +0100 Subject: [PATCH] Server: paths refractoring --- server/controllers/api/pods.js | 3 ++- server/controllers/client.js | 2 +- server/helpers/peertube-crypto.js | 38 +++++++++++++++++++++++-------- server/initializers/constants.js | 8 +++++-- server/lib/friends.js | 9 ++------ server/models/video.js | 31 ++++++++++++++----------- server/tests/api/single-pod.js | 4 ++-- server/tests/utils/servers.js | 2 +- 8 files changed, 60 insertions(+), 37 deletions(-) diff --git a/server/controllers/api/pods.js b/server/controllers/api/pods.js index 38702face..e1fe6fb5d 100644 --- a/server/controllers/api/pods.js +++ b/server/controllers/api/pods.js @@ -5,6 +5,7 @@ const waterfall = require('async/waterfall') const db = require('../../initializers/database') const logger = require('../../helpers/logger') +const peertubeCrypto = require('../../helpers/peertube-crypto') const utils = require('../../helpers/utils') const friends = require('../../lib/friends') const middlewares = require('../../middlewares') @@ -67,7 +68,7 @@ function addPods (req, res, next) { }, function fetchMyCertificate (callback) { - friends.getMyCertificate(function (err, cert) { + peertubeCrypto.getMyPublicCert(function (err, cert) { if (err) { logger.error('Cannot read cert file.') return callback(err) diff --git a/server/controllers/client.js b/server/controllers/client.js index 8c242af07..83243a4f7 100644 --- a/server/controllers/client.js +++ b/server/controllers/client.js @@ -12,7 +12,7 @@ const db = require('../initializers/database') const router = express.Router() const opengraphComment = '' -const distPath = path.join(__dirname, '../../client/dist') +const distPath = path.join(__dirname, '..', '..', 'client/dist') const embedPath = path.join(distPath, 'standalone/videos/embed.html') const indexPath = path.join(distPath, 'index.html') diff --git a/server/helpers/peertube-crypto.js b/server/helpers/peertube-crypto.js index 0f1e02ad6..ef6808d5c 100644 --- a/server/helpers/peertube-crypto.js +++ b/server/helpers/peertube-crypto.js @@ -4,6 +4,7 @@ const crypto = require('crypto') const bcrypt = require('bcrypt') const fs = require('fs') const openssl = require('openssl-wrapper') +const pathUtils = require('path') const constants = require('../initializers/constants') const logger = require('./logger') @@ -13,6 +14,8 @@ const peertubeCrypto = { comparePassword, createCertsIfNotExist, cryptPassword, + getMyPrivateCert, + getMyPublicCert, sign } @@ -55,7 +58,8 @@ function sign (data) { sign.update(dataString, 'utf8') // 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) 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 @@ -98,7 +112,8 @@ module.exports = peertubeCrypto // --------------------------------------------------------------------------- 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) }) } @@ -113,24 +128,27 @@ function createCerts (callback) { logger.info('Generating a RSA key...') - let options = { - 'out': constants.CONFIG.STORAGE.CERT_DIR + 'peertube.key.pem', + const privateCertPath = pathUtils.join(constants.CONFIG.STORAGE.CERT_DIR, constants.PRIVATE_CERT_NAME) + const genRsaOptions = { + 'out': privateCertPath, '2048': false } - openssl.exec('genrsa', options, function (err) { + openssl.exec('genrsa', genRsaOptions, function (err) { if (err) { logger.error('Cannot create private key on this pod.') return callback(err) } + logger.info('RSA key generated.') + logger.info('Managing public key...') - options = { - 'in': constants.CONFIG.STORAGE.CERT_DIR + 'peertube.key.pem', + const publicCertPath = pathUtils.join(constants.CONFIG.STORAGE.CERT_DIR, 'peertube.pub') + const rsaOptions = { + 'in': privateCertPath, 'pubout': true, - 'out': constants.CONFIG.STORAGE.CERT_DIR + 'peertube.pub' + 'out': publicCertPath } - logger.info('Manage public key...') - openssl.exec('rsa', options, function (err) { + openssl.exec('rsa', rsaOptions, function (err) { if (err) { logger.error('Cannot create public key on this pod.') return callback(err) diff --git a/server/initializers/constants.js b/server/initializers/constants.js index 0c080ccd2..90adbf406 100644 --- a/server/initializers/constants.js +++ b/server/initializers/constants.js @@ -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_ENCODING = 'hex' @@ -189,13 +191,15 @@ module.exports = { PAGINATION_COUNT_DEFAULT, PODS_SCORE, PREVIEWS_SIZE, + PRIVATE_CERT_NAME, + PUBLIC_CERT_NAME, REMOTE_SCHEME, - REQUEST_ENDPOINTS, REQUEST_ENDPOINT_ACTIONS, + REQUEST_ENDPOINTS, REQUESTS_IN_PARALLEL, REQUESTS_INTERVAL, - REQUESTS_LIMIT_PODS, REQUESTS_LIMIT_PER_POD, + REQUESTS_LIMIT_PODS, RETRY_REQUESTS, SEARCHABLE_COLUMNS, SIGNATURE_ALGORITHM, diff --git a/server/lib/friends.js b/server/lib/friends.js index 1e8037c37..2ea837c99 100644 --- a/server/lib/friends.js +++ b/server/lib/friends.js @@ -3,13 +3,13 @@ const each = require('async/each') const eachLimit = require('async/eachLimit') const eachSeries = require('async/eachSeries') -const fs = require('fs') const request = require('request') const waterfall = require('async/waterfall') const constants = require('../initializers/constants') const db = require('../initializers/database') const logger = require('../helpers/logger') +const peertubeCrypto = require('../helpers/peertube-crypto') const requests = require('../helpers/requests') const ENDPOINT_ACTIONS = constants.REQUEST_ENDPOINT_ACTIONS[constants.REQUEST_ENDPOINTS.VIDEOS] @@ -19,7 +19,6 @@ const friends = { updateVideoToFriends, reportAbuseVideoToFriend, hasFriends, - getMyCertificate, makeFriends, quitFriends, 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) { const podsScore = {} logger.info('Make friends!') - getMyCertificate(function (err, cert) { + peertubeCrypto.getMyPublicCert(function (err, cert) { if (err) { logger.error('Cannot read public cert.') return callback(err) diff --git a/server/models/video.js b/server/models/video.js index 17eff6428..742150d69 100644 --- a/server/models/video.js +++ b/server/models/video.js @@ -157,8 +157,7 @@ function beforeCreate (video, options, next) { const videoPath = pathUtils.join(constants.CONFIG.STORAGE.VIDEOS_DIR, video.getVideoFilename()) tasks.push( - // TODO: refractoring - function (callback) { + function createVideoTorrent (callback) { const options = { announceList: [ [ 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) { 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) const parsedTorrent = parseTorrent(torrent) @@ -180,10 +180,12 @@ function beforeCreate (video, options, next) { }) }) }, - function (callback) { + + function createVideoThumbnail (callback) { createThumbnail(video, videoPath, callback) }, - function (callback) { + + function createVIdeoPreview (callback) { createPreview(video, videoPath, callback) } ) @@ -205,19 +207,19 @@ function afterDestroy (video, options, next) { if (video.isOwned()) { tasks.push( - function (callback) { + function removeVideoFile (callback) { removeFile(video, callback) }, - function (callback) { + function removeVideoTorrent (callback) { removeTorrent(video, callback) }, - function (callback) { + function removeVideoPreview (callback) { removePreview(video, callback) }, - function (callback) { + function removeVideoToFriends (callback) { const params = { remoteId: video.id } @@ -395,7 +397,7 @@ function generateThumbnailFromData (video, thumbnailData, callback) { // Creating the thumbnail for a remote video 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) { if (err) return callback(err) @@ -596,15 +598,18 @@ function searchAndPopulateAuthorAndPodAndTags (value, field, start, count, sort, // --------------------------------------------------------------------------- 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) { - 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) { - 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) { diff --git a/server/tests/api/single-pod.js b/server/tests/api/single-pod.js index 2db60448f..83a2b4411 100644 --- a/server/tests/api/single-pod.js +++ b/server/tests/api/single-pod.js @@ -251,12 +251,12 @@ describe('Test a single pod', function () { videosUtils.removeVideo(server.url, server.accessToken, videoId, function (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 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 expect(files.length).to.equal(0) diff --git a/server/tests/utils/servers.js b/server/tests/utils/servers.js index 1946ef49a..c07db4c40 100644 --- a/server/tests/utils/servers.js +++ b/server/tests/utils/servers.js @@ -81,7 +81,7 @@ function runServer (number, callback) { 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) { let dontContinue = false -- 2.25.1