path mainp

This commit is contained in:
Manav Rathi 2024-04-18 13:47:46 +05:30
parent 518d573add
commit 9c336a38c0
No known key found for this signature in database
2 changed files with 64 additions and 2 deletions

View file

@ -110,14 +110,59 @@ export function areFileWithCollectionsSame(
return firstFile.localID === secondFile.localID;
}
export function getImportSuggestion(
/**
* Return true if all the paths in the given list are items that belong to the
* same (arbitrary) directory.
*/
export const areInSameDirectory = (
uploadType: PICKED_UPLOAD_TYPE,
paths: string[],
toUploadFiles: File[] | ElectronFile[],
): ImportSuggestion {
if (isElectron() && uploadType === PICKED_UPLOAD_TYPE.FILES) {
return DEFAULT_IMPORT_SUGGESTION;
}
const paths: string[] = toUploadFiles.map((file) => file["path"]);
const getCharCount = (str: string) => (str.match(/\//g) ?? []).length;
paths.sort((path1, path2) => getCharCount(path1) - getCharCount(path2));
const firstPath = paths[0];
const lastPath = paths[paths.length - 1];
const L = firstPath.length;
let i = 0;
const firstFileFolder = firstPath.substring(0, firstPath.lastIndexOf("/"));
const lastFileFolder = lastPath.substring(0, lastPath.lastIndexOf("/"));
while (i < L && firstPath.charAt(i) === lastPath.charAt(i)) i++;
let commonPathPrefix = firstPath.substring(0, i);
if (commonPathPrefix) {
commonPathPrefix = commonPathPrefix.substring(
0,
commonPathPrefix.lastIndexOf("/"),
);
if (commonPathPrefix) {
commonPathPrefix = commonPathPrefix.substring(
commonPathPrefix.lastIndexOf("/") + 1,
);
}
}
return {
rootFolderName: commonPathPrefix || null,
hasNestedFolders: firstFileFolder !== lastFileFolder,
hasRootLevelFileWithFolder: firstFileFolder === "",
};
}
export function getImportSuggestion(
uploadType: PICKED_UPLOAD_TYPE,
toUploadFiles: File[] | ElectronFile[],
): ImportSuggestion {
if (isElectron() && uploadType === PICKED_UPLOAD_TYPE.FILES) {
return DEFAULT_IMPORT_SUGGESTION;
}
const paths: string[] = toUploadFiles.map((file) => file["path"]);
const getCharCount = (str: string) => (str.match(/\//g) ?? []).length;
paths.sort((path1, path2) => getCharCount(path1) - getCharCount(path2));
const firstPath = paths[0];

View file

@ -29,6 +29,23 @@ export const nameAndExtension = (fileName: string): FileNameComponents => {
export const fileNameFromComponents = (components: FileNameComponents) =>
components.filter((x) => !!x).join(".");
/**
* Extract the fileName from the given path.
*/
export const fileNameFromPOSIXPath = (path: string) => {
const pathComponents = path.split("/");
return pathComponents[pathComponents.length - 1] ?? path;
};
/**
* Extract the directory path (leading up to the item) from the given path.
*/
export const directoryNameFromPOSIXPath = (path: string) => {
const pathComponents = path.split("/");
pathComponents.pop();
return pathComponents.join("/");
};
export function getFileNameSize(file: File | ElectronFile) {
return `${file.name}_${convertBytesToHumanReadable(file.size)}`;
}