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).

!7

static S password;
static S dir = "/storage/sdcard0";

p {
  password = loadSecretTextFile("password"); // looks in JavaX-Secret/#1005938/password
  serveHttp(7777);
  run("#1000842"); // upload server on 8888
}

static NanoHTTPD.Response serve(S uri, NanoHTTPD.Method method,
  Map<S,S> header, Map<S,S> params, Map<S,S> files) {
  print("Serving HTML.");
  
  File d = new File(dir);
  File sub = new File(dir, dropPrefix("/", uri));
  print("File: " + sub.getAbsolutePath() + ", exists: " + sub.exists());
  
  S html;
  
  if (!sub.exists())
    html = "Not found: " + quote(f2s(sub)) + " (uri: " + quote(uri) + ")";
  else if (sub.isFile())
    ret serveFile(sub);
  else {
    S setPW = params.get("setpw");
    if (empty(password) && nempty(setPW)) {
      saveSecretTextFile("password", password = setPW);
      ret serveHTML("Password set.");
    } else if (neq(password, params.get("pw")))
      ret serveHTML("No...");
    html = fileLinks(findAllFiles(sub), d);
  }

  ret serveHTML(htitle("Phone Server.")
    + p(href("http://" + getMyIP() + ":8888", "Upload stuff"))
    + h3(sub.getAbsolutePath()) + html);
}

static S fileLinks(L<File> files, File rootDir) {
  new L<S> l;
  for (File f : files) {
    S path = getRelPath(f, rootDir);
    l.add(href(urlencode(dropPrefix("/", path)), path));
  }
  ret ul(l);
}

static S getRelPath(File f, File root) ctex {
  ret dropPrefix(root.getCanonicalPath(), f.getCanonicalPath());
}

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: 513 / 1077
Version history: 1 change(s)
Referenced in: [show references]