123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240 |
- void HandleCreateShare(AsyncWebServerRequest *r) {
- if (!HandleAuth(r)) {
- return;
- }
-
- String filepath = GetPara(r, "filename");
- filepath.trim();
-
-
- if (filepath == "") {
- SendErrorResp(r, "invalid filename given");
- return;
- }
- if (IsFileShared(filepath)) {
- SendErrorResp(r, "target file already shared");
- return;
- }
-
- String shareID = GeneratedRandomHex();
- bool succ = DBWrite("shln", shareID, filepath);
- if (!succ) {
- SendErrorResp(r, "unable to save share entry");
- return;
- }
- succ = DBWrite("shfn", filepath, shareID);
- if (!succ) {
- SendErrorResp(r, "unable to save share entry");
- return;
- }
- Serial.println("Shared: " + filepath + " with ID: " + shareID);
- SendOK(r);
- }
- void HandleRemoveShare(AsyncWebServerRequest *r) {
- if (!HandleAuth(r)) {
- return;
- }
-
- String filepath = GetPara(r, "filename");
- filepath.trim();
- if (filepath == "") {
- SendErrorResp(r, "invalid filename given");
- return;
- }
- if (!IsFileShared(filepath)) {
- SendErrorResp(r, "target file is not shared");
- return;
- }
-
- String shareId = DBRead("shfn", filepath);
- if (shareId == "") {
- SendErrorResp(r, "unable to load share entry");
- return;
- }
-
- bool succ = DBRemove("shln", shareId);
- if (!succ) {
- SendErrorResp(r, "unable to remove share entry");
- return;
- }
- succ = DBRemove("shfn", filepath);
- if (!succ) {
- SendErrorResp(r, "unable to remove share entry");
- return;
- }
- Serial.println("Removed shared file " + filepath + " (share ID: " + shareId + ")");
- SendOK(r);
- }
- void HandleShareList(AsyncWebServerRequest *r) {
- if (!HandleAuth(r)) {
- return;
- }
-
- String jsonString = "[";
-
- File root = SD.open(DB_root + "shln/");
- bool firstObject = true;
- if (root) {
- while (true) {
- File entry = root.openNextFile();
- if (!entry) {
-
- break;
- } else {
-
- if (!firstObject) {
- jsonString = jsonString + ",";
- } else {
- firstObject = false;
- }
-
- if (entry.isDirectory()) {
- continue;
- }
-
- String filename = "";
- while (entry.available()) {
- filename = filename + entry.readString();
- }
-
- jsonString = jsonString + "{\"filename\":\"" + basename(filename) + "\", \"filepath\":\"" + filename + "\", \"shareid\":\"" + entry.name() + "\"}";
- }
- }
- }
- jsonString += "]";
- r->send(200, "application/json", jsonString);
- }
- void HandleShareAccess(AsyncWebServerRequest *r) {
- String shareID = GetPara(r, "id");
- if (shareID == "") {
- r->send(404, "text/plain", "Not Found");
- return;
- }
-
- String sharedFilename = GetFilenameFromShareID(shareID);
- if (sharedFilename == "") {
- r->send(404, "text/plain", "Share not found");
- return;
- }
-
- String realFilepath = "/www" + sharedFilename;
- File targetFile = SD.open(realFilepath);
- if (!targetFile) {
- r->send(404, "text/plain", "Shared file no longer exists");
- return;;
- }
- if (r->hasParam("download")) {
-
- r->send(SDFS, realFilepath, getMime(sharedFilename), false);
- } else if (r->hasParam("prop")) {
-
- File targetFile = SD.open(realFilepath);
- if (!targetFile) {
- SendErrorResp(r, "File open failed");
- return;
- }
- String resp = "{\"filename\":\"" + basename(sharedFilename) + "\",\"filepath\":\"" + sharedFilename + "\",\"isDir\":false,\"filesize\":" + String(targetFile.size()) + ",\"shareid\":\"" + shareID + "\"}";
- targetFile.close();
- SendJsonResp(r, resp);
- } else {
-
- r->send(SDFS, "/www/admin/share.html", "text/html", false);
- return;
- }
- }
- void HandleShareListCleaning(AsyncWebServerRequest *r) {
- if (!HandleAuth(r)) {
- return;
- }
- File root = SD.open(DB_root + "shln/");
- bool firstObject = true;
- if (root) {
- while (true) {
- File entry = root.openNextFile();
- if (!entry) {
-
- break;
- } else {
-
- if (entry.isDirectory()) {
- continue;
- }
-
- String filename = "";
- while (entry.available()) {
- filename = filename + entry.readString();
- }
-
- File targetFile = SD.open("/www" + filename);
- if (!targetFile) {
-
- DBRemove("shln", entry.name());
- DBRemove("shfn", filename);
- } else {
-
- targetFile.close();
- }
- }
- }
- }
- SendOK(r);
- }
- String GetFileShareIDByFilename(String filepath) {
- return DBRead("shfn", filepath);
- }
- String GetFilenameFromShareID(String shareid) {
- return DBRead("shln", shareid);
- }
- bool IsFileShared(String filepath) {
-
- return DBKeyExists("shfn", filepath);
- }
|