added download directory retrieval to startup process

This commit is contained in:
bill bittner 2017-07-18 09:52:18 -07:00
parent ba3fd14ad3
commit 2f4fa6d970
4 changed files with 44 additions and 25 deletions

View file

@ -192,8 +192,10 @@ module.exports = {
getClaimByClaimId (name, claimId) {
const deferred = new Promise((resolve, reject) => {
let uri;
validateClaimId(name, claimId) // 1. validate the claim id & retrieve the full claim id if needed
.then(validClaimId => { // 2. check locally for the claim
// 1. validate the claim id & retrieve the full claim id if needed
validateClaimId(name, claimId)
.then(validClaimId => {
// 2. check locally for the claim
logger.debug('valid claim id:', validClaimId);
uri = `${name}#${validClaimId}`;
return db.File.findOne({ where: { name, claimId: validClaimId } });
@ -206,7 +208,7 @@ module.exports = {
resolve(result.dataValues);
// update the file, as needed
updateFileIfNeeded(uri, name, claimId, result.dataValues.outpoint, result.dataValues.outpoint);
// 3. if a match was not found use the daemon to retrieve the claim & return the db data once it is created
// 3. if a match was not found locally, use the daemon to retrieve the claim & return the db data once it is created
} else {
logger.debug('No result found in File table');
lbryApi

View file

@ -103,4 +103,24 @@ module.exports = {
});
return deferred;
},
getDownloadDirectory () {
logger.debug('Retrieving the download directory path from lbry daemon...');
const deferred = new Promise((resolve, reject) => {
axios
.post('http://localhost:5279/lbryapi', {
method: 'settings_get',
})
.then(({ data }) => {
if (data.result) {
resolve(data.result.download_directory);
} else {
reject(new Error('Successfully connected to lbry daemon, but unable to retrieve the download directory.'));
}
})
.catch((error) => {
reject(error);
});
});
return deferred;
},
};

View file

@ -7,13 +7,10 @@ const { createPublishParams, validateFile } = require('../helpers/libraries/publ
const errorHandlers = require('../helpers/libraries/errorHandlers.js');
const { postToStats, sendGoogleAnalytics } = require('../controllers/statsController.js');
const config = require('config');
const hostedContentPath = config.get('Database.DownloadDirectory');
module.exports = app => {
module.exports = (app, hostedContentPath) => {
// route to return a file directly
app.get('/api/streamFile/:name', ({ params }, res) => {
const filePath = `${hostedContentPath}${params.name}`;
const filePath = `${hostedContentPath}/${params.name}`;
res.status(200).sendFile(filePath);
});
// route to run a claim_list request on the daemon

View file

@ -6,8 +6,7 @@ const expressHandlebars = require('express-handlebars');
const Handlebars = require('handlebars');
const config = require('config');
const logger = require('winston');
const hostedContentPath = config.get('Database.DownloadDirectory');
const { getDownloadDirectory } = require('./helpers/libraries/lbryApi');
// configure logging
const logLevel = config.get('Logging.LogLevel');
@ -82,25 +81,26 @@ const hbs = expressHandlebars.create({
app.engine('handlebars', hbs.engine);
app.set('view engine', 'handlebars');
// require express routes
require('./routes/api-routes.js')(app);
require('./routes/show-routes.js')(app);
require('./routes/serve-routes.js')(app);
require('./routes/home-routes.js')(app);
// require socket.io routes
const server = require('./routes/sockets-routes.js')(app, siofu, hostedContentPath);
// sync sequelize
// wrap the server in socket.io to intercept incoming sockets requests
// start server
db.sequelize.sync()
.then(() => {
// start the server
db.sequelize
.sync() // sync sequelize
.then(() => { // get the download directory from the daemon
logger.info('Retrieving daemon download directory');
return getDownloadDirectory();
})
.then(hostedContentPath => { // require routes & wrap in socket.io
require('./routes/api-routes.js')(app, hostedContentPath);
require('./routes/show-routes.js')(app);
require('./routes/serve-routes.js')(app);
require('./routes/home-routes.js')(app);
return require('./routes/sockets-routes.js')(app, siofu, hostedContentPath);
})
.then(server => { // start the server
server.listen(PORT, () => {
logger.info('Trusting proxy?', app.get('trust proxy'));
logger.info(`Server is listening on PORT ${PORT}`);
});
})
.catch((error) => {
logger.log('Error syncing sequelize db:', error);
logger.error('Startup Error >>', error);
});