Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Staging #44

Merged
merged 8 commits into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM node:14
FROM node:20
MAINTAINER [email protected]

ENV NAME fw-exports
Expand Down
16 changes: 12 additions & 4 deletions app/src/routes/v3/answers.router.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ class AnswerRouter {
ctx.throw(400, "File type must be pdf or zip");
}

const answer = await AnswerService.getAnswer({
const answer = await AnswerService.getAnswerWithUrl({
reportid: answerId,
templateid: answerId
});
Expand Down Expand Up @@ -159,10 +159,19 @@ class AnswerRouter {
let imageUrls = imageResponse.value ?? [];
if (typeof imageUrls === "string") {
imageUrls = [imageUrls];
} else if (Array.isArray(imageUrls)) {
imageUrls = imageUrls.map(url => (typeof url === "object" ? url.url : url));
} else if (typeof imageUrls === "object") {
imageUrls = [imageUrls.url];
}

for (const url of imageUrls) {
imagePromises.push(axios.get(url, { responseType: "arraybuffer" }).then(res => ({ data: res.data, url })));
const urlToDownload = typeof url === "object" ? url.url : url;
imagePromises.push(
axios
.get(urlToDownload, { responseType: "arraybuffer" })
.then(res => ({ data: res.data, url: urlToDownload }))
);
}
}
const imageBuffers = await Promise.all(imagePromises);
Expand All @@ -183,8 +192,7 @@ class AnswerRouter {
if (fileType === "pdf") {
const imagesPdfInput = [];
for (const buffer of imageBuffers) {
const fileExt = buffer.url.split("/").pop().split(".").pop();

const fileExt = buffer.url.split("/").pop().split(".").pop().split("?")[0];
// Rotate images if they are in the wrong orientation (only for jpeg) as pdfgen does not do this
if (fileExt === "jpeg" || fileExt === "jpg") {
try {
Expand Down
27 changes: 27 additions & 0 deletions app/src/services/answers.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,33 @@ export class AnswerService {
}
}

/**
* Returns the answer for a given id
* @param {{templateid: string, reportid: string}} params The id for the answer to fetch as well as the parent report of the answer
* @returns The answer object with the id
*/
static async getAnswerWithUrl(params) {
const { templateid, reportid } = params;
logger.info(`Getting answer with id ${reportid} of template id ${templateid}`);
try {
const baseURL = config.get("coreAPI.url");
const response = await axios.default({
baseURL,
url: `/templates/${templateid}/answers/imageExports/${reportid}`,
method: "GET",
headers: {
authorization: loggedInUserService.token
}
});
const answer = response.data;
logger.info("Got answer", answer);
return answer && answer.data;
} catch (e) {
logger.error("Error while fetching answer", e);
return null; // log the error but still return
}
}

static async getAllAnswers() {
logger.info(`Getting all answers`);
try {
Expand Down
17 changes: 13 additions & 4 deletions app/src/services/reportFile.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class ReportFileService {
const fileDownloadPromises = fileUrls.map(url => {
if (url)
return axios({
url: url,
url: typeof url === "object" ? url.url : url,
responseType: "stream",
responseEncoding: "utf-8"
});
Expand Down Expand Up @@ -214,7 +214,7 @@ class ReportFileService {
const fileDownloadPromises = fileUrls.map(url => {
if (url)
return axios({
url: url,
url: typeof url === "object" ? url.url : url,
responseType: "stream",
responseEncoding: "utf-8"
});
Expand Down Expand Up @@ -552,10 +552,13 @@ class ReportFileService {
questions.push(question);
}
let files = [];

// check if the answer is a file
if (["blob", "audio"].includes(question.type)) {
files = Array.isArray(response.value) ? response.value : [response.value];
responseToShow = `File(s) found at: \n${files.join("\n")}`;
responseToShow = `File(s) found at: \n${files
.map(file => (typeof file === "object" ? file.url : file))
.join("\n")}`;
} else responseToShow = response.value;

doc
Expand All @@ -565,7 +568,13 @@ class ReportFileService {
doc.moveDown(0.5);
if (files.length > 0)
files.forEach(file => {
doc.font("Regular").fontSize(11).text(file, 50, doc.y, { link: file, underline: true }); //, lineY + 30 + 50 * i);
doc
.font("Regular")
.fontSize(11)
.text(typeof file === "object" ? file.url : file, 50, doc.y, {
link: typeof file === "object" ? file.url : file,
underline: true
}); //, lineY + 30 + 50 * i);
doc.moveDown(1);
});
else {
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,6 @@
},
"prettier": "@3sidedcube/prettier-config",
"engines": {
"node": "~14"
"node": "~20"
}
}
}