changed log approach and File model

This commit is contained in:
bill bittner 2017-06-19 15:25:14 -07:00
parent 61d8431d38
commit 14fc367c15
7 changed files with 52 additions and 63 deletions

View file

@ -9,9 +9,9 @@ function getClaimAndHandleResponse (claimUri, resolve, reject) {
.getClaim(claimUri) .getClaim(claimUri)
.then(({ file_name, download_path, mime_type }) => { .then(({ file_name, download_path, mime_type }) => {
resolve({ resolve({
file_name, fileName: file_name,
file_path: download_path, filePath: download_path,
file_type: mime_type, fileType: mime_type,
}); });
}) })
.catch(error => { .catch(error => {
@ -33,7 +33,7 @@ module.exports = {
console.log('>> Decided on public claim id:', claimId); console.log('>> Decided on public claim id:', claimId);
// check to see if the file is available locally // check to see if the file is available locally
db.File db.File
.findOne({ where: { name: name, claim_id: claimId } }) .findOne({ where: { name: name, claimId: claimId } })
.then(claim => { .then(claim => {
// if a matching claim is found locally... // if a matching claim is found locally...
if (claim) { if (claim) {
@ -76,7 +76,7 @@ module.exports = {
const resolvedOutpoint = `${result[uri].claim.txid}:${result[uri].claim.nout}`; const resolvedOutpoint = `${result[uri].claim.txid}:${result[uri].claim.nout}`;
// check locally for the claim // check locally for the claim
db.File db.File
.findOne({ where: { claim_id: claimId } }) .findOne({ where: { name: claimName, claimId: claimId } })
.then(claim => { .then(claim => {
// if a found locally... // if a found locally...
if (claim) { if (claim) {

View file

@ -1,51 +1,52 @@
const axios = require('axios'); const axios = require('axios');
const db = require('../../models'); const db = require('../../models');
const logger = require('winston');
function createFilesRecord (name, claimId, outpoint, fileName, filePath, fileType, nsfw) {
db.File
.create({ name, claimId, outpoint, fileName, filePath, fileType, nsfw })
.catch(error => {
logger.error(`Sequelize File.create error`, error);
});
}
module.exports = { module.exports = {
publishClaim (publishParams, fileName, fileType) { publishClaim (publishParams, fileName, fileType) {
logger.silly(`publishClaim start for ${fileName}`);
const deferred = new Promise((resolve, reject) => { const deferred = new Promise((resolve, reject) => {
console.log('>> lbryApi >> publishClaim:', publishParams);
axios axios
.post('http://localhost:5279/lbryapi', { .post('http://localhost:5279/lbryapi', {
method: 'publish', method: 'publish',
params: publishParams, params: publishParams,
}) })
.then(response => { .then(response => {
console.log(">> 'publish' success", response); logger.silly(`publishClaim success for ${fileName}`);
const result = response.data.result; const result = response.data.result;
db.File createFilesRecord(
.create({ publishParams.name, result.claim_id, `${result.txid}:${result.nout}`, fileName, publishParams.file_path, fileType, publishParams.metadata.nsfw);
name : publishParams.name,
claim_id : result.claim_id,
outpoint : `${result.txid}:${result.nout}`,
file_name: fileName,
file_path: publishParams.file_path,
file_type: fileType,
nsfw : publishParams.metadata.nsfw,
})
.catch(error => {
console.log('An error occurred when writing to the MySQL database:', error);
});
resolve(result); resolve(result);
}) })
.catch(error => { .catch(error => {
console.log(">> 'publish' error"); logger.error(`publishClaim error for ${fileName}`, error);
reject(error); reject(error);
}); });
}); });
return deferred; return deferred;
}, },
getClaim (uri) { getClaim (uri) {
logger.silly(`getClaim start for ${uri}`);
const deferred = new Promise((resolve, reject) => { const deferred = new Promise((resolve, reject) => {
console.log('>> lbryApi >> getClaim:', uri);
axios axios
.post('http://localhost:5279/lbryapi', { .post('http://localhost:5279/lbryapi', {
method: 'get', method: 'get',
params: { uri, timeout: 60 }, params: { uri, timeout: 30 },
}) })
.then(({ data }) => { .then(({ data }) => {
console.log(">> 'get' success"); logger.silly(`getClaim success for ${uri}`);
// check to make sure the daemon didn't just time out // check to make sure the daemon didn't just time out
if (!data.result) {
reject(JSON.stringify(data));
}
if (data.result.error) { if (data.result.error) {
reject(data.result.error); reject(data.result.error);
} }
@ -54,61 +55,50 @@ module.exports = {
*/ */
// save a record of the file to the Files table // save a record of the file to the Files table
const result = data.result; const result = data.result;
db.File createFilesRecord(
.create({ result.name, result.claim_id, result.outpoint, result.file_name, result.download_path, result.mime_type, result.metadata.stream.metadata.nsfw);
name : result.name,
claim_id : result.claim_id,
outpoint : result.outpoint,
file_name: result.file_name,
file_path: result.download_path,
file_type: result.mime_type,
nsfw : result.metadata.stream.metadata.nsfw,
})
.catch(error => {
console.log('An error occurred when writing to the MySQL database:', error);
});
resolve(result); resolve(result);
}) })
.catch(error => { .catch(error => {
console.log(">> 'get' error"); logger.error(`getClaim error for ${uri}`, error);
reject(error); reject(error);
}); });
}); });
return deferred; return deferred;
}, },
getClaimsList (claimName) { getClaimsList (claimName) {
logger.silly(`getClaimsList start for ${claimName}`);
const deferred = new Promise((resolve, reject) => { const deferred = new Promise((resolve, reject) => {
console.log('>> lbryApi >> getClaimList:', claimName);
axios axios
.post('http://localhost:5279/lbryapi', { .post('http://localhost:5279/lbryapi', {
method: 'claim_list', method: 'claim_list',
params: { name: claimName }, params: { name: claimName },
}) })
.then(({ data }) => { .then(({ data }) => {
console.log(">> 'claim_list' success"); logger.silly(`getClaimsList success for ${claimName}`);
resolve(data.result); resolve(data.result);
}) })
.catch(error => { .catch(error => {
console.log(">> 'claim_list' error"); logger.error(`getClaimsList error for ${claimName}`, error);
reject(error); reject(error);
}); });
}); });
return deferred; return deferred;
}, },
resolveUri (uri) { resolveUri (uri) {
logger.silly(`resolveUri start for ${uri}`);
const deferred = new Promise((resolve, reject) => { const deferred = new Promise((resolve, reject) => {
console.log('>> lbryApi >> resolveUri:', uri);
axios axios
.post('http://localhost:5279/lbryapi', { .post('http://localhost:5279/lbryapi', {
method: 'resolve', method: 'resolve',
params: { uri }, params: { uri },
}) })
.then(({ data }) => { .then(({ data }) => {
console.log(">> 'resolve' success"); logger.silly(`resolveUri success for ${uri}`);
resolve(data.result); resolve(data.result);
}) })
.catch(error => { .catch(error => {
console.log(">> 'resolve' error"); logger.error(`resolveUri error for ${uri}`, error);
reject(error); reject(error);
}); });
}); });

View file

@ -6,7 +6,7 @@ module.exports = (sequelize, { STRING, BOOLEAN }) => {
type : STRING, type : STRING,
allowNull: false, allowNull: false,
}, },
claim_id: { claimId: {
type : STRING, type : STRING,
allowNull: false, allowNull: false,
}, },
@ -14,15 +14,15 @@ module.exports = (sequelize, { STRING, BOOLEAN }) => {
type : STRING, type : STRING,
allowNull: false, allowNull: false,
}, },
file_name: { fileName: {
type : STRING, type : STRING,
allowNull: false, allowNull: false,
}, },
file_path: { filePath: {
type : STRING, type : STRING,
allowNull: false, allowNull: false,
}, },
file_type: { fileType: {
type: STRING, type: STRING,
}, },
nsfw: { nsfw: {

View file

@ -5,27 +5,25 @@ const logger = require('winston');
module.exports = app => { module.exports = app => {
// route to run a claim_list request on the daemon // route to run a claim_list request on the daemon
app.get('/api/claim_list/:claim', ({ originalUrl, params }, res) => { app.get('/api/claim_list/:claim', ({ originalUrl, params }, res) => {
logger.debug(`GET request on ${originalUrl}`);
lbryApi lbryApi
.getClaimsList(params.claim) .getClaimsList(params.claim)
.then(claimsList => { .then(claimsList => {
logger.info(`${originalUrl} returned successfully.`);
res.status(200).json(claimsList); res.status(200).json(claimsList);
}) })
.catch(error => { .catch(error => {
logger.error(`${originalUrl} returned an error.`, error);
errorHandlers.handleRequestError(error, res); errorHandlers.handleRequestError(error, res);
}); });
}); });
// route to run a resolve request on the daemon // route to run a resolve request on the daemon
app.get('/api/resolve/:uri', ({ originalUrl, params }, res) => { app.get('/api/resolve/:uri', ({ originalUrl, params }, res) => {
logger.debug(`GET request on ${originalUrl}`);
lbryApi lbryApi
.resolveUri(params.uri) .resolveUri(params.uri)
.then(resolvedUri => { .then(resolvedUri => {
logger.info(`${originalUrl} returned successfully.`);
res.status(200).json(resolvedUri); res.status(200).json(resolvedUri);
}) })
.catch(error => { .catch(error => {
logger.error(`${originalUrl} returned an error.`, error);
errorHandlers.handleRequestError(error, res); errorHandlers.handleRequestError(error, res);
}); });
}); });

View file

@ -3,7 +3,7 @@ const logger = require('winston');
module.exports = app => { module.exports = app => {
// route for the home page // route for the home page
app.get('/', (req, res) => { app.get('/', (req, res) => {
logger.silly(`Get request on /`); logger.debug(`Get request on /`);
res.status(200).render('index'); res.status(200).render('index');
}); });
// a catch-all route if someone visits a page that does not exist // a catch-all route if someone visits a page that does not exist

View file

@ -2,17 +2,18 @@ const errorHandlers = require('../helpers/libraries/errorHandlers.js');
const serveController = require('../controllers/serveController.js'); const serveController = require('../controllers/serveController.js');
const logger = require('winston'); const logger = require('winston');
function serveFile ({ file_type, file_path }, res) { function serveFile ({ fileName, fileType, filePath }, res) {
logger.info(`serving file ${fileName} from ${filePath}`);
// set default options // set default options
const options = { const options = {
headers: { headers: {
'X-Content-Type-Options': 'nosniff', 'X-Content-Type-Options': 'nosniff',
'Content-Type' : file_type, 'Content-Type' : fileType,
}, },
}; };
// adjust default options as needed // adjust default options as needed
// eslint-disable-next-line camelcase // eslint-disable-next-line camelcase
switch (file_type) { switch (fileType) {
case 'image/jpeg': case 'image/jpeg':
break; break;
case 'image/gif': case 'image/gif':
@ -27,7 +28,7 @@ function serveFile ({ file_type, file_path }, res) {
break; break;
} }
// send file // send file
res.status(200).sendFile(file_path, options); res.status(200).sendFile(filePath, options);
} }
module.exports = (app, ua, googleAnalyticsId) => { module.exports = (app, ua, googleAnalyticsId) => {
@ -41,11 +42,11 @@ module.exports = (app, ua, googleAnalyticsId) => {
serveController serveController
.getClaimByClaimId(params.name, params.claim_id) .getClaimByClaimId(params.name, params.claim_id)
.then(fileInfo => { .then(fileInfo => {
logger.info(`${originalUrl} returned successfully.`); logger.debug(`${originalUrl} getClaimByClaimId returned successfully.`);
serveFile(fileInfo, res); serveFile(fileInfo, res);
}) })
.catch(error => { .catch(error => {
logger.error(`${originalUrl} returned an error.`, error); logger.error(`${originalUrl} getClaimByClaimId returned an error.`, error);
errorHandlers.handleRequestError(error, res); errorHandlers.handleRequestError(error, res);
}); });
}); });
@ -58,11 +59,11 @@ module.exports = (app, ua, googleAnalyticsId) => {
serveController serveController
.getClaimByName(params.name) .getClaimByName(params.name)
.then(fileInfo => { .then(fileInfo => {
logger.info(`${originalUrl} returned successfully.`); logger.debug(`${originalUrl} getClaimByName returned successfully.`);
serveFile(fileInfo, res); serveFile(fileInfo, res);
}) })
.catch(error => { .catch(error => {
logger.error(`${originalUrl} returned an error.`, error); logger.error(`${originalUrl} getClaimByName returned an error.`, error);
errorHandlers.handleRequestError(error, res); errorHandlers.handleRequestError(error, res);
}); });
}); });

View file

@ -12,11 +12,11 @@ module.exports = (app, ua, googleAnalyticsId) => {
showController showController
.getAllClaims(params.name) .getAllClaims(params.name)
.then(orderedFreePublicClaims => { .then(orderedFreePublicClaims => {
logger.info(`${originalUrl} returned successfully.`); logger.debug(`${originalUrl} getAllClaims returned successfully.`);
res.status(200).render('allClaims', { claims: orderedFreePublicClaims }); res.status(200).render('allClaims', { claims: orderedFreePublicClaims });
}) })
.catch(error => { .catch(error => {
logger.error(`${originalUrl} returned an error.`, error); logger.error(`${originalUrl} getAllClaims returned an error.`, error);
errorHandlers.handleRequestError(error, res); errorHandlers.handleRequestError(error, res);
}); });
}); });