Warning: session_start(): open(/var/lib/php/sessions/sess_5g3n2rl4muehhks0orq7u51irj, O_RDWR) failed: No space left on device (28) in /var/www/tb-usercake/models/config.php on line 51
Warning: session_start(): Failed to read session data: files (path: /var/lib/php/sessions) in /var/www/tb-usercake/models/config.php on line 51
!7
static int id = 5000000;
concept ImageMeta {
int imageID;
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");
concepts();
for (ImageMeta meta)
setMD5(meta);
botWithCommandList("Image DB.");
}
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 (eq(uri, "random-id"))
ret random(list(ImageMeta)).imageID;
if (uri.startsWith("title/")) {
int id = parseFirstInt(uri);
ImageMeta meta = findConcept(ImageMeta, imageID := id);
ret meta == null ? "" : meta.name;
}
if (isInteger(uri)) {
// serve image
int id = parseInt(uri);
File file = imageFile(id);
ImageMeta meta = findConcept(ImageMeta, imageID := id);
if (meta == null)
ret "Not found";
if (!p0rnOK && isPornFlag(id))
ret "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")) {
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));
}
saveImageMeta(id, 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")));
}
answer {
if "upload image data * with name *" {
int id = newImageID();
saveBinaryFile(imageFile(id), hexToBytes($1));
saveImageMeta(id, $2);
ret "OK " + id;
}
}
svoid saveImageMeta(int id, S name) {
ImageMeta meta = new(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);
}