Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

52
LINES

< > BotCompany Repo | #1005938 // "Phone Server" - Serve local directory (HTTP, Android, weakly password-protected)

JavaX source code [tags: use-pretranspiled] - run with: x30.jar

Libraryless. Click here for Pure Java version (5729L/41K/116K).

1  
!7
2  
3  
static S password;
4  
static S dir = "/storage/sdcard0";
5  
6  
p {
7  
  password = loadSecretTextFile("password"); // looks in JavaX-Secret/#1005938/password
8  
  serveHttp(7777);
9  
  run("#1000842"); // upload server on 8888
10  
}
11  
12  
static NanoHTTPD.Response serve(S uri, NanoHTTPD.Method method,
13  
  Map<S,S> header, Map<S,S> params, Map<S,S> files) {
14  
  print("Serving HTML.");
15  
  
16  
  File d = new File(dir);
17  
  File sub = new File(dir, dropPrefix("/", uri));
18  
  print("File: " + sub.getAbsolutePath() + ", exists: " + sub.exists());
19  
  
20  
  S html;
21  
  
22  
  if (!sub.exists())
23  
    html = "Not found: " + quote(f2s(sub)) + " (uri: " + quote(uri) + ")";
24  
  else if (sub.isFile())
25  
    ret serveFile(sub);
26  
  else {
27  
    S setPW = params.get("setpw");
28  
    if (empty(password) && nempty(setPW)) {
29  
      saveSecretTextFile("password", password = setPW);
30  
      ret serveHTML("Password set.");
31  
    } else if (neq(password, params.get("pw")))
32  
      ret serveHTML("No...");
33  
    html = fileLinks(findAllFiles(sub), d);
34  
  }
35  
36  
  ret serveHTML(htitle("Phone Server.")
37  
    + p(href("http://" + getMyIP() + ":8888", "Upload stuff"))
38  
    + h3(sub.getAbsolutePath()) + html);
39  
}
40  
41  
static S fileLinks(L<File> files, File rootDir) {
42  
  new L<S> l;
43  
  for (File f : files) {
44  
    S path = getRelPath(f, rootDir);
45  
    l.add(href(urlencode(dropPrefix("/", path)), path));
46  
  }
47  
  ret ul(l);
48  
}
49  
50  
static S getRelPath(File f, File root) ctex {
51  
  ret dropPrefix(root.getCanonicalPath(), f.getCanonicalPath());
52  
}

Author comment

Began life as a copy of #1002646

download  show line numbers  debug dex  old transpilations   

Travelled to 14 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, nbgitpuheiab, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1005938
Snippet name: "Phone Server" - Serve local directory (HTTP, Android, weakly password-protected)
Eternal ID of this version: #1005938/2
Text MD5: 2ed642691a35d368204ac63c3c03a664
Transpilation MD5: 9403ec09ec4612692ce8d72c831ca085
Author: stefan
Category: javax
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2017-03-02 16:16:02
Source code size: 1496 bytes / 52 lines
Pitched / IR pitched: No / No
Views / Downloads: 590 / 1176
Version history: 1 change(s)
Referenced in: [show references]