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

1  
!7
2  
3  
static int id = 1200000;
4  
5  
concept FileMeta {
6  
  int fileID;
7  
  S name;
8  
  S md5; // md5 of RGBImage
9  
  
10  
  *() {}
11  
  *(int *fileID, S *name) { _doneLoading(); change(); }
12  
  
13  
  void _doneLoading() {
14  
    //print("_doneLoading " + fileID);
15  
    metaMap.put(fileID, this);
16  
  }
17  
}
18  
19  
static Map<Int, FileMeta> metaMap = synchroMap();
20  
21  
p {
22  
  load("id");
23  
  concepts();
24  
}
25  
26  
svoid setMD5(FileMeta meta) {
27  
  if (nempty(meta.md5)) ret;
28  
  S md5 = "error";
29  
  pcall {
30  
    md5 = md5(fileFile(meta.fileID));
31  
  }
32  
  cset(meta, +md5);
33  
}
34  
35  
static File filesDir() {
36  
  ret prepareProgramDir("files");
37  
}
38  
39  
synchronized html {
40  
  print("uri: " + uri);
41  
  uri = dropPrefix("/", uri);
42  
  
43  
  if (isInteger(uri)) {
44  
    if (neq(masterPW(), params.get("_pass")) && !webAuthed()) ret "";
45  
    int id = parseInt(uri);
46  
    FileMeta c = findConcept(FileMeta, fileID := id);
47  
    if (nempty(params.get("remd5"))) {
48  
      cset(c, md5 := null);
49  
      setMD5(c);
50  
    }
51  
    File file = fileFile(id);
52  
    ret addHeader("Content-Disposition", "attachment; filename=" + quote(c.name),
53  
      call(getMainBot(), "serveFile", file, "application/binary"));
54  
  }
55  
  
56  
  if (!webAuthed()) ret ahref(relativeBotLink(#1002590), "please log in");
57  
  
58  
  if (uri.startsWith("md5/")) {
59  
    uri = dropPrefix("md5/", uri);
60  
    FileMeta meta = findConcept(FileMeta, md5 := uri);
61  
    if (meta == null)
62  
      ret "Not found";
63  
    ret call(getMainBot(), "serveFile_maxCache", fileFile(meta.fileID), "application/binary");
64  
  }
65  
  
66  
  // finds a file by md5
67  
  if (uri.startsWith("checkmd5/")) {
68  
    uri = dropPrefix("checkmd5/", uri);
69  
    FileMeta meta = findConcept(FileMeta, md5 := uri);
70  
    ret meta == null ? "Not found" : str(meta.fileID);
71  
  }
72  
  
73  
  if (eq(uri, "upload")) {
74  
    print("upload called");
75  
    S name = params.get("name");
76  
    
77  
    SS files = getUploadFiles();
78  
    print("got files: " + l(files));
79  
    int id;
80  
    S data = params.get("data");
81  
    if (data != null) {
82  
      print("got data: " + l(data));
83  
      id = newfileID();
84  
      saveBinaryFile(fileFile(id), hexToBytes(data));
85  
    } else {
86  
      String tmpfile = files.get("thefile");
87  
      String originalName = params.get("thefile");
88  
      name = or2(name, originalName);
89  
      assertNotNull(tmpfile);
90  
      File tmp = new File(tmpfile);
91  
      long l = tmp.length();
92  
      if (l == 0) ret "Empty file, exiting";
93  
      id = newfileID();
94  
      copyFile(tmp, fileFile(id));
95  
    }
96  
    setMD5(new FileMeta(id, unnull(name)));
97  
    ret hrefresh(2, rawLink()) + p("OK:<br>" + ahref(rawLink(str(id)), or2(name, "?")))
98  
      + ahref(rawLink(), "[back]");
99  
  }
100  
  
101  
  if (eq(uri, "uploadform")) ret htitle("Upload")
102  
    + hmobilefix()
103  
    + uploadform();
104  
  
105  
  if (eq(uri, "")) {
106  
    // main list
107  
    
108  
    S msg = "";
109  
    S woodyID = params.get("woodyid");
110  
    if (nempty(woodyID)) {
111  
      woodySetBotID(woodyID);
112  
      S reload = askSelf("reload sub bot #1008855");
113  
      msg = "Aktiver Woody jetzt: " + woodyGetBotID()
114  
        + " Reload-Ergebnis: " + reload;
115  
    }
116  
  
117  
    new L<int> ids;
118  
    for (File f : listFiles(filesDir())) try {
119  
      S s = /*dropSuffix(".png",*/ f.getName() /*)*/;
120  
      if (!isInteger(s)) continue;
121  
      int id = parseInt(s);
122  
      ids.add(id);
123  
    } catch e {
124  
      silentException(e);
125  
    }
126  
    sortListDesc(ids);
127  
    ret (nempty(msg) ? p(msg) : "")
128  
      + p(ahref("http://botcompany.de", "Zu BotCompany.de")
129  
      + " | " + ahref("http://mech.tinybrain.de", "Basis-Bot"))
130  
      + h3("Upload") + uploadform()
131  
      + "<hr>" + h3("Aktiver Woody")
132  
      + hpostform(
133  
          hselect(pairsToOrderedMap(
134  
          map(func(Int id) -> Pair<Int, S> { 
135  
            FileMeta meta = metaMap.get(id);
136  
            S title = or2(getString(meta, "name"), "Untitled");
137  
            ret pair(id, "#" + id + " - " + title);
138  
          }, ids)), parseSnippetIDOpt(woodyGetBotID()), name := "woodyid")
139  
        + " " + hsubmit("OK"))
140  
      + "<hr>"
141  
      + p(n(ids, "File") + (empty(ids) ? "." : ":")) + ul(map(ids, func(Int id) -> S {
142  
      FileMeta meta = metaMap.get(id);
143  
      S title = or2(getString(meta, "name"), "Untitled");
144  
      S linkText = "#" + id + " - " + htmlencode(title);
145  
      S s = ahref(rawLink(str(id)), linkText);
146  
      s += " " + n(fileSize(fileFile(id)), "byte");
147  
      if (meta != null)
148  
        s += " [md5: " + ahref(rawLink("md5/" + meta.md5), meta.md5) + "]";
149  
      ret s;
150  
    })) 
151  
      //+ p(ahref(pageLink("/uploadform"), "Upload a file"))
152  
      ;
153  
  }
154  
  
155  
  null;
156  
}
157  
158  
static synchronized int newfileID() {
159  
  while (fileFile(id).exists())
160  
    ++id;
161  
  int i = id++;
162  
  save("id");
163  
  ret i;
164  
}
165  
166  
static File fileFile(int id) {
167  
  ret new File(filesDir(), str(id));
168  
}
169  
170  
sS uploadform() {
171  
  ret huploadform(
172  
        p("File to upload: " + hfileupload())
173  
      + p("Optional name: " + hinputfield("name"))
174  
      + p(hsubmit("OK")),
175  
      "action", rawLink("upload"));
176  
}

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: 594 / 4550
Version history: 32 change(s)
Referenced in: [show references]