Where does `file://` reference in an electron server - node.js

I wish to access the file system, but the file:// prefix does not seem to correspond to the absolute path.
When trying to fetch('file://<absolute path to file>')
e.g. Users/name/dir/file I seem to be unable to locate the file required.
Omitting the file part, fetch appends the relative path to the end of localhost, what does file reference if it's not the absolute path?

Related

how I can prevent res.sendFile changing file path?

I'm using node.js and want to send file to the frontend. So I specified the direct path to my file like:
path = "c:/app/A"
and when I run res.sendFile(path, fileName);
I'm getting the Error: ENOENT: no such file or directory, stat '/home/projects/c:/app/A'
How I can disable this auto path adding "/home/projects" part?
I want to download file that is not in my project folder with my code. File is in my computer in different folder.
Try to use \\ as path delimiter for Windows (c:\\app\\A) and read about Node.js module "path".
so I need use just new URL(file:${"c:/app/A"});
so it will be like that:
let filename = "someName.com"
let absPath = "c:/app/someName.com";
fs.writeFileSync(`${filename}`, fs.readFileSync(new URL(`file:${absPath}`)));
res.download(`${filename}`, `${filename}`)

Linux Command to Convert an Absolute Path to a Current Existing Relative Path

I already have the relative path: /home/Folder1/Folder2 which its original absolute path is /home/user1/Folder1/Folder2. And I have several scripts that are using /home/Folder1/Folder2. Now, I need to delete user1 so I created user2 with the same structure of user1 so now I have a new path which is /home/user2/Folder1/Folder2. If I delete user1, my scripts will then fail because they are using the relative path /home/Folder1/Folder2 which its original absolute path is /home/user1/Folder1/Folder2. So I want my new path /home/user2/Folder1/Folder2 to point to /home/Folder1/Folder2 so that my scripts won't fail and I don't want to go through the trouble of opening each script and change the relative path to my new created path. Any idea how I can do this?
I guess, you got confused between soft links and absolute/relative path.
I assume you have a soft link created from "/home/Folder1/Folder2" pointing to "/home/user1/Folder1/Folder2" and you want to delete user1 directory and create user2 directory with same structure. If my assumption is right, recreate the softlink "/home/Folder1/Folder2" to point to "/home/user2/Folder1/Folder2". Your existing scripts will work seamlessly.

Relative addressing files python3

I have variable address = /data/train/1.jpg, and I'm trying to read file by
im = Image.open(address)
FileNotFoundError: [Errno 2] No such file or directory: '/data/train/1.jpg'
By some reasons I can't use full name of file.
I started jupyter notebook from folder which actually contains file 1.jpg in /data/train/.
How can I fix it?
Relative addressing means from the perspective of the current working directory. So if you script is in the same directory that the data folder is in, your path to the file would be ./data/train/1.jpg. Note the ./, which means the current directory.
use relative path, this one is absolute
address = './data/train/1.jpg'
im = Image.open(address)
in this case . means current location while slash means the environments root (view and explanation) on this depends on your OS

Resolving paths in a custom root directory and avoiding going in parent folders

Using path.resolve we can resolve paths using Node.js.
I want to have a directory containing files and other directories and so on. From the client I receive a path as string and I have to resolve it in this directory.
Because of security, I have to prevent the result path to be outside of the directory:
// This comes from the client
let filePath = "/../../../some-sensitive-data-from/disk";
/* Here I have to check if the path is inside of the directory */
path.resolve(ABSOLUTE_PATH_TO_MY_DIR + filePath);
How to prevent the access to parent directories of ABSOLUTE_PATH_TO_MY_DIR?
Obviously a solution would be to check if the final path is still inside of the ABSOLUTE_PATH_TO_MY_DIR path. But maybe there are other better solutions.

Malformed url exception linux local file system

I have been trying to execute a selenium.attachFile command to upload a file-
sel.attachFile(dom_locator,"/home/xyz/Desktop/tstfl.txt");
but getting a Malformed URL Exception for the file path specified. The file is present in the linux local file system. Please help me with the proper format of the file path.
Likely what you need is
sel.attachFile(dom_locator,"file:///home/xyz/Desktop/tstfl.txt");
For an explanation of file uris, see File URI scheme in wikipedia.

Resources