!7 static int id = 1100000; concept ImageMeta { int imageID; bool pornFlag; // TODO: set S name; S md5; // md5 of RGBImage S format = "png"; *() {} *(int *imageID, S *name) { _doneLoading(); change(); } void _doneLoading() { //print("_doneLoading " + imageID); metaMap.put(imageID, this); } S mimeType() { ret "image/" + or2(format, "png"); } } static Map metaMap = synchroMap(); p { load("id"); db(); indexConceptFieldIC(ImageMeta, 'name); indexConceptField(ImageMeta, 'imageID); 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("image server uri: " + uri + ", https: " + subBot_isHttps()); uri = dropPrefix("/", uri); new Matches m; 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; // TODO: should use uri2? if (uri.endsWith("p") && isInteger(dropLast(uri))) { p0rnOK = true; uri = dropLast(uri); } S uri2 = dropAfterSlash(uri); if (eq(uri, "random-id")) ret random(list(ImageMeta)).imageID; if (eq(uri, "highest-id")) ret id-1; if (uri.startsWith("title/")) { int id = parseFirstInt(uri); ImageMeta meta = findConcept(ImageMeta, imageID := id); ret meta == null ? "" : meta.name; } if (isInteger(uri2)) { // serve image int id = parseInt(uri2); File file = imageFile(id); ImageMeta meta = findConcept(ImageMeta, imageID := id); if (meta == null) ret subBot_serve404("Not found"); if (!p0rnOK && isPornFlag(id)) ret subBot_serve404("n0 p0rn"); ret call(getMainBot(), "serveFile_maxCache", imageFile(meta.imageID), meta.mimeType()); } 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 && neq(meta.format, "jpeg")) ret call(getMainBot(), "serveByteArray_maxCache", toJPEG(imageFile(meta.imageID)), "image/jpeg"); ret call(getMainBot(), "serveFile_maxCache", imageFile(meta.imageID), meta.mimeType()); } 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") || swic(uri, "upload/")) { print("upload called."); pnlStruct(params); 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)); } ImageMeta meta = cregister ImageMeta(id, unnull(name)); byte[] bytes = loadBeginningOfBinaryFile(imageFile(id), 16); cset(meta, format := isJPEG(bytes) ? "jpeg" /* correct for mime type! */ : isPNG(bytes) ? "png" : null); setMD5(meta); S imageID = fsI(meta.imageID); ret htitle(imageID) + p(ahref(myLink_nonRaw("/"), "Home.") + " " + ahref(myLink_nonRaw("/uploadform"), "Upload more.")) + "OK (ID=" + imageID + "):
" + himg(imageLink(id)) + uploadForm(); } if (eq(uri, "uploadform")) ret htitle("Upload") + hmobilefix() + uploadForm(); if (eqic(uri, "bot/list")) { bool withName = not0_gen(params.get('withName)); bool withDate = not0_gen(params.get('withDate)); Cl names = subBot_paramsAsMultiMap().get('name); Cl l; if (empty(names)) l = list(ImageMeta); else l = (Set) concatListsAsSet(map(names, name -> conceptsWhereIC(ImageMeta, +name))); ret subBot_serveJSON(map(sortedByFieldDesc created(objectsWhere(l, pornFlag := false)), im -> { new LinkedHashMap map; map.put(id := fsI(im.imageID)); if (withName) map.put(name := im.name); if (withDate) map.put(uploaded := im.created); map.put(md5 := im.md5); ret map; } )); } if (eq(uri, "")) { // list images L ids = collect imageID(conceptsWhere(ImageMeta, pornFlag := false)); sortListDesc(ids); ret htitle("Image Server") + p(ahref(pageLink("/uploadform"), "Upload.")) + p(n(ids, "N0n-p0rn image") + ":") + ul(map(ids, func(Int id) -> S { ImageMeta meta = metaMap.get(id); S title = or2(getString(meta, "name"), "Untitled"); S linkText = "#" + id + " - " + htmlencode(title); S s = ahref(snippetImageURL(id), linkText); if (meta != null) s += " [md5: " + ahref(rawLink("md5/" + meta.md5), meta.md5) + "]"; ret s; })); } if (!webAuthed(params)) null; if (eq(uri, "setPornFlags")) { for (ImageMeta meta) if (isPornFlag(meta.imageID)) cset(meta, pornFlag := true); ret "Yo"; } if (startsWith(uri, "delete/", m)) { S s = m.rest(); ImageMeta meta = isMD5(s) ? findConcept(ImageMeta, md5 := s) : findConcept(ImageMeta, imageID := parseInt(s)); if (meta == null) ret "Not found"; deleteFile(imageFile(meta)); removeConcept(meta); ret "Deleted!"; } null; } static synchronized int newImageID() { while (imageFile(id).exists()) ++id; int i = id++; save("id"); ret i; } static File imageFile(ImageMeta meta) { ret meta == null ? null : imageFile(meta.imageID); } 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"))); } sS uploadForm() { ret huploadform( "Image file: " + hfileupload("accept", "image/png,image/jpeg,image/gif") + " " + "
" + "Optional name: " + hinputfield("name") + hsubmit(), "action", rawLink("upload")); } sS imageLink(long id) { //ret rawLink(str(id)); ret "https://botcompany.de/images/" + id; }