refactored mysql calls db fields

This commit is contained in:
bill bittner 2017-06-16 10:07:31 -07:00
parent 99413abdf6
commit 2f3f2784b5
6 changed files with 60 additions and 61 deletions

View file

@ -2,15 +2,26 @@ var axios = require('axios');
var db = require("../models");
module.exports = {
publishClaim: function(publishParams){
publishClaim: function(publishParams, fileType){
var deferred = new Promise(function(resolve, reject){
console.log(">> lbryApi >> publishClaim:", publishParams);
axios.post('http://localhost:5279/lbryapi', {
"method": "publish",
"params": publishParams
}).then(function (response) {
console.log(">> 'publish' success");
resolve(response.data);
console.log(">> 'publish' success", response);
db.File.create({
name: publishParams.name,
claim_id: response.data.result.claim_id,
outpoint: response.data.result.outpoint,
file_name: "test",
file_path: publishParams.filePath,
file_type: fileType,
nsfw: nsfw,
}).catch(function(error){
console.log('An error occurred when writing to the MySQL database:', error);
});
resolve(response.data.result);
}).catch(function(error){
console.log(">> 'publish' error");
reject(error);
@ -24,32 +35,33 @@ module.exports = {
axios.post('http://localhost:5279/lbryapi', {
"method": "get",
"params": { "uri": uri, "timeout": 20}
}).then(function (getResponse) {
console.log(">> 'get' success", getResponse.data);
}).then(function (response) {
console.log(">> 'get' success", response.data);
//check to make sure the daemon didn't just time out
if (getResponse.data.result.error){
reject(getResponse.data.result.error);
if (response.data.result.error){
reject(response.data.result.error);
}
/*
note: put in a check to make sure we do not resolve until the download is actually complete (response.data.completed === true)
*/
// save a record of the file to the Files table
db.File.create({
name: getResponse.data.result.file_name,
path: getResponse.data.result.download_path,
file_type: getResponse.data.result.mime_type,
claim_id: getResponse.data.result.claim_id,
outpoint: getResponse.data.result.outpoint,
nsfw: getResponse.data.result.metadata.stream.metadata.nsfw,
name: response.data.result.name,
claim_id: response.data.result.claim_id,
outpoint: response.data.result.outpoint,
file_name: response.data.result.file_name,
file_path: response.data.result.download_path,
file_type: response.data.result.mime_type,
nsfw: response.data.result.metadata.stream.metadata.nsfw,
}).catch(function(error){
console.log('an error occurred when writing to the MySQL database. Check the logs.');
console.log('An error occurred when writing to the MySQL database:', error);
});
// resolve the promise
resolve(getResponse.data);
}).catch(function(getUriError){
resolve(response.data.result);
}).catch(function(error){
console.log(">> 'get' error");
// reject the promise with an error message
reject(getUriError);
reject(error);
return;
});
});

View file

@ -90,21 +90,17 @@ module.exports = {
console.log(">> Asset was found locally");
// if a record is found, return it
if (claim){
var fileInfo = {
file_name: claim.dataValues.name,
download_path: claim.dataValues.path,
content_type: claim.dataValues.file_type
}
resolve(fileInfo);
console.log("claim.dataValues", claim.dataValues);
resolve(claim.dataValues);
// ... otherwise use daemon to retrieve it
} else {
// promise to get the chosen uri
lbryApi.getClaim(freePublicClaimUri)
.then(function(data){
.then(function(result){
resolve({
file_name: data.result.file_name,
download_path: data.result.download_path,
content_type: data.result.metadata.stream.source.contentType
file_name: result.file_name,
file_path: result.download_path,
file_type: result.mime_type
});
}).catch(function(error){
reject(error)
@ -129,11 +125,7 @@ module.exports = {
console.log(">> Asset was found locally");
// if a record is found, return it
if (claim){
resolve({
file_name: claim.dataValues.name,
download_path: claim.dataValues.path,
content_type: claim.dataValues.file_type
});
resolve(claim.dataValues);
// ... otherwise use daemon to retrieve it
} else {
// get the claim info via 'resolve'
@ -143,11 +135,11 @@ module.exports = {
if (isFreePublicClaim(resolvedUri.result[uri].claim)){
// 'get' the claim
lbryApi.getClaim(uri)
.then(function(data){
.then(function(result){
resolve({
file_name: data.result.file_name,
download_path: data.result.download_path,
content_type: data.result.metadata.stream.source.contentType
file_name: result.file_name,
file_path: result.download_path,
file_type: result.mime_type
});
}).catch(function(error){
reject(error)

View file

@ -51,21 +51,11 @@ module.exports = {
// create the publish object
var publishParams = createPublishParams(name, filePath, license, nsfw);
// get a promise to publish
lbryApi.publishClaim(publishParams)
.then(function(data){
lbryApi.publishClaim(publishParams, fileType)
.then(function(result){
visitor.event("Publish Route", "Publish Success", filePath).send();
console.log("publish promise success. Tx info:", data)
socket.emit("publish-complete", {name: name, result: data.result});
db.File.create({
name: name,
path: filePath,
file_type: fileType,
claim_id: data.result.claim_id,
outpoint: getResponse.data.result.outpoint,
nsfw: nsfw,
}).catch(function(error){
console.log('an error occurred when writing to the MySQL database. Check the logs.');
});
console.log("publish promise success. Tx info:", result)
socket.emit("publish-complete", {name: name, result: result});
})
.catch(function(error){
visitor.event("Publish Route", "Publish Failure", filePath).send();

View file

@ -4,13 +4,6 @@ module.exports = function(sequelize, DataTypes){
type: DataTypes.STRING,
allowNull: false
},
path: {
type: DataTypes.STRING,
allowNull: false
},
file_type: {
type: DataTypes.STRING,
},
claim_id: {
type: DataTypes.STRING,
allowNull: false
@ -19,6 +12,17 @@ module.exports = function(sequelize, DataTypes){
type: DataTypes.STRING,
allowNull: false
},
file_name: {
type: DataTypes.STRING,
allowNull: false
},
file_path: {
type: DataTypes.STRING,
allowNull: false
},
file_type: {
type: DataTypes.STRING,
},
nsfw: {
type: DataTypes.BOOLEAN,
allowNull: false,

View file

@ -3,11 +3,11 @@ function serveFile(fileInfo, res){
var options = {
headers: {
"X-Content-Type-Options": "nosniff",
"Content-Type": fileInfo.content_type
"Content-Type": fileInfo.file_type
}
};
// adjust default options as needed
switch (fileInfo.content_type){
switch (fileInfo.file_type){
case "image/jpeg":
break;
case "image/gif":
@ -22,7 +22,7 @@ function serveFile(fileInfo, res){
break;
}
// send file
res.status(200).sendFile(fileInfo.download_path, options);
res.status(200).sendFile(fileInfo.file_path, options);
}
module.exports = function(app, routeHelpers, lbryHelpers, ua, googleAnalyticsId){
@ -50,6 +50,7 @@ module.exports = function(app, routeHelpers, lbryHelpers, ua, googleAnalyticsId)
console.log(">> GET request on /" + req.params.name);
lbryHelpers.getClaimBasedOnNameOnly(req.params.name)
.then(function(fileInfo){
console.log("/:name success.", fileInfo.file_name);
serveFile(fileInfo, res);
}).catch(function(error){

View file

@ -4,7 +4,7 @@
<div class="card-title">
<div class="row">
<div class="col-md-2">
<img src="https://spee.ch/speech/900227fe5c778eb2a6424d923af806c669ea3a3c"/>
<img src="/speech/900227fe5c778eb2a6424d923af806c669ea3a3c"/>
</div>
<div class="col-md-10 align-self-center">
<h1><a class="white-text" href="/">Spee.ch</a></h1>