Warning: session_start(): open(/var/lib/php/sessions/sess_dq5kgbl3j0b136rovra1tvtrsk, 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 = 1200000;
concept FileMeta {
int fileID;
S name;
S md5; // md5 of RGBImage
*() {}
*(int *fileID, S *name) { _doneLoading(); change(); }
void _doneLoading() {
//print("_doneLoading " + fileID);
metaMap.put(fileID, this);
}
}
static Map metaMap = synchroMap();
p {
load("id");
concepts();
}
svoid setMD5(FileMeta meta) {
if (nempty(meta.md5)) ret;
S md5 = "error";
pcall {
md5 = md5(fileFile(meta.fileID));
}
cset(meta, +md5);
}
static File filesDir() {
ret prepareProgramDir("files");
}
synchronized html {
if (!webAuthed()) ret "please log in";
print("uri: " + uri);
uri = dropPrefix("/", uri);
if (isInteger(uri)) {
int id = parseInt(uri);
if (nempty(params.get("remd5"))) {reMD5(
File file = fileFile(id);
ret call(getMainBot(), "serveFile", file, "application/binary");
}
if (uri.startsWith("md5/")) {
uri = dropPrefix("md5/", uri);
FileMeta meta = findConcept(FileMeta, md5 := uri);
if (meta == null)
ret "Not found";
ret call(getMainBot(), "serveFile_maxCache", fileFile(meta.fileID), "application/binary");
}
// finds a file by md5
if (uri.startsWith("checkmd5/")) {
uri = dropPrefix("checkmd5/", uri);
FileMeta meta = findConcept(FileMeta, md5 := uri);
ret meta == null ? "Not found" : str(meta.fileID);
}
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 = newfileID();
saveBinaryFile(fileFile(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 = newfileID();
copyFile(tmp, fileFile(id));
}
setMD5(new FileMeta(id, unnull(name)));
ret "OK:
" + himg(rawLink(str(id)));
}
if (eq(uri, "uploadform")) {
ret htitle("Upload")
+ hmobilefix()
+ huploadform(
p("File to upload: " + hfileupload())
+ p("Optional name: " + hinputfield("name"))
+ p(hsubmit()),
"action", rawLink("upload"));
}
if (eq(uri, "")) {
// list images
new L ids;
for (File f : listFiles(filesDir())) try {
S s = /*dropSuffix(".png",*/ f.getName() /*)*/;
if (!isInteger(s)) continue;
int id = parseInt(s);
ids.add(id);
} catch e {
silentException(e);
}
sortListDesc(ids);
ret p(n(ids, "File") + (empty(ids) ? "." : ":")) + ul(map(ids, func(int id) {
FileMeta 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 a file"));
}
null;
}
static synchronized int newfileID() {
while (fileFile(id).exists())
++id;
int i = id++;
save("id");
ret i;
}
static File fileFile(int id) {
ret new File(filesDir(), str(id));
}