updated serve route to use default content_type image/jpeg

This commit is contained in:
bill bittner 2017-06-14 12:53:55 -07:00
parent bd53a27c35
commit 64b1b720ae
4 changed files with 63 additions and 25 deletions

View file

@ -2,8 +2,8 @@ var axios = require('axios');
module.exports = {
publishClaim: function(publishParams){
console.log("publish params:>", publishParams);
var deferred = new Promise(function(resolve, reject){
console.log(">> lbryApi >> publishClaim:", publishParams);
axios.post('http://localhost:5279/lbryapi', {
"method": "publish",
"params": publishParams
@ -19,11 +19,12 @@ module.exports = {
},
getClaim: function(uri){
var deferred = new Promise(function(resolve, reject){
console.log(">> lbryApi >> getClaim:", uri);
axios.post('http://localhost:5279/lbryapi', {
"method": "get",
"params": { "uri": uri, "timeout": 30}
"params": { "uri": uri, "timeout": 20}
}).then(function (getResponse) {
console.log(">> 'get claim' success...");
console.log(">> 'get' success");
//check to make sure the daemon didn't just time out (or otherwise send an error that appears to be a success response)
if (getResponse.data.result.error){
reject(getResponse.data.result.error);
@ -34,7 +35,7 @@ module.exports = {
*/
resolve(getResponse.data);
}).catch(function(getUriError){
console.log(">> 'get' error.");
console.log(">> 'get' error");
// reject the promise with an error message
reject(getUriError);
return;
@ -44,15 +45,15 @@ module.exports = {
},
getClaimsList: function(claimName){
var deferred = new Promise(function(resolve, reject){
console.log(">> claims_list for", claimName)
console.log(">> lbryApi >> getClaimList:", claimName);
axios.post('http://localhost:5279/lbryapi', {
method: "claim_list",
params: { name: claimName }
}).then(function (response) {
console.log(">> claim_list success");
console.log(">> 'claim_list' success");
resolve(response.data);
}).catch(function(error){
console.log(">> claim_list error");
console.log(">> 'claim_list' error");
reject(error);
});
});
@ -60,12 +61,12 @@ module.exports = {
},
resolveUri: function(uri){
var deferred = new Promise(function(resolve, reject){
console.log(">> resolve uri for", uri)
console.log(">> lbryApi >> resolveUri:", claimName);
axios.post('http://localhost:5279/lbryapi', {
"method": "resolve",
"params": { "uri": uri}
}).then(function(response){
console.log(response.data);
console.log(">> 'resolve' success");
resolve(response.data);
}).catch(function(error){
console.log(">> 'resolve' error");

View file

@ -73,17 +73,20 @@ function getAllFreePublicClaims(claimName){ // note: work in progress
module.exports = {
getClaimBasedOnNameOnly: function(claimName){
var deferred = new Promise(function (resolve, reject){
console.log(">> get all free claims for", claimName);
console.log(">> lbryHelpers >> getClaim BasedOnNameOnly:", claimName);
// promise to get all free public claims
getAllFreePublicClaims(claimName)
.then(function(freePublicClaimList){
var freePublicClaimUri = freePublicClaimList[0].name + "#" + freePublicClaimList[0].claim_id;
console.log(">> your free public claim URI:", freePublicClaimUri);
console.log(">> successfully received free public claim URI:", freePublicClaimUri);
// promise to get the chosen uri
lbryApi.getClaim(freePublicClaimUri)
.then(function(data){
console.log(">> dl path =", data.result.download_path)
resolve(data.result.download_path)
resolve({
fileName: data.result.file_name,
directory: data.result.download_directory,
contentType: data.result.metadata.stream.source.contentType
});
}).catch(function(error){
reject(error)
});
@ -98,12 +101,15 @@ module.exports = {
to do: need to pass the URI through a test to see if it is free and public. Right now it is jumping straight to 'get'ing and serving the asset.
*/
var deferred = new Promise(function (resolve, reject){
console.log(">> get claim based on URI:", uri);
console.log(">> lbryHelpers >> getClaimBasedOnUri:", uri);
// promise to get the chosen uri
lbryApi.getClaim(uri)
.then(function(data){
console.log(">> dl path =", data.result.download_path)
resolve(data.result.download_path)
resolve({
fileName: data.result.file_name,
directory: data.result.download_directory,
contentType: data.result.metadata.stream.source.contentType
});
}).catch(function(error){
reject(error)
});

View file

@ -1,3 +1,30 @@
function serveFile(fileInfo, res){
var options = {
root: fileInfo.directory,
headers: { "contentType": fileInfo.contentType}
};
switch (fileInfo.contentType){
case "image/jpeg":
console.log("sending jpeg");
break;
case "image/gif":
console.log("sending gif");
break;
case "image/png":
console.log("sending png");
break;
case "video/mp4":
console.log("sending mp4");
break;
default:
console.log("sending unknown file type as .jpeg");
options["headers"]["contentType"] = "image/jpeg";
break;
}
console.log('options', options);
res.status(200).sendFile(fileInfo.fileName, options);
}
module.exports = function(app, routeHelpers, lbryHelpers, ua, googleAnalyticsId){
// route to fetch one free public claim
app.get("/:name/:claim_id", function(req, res){
@ -6,11 +33,10 @@ module.exports = function(app, routeHelpers, lbryHelpers, ua, googleAnalyticsId)
console.log(">> GET request on /" + uri);
// create promise
lbryHelpers.getClaimBasedOnUri(uri)
.then(function(filePath){
console.log("/:name/:claim_id success.");
res.status(200).sendFile(filePath);
})
.catch(function(error){
.then(function(fileInfo){
console.log("/:name/:claim_id success.", fileInfo.fileName);
serveFile(fileInfo, res);
}).catch(function(error){
console.log("/:name/:claim_id error:", error)
routeHelpers.handleRequestError(error, res);
});
@ -21,9 +47,9 @@ module.exports = function(app, routeHelpers, lbryHelpers, ua, googleAnalyticsId)
console.log(">> GET request on /" + req.params.name);
// create promise
lbryHelpers.getClaimBasedOnNameOnly(req.params.name)
.then(function(filePath){
console.log("/:name success.")
res.status(200).sendFile(filePath);
.then(function(fileInfo){
console.log("/:name success.", fileInfo.fileName);
serveFile(fileInfo, res);
}).catch(function(error){
console.log("/:name error:", error);
routeHelpers.handleRequestError(error, res);

View file

@ -10,10 +10,15 @@ module.exports = function(app, path, siofu, socketHelpers, ua, googleAnalyticsId
var uploader = new siofu();
uploader.dir = path.join(__dirname, '../../Uploads');
uploader.listen(socket);
uploader.on("start", function(event){
if (/\.exe$/.test(event.file.name)) {
uploader.abort(event.file.id, socket);
}
});
uploader.on("error", function(event){
console.log("an error occured while uploading", event.error);
socket.emit("publish-status", event.error)
})
});
uploader.on("saved", function(event){
console.log("saved " + event.file.name);
if (event.file.success){