From 661677b15e1ee0701cc7681fc7e99a55165f0658 Mon Sep 17 00:00:00 2001 From: NimeshaKahingala Date: Fri, 7 Mar 2025 14:46:02 -0600 Subject: [PATCH 1/5] Fixed file extension extraction issue for filenames with multiple dots. --- packages/back-end/src/functions/httpFunctions.ts | 11 +++++++---- packages/back-end/src/utils/utils.ts | 12 ++++++++++++ packages/front-end/pages/products/[id].vue | 8 ++++---- 3 files changed, 23 insertions(+), 8 deletions(-) create mode 100644 packages/back-end/src/utils/utils.ts diff --git a/packages/back-end/src/functions/httpFunctions.ts b/packages/back-end/src/functions/httpFunctions.ts index ec3301a..9d6f093 100644 --- a/packages/back-end/src/functions/httpFunctions.ts +++ b/packages/back-end/src/functions/httpFunctions.ts @@ -12,6 +12,7 @@ import { } from "../lib/azure-storage.js"; import { DICT_TYPE } from "../types/generalTypes.js"; import { example_products } from "../dummyData/exampleProducts.js"; +import {getFileNameAndFileType} from '../utils/utils.js'; import _ from "lodash"; // make sure important environment variables are present @@ -38,7 +39,8 @@ const routeFunctions: DICT_TYPE = { getOKH, getOKHs, getExampleProducts, - "getFile/{containerName}/{fileName}/{fileType}": getFile, // Example: "getFile/okh/bread/yml" + // "getFile/{containerName}/{fileName}/{fileType}": getFile, // Example: "getFile/okh/bread/yml" + "getFile/{containerName}/{fullFileName}": getFile, // Example: "getFile/okh/bread.yml" "listFiles/{containerName}": listFilesByContainerName, // Example: http://localhost:7071/api/listFiles/okw OR http://localhost:7071/api/listFiles/okh listOKHsummaries, // This is specifically meant to provide thumbnails for the frontend }; @@ -208,7 +210,7 @@ export async function listOKHsummaries( const fdata = await getOKHByFileName(fname, "okh", extension); const product_summary = convertToProduct(fname+"."+extension,fdata, id_cnt++); summaries.push(product_summary); - console.log("SUMMARY",product_summary); + // console.log("SUMMARY",product_summary); } } let productsObj = { productSummaries: summaries }; @@ -222,9 +224,10 @@ export async function getFile( context: any ): Promise { context.log("getFile"); - const { containerName, fileName, fileType } = request.params; + // const { containerName, fileName, fileType } = request.params; +const { containerName, fullFileName } = request.params; + const{fileName, fileType} = getFileNameAndFileType(fullFileName); context.log(containerName, fileName, fileType); - if (!containerName || !fileName || !fileType) { return { jsonBody: "error, no containerName or fileName" }; } diff --git a/packages/back-end/src/utils/utils.ts b/packages/back-end/src/utils/utils.ts new file mode 100644 index 0000000..57fca8b --- /dev/null +++ b/packages/back-end/src/utils/utils.ts @@ -0,0 +1,12 @@ +export function getFileNameAndFileType(filename: string): { fileName: string; fileType: string } { + const lastDotIndex = filename.lastIndexOf("."); + + if (lastDotIndex === -1) { + return { fileName: filename, fileType: "" }; // No extension found + } + + const fileName = filename.substring(0, lastDotIndex); // Extract name before last dot + const fileType = filename.substring(lastDotIndex + 1); // Extract extension after last dot + + return { fileName, fileType }; +} \ No newline at end of file diff --git a/packages/front-end/pages/products/[id].vue b/packages/front-end/pages/products/[id].vue index 65fe295..f8ab276 100644 --- a/packages/front-end/pages/products/[id].vue +++ b/packages/front-end/pages/products/[id].vue @@ -10,12 +10,12 @@ const route = useRoute(); const baseUrl = useRuntimeConfig().public.baseUrl; const productFilename = route.params.id as string; -const [fname, fileExt] = productFilename.split("."); +// const [fname, fileExt] = productFilename.split("."); -const url = baseUrl + "/getFile/okh/" + fname + "/" + fileExt; +// const url = baseUrl + "/getFile/okh/" + fname + "/" + fileExt; + +const url = baseUrl + "/getFile/okh/" + productFilename -// const url = baseUrl + "/getFile/okh/" + productFilename -//const url = "http://demo4460398.mockable.io/details"; const { data, status, error } = await useFetch(url, { From 2fa096aaed4c89c172c501437125e6abf7d944ef Mon Sep 17 00:00:00 2001 From: NimeshaKahingala Date: Sun, 27 Apr 2025 20:11:52 -0500 Subject: [PATCH 2/5] resolved conflicts --- .../back-end/src/functions/httpFunctions.ts | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/packages/back-end/src/functions/httpFunctions.ts b/packages/back-end/src/functions/httpFunctions.ts index 388ebdc..316e21c 100644 --- a/packages/back-end/src/functions/httpFunctions.ts +++ b/packages/back-end/src/functions/httpFunctions.ts @@ -318,14 +318,14 @@ async function getOKHByFileName( ): Promise { const fileExt: string = fileType || name.split(".").pop() || ""; -// // Create a cache key based on the name and fileType -// const cacheKey = `${name}-${fileExt}`; + // Create a cache key based on the name and fileType + const cacheKey = `${name}-${fileExt}`; -// //Check if the result is already cached -// if (cache.has(cacheKey)) { -// console.log("Returning cached result for:", cacheKey); -// return cache.get(cacheKey); // Return the cached result -// } + //Check if the result is already cached + if (cache.has(cacheKey)) { + console.log("Returning cached result for:", cacheKey); + return cache.get(cacheKey); // Return the cached result + } let result: any = null; @@ -344,10 +344,10 @@ async function getOKHByFileName( ); } - // // Cache the result if it's not null - // if (result !== null) { - // cache.set(cacheKey, result); - // } + // Cache the result if it's not null + if (result !== null) { + cache.set(cacheKey, result); + } return result; } From da68b4b74741d122adb160b63c0c63386f66a9fd Mon Sep 17 00:00:00 2001 From: NimeshaKahingala Date: Sun, 27 Apr 2025 20:12:35 -0500 Subject: [PATCH 3/5] resolved conflicts --- packages/back-end/src/functions/httpFunctions.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/back-end/src/functions/httpFunctions.ts b/packages/back-end/src/functions/httpFunctions.ts index 316e21c..b193e09 100644 --- a/packages/back-end/src/functions/httpFunctions.ts +++ b/packages/back-end/src/functions/httpFunctions.ts @@ -321,7 +321,7 @@ async function getOKHByFileName( // Create a cache key based on the name and fileType const cacheKey = `${name}-${fileExt}`; - //Check if the result is already cached + // Check if the result is already cached if (cache.has(cacheKey)) { console.log("Returning cached result for:", cacheKey); return cache.get(cacheKey); // Return the cached result From f7a3fff4bae0c45b961ac7dda964329a3895154f Mon Sep 17 00:00:00 2001 From: NimeshaKahingala Date: Sun, 27 Apr 2025 20:29:57 -0500 Subject: [PATCH 4/5] handle file extension error --- packages/back-end/src/utils/utils.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/back-end/src/utils/utils.ts b/packages/back-end/src/utils/utils.ts index 57fca8b..3e4aa6c 100644 --- a/packages/back-end/src/utils/utils.ts +++ b/packages/back-end/src/utils/utils.ts @@ -1,8 +1,8 @@ export function getFileNameAndFileType(filename: string): { fileName: string; fileType: string } { const lastDotIndex = filename.lastIndexOf("."); - if (lastDotIndex === -1) { - return { fileName: filename, fileType: "" }; // No extension found + if (lastDotIndex === -1 || lastDotIndex === filename.length - 1) { + throw new Error("Invalid filename: no valid file extension found."); } const fileName = filename.substring(0, lastDotIndex); // Extract name before last dot From 15dbb8afe00fbbf5a2a2f81fe2d64a5f56ff9863 Mon Sep 17 00:00:00 2001 From: NimeshaKahingala Date: Sun, 27 Apr 2025 20:31:37 -0500 Subject: [PATCH 5/5] handle file extension error --- packages/front-end/pages/products/[id].vue | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/front-end/pages/products/[id].vue b/packages/front-end/pages/products/[id].vue index 888da56..b5d52bf 100644 --- a/packages/front-end/pages/products/[id].vue +++ b/packages/front-end/pages/products/[id].vue @@ -13,7 +13,7 @@ const productFilename = route.params.id as string; // const url = baseUrl + "/getFile/okh/" + fname + "/" + fileExt; -const url = baseUrl + "/getFile/okh/" + productFilename +const url = baseUrl + "/getFile/okh/" + productFilename;