Get File Path with WebDev

I am trying to iterate through a mounted folder and find a specific file. The file has multiple folders inside of that folder and then has the specific pdf file that I want to open. In the end want to open in pdf viewer in ignition. Is there any way to either return the json of the mounted folder where I can then iterate and set path or something similar?

No. Mounted folders work on "just-in-time" discovery - when a request is made for file X, a request is made for folder Y/file X, right then. There's no indexing or crawling or anything of your filesystem done by Ignition.

If you want more capability, you'll have to script it yourself using a Python endpoint.

I am ok with scripting but I don't know the best approach for that. Would I need to have a layout of all my files and then use that?

Start by hardcoding the directory you care about into your Python endpoint.
Then, you could pay attention to the remainingPath request parameter in a Python endpoint:
https://docs.inductiveautomation.com/display/DOC81/Web+Dev#WebDev-PythonResources

If it's None, then this is a request to the 'root', so you're going to use Java's File class to list the child files in that directory:
https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/io/File.html#listFiles()
And assemble that into a JSON payload or whatever else.

If there is a remaining path, then you're going to try to find that file, load the file, and then write it to the request's output stream.

Be careful with file traversal! A malicious actor could pass a relative path like ../../../../etc/passwd to your endpoint!

To avoid this risk, I'd suggest taking advantage of our SecurePathUtils class.

2 Likes

Could you describe a little more about the directory that I need to hard code. I am very new to this part of ignition so a bit confused. There are a lot of paths so would I have to list all those paths out?

You currently have a mounted folder, right? The path to hardcode is whatever path you currently have in that mounted folder. That's the "root directory", as far as the rest of the handler is concerned. Your browse operation to list files will be relative to that directory. It's up to you whether to support multiple layers of nesting, or just a flat browse of files at that directory.