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

1  
!752
2  
3  
static int id = 1100000;
4  
5  
concepts.
6  
7  
concept ImageMeta {
8  
  int imageID;
9  
  S name;
10  
  S md5; // md5 of RGBImage
11  
  
12  
  *() {}
13  
  *(int *imageID, S *name) { _doneLoading(); change(); }
14  
  
15  
  void _doneLoading() {
16  
    //print("_doneLoading " + imageID);
17  
    metaMap.put(imageID, this);
18  
  }
19  
}
20  
21  
static Map<Int, ImageMeta> metaMap = synchroMap();
22  
23  
p {
24  
  load("id");
25  
  concepts();
26  
  for (ImageMeta meta)
27  
    setMD5(meta);
28  
}
29  
30  
svoid setMD5(ImageMeta meta) {
31  
  if (nempty(meta.md5)) ret;
32  
  S md5 = "error";
33  
  pcall {
34  
    md5 = md5OfRGBImage(new RGBImage(loadPNG(imageFile(meta.imageID))));
35  
  }
36  
  cset(meta, +md5);
37  
}
38  
static File imagesDir() {
39  
  ret prepareProgramDir("images");
40  
}
41  
42  
synchronized html {
43  
  print("uri: " + uri);
44  
  uri = dropPrefix("/", uri);
45  
  
46  
  if (eq(uri, "setp0rn")) {
47  
    setPornFlag(parseInt(params.get("id")), true);
48  
    ret "OK";
49  
  }
50  
51  
  if (eq(uri, "unp0rn")) {
52  
    setPornFlag(parseInt(params.get("id")), false);
53  
    ret "OK";
54  
  }
55  
56  
  bool p0rnOK = false;
57  
  
58  
  if (uri.endsWith("p") && isInteger(dropLast(uri))) {
59  
    p0rnOK = true;
60  
    uri = dropLast(uri);
61  
  }
62  
  
63  
  if (isInteger(uri)) {
64  
    int id = parseInt(uri);
65  
    File file = imageFile(id);
66  
    if (!p0rnOK && isPornFlag(id))
67  
      ret "n0 p0rn";
68  
    ret call(getMainBot(), "serveFile", file, "image/png");
69  
  }
70  
  
71  
  if (uri.startsWith("md5/")) {
72  
    uri = dropPrefix("md5/", uri);
73  
    bool jpg = uri.startsWith("jpg/");
74  
    if (jpg) uri = dropPrefix("jpg/", uri);
75  
    ImageMeta meta = findConcept(ImageMeta, md5 := uri);
76  
    if (meta == null)
77  
      ret "Not found";
78  
    if (jpg)
79  
      ret call(getMainBot(), "serveByteArray_maxCache", toJPEG(imageFile(meta.imageID)), "image/jpeg");
80  
    ret call(getMainBot(), "serveFile_maxCache", imageFile(meta.imageID), "image/png");
81  
  }
82  
  
83  
  if (uri.startsWith("checkmd5/")) {
84  
    uri = dropPrefix("checkmd5/", uri);
85  
    ImageMeta meta = findConcept(ImageMeta, md5 := uri);
86  
    ret meta == null ? "Not found" : str(meta.imageID);
87  
  }
88  
  
89  
  if (eq(uri, "upload")) {
90  
    print("upload called");
91  
    S name = params.get("name");
92  
    
93  
    SS files = getUploadFiles();
94  
    print("got files: " + l(files));
95  
    int id;
96  
    S data = params.get("data");
97  
    if (data != null) {
98  
      print("got data: " + l(data));
99  
      id = newImageID();
100  
      saveBinaryFile(imageFile(id), hexToBytes(data));
101  
    } else {
102  
      String tmpfile = files.get("thefile");
103  
      String originalName = params.get("thefile");
104  
      name = or2(name, originalName);
105  
      assertNotNull(tmpfile);
106  
      File tmp = new File(tmpfile);
107  
      long l = tmp.length();
108  
      if (l == 0) ret "Empty file, exiting";
109  
      id = newImageID();
110  
      copyFile(tmp, imageFile(id));
111  
    }
112  
    setMD5(new ImageMeta(id, unnull(name)));
113  
    ret "OK:<br>" + himg(rawLink(str(id)));
114  
  }
115  
  
116  
  if (eq(uri, "uploadform")) {
117  
    ret htitle("Upload")
118  
      + hmobilefix()
119  
      + huploadform(
120  
          "Image file: " + hfileupload("accept", "image/png,image/jpeg") + " "
121  
        + "<br>"
122  
        + "Optional name: " + hinputfield("name")
123  
        + hsubmit(),
124  
        "action", rawLink("upload"));
125  
  }
126  
  
127  
  if (eq(uri, "")) {
128  
    // list images
129  
    
130  
    new L<int> ids;
131  
    for (File f : listFiles(imagesDir())) try {
132  
      S s = dropSuffix(".png", f.getName());
133  
      if (!isInteger(s)) continue;
134  
      int id = parseInt(s);
135  
      if (!isPornFlag(id))
136  
        ids.add(id);
137  
    } catch e {
138  
      silentException(e);
139  
    }
140  
    sortListDesc(ids);
141  
    ret p(n(ids, "N0n-p0rn image") + ":") + ul(map(ids, func(int id) {
142  
      ImageMeta meta = metaMap.get(id);
143  
      S title = or2(getString(meta, "name"), "Untitled");
144  
      S linkText = "#" + id + " - " + htmlencode(title);
145  
      S s = ahref(rawLink(str(id)), linkText);
146  
      if (meta != null)
147  
        s += " [md5: " + ahref(rawLink("md5/" + meta.md5), meta.md5) + "]";
148  
      ret s;
149  
    })) + p(ahref(pageLink("/uploadform"), "Upload."));
150  
  }
151  
  
152  
  null;
153  
}
154  
155  
static synchronized int newImageID() {
156  
  while (imageFile(id).exists())
157  
    ++id;
158  
  int i = id++;
159  
  save("id");
160  
  ret i;
161  
}
162  
163  
static File imageFile(int id) {
164  
  ret new File(imagesDir(), id + ".png");
165  
}
166  
167  
static void setPornFlag(int id, bool flag) {
168  
  saveTextFile(new File(imagesDir(), id + ".p0rn"), flag ? "t" : null);
169  
}
170  
171  
static bool isPornFlag(int id) {
172  
  ret eq("t", readTextFile(new File(imagesDir(), id + ".p0rn")));
173  
}

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: 649 / 655
Referenced in: [show references]