improve `getMediaFromDirectory` function

- Sanatize input to be independent of OS.
- Extract right slug for both cases: if input parameter is file or is directory.

fixes #5
pull/6/head
David Ordás 2022-09-13 15:29:00 +02:00
parent ee6f2f61bc
commit c91ac28dbf
No known key found for this signature in database
GPG Key ID: 6FD751229911593E
1 changed files with 11 additions and 3 deletions

View File

@ -174,9 +174,17 @@ function getFilesFromDir(dir) {
* @returns {String} The extracted directory name
*/
function getMediaTypeFromDirectoryPath(str) {
const slash = str.lastIndexOf("/");
let mediaType = str.slice(2, slash);
return mediaType;
str = path.resolve(str); // sanatize and expand (OS independent)
let type;
if (fs.lstatSync(str).isDirectory()) {
// if path is itself a directory, use it name as result
type = path.basename(str);
} else {
// if not... parent/previous slug is always a directory; extract this part
// path.sep: Windows -> "\", Unix -> "/"
type = str.split(path.sep).slice(-2, -1).join(path.sep);
}
return type;
}
/**