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

View file

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

View file

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

View file

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

View file

@ -3,7 +3,7 @@ const logger = require('winston');
module.exports = app => {
// route for the home page
app.get('/', (req, res) => {
logger.silly(`Get request on /`);
logger.debug(`Get request on /`);
res.status(200).render('index');
});
// 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 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
const options = {
headers: {
'X-Content-Type-Options': 'nosniff',
'Content-Type' : file_type,
'Content-Type' : fileType,
},
};
// adjust default options as needed
// eslint-disable-next-line camelcase
switch (file_type) {
switch (fileType) {
case 'image/jpeg':
break;
case 'image/gif':
@ -27,7 +28,7 @@ function serveFile ({ file_type, file_path }, res) {
break;
}
// send file
res.status(200).sendFile(file_path, options);
res.status(200).sendFile(filePath, options);
}
module.exports = (app, ua, googleAnalyticsId) => {
@ -41,11 +42,11 @@ module.exports = (app, ua, googleAnalyticsId) => {
serveController
.getClaimByClaimId(params.name, params.claim_id)
.then(fileInfo => {
logger.info(`${originalUrl} returned successfully.`);
logger.debug(`${originalUrl} getClaimByClaimId returned successfully.`);
serveFile(fileInfo, res);
})
.catch(error => {
logger.error(`${originalUrl} returned an error.`, error);
logger.error(`${originalUrl} getClaimByClaimId returned an error.`, error);
errorHandlers.handleRequestError(error, res);
});
});
@ -58,11 +59,11 @@ module.exports = (app, ua, googleAnalyticsId) => {
serveController
.getClaimByName(params.name)
.then(fileInfo => {
logger.info(`${originalUrl} returned successfully.`);
logger.debug(`${originalUrl} getClaimByName returned successfully.`);
serveFile(fileInfo, res);
})
.catch(error => {
logger.error(`${originalUrl} returned an error.`, error);
logger.error(`${originalUrl} getClaimByName returned an error.`, error);
errorHandlers.handleRequestError(error, res);
});
});

View file

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