2017-06-17 02:48:51 +02:00
|
|
|
var errorHandlers = require("../helpers/libraries/errorHandlers.js");
|
|
|
|
var serveController = require("../controllers/serveController.js");
|
|
|
|
|
2017-06-14 21:53:55 +02:00
|
|
|
function serveFile(fileInfo, res){
|
2017-06-15 00:02:38 +02:00
|
|
|
// set default options
|
2017-06-14 21:53:55 +02:00
|
|
|
var options = {
|
2017-06-14 22:11:42 +02:00
|
|
|
headers: {
|
|
|
|
"X-Content-Type-Options": "nosniff",
|
2017-06-16 19:07:31 +02:00
|
|
|
"Content-Type": fileInfo.file_type
|
2017-06-14 22:11:42 +02:00
|
|
|
}
|
2017-06-14 21:53:55 +02:00
|
|
|
};
|
2017-06-15 00:02:38 +02:00
|
|
|
// adjust default options as needed
|
2017-06-16 19:07:31 +02:00
|
|
|
switch (fileInfo.file_type){
|
2017-06-14 21:53:55 +02:00
|
|
|
case "image/jpeg":
|
|
|
|
break;
|
|
|
|
case "image/gif":
|
|
|
|
break;
|
|
|
|
case "image/png":
|
|
|
|
break;
|
|
|
|
case "video/mp4":
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
console.log("sending unknown file type as .jpeg");
|
2017-06-14 22:04:55 +02:00
|
|
|
options["headers"]["Content-Type"] = "image/jpeg";
|
2017-06-14 21:53:55 +02:00
|
|
|
break;
|
|
|
|
}
|
2017-06-15 00:02:38 +02:00
|
|
|
// send file
|
2017-06-16 19:07:31 +02:00
|
|
|
res.status(200).sendFile(fileInfo.file_path, options);
|
2017-06-14 21:53:55 +02:00
|
|
|
}
|
|
|
|
|
2017-06-17 02:48:51 +02:00
|
|
|
module.exports = function(app, ua, googleAnalyticsId){
|
2017-06-13 20:00:50 +02:00
|
|
|
// route to fetch one free public claim
|
|
|
|
app.get("/:name/:claim_id", function(req, res){
|
2017-06-15 22:06:35 +02:00
|
|
|
var routeString = req.params.name + "/" + req.params.claim_id;
|
|
|
|
// google analytics
|
|
|
|
ua(googleAnalyticsId, {https: true}).event("Serve Route", "/name/claimId", routeString).send();
|
|
|
|
// begin image-serve processes
|
|
|
|
console.log(">> GET request on /" + routeString);
|
2017-06-17 02:48:51 +02:00
|
|
|
serveController.getClaimByClaimId(req.params.name, req.params.claim_id)
|
2017-06-14 21:53:55 +02:00
|
|
|
.then(function(fileInfo){
|
2017-06-15 22:06:35 +02:00
|
|
|
console.log("/:name/:claim_id success.", fileInfo.file_name);
|
2017-06-14 21:53:55 +02:00
|
|
|
serveFile(fileInfo, res);
|
|
|
|
}).catch(function(error){
|
2017-06-13 20:00:50 +02:00
|
|
|
console.log("/:name/:claim_id error:", error)
|
2017-06-17 02:48:51 +02:00
|
|
|
errorHandlers.handleRequestError(error, res);
|
2017-06-13 20:00:50 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
// route to fetch one free public claim
|
|
|
|
app.get("/:name", function(req, res){
|
2017-06-15 22:06:35 +02:00
|
|
|
// google analytics
|
2017-06-13 20:00:50 +02:00
|
|
|
ua(googleAnalyticsId, {https: true}).event("Serve Route", "/name", req.params.name).send();
|
2017-06-15 22:06:35 +02:00
|
|
|
// begin image-serve processes
|
2017-06-13 20:00:50 +02:00
|
|
|
console.log(">> GET request on /" + req.params.name);
|
2017-06-17 02:48:51 +02:00
|
|
|
serveController.getClaimByName(req.params.name)
|
2017-06-14 21:53:55 +02:00
|
|
|
.then(function(fileInfo){
|
2017-06-15 22:06:35 +02:00
|
|
|
console.log("/:name success.", fileInfo.file_name);
|
2017-06-14 21:53:55 +02:00
|
|
|
serveFile(fileInfo, res);
|
2017-06-13 20:00:50 +02:00
|
|
|
}).catch(function(error){
|
|
|
|
console.log("/:name error:", error);
|
2017-06-17 02:48:51 +02:00
|
|
|
errorHandlers.handleRequestError(error, res);
|
2017-06-13 20:00:50 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|