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

173
LINES

< > BotCompany Repo | #1007909 // ImgServer 2

JavaX source code - run with: x30.jar

!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<Int, ImageMeta> 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:<br>" + himg(rawLink(str(id)));
  }
  
  if (eq(uri, "uploadform")) {
    ret htitle("Upload")
      + hmobilefix()
      + huploadform(
          "Image file: " + hfileupload("accept", "image/png,image/jpeg") + " "
        + "<br>"
        + "Optional name: " + hinputfield("name")
        + hsubmit(),
        "action", rawLink("upload"));
  }
  
  if (eq(uri, "")) {
    // list images
    
    new L<int> 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")));
}

Author comment

Began life as a copy of #1004590

It is currently just a close, but I'll modify it. Alot.

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1007909
Snippet name: ImgServer 2
Eternal ID of this version: #1007909/1
Text MD5: 635dd9335563fcca769ef244d8ad0c5f
Author: caramel
Category: javax / networking
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2017-04-17 09:01:12
Source code size: 4445 bytes / 173 lines
Pitched / IR pitched: No / No
Views / Downloads: 560 / 566
Referenced in: [show references]