Skip to content

Fixed file extension extraction issue for filenames with multiple dots. #62

New issue

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

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

Already on GitHub? # to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
13 changes: 7 additions & 6 deletions packages/back-end/src/functions/httpFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
};
Expand Down Expand Up @@ -210,7 +212,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 };
Expand All @@ -224,9 +226,10 @@ export async function getFile(
context: any
): Promise<HttpResponseInit> {
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" };
}
Expand All @@ -248,8 +251,6 @@ async function getOKHByFileName(

const fileExt: string = fileType || name.split(".").pop() || "";

console.log("AADD",name.split("."));

// // Warning!! This function does NOT match the apparent
// // documentation: https://learn.microsoft.com/en-us/azure/storage/blobs/storage-blob-download-javascript
// // I had to figure it out by guessing. The documentation there
Expand Down
12 changes: 12 additions & 0 deletions packages/back-end/src/utils/utils.ts
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this would be slightly better if rewritten so that there is only one return statement, with the conditional statement setting the values.

export function getFileNameAndFileType(filename: string): { fileName: string; fileType: string } {
const lastDotIndex = filename.lastIndexOf(".");

const fileName = (lastDotIndex === -1) ? filename : filename.substring(0, lastDotIndex); // Extract name before last dot
const fileType = (lastDotIndex === -1) ? "" : filename.substring(lastDotIndex + 1); // Extract extension after last dot

return { fileName, fileType };

}

Original file line number Diff line number Diff line change
@@ -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 };
}
19 changes: 3 additions & 16 deletions packages/front-end/pages/products/[id].vue
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think line 16 should have a semicolon appended at the end (;)

Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,12 @@ const route = useRoute();
const baseUrl = useRuntimeConfig().public.baseUrl;

const productFilename = route.params.id as string;
// const [fname, fileExt] = productFilename.split(".");

let purename = "";
let fileExt = ""
if (productFilename.endsWith(".yml")) {
purename = productFilename.slice(0,-4);
fileExt = "yml";
} else if (productFilename.endsWith(".json")) {
purename = productFilename.slice(0,-5);
fileExt = "json";
} else {
console.log("BAD FILENAME, MUST END IN yml or json:",productFilename);
}

console.log("purename", purename);
// const url = baseUrl + "/getFile/okh/" + fname + "/" + fileExt;

const url = baseUrl + "/getFile/okh/" + purename + "/" + 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, {
Expand Down