Removed the first attempt to read the directory to check if the model was available. Replaced with direct model-reading with error handling

This commit is contained in:
Alexander Cerutti
2018-05-07 19:19:31 +02:00
parent b93965efde
commit 65e4ba6bd0

View File

@@ -114,13 +114,12 @@ function generateManifest(fromObject, tempFolderPath) {
} }
const source = typeof fromObject === "object" ? JSON.stringify(fromObject) : fromObject; const source = typeof fromObject === "object" ? JSON.stringify(fromObject) : fromObject;
const manifestBuffer = Buffer.from(source);
let manifestWS = fs.createWriteStream(`${tempFolderPath}/manifest.json`); let manifestWS = fs.createWriteStream(`${tempFolderPath}/manifest.json`);
manifestWS.write(source); manifestWS.write(source);
manifestWS.end(); manifestWS.end();
return done(manifestBuffer); return done(Buffer.from(source));
}); });
} }
@@ -133,31 +132,31 @@ instance.get("/", function (req, res) {
}); });
instance.get("/gen/:type/", function (req, res) { instance.get("/gen/:type/", function (req, res) {
fs.readdir(passModelsDir, {}, function (err, result) { if (!supportedTypesOfPass.test(req.params.type)) {
res.set("Content-Type", "application/json");
res.status(418).send({ ecode: 418, status: false, message: `Model unsupported. Refer to https://apple.co/2KKcCrB for supported pass models.`});
return;
}
fs.readdir(`${passModelsDir}/${req.params.type}.pass`, {}, function (err, files) {
/* Invalid path for passModelsDir */ /* Invalid path for passModelsDir */
if (err) { if (err) {
console.error(err); res.set("Content-Type", "application/json");
throw err; res.status(418).send({ ecode: 418, status: false, message: `Model not available for requested type [${res.params.type}]. Provide a folder with specified name and .pass extension.`});
}
/* Removing all the files and folders which start with "." (hidden files and folder) from the Array */
result = removeDotFiles(result);
/* No folders inside passModelsDir */
if (!result) {
res.write("No pass models found.");
return; return;
} }
/* Type in URL not conformant to supportedTypesOfPass */ let list = removeDotFiles(files);
if (!supportedTypesOfPass.test(req.params.type)) {
res.send("The requested type of pass is not supported."); if (!list.length) {
res.set("Content-Type", "application/json");
res.status(418).send({ ecode: 418, status: false, message: `Model for type [${req.params.type}] has no contents. Refer to https://apple.co/2IhJr0Q `});
return; return;
} }
/* type in URL has not corresponding model in pass folder */ if (!list.includes("pass.json")) {
if (!result.some(model => model.toLowerCase().includes(req.params.type.toLowerCase()))) { res.set("Content-Type", "application/json");
res.send("No models available for this query."); res.status(418).send({ ecode: 418, status: false, message: "I'm a tea pot. How am I supposed to serve you pass without pass.json in the chosen model as tea without water?" });
return; return;
} }
@@ -167,29 +166,8 @@ instance.get("/gen/:type/", function (req, res) {
} }
// Manifest dictionary // Manifest dictionary
let manifest = {};
fs.readdir(`${passModelsDir}/${req.params.type}.pass`, function(err, files) {
if (err) {
throw err;
}
let list = removeDotFiles(files);
if (!list.length) {
res.set("Content-Type", "application/json");
res.status(418).send({ ecode: 418, status: false, message: `Model for type [${req.params.type}] has no contents. Refer to https://apple.co/2IhJr0Q `})
return;
}
if (!list.includes("pass.json")) {
res.set("Content-Type", "application/json");
res.status(418).send({ ecode: 418, status: false, message: "I'm a tea pot. How am I supposed to serve you pass without Pass.json in the chosen model as tea without water?" });
return;
}
let manifestRaw = {}; let manifestRaw = {};
let archive = archiver("zip") let archive = archiver("zip");
async.each(list, function getHashAndArchive(file, callback) { async.each(list, function getHashAndArchive(file, callback) {
let passFileStream = fs.createReadStream(`${passModelsDir}/${req.params.type}.pass/${file}`); let passFileStream = fs.createReadStream(`${passModelsDir}/${req.params.type}.pass/${file}`);
@@ -251,8 +229,6 @@ instance.get("/gen/:type/", function (req, res) {
throw error; throw error;
}); });
}); });
});
}); });
}); });
}); });