I don't often post anything of a software nature, but it is what I do for a living and though most of what I do is fairly mundane, every now and then I do something that I'm rather proud of given my limited abilities. Today I'm going to bore you with one of these. To do so, I'm going to have to give you a little background.
Websites are what we call "stateless" applications. That means that everytime you do something on a site, by nature it doesn't hold anything about itself. It doesn't automatically remember anything. We developers have a lot of tricks we can use in a web browser to get around this such as storing in a database, on your actual computers (cookies), or simply passing along information that we can use to "remember" what just happened. When we do the latter two things we have to be careful what we do because we could be passing along information that hackers could use to compromise data.
Now to the actual problem I had this week. Imagine a site that has a bunch of files and you're wanting to browse through them. On your own computer, be it a Mac or PC, you have a tool to navigate through directories where you can look at files until you find the one you want. Browsers however are applications that sit on your computer and you're wanting to see something on the web server - where the files actually reside.
The most intuitive way is to make the browser look like that tool you're used to using. In this case, making it look like Windows Explorer (sorry Apple people!). You click on directory icons to expand or contract your directories and it shows what's in those directories. There's nothing built-in to a browser to make it easy to do this. Sure there are libraries you can purchase that have amazing tools to do this, but if you're cheap like me you don't want to spend the money. Heck, you're a developer - make your own!
So that's what I did. I made a somewhat self-contained tool that takes a starting location and shows all the directories and files inside there. The stateless nature plays out in that I have to look at the target directory and recursively* get the files everytime. But I keep track of what directories the user has expanded and pass that back and forth between the user's computer and the web server. For security I don't show the full path, instead just the relative path. You can open and close the directories by clicking on the directories and each file has a link that you can do something with, depending on what the developer needs to do. It works pretty well and scales to large directories by only looking ahead as far as what the user has expanded. In other words it doesn't look at every possible file from the initial directory downwards. Instead it only looks at what the user is currently looking at. As the user "goes down" the directory structure, the tool looks just that far ahead.
Now, clearly you have to be careful where you use this too because it exposes whatever files are in the target directory. But if your website is allowing access to areas already set up for that purpose, this tool is great for allowing the user to find the files they're looking for.
It was a fun little tool to build and I needed it for functionality on the web application I developed at my job, so it wasn't just some academic little thing. If you're interested and want the source code for it, send me an email and I've send it to you. It's written in C# for MVC web applications in Visual Studio. But the principle could easily be applied to other languages. It's not copyrightable.
* - recursion is the process of a function calling itself. In this case the function GetDirectory looks at a directory and finds everything in it. If one of the things is a directory, it calls GetDirectory on it. See how that works? If you've ever seen a painting by M.C. Escher, you've probably looked at a visual example of recursion.