Skip to content

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

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

Open
wants to merge 7 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
9 changes: 6 additions & 3 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
getRelatedOKH,
Expand Down Expand Up @@ -290,9 +292,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 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 };

}

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I did the following modifications.

  • Improved error handling compared to the previous behavior where invalid filenames would silently return an empty file type.
  • Now throws errors for missing or invalid file extensions (e.g., no . or filename ends with a .).

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 || 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
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 (;)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I did this modification.

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