Nodejs checks if a given string matches a valid file system path without actually checking the file system

As in nodejs, I can check if a given string is a valid file system path without actually checking the file system.

I am using the telegram bot nodejs api and when sending files it checks if the file exists or not. But the second time I use telegram file id, which I received from the preview download, and not the actual file path for sending the file, so in this case I want to check if the line is the actual file path, and not the id (exp:) file AgADBAADuqcxG-ysuwhqkRaOdVnJI0CZXhkABL1NBSyVlK3gduoAAgI, before rather than checking if a file exists to improve performance and avoid unnecessary access to the file system.

+4
source share
1 answer

1.

You can check if the string is equal path.basename(string). Then it does not contain path separators and is definitely not a file in another directory. This funtion does not affect the file system and only checks the string for the directory separator for specific directories and therefore matches your performance needs.

string === path.basename(string)

2.

Another idea: if the identifiers match a specific format, create a regular expression for the format and check each line for consistency and act accordingly.

/identifierregex/.test(string)
+4
source

Source: https://habr.com/ru/post/1651394/


All Articles