!7 static int id = 1300000; concept FileMeta { int fileID; S name; S md5; bool authed; *() {} *(int *fileID, S *name) { _doneLoading(); change(); } void _doneLoading() { //print("_doneLoading " + fileID); metaMap.put(fileID, this); } } static Map metaMap = synchroMap(); p { load("id"); dbIndexing(FileMeta, 'fileID, FileMeta, 'name, FileMeta, 'md5); } 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); S mimeType = extensionToMimeType(c.name); if (nempty(mimeType)) ret call(getMainBot(), 'serveFile_maxCache, file, mimeType); mimeType = "application/binary"; ret addHeader("Content-Disposition", "attachment; filename=" + quote(c.name), call(getMainBot(), 'serveFile_maxCache, file, mimeType)); } //if (!webAuthed()) ret ahref(botLink(#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)); } FileMeta m = new FileMeta(id, unnull(name)); cset(m, authed := webAuthed(params)); setMD5(m); ret hrefresh(2, rawLink()) + p("OK:
" + ahref(rawLink(str(id)), or2(name, "?"))) + ahref(rawLink(), "[back]"); } if (eq(uri, "uploadform")) ret htitle("Upload") + hmobilefix() + uploadform(); new Matches mm; // Note: returns only authed files if (startsWith(uri, "latest-file-named/", mm)) { S name = urldecode(mm.get(0)); FileMeta m = first(findAuthedFiles(name)); ret struct(m == null ? null : ll(m.fileID, m.md5)); } if (eq(uri, "ids-and-md5s")) ret struct(flattenList(map(list(FileMeta), func(FileMeta m) { ll(m.fileID, m.md5) }))); if (eq(uri, "typical-set")) { L l = findAuthedFiles( "Circles From tvejysmllsmz.gz", "Webs from program #1011161 on tvejysmllsmz.gz"); ret struct(flattenList(map(l, func(FileMeta m) { ll(m.fileID, m.md5) }))); } if (eq(uri, "")) { // main list S msg = ""; L ids = collect(list(FileMeta), 'fileID); sortListDesc(ids); ret (nempty(msg) ? p(msg) : "") + h3("Upload") + uploadform() + "
" + p(n(ids, "File") + (empty(ids) ? "." : ":")) + ul(map(ids, func(int id) { 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 += " [" + formatDateGMT(meta.created) + ", md5: " + ahref(rawLink("md5/" + meta.md5), meta.md5) + "]"; if (meta.authed) s += " " + dottedSpan("*", "Authorized Upload"); } 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")); } static L findAuthedFiles(S... names) { new L l; for (S name : names) addIfNotNull(l, highestByField('id, conceptsWhere(FileMeta, +name, authed := true))); ret l; }