0

Using nanohttpd I can select a chosen file and start a server to serve that one file.

Is it possible to serve a list of lot of files?
That is, I have lot of files in sd card and I want to serve the selected ones. So how to give an array of file paths of those files and generate and return URL for them, so that I can access them from network.

Not an HTML page which lists all those files and folders.
I have gone through this, this is not what I am referring to. In this it it just lists the root folder and lists them all in a HTML page, for a user to view/select. Not what I am after.

Just an array of server URLs for a selected, chosen list of files in sdcard, which I can then use programmatically.

As of now I have this

protected void onCreate(Bundle savedInstanceState) {
        ...
        server = new Mp3Server();
        try {
            server.start();
        } catch(IOException ioe) {
            Log.w("Httpd", "The server could not start.");
        }
        Log.w("Httpd", "Web server initialized.");
}
...
...
public class Mp3Server extends NanoHTTPD {

    public Mp3Server() {
        super(8089);
    }

    @Override
    public Response serve(String uri, Method method,
                          Map<String, String> header, Map<String, String> parameters,
                          Map<String, String> files) {
        String answer = "";

        FileInputStream fis = null;
        try {
            fis = new FileInputStream("/storage/C67A-18F7/Music/music.mp3");
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return newChunkedResponse(Status.OK, "audio/mpeg", fis);
    }
}

Or do I have to pass the chosen file and start/stop server each time for each file? But this sounds inefficient.

arjun
  • 1,594
  • 16
  • 33
  • 1
    You can serve as many files as you want. But it is the browser which should indicate which file it wants. So the browser should provide a filename already. In the url or as query parameter. Then in the serve() you inspect parameters `uri` and `parameters`. – greenapps Feb 24 '18 at 11:33
  • Further if you can generate a html page with urls and links then you can of course also only generate the urls. I do not see that as a problem. – greenapps Feb 24 '18 at 11:39
  • Basically what you are saying is, the `String uri` can be a known file name and in the `FileInputStream` I just need to pass that? – arjun Feb 24 '18 at 14:09
  • 1
    Yes. Nearly. If it is a file name you have to add a folder path of course. But just experiment a bit. Put Log.d() statements in `serve()` to log uri and parameters while in the browser you try urls like `http:///song3.mp3` or `http:///myfolder/song3.mp3` or `http:///folder?song=song3.mp3`. – greenapps Feb 24 '18 at 14:14
  • (Do not use Toast() in serve() as the server will silently not response). – greenapps Feb 24 '18 at 14:15
  • You can make it like that that the server serves the urls list if you call it like `http://`. or whatever. Use your fantasy. – greenapps Feb 24 '18 at 14:20
  • Sounds plausible. I will try this now. – arjun Feb 24 '18 at 14:30
  • This works. This also assumes that I initalize the folder/s before the server is started, right? Because allowing folder path in the URI opens up system access. Not good. Any way to allow multiple folder path to be initialized dureing server start? And if I just run with this technique, what possible abuse am I opening up, like thread block, security, etc. Basically what else am I suppose to study/implement to to have a robust method.? – arjun Feb 24 '18 at 15:05
  • I was thinking to create a hash lookup of allowed files. When uri is given it is checked against the hash, if it matches only then it is served else rejected. Is this good? or inefficient? – arjun Feb 24 '18 at 15:08
  • Why isntit sufficient that a requested file does not exist? – greenapps Feb 24 '18 at 18:25
  • Yes I meant the FileNotFoundException. So doing a hash lookup would be a decent security setup? – arjun Feb 24 '18 at 19:37
  • No. No file not found exeption but File:exists(). – greenapps Feb 24 '18 at 19:58
  • What I meant is only checking those files which are in an array? Not just any file name being passed to the uri and the FileInputStream trying to fetch it. Only those which are allowed. – arjun Feb 24 '18 at 20:01
  • Files are not in arrays but in folders. If you take one folder and put all the files in it you wanna serve you are done. – greenapps Feb 24 '18 at 20:02
  • Okay. Can you please tell me or link a resource, things to configure with a server so that it is not misused? I want to know and please write as an answer. I want to accept it. – arjun Feb 24 '18 at 20:03
  • If you only wanna serve some files from a folder there is nothing to configure. And i do not know which use you consider abuse. – greenapps Feb 24 '18 at 20:04
  • Ok. So thank you for you continued help. You are great! – arjun Feb 24 '18 at 20:05
  • When I said misused, I meant the system files are not accessed in any manner. – arjun Feb 24 '18 at 20:06

0 Answers0