Download Jar. Libraryless. Click here for Pure Java version (28096L/180K).
1 | !7 |
2 | |
3 | static int id = 1100000; |
4 | |
5 | concept ImageMeta { |
6 | int imageID; |
7 | bool pornFlag; // TODO: set |
8 | S name; |
9 | S md5; // md5 of RGBImage |
10 | S format = "png"; |
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 | S mimeType() { |
21 | ret "image/" + or2(format, "png"); |
22 | } |
23 | } |
24 | |
25 | static Map<Int, ImageMeta> metaMap = synchroMap(); |
26 | |
27 | p { |
28 | load("id"); |
29 | db(); |
30 | indexConceptFieldIC(ImageMeta, 'name); |
31 | indexConceptField(ImageMeta, 'imageID); |
32 | for (ImageMeta meta) |
33 | setMD5(meta); |
34 | } |
35 | |
36 | svoid setMD5(ImageMeta meta) { |
37 | if (nempty(meta.md5)) ret; |
38 | S md5 = "error"; |
39 | pcall { |
40 | md5 = md5OfRGBImage(new RGBImage(loadPNG(imageFile(meta.imageID)))); |
41 | } |
42 | cset(meta, +md5); |
43 | } |
44 | |
45 | static File imagesDir() { |
46 | ret prepareProgramDir("images"); |
47 | } |
48 | |
49 | synchronized html { |
50 | print("image server uri: " + uri + ", https: " + subBot_isHttps()); |
51 | uri = dropPrefix("/", uri); |
52 | new Matches m; |
53 | |
54 | if (eq(uri, "setp0rn")) { |
55 | setPornFlag(parseInt(params.get("id")), true); |
56 | ret "OK"; |
57 | } |
58 | |
59 | if (eq(uri, "unp0rn")) { |
60 | setPornFlag(parseInt(params.get("id")), false); |
61 | ret "OK"; |
62 | } |
63 | |
64 | bool p0rnOK = false; |
65 | |
66 | // TODO: should use uri2? |
67 | if (uri.endsWith("p") && isInteger(dropLast(uri))) { |
68 | p0rnOK = true; |
69 | uri = dropLast(uri); |
70 | } |
71 | |
72 | S uri2 = dropAfterSlash(uri); |
73 | |
74 | if (eq(uri, "random-id")) ret random(list(ImageMeta)).imageID; |
75 | if (eq(uri, "highest-id")) ret id-1; |
76 | |
77 | if (uri.startsWith("title/")) { |
78 | int id = parseFirstInt(uri); |
79 | ImageMeta meta = findConcept(ImageMeta, imageID := id); |
80 | ret meta == null ? "" : meta.name; |
81 | } |
82 | |
83 | if (isInteger(uri2)) { |
84 | // serve image |
85 | |
86 | int id = parseInt(uri2); |
87 | File file = imageFile(id); |
88 | ImageMeta meta = findConcept(ImageMeta, imageID := id); |
89 | if (meta == null) |
90 | ret subBot_serve404("Not found"); |
91 | if (!p0rnOK && isPornFlag(id)) |
92 | ret subBot_serve404("n0 p0rn"); |
93 | ret call(getMainBot(), "serveFile_maxCache", imageFile(meta.imageID), meta.mimeType()); |
94 | } |
95 | |
96 | if (uri.startsWith("md5/")) { |
97 | uri = dropPrefix("md5/", uri); |
98 | bool jpg = uri.startsWith("jpg/"); |
99 | if (jpg) uri = dropPrefix("jpg/", uri); |
100 | ImageMeta meta = findConcept(ImageMeta, md5 := uri); |
101 | if (meta == null) |
102 | ret "Not found"; |
103 | if (jpg && neq(meta.format, "jpeg")) |
104 | ret call(getMainBot(), "serveByteArray_maxCache", toJPEG(imageFile(meta.imageID)), "image/jpeg"); |
105 | ret call(getMainBot(), "serveFile_maxCache", imageFile(meta.imageID), meta.mimeType()); |
106 | } |
107 | |
108 | if (uri.startsWith("checkmd5/")) { |
109 | uri = dropPrefix("checkmd5/", uri); |
110 | ImageMeta meta = findConcept(ImageMeta, md5 := uri); |
111 | ret meta == null ? "Not found" : str(meta.imageID); |
112 | } |
113 | |
114 | if (eq(uri, "upload") || swic(uri, "upload/")) { |
115 | print("upload called."); |
116 | pnlStruct(params); |
117 | S name = params.get("name"); |
118 | |
119 | SS files = getUploadFiles(); |
120 | print("got files: " + l(files)); |
121 | int id; |
122 | S data = params.get("data"); |
123 | if (data != null) { |
124 | print("got data: " + l(data)); |
125 | id = newImageID(); |
126 | saveBinaryFile(imageFile(id), hexToBytes(data)); |
127 | } else { |
128 | String tmpfile = files.get("thefile"); |
129 | String originalName = params.get("thefile"); |
130 | name = or2(name, originalName); |
131 | assertNotNull(tmpfile); |
132 | File tmp = new File(tmpfile); |
133 | long l = tmp.length(); |
134 | if (l == 0) ret "Empty file, exiting"; |
135 | id = newImageID(); |
136 | copyFile(tmp, imageFile(id)); |
137 | } |
138 | ImageMeta meta = cregister ImageMeta(id, unnull(name)); |
139 | byte[] bytes = loadBeginningOfBinaryFile(imageFile(id), 16); |
140 | cset(meta, format := isJPEG(bytes) ? "jpeg" /* correct for mime type! */ : isPNG(bytes) ? "png" : null); |
141 | setMD5(meta); |
142 | S imageID = fsI(meta.imageID); |
143 | ret htitle(imageID) |
144 | + p(ahref(myLink_nonRaw("/"), "Home.") + " " + ahref(myLink_nonRaw("/uploadform"), "Upload more.")) |
145 | + "OK (ID=" + imageID + "):<br>" + himg(imageLink(id)) |
146 | + uploadForm(); |
147 | } |
148 | |
149 | if (eq(uri, "uploadform")) |
150 | ret htitle("Upload") |
151 | + hmobilefix() |
152 | + uploadForm(); |
153 | |
154 | if (eqic(uri, "bot/list")) { |
155 | bool withName = not0_gen(params.get('withName)); |
156 | bool withDate = not0_gen(params.get('withDate)); |
157 | Cl<S> names = subBot_paramsAsMultiMap().get('name); |
158 | |
159 | Cl<ImageMeta> l; |
160 | if (empty(names)) l = list(ImageMeta); |
161 | else l = (Set) concatListsAsSet(map(names, name -> conceptsWhereIC(ImageMeta, +name))); |
162 | |
163 | ret subBot_serveJSON(map(sortedByFieldDesc created(objectsWhere(l, pornFlag := false)), im -> { |
164 | new LinkedHashMap<S, O> map; |
165 | map.put(id := fsI(im.imageID)); |
166 | if (withName) map.put(name := im.name); |
167 | if (withDate) map.put(uploaded := im.created); |
168 | map.put(md5 := im.md5); |
169 | ret map; |
170 | } )); |
171 | } |
172 | |
173 | if (eq(uri, "")) { |
174 | // list images |
175 | |
176 | L<Int> ids = collect imageID(conceptsWhere(ImageMeta, pornFlag := false)); |
177 | sortListDesc(ids); |
178 | ret htitle("Image Server") |
179 | + p(ahref(pageLink("/uploadform"), "Upload.")) |
180 | + p(n(ids, "N0n-p0rn image") + ":") + ul(map(ids, func(Int id) -> S { |
181 | ImageMeta meta = metaMap.get(id); |
182 | S title = or2(getString(meta, "name"), "Untitled"); |
183 | S linkText = "#" + id + " - " + htmlencode(title); |
184 | S s = ahref(snippetImageURL(id), linkText); |
185 | if (meta != null) |
186 | s += " [md5: " + ahref(rawLink("md5/" + meta.md5), meta.md5) + "]"; |
187 | ret s; |
188 | })); |
189 | } |
190 | |
191 | if (!webAuthed(params)) null; |
192 | |
193 | if (eq(uri, "setPornFlags")) { |
194 | for (ImageMeta meta) |
195 | if (isPornFlag(meta.imageID)) |
196 | cset(meta, pornFlag := true); |
197 | ret "Yo"; |
198 | } |
199 | |
200 | if (startsWith(uri, "delete/", m)) { |
201 | S s = m.rest(); |
202 | ImageMeta meta = isMD5(s) |
203 | ? findConcept(ImageMeta, md5 := s) |
204 | : findConcept(ImageMeta, imageID := parseInt(s)); |
205 | if (meta == null) ret "Not found"; |
206 | deleteFile(imageFile(meta)); |
207 | removeConcept(meta); |
208 | ret "Deleted!"; |
209 | } |
210 | |
211 | null; |
212 | } |
213 | |
214 | static synchronized int newImageID() { |
215 | while (imageFile(id).exists()) |
216 | ++id; |
217 | int i = id++; |
218 | save("id"); |
219 | ret i; |
220 | } |
221 | |
222 | static File imageFile(ImageMeta meta) { |
223 | ret meta == null ? null : imageFile(meta.imageID); |
224 | } |
225 | |
226 | static File imageFile(int id) { |
227 | ret new File(imagesDir(), id + ".png"); |
228 | } |
229 | |
230 | static void setPornFlag(int id, bool flag) { |
231 | saveTextFile(new File(imagesDir(), id + ".p0rn"), flag ? "t" : null); |
232 | } |
233 | |
234 | static bool isPornFlag(int id) { |
235 | ret eq("t", readTextFile(new File(imagesDir(), id + ".p0rn"))); |
236 | } |
237 | |
238 | sS uploadForm() { |
239 | ret huploadform( |
240 | "Image file: " + hfileupload("accept", "image/png,image/jpeg,image/gif") + " " |
241 | + "<br>" |
242 | + "Optional name: " + hinputfield("name") |
243 | + hsubmit(), |
244 | "action", rawLink("upload")); |
245 | } |
246 | |
247 | sS imageLink(long id) { |
248 | //ret rawLink(str(id)); |
249 | ret "https://botcompany.de/images/" + id; |
250 | } |
download show line numbers debug dex old transpilations
Travelled to 19 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, irmadwmeruwu, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, nbgitpuheiab, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, sawdedvomwva, tslmcundralx, tvejysmllsmz, tzxuzeklshpk, vouqrxazstgt, xrpafgyirdlv
No comments. add comment
Snippet ID: | #1004590 |
Snippet name: | Image Server [LIVE at botcompany.de/images] |
Eternal ID of this version: | #1004590/62 |
Text MD5: | 41925e01ee84e67d203ed37289494185 |
Transpilation MD5: | 13107000cc64d1dfee79245a0a89a960 |
Author: | stefan |
Category: | javax / networking |
Type: | JavaX source code (desktop) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2022-04-29 04:56:04 |
Source code size: | 7055 bytes / 250 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 1291 / 18802 |
Version history: | 61 change(s) |
Referenced in: | [show references] |