1
0
Fork 0

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 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)

View file

@ -12,7 +12,7 @@ const db = require('../initializers/database')
const router = express.Router()
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 indexPath = path.join(distPath, 'index.html')

View file

@ -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.')
options = {
'in': constants.CONFIG.STORAGE.CERT_DIR + 'peertube.key.pem',
logger.info('RSA key generated.')
logger.info('Managing public key...')
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)

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_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,

View file

@ -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)

View file

@ -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) {

View file

@ -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)

View file

@ -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