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

187
LINES

< > BotCompany Repo | #1010474 // Diagram Server

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

Libraryless. Click here for Pure Java version (9118L/61K/203K).

1  
!7
2  
3  
static int id = 1300000;
4  
5  
concept FileMeta {
6  
  int fileID;
7  
  S name;
8  
  S md5;
9  
  bool authed;
10  
  
11  
  *() {}
12  
  *(int *fileID, S *name) { _doneLoading(); change(); }
13  
  
14  
  void _doneLoading() {
15  
    //print("_doneLoading " + fileID);
16  
    metaMap.put(fileID, this);
17  
  }
18  
}
19  
20  
static Map<Int, FileMeta> metaMap = synchroMap();
21  
22  
p {
23  
  load("id");
24  
  dbIndexing(FileMeta, 'fileID, FileMeta, 'name, FileMeta, 'md5);
25  
}
26  
27  
svoid setMD5(FileMeta meta) {
28  
  if (nempty(meta.md5)) ret;
29  
  S md5 = "error";
30  
  pcall {
31  
    md5 = md5(fileFile(meta.fileID));
32  
  }
33  
  cset(meta, +md5);
34  
}
35  
36  
static File filesDir() {
37  
  ret prepareProgramDir("files");
38  
}
39  
40  
synchronized html {
41  
  print("uri: " + uri);
42  
  uri = dropPrefix("/", uri);
43  
  
44  
  if (isInteger(uri)) {
45  
    //if (neq(masterPW(), params.get("_pass")) && !webAuthed()) ret "";
46  
    int id = parseInt(uri);
47  
    FileMeta c = findConcept(FileMeta, fileID := id);
48  
    if (nempty(params.get("remd5"))) {
49  
      cset(c, md5 := null);
50  
      setMD5(c);
51  
    }
52  
    File file = fileFile(id);
53  
    
54  
    S mimeType = extensionToMimeType(c.name);
55  
    if (nempty(mimeType))
56  
      ret call(getMainBot(), 'serveFile_maxCache, file, mimeType);
57  
      
58  
    mimeType = "application/binary";
59  
    ret addHeader("Content-Disposition", "attachment; filename=" + quote(c.name),
60  
      call(getMainBot(), 'serveFile_maxCache, file, mimeType));
61  
  }
62  
  
63  
  //if (!webAuthed()) ret ahref(botLink(#1002590), "please log in");
64  
  
65  
  if (uri.startsWith("md5/")) {
66  
    uri = dropPrefix("md5/", uri);
67  
    FileMeta meta = findConcept(FileMeta, md5 := uri);
68  
    if (meta == null)
69  
      ret "Not found";
70  
    ret call(getMainBot(), "serveFile_maxCache", fileFile(meta.fileID), "application/binary");
71  
  }
72  
  
73  
  // finds a file by md5
74  
  if (uri.startsWith("checkmd5/")) {
75  
    uri = dropPrefix("checkmd5/", uri);
76  
    FileMeta meta = findConcept(FileMeta, md5 := uri);
77  
    ret meta == null ? "Not found" : str(meta.fileID);
78  
  }
79  
  
80  
  if (eq(uri, "upload")) {
81  
    print("upload called");
82  
    S name = params.get("name");
83  
    
84  
    SS files = getUploadFiles();
85  
    print("got files: " + l(files));
86  
    int id;
87  
    S data = params.get("data");
88  
    if (data != null) {
89  
      print("got data: " + l(data));
90  
      id = newFileID();
91  
      saveBinaryFile(fileFile(id), hexToBytes(data));
92  
    } else {
93  
      String tmpfile = files.get("thefile");
94  
      String originalName = params.get("thefile");
95  
      name = or2(name, originalName);
96  
      assertNotNull(tmpfile);
97  
      File tmp = new File(tmpfile);
98  
      long l = tmp.length();
99  
      if (l == 0) ret "Empty file, exiting";
100  
      id = newFileID();
101  
      copyFile(tmp, fileFile(id));
102  
    }
103  
    FileMeta m = new FileMeta(id, unnull(name));
104  
    cset(m, authed := webAuthed(params));
105  
    setMD5(m);
106  
    ret hrefresh(2, rawLink()) + p("OK:<br>" + ahref(rawLink(str(id)), or2(name, "?")))
107  
      + ahref(rawLink(), "[back]");
108  
  }
109  
  
110  
  if (eq(uri, "uploadform")) ret htitle("Upload")
111  
    + hmobilefix()
112  
    + uploadform();
113  
    
114  
  new Matches mm;
115  
  // Note: returns only authed files
116  
  if (startsWith(uri, "latest-file-named/", mm)) {
117  
    S name = urldecode(mm.get(0));
118  
    FileMeta m = first(findAuthedFiles(name));
119  
    ret struct(m == null ? null : ll(m.fileID, m.md5));
120  
  }
121  
122  
  if (eq(uri, "ids-and-md5s"))
123  
    ret struct(flattenList(map(list(FileMeta), func(FileMeta m) { ll(m.fileID, m.md5) })));
124  
125  
  if (eq(uri, "typical-set")) {
126  
    L<FileMeta> l = findAuthedFiles(
127  
      "Circles From tvejysmllsmz.gz",
128  
      "Webs from program #1011161 on tvejysmllsmz.gz");
129  
    ret struct(flattenList(map(l, func(FileMeta m) { ll(m.fileID, m.md5) })));
130  
  }
131  
132  
  if (eq(uri, "")) {
133  
    // main list
134  
    
135  
    S msg = "";
136  
137  
    L<int> ids = collect(list(FileMeta), 'fileID);
138  
    sortListDesc(ids);
139  
    ret (nempty(msg) ? p(msg) : "")
140  
      + h3("Upload") + uploadform()
141  
      + "<hr>"
142  
      + p(n(ids, "File") + (empty(ids) ? "." : ":")) + ul(map(ids, func(int id) {
143  
      FileMeta meta = metaMap.get(id);
144  
      S title = or2(getString(meta, "name"), "Untitled");
145  
      S linkText = "#" + id + " - " + htmlencode(title);
146  
      S s = ahref(rawLink(str(id)), linkText);
147  
      s += " " + n(fileSize(fileFile(id)), "byte");
148  
      if (meta != null) {
149  
        s += " [" + formatDateGMT(meta.created) + ", md5: " + ahref(rawLink("md5/" + meta.md5), meta.md5) + "]";
150  
        if (meta.authed) s += " " + dottedSpan("*", "Authorized Upload");
151  
      }
152  
      ret s;
153  
    })) 
154  
      //+ p(ahref(pageLink("/uploadform"), "Upload a file"))
155  
      ;
156  
  }
157  
  
158  
  null;
159  
}
160  
161  
static synchronized int newFileID() {
162  
  while (fileFile(id).exists())
163  
    ++id;
164  
  int i = id++;
165  
  save("id");
166  
  ret i;
167  
}
168  
169  
static File fileFile(int id) {
170  
  ret new File(filesDir(), str(id));
171  
}
172  
173  
sS uploadform() {
174  
  ret huploadform(
175  
        p("File to upload: " + hfileupload())
176  
      + p("Optional name: " + hinputfield("name"))
177  
      + p(hsubmit("OK")),
178  
      "action", rawLink("upload"));
179  
}
180  
181  
static L<FileMeta> findAuthedFiles(S... names) {
182  
  new L<FileMeta> l;
183  
  for (S name : names)
184  
    addIfNotNull(l, highestByField('id, conceptsWhere(FileMeta, +name, authed := true)));
185  
  ret l;
186  
187  
}

Author comment

Began life as a copy of #1008823

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1010474
Snippet name: Diagram Server
Eternal ID of this version: #1010474/28
Text MD5: aff0be2fb37b7ace75ae6f1cdc22dd70
Transpilation MD5: 814165e20e943c2fc36c361b8c2c6f95
Author: stefan
Category: javax / networking
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2017-10-29 17:31:34
Source code size: 5206 bytes / 187 lines
Pitched / IR pitched: No / No
Views / Downloads: 451 / 4388
Version history: 27 change(s)
Referenced in: [show references]