!752 static int id = 1100000; concepts. concept ImageMeta { int imageID; S name; S md5; // md5 of RGBImage *() {} *(int *imageID, S *name) { _doneLoading(); change(); } void _doneLoading() { //print("_doneLoading " + imageID); metaMap.put(imageID, this); } } static Map metaMap = synchroMap(); p { load("id"); concepts(); for (ImageMeta meta) setMD5(meta); } svoid setMD5(ImageMeta meta) { if (nempty(meta.md5)) ret; S md5 = "error"; pcall { md5 = md5OfRGBImage(new RGBImage(loadPNG(imageFile(meta.imageID)))); } cset(meta, +md5); } static File imagesDir() { ret prepareProgramDir("images"); } synchronized html { print("uri: " + uri); uri = dropPrefix("/", uri); if (eq(uri, "setp0rn")) { setPornFlag(parseInt(params.get("id")), true); ret "OK"; } if (eq(uri, "unp0rn")) { setPornFlag(parseInt(params.get("id")), false); ret "OK"; } bool p0rnOK = false; if (uri.endsWith("p") && isInteger(dropLast(uri))) { p0rnOK = true; uri = dropLast(uri); } if (isInteger(uri)) { int id = parseInt(uri); File file = imageFile(id); if (!p0rnOK && isPornFlag(id)) ret "n0 p0rn"; ret call(getMainBot(), "serveFile", file, "image/png"); } if (uri.startsWith("md5/")) { uri = dropPrefix("md5/", uri); bool jpg = uri.startsWith("jpg/"); if (jpg) uri = dropPrefix("jpg/", uri); ImageMeta meta = findConcept(ImageMeta, md5 := uri); if (meta == null) ret "Not found"; if (jpg) ret call(getMainBot(), "serveByteArray_maxCache", toJPEG(imageFile(meta.imageID)), "image/jpeg"); ret call(getMainBot(), "serveFile_maxCache", imageFile(meta.imageID), "image/png"); } if (uri.startsWith("checkmd5/")) { uri = dropPrefix("checkmd5/", uri); ImageMeta meta = findConcept(ImageMeta, md5 := uri); ret meta == null ? "Not found" : str(meta.imageID); } 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 = newImageID(); saveBinaryFile(imageFile(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 = newImageID(); copyFile(tmp, imageFile(id)); } setMD5(new ImageMeta(id, unnull(name))); ret "OK:
" + himg(rawLink(str(id))); } if (eq(uri, "uploadform")) { ret htitle("Upload") + hmobilefix() + huploadform( "Image file: " + hfileupload("accept", "image/png,image/jpeg") + " " + "
" + "Optional name: " + hinputfield("name") + hsubmit(), "action", rawLink("upload")); } if (eq(uri, "")) { // list images new L ids; for (File f : listFiles(imagesDir())) try { S s = dropSuffix(".png", f.getName()); if (!isInteger(s)) continue; int id = parseInt(s); if (!isPornFlag(id)) ids.add(id); } catch e { silentException(e); } sortListDesc(ids); ret p(n(ids, "N0n-p0rn image") + ":") + ul(map(ids, func(int id) { ImageMeta meta = metaMap.get(id); S title = or2(getString(meta, "name"), "Untitled"); S linkText = "#" + id + " - " + htmlencode(title); S s = ahref(rawLink(str(id)), linkText); if (meta != null) s += " [md5: " + ahref(rawLink("md5/" + meta.md5), meta.md5) + "]"; ret s; })) + p(ahref(pageLink("/uploadform"), "Upload.")); } null; } static synchronized int newImageID() { while (imageFile(id).exists()) ++id; int i = id++; save("id"); ret i; } static File imageFile(int id) { ret new File(imagesDir(), id + ".png"); } static void setPornFlag(int id, bool flag) { saveTextFile(new File(imagesDir(), id + ".p0rn"), flag ? "t" : null); } static bool isPornFlag(int id) { ret eq("t", readTextFile(new File(imagesDir(), id + ".p0rn"))); }