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

176
LINES

< > BotCompany Repo | #1008823 // Woody File Server [web bot]

JavaX source code [tags: ai1-lol butter use-pretranspiled] - run with: x30.jar - homepage - homepage

Libraryless. Click here for Pure Java version (10756L/72K/239K).

!7

static int id = 1200000;

concept FileMeta {
  int fileID;
  S name;
  S md5; // md5 of RGBImage
  
  *() {}
  *(int *fileID, S *name) { _doneLoading(); change(); }
  
  void _doneLoading() {
    //print("_doneLoading " + fileID);
    metaMap.put(fileID, this);
  }
}

static Map<Int, FileMeta> metaMap = synchroMap();

p {
  load("id");
  concepts();
}

svoid setMD5(FileMeta meta) {
  if (nempty(meta.md5)) ret;
  S md5 = "error";
  pcall {
    md5 = md5(fileFile(meta.fileID));
  }
  cset(meta, +md5);
}

static File filesDir() {
  ret prepareProgramDir("files");
}

synchronized html {
  print("uri: " + uri);
  uri = dropPrefix("/", uri);
  
  if (isInteger(uri)) {
    if (neq(masterPW(), params.get("_pass")) && !webAuthed()) ret "";
    int id = parseInt(uri);
    FileMeta c = findConcept(FileMeta, fileID := id);
    if (nempty(params.get("remd5"))) {
      cset(c, md5 := null);
      setMD5(c);
    }
    File file = fileFile(id);
    ret addHeader("Content-Disposition", "attachment; filename=" + quote(c.name),
      call(getMainBot(), "serveFile", file, "application/binary"));
  }
  
  if (!webAuthed()) ret ahref(relativeBotLink(#1002590), "please log in");
  
  if (uri.startsWith("md5/")) {
    uri = dropPrefix("md5/", uri);
    FileMeta meta = findConcept(FileMeta, md5 := uri);
    if (meta == null)
      ret "Not found";
    ret call(getMainBot(), "serveFile_maxCache", fileFile(meta.fileID), "application/binary");
  }
  
  // finds a file by md5
  if (uri.startsWith("checkmd5/")) {
    uri = dropPrefix("checkmd5/", uri);
    FileMeta meta = findConcept(FileMeta, md5 := uri);
    ret meta == null ? "Not found" : str(meta.fileID);
  }
  
  if (eq(uri, "upload")) {
    print("upload called");
    S name = params.get("name");
    
    SS files = getUploadFiles();
    print("got files: " + l(files));
    int id;
    S data = params.get("data");
    if (data != null) {
      print("got data: " + l(data));
      id = newfileID();
      saveBinaryFile(fileFile(id), hexToBytes(data));
    } else {
      String tmpfile = files.get("thefile");
      String originalName = params.get("thefile");
      name = or2(name, originalName);
      assertNotNull(tmpfile);
      File tmp = new File(tmpfile);
      long l = tmp.length();
      if (l == 0) ret "Empty file, exiting";
      id = newfileID();
      copyFile(tmp, fileFile(id));
    }
    setMD5(new FileMeta(id, unnull(name)));
    ret hrefresh(2, rawLink()) + p("OK:<br>" + ahref(rawLink(str(id)), or2(name, "?")))
      + ahref(rawLink(), "[back]");
  }
  
  if (eq(uri, "uploadform")) ret htitle("Upload")
    + hmobilefix()
    + uploadform();
  
  if (eq(uri, "")) {
    // main list
    
    S msg = "";
    S woodyID = params.get("woodyid");
    if (nempty(woodyID)) {
      woodySetBotID(woodyID);
      S reload = askSelf("reload sub bot #1008855");
      msg = "Aktiver Woody jetzt: " + woodyGetBotID()
        + " Reload-Ergebnis: " + reload;
    }
  
    new L<int> ids;
    for (File f : listFiles(filesDir())) try {
      S s = /*dropSuffix(".png",*/ f.getName() /*)*/;
      if (!isInteger(s)) continue;
      int id = parseInt(s);
      ids.add(id);
    } catch e {
      silentException(e);
    }
    sortListDesc(ids);
    ret (nempty(msg) ? p(msg) : "")
      + p(ahref("http://botcompany.de", "Zu BotCompany.de")
      + " | " + ahref("http://mech.tinybrain.de", "Basis-Bot"))
      + h3("Upload") + uploadform()
      + "<hr>" + h3("Aktiver Woody")
      + hpostform(
          hselect(pairsToOrderedMap(
          map(func(Int id) -> Pair<Int, S> { 
            FileMeta meta = metaMap.get(id);
            S title = or2(getString(meta, "name"), "Untitled");
            ret pair(id, "#" + id + " - " + title);
          }, ids)), parseSnippetIDOpt(woodyGetBotID()), name := "woodyid")
        + " " + hsubmit("OK"))
      + "<hr>"
      + p(n(ids, "File") + (empty(ids) ? "." : ":")) + ul(map(ids, func(Int id) -> S {
      FileMeta meta = metaMap.get(id);
      S title = or2(getString(meta, "name"), "Untitled");
      S linkText = "#" + id + " - " + htmlencode(title);
      S s = ahref(rawLink(str(id)), linkText);
      s += " " + n(fileSize(fileFile(id)), "byte");
      if (meta != null)
        s += " [md5: " + ahref(rawLink("md5/" + meta.md5), meta.md5) + "]";
      ret s;
    })) 
      //+ p(ahref(pageLink("/uploadform"), "Upload a file"))
      ;
  }
  
  null;
}

static synchronized int newfileID() {
  while (fileFile(id).exists())
    ++id;
  int i = id++;
  save("id");
  ret i;
}

static File fileFile(int id) {
  ret new File(filesDir(), str(id));
}

sS uploadform() {
  ret huploadform(
        p("File to upload: " + hfileupload())
      + p("Optional name: " + hinputfield("name"))
      + p(hsubmit("OK")),
      "action", rawLink("upload"));
}

Author comment

Began life as a copy of #1004590

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1008823
Snippet name: Woody File Server [web bot]
Eternal ID of this version: #1008823/33
Text MD5: 563422e6ffe23452c6df4142f3bb2f1d
Transpilation MD5: 6f557f95013852f40fa4407eb565bd80
Author: stefan
Category: javax / networking
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2018-09-14 17:22:12
Source code size: 4953 bytes / 176 lines
Pitched / IR pitched: No / No
Views / Downloads: 590 / 4541
Version history: 32 change(s)
Referenced in: [show references]