mnutt / davros

Personal file storage server
Apache License 2.0
298 stars 35 forks source link

Question: How to dynamically list files in index.html? #20

Closed sharpedavid closed 8 years ago

sharpedavid commented 8 years ago

Although I'm a server-side Java developer, I do appreciate some of the technical complexities of my question. I would presumably write some Javascript to hit an endpoint exposed by Davros to get the file list, if such a consumable endpoint even exists.

I'm using Davros on Sandstorm.

Thanks for your time. Please feel free to delete this question if it's not productive.

mnutt commented 8 years ago

Interesting use case! Davros has a "web publishing" feature where it exposes the static files to outside users without any sort of auth. The simplest way to have index list its files would be to pre-generate index.html, but given that you want to dynamically list them a little bit more would be required.

Davros exposes a WebDAV API through sandstorm, usually for consumption by desktop apps. But there is not reason you couldn't access it from javascript, it's just a little more work. You'll want to hit the URL shown on the Clients tab in Davros, but Sandstorm won't let you use HTTP basic auth. Instead, you need to use ajax to hit the URL and use the bearer token. Some possibly non-syntactically-correct code:

var sandstormURI = 'https://api.foo.sandcats.io/remote.php/webdav'
var xhr = new XMLHttpRequest();
xhr.open('PROPFIND', sandstormURI, true);
xhr.setRequestHeader('Authorization', 'bearer ' + token);
xhr.send();

You'll receive back XML that you can parse to list the files in that directory. Make sure you get a webkey that has only read access or you'll be giving other people access to modify. Hope this helps!

mnutt commented 8 years ago

I should also mention that if Davros' built-in directory listing is good enough, you can just share access via the share link and give them view access. But if you have some other goal in mind, the above should work.

sharpedavid commented 8 years ago

Thank you for these helpful instructions, mnutt. To be honest, I thought my question was a bit silly, but I have learned a lot by asking it.

Following your instructions worked perfectly: I have obtained the filelist. (The service replies were helpful. Without an authorization header we get "Missing or invalid authorization header", and without specifying content type we get "expected XML request body".)

One thing I am not certain about is how to "get a webkey that has only read access". The Clients tab in Davros doesn't allow me to generate different kinds of webkeys. Please don't feel the need to address this, as I am asking merely for my knowledge.

sharpedavid commented 8 years ago

Oh, I just spotted the webkeys button at the top of the page. :sweat_smile:

So nevermind, I understand everything now. Thank you very much!