Skip to content

Commit b6a68ad

Browse files
committed
CodeRabbit fixes
1 parent 055a2e4 commit b6a68ad

File tree

2 files changed

+34
-16
lines changed

2 files changed

+34
-16
lines changed
Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
import { getFileStreamAndMetadata } from "@pipedream/platform";
22

33
export const isValidUrl = (urlString) => {
4-
var urlPattern = new RegExp("^(https?:\\/\\/)?" + // validate protocol
5-
"((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|" + // validate domain name
6-
"((\\d{1,3}\\.){3}\\d{1,3}))" + // validate OR ip (v4) address
7-
"(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*" + // validate port and path
8-
"(\\?[;&a-z\\d%_.~+=-]*)?" + // validate query string
9-
"(\\#[-a-z\\d_]*)?$", "i"); // validate fragment locator
4+
var urlPattern = new RegExp(
5+
"^(https?:\\/\\/)?" + // validate protocol
6+
"((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|" + // validate domain name
7+
"((\\d{1,3}\\.){3}\\d{1,3}))" + // validate OR ip (v4) address
8+
"(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*" + // validate port and path
9+
"(\\?[;&a-z\\d%_.~+=-]*)?" + // validate query string
10+
"(\\#[-a-z\\d_]*)?$",
11+
"i",
12+
); // validate fragment locator
1013
return !!urlPattern.test(urlString);
1114
};
1215

@@ -19,10 +22,14 @@ export const checkTmp = (filename) => {
1922

2023
export const getUrlOrFile = async (url) => {
2124
const {
22-
stream, metadata: { contentType },
25+
stream,
26+
metadata: { contentType },
2327
} = await getFileStreamAndMetadata(url);
24-
const base64Image = Buffer.from(stream, "binary").toString("base64");
25-
return {
26-
file: `data:${contentType};base64,${base64Image}`,
27-
};
28+
const chunks = [];
29+
for await (const chunk of stream) {
30+
chunks.push(chunk);
31+
}
32+
const buffer = Buffer.concat(chunks);
33+
const base64Image = buffer.toString("base64");
34+
return `data:${contentType};base64,${base64Image}`;
2835
};

components/pdf4me/common/utils.mjs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ function normalizeFilePath(path) {
99
: `/tmp/${path}`;
1010
}
1111

12+
function checkForExtension(filename, ext = "pdf") {
13+
return filename.endsWith(`.${ext}`)
14+
? filename
15+
: `${filename}.${ext}`;
16+
}
17+
1218
function downloadToTmp(response, filename) {
1319
const rawcontent = response.toString("base64");
1420
const buffer = Buffer.from(rawcontent, "base64");
@@ -38,15 +44,20 @@ function handleErrorMessage(error) {
3844
}
3945

4046
async function getBase64File(filePath) {
41-
const stream = await getFileStream(filePath);
42-
const chunks = [];
43-
for await (const chunk of stream) {
44-
chunks.push(chunk);
47+
try {
48+
const stream = await getFileStream(filePath);
49+
const chunks = [];
50+
for await (const chunk of stream) {
51+
chunks.push(chunk);
52+
}
53+
return Buffer.concat(chunks).toString("base64");
54+
} catch (error) {
55+
throw new ConfigurationError(`Error parsing file \`${filePath}\`: ${error.message}`);
4556
}
46-
return Buffer.concat(chunks).toString("base64");
4757
}
4858

4959
export default {
60+
checkForExtension,
5061
downloadToTmp,
5162
handleErrorMessage,
5263
getBase64File,

0 commit comments

Comments
 (0)