Let's say I get a directory listing of .jpg files and then want to display them
const fs = require('fs');
fs.readDirSync(someAbsolutePathToFolder)
.filter(f => f.endsWith('.jpg'))
.forEach(filename => {
const img = new Image();
const filepath = path.join(someAbsolutePathToFolder, filename);
img.src = `file://${filepath.replace(/\\/g, '/')}`;
document.body.appendChild(img);
});
This will fail. As just one example, if the listing I got had names like
#001-image.jpg
#002-image.jpg
the code above results in URLs like
file://some/path/#001-image.jpg
Which you can see is a problem. The URL will be cut at the #
.
This answer claims I should be using encodeURI
but that also fails as it leaves the #
unescaped.
Using encodeURIComponent
also does not work. It replaces /
and :
and space and Electron does not find the file, at least not on Windows.
Up until this point I had something like this
const filepath = path.join(someAbsolutePathToFolder, filename).replace(/\\/g, '/');
img.src = `file://${filepath.split('/').map(encodeURIComponent).join('/')}`;
but that also fails on windows because drive letters get convert from c:\dir\file
to c%3a\dir\file
which then appears to be a relative path to electron.
So I have more special cases trying to check for [a-z]:
at at the beginning of the path as well as \\
for UNC paths.
Today I ran into the #
issue mentioned above and I'm expecting more time bombs are waiting for me so I'm asking...
What is the correct way to convert a filename to a URL in a cross platform way?
Or to be more specific, how to solve the problem above. Given a directory listing of absolute paths of image files on the local platform generate URLs that will load those images if assigned to the src
property of an image.