speed up deletion of files (#2389)

* speed up deletion of files

* .

* .

* .

* .

* .

---------

Co-authored-by: CaCO3 <caco@ruinelli.ch>
This commit is contained in:
CaCO3
2023-05-05 18:43:46 +02:00
committed by GitHub
parent ba7d6b3621
commit 431551fb45

View File

@@ -837,38 +837,22 @@ static esp_err_t delete_post_handler(httpd_req_t *req)
return ESP_FAIL; return ESP_FAIL;
} }
if (stat(filepath, &file_stat) == -1) { if (stat(filepath, &file_stat) == -1) { // File does not exist
LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "File does not exist: " + string(filename)); /* This is ok, we would delete it anyway */
/* Respond with 400 Bad Request */ LogFile.WriteToFile(ESP_LOG_INFO, TAG, "File does not exist: " + string(filename));
httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "File does not exist");
return ESP_FAIL;
} }
else {
LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Deleting file: " + string(filename)); LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Deleting file: " + string(filename));
/* Delete file */ /* Delete file */
unlink(filepath); unlink(filepath);
directory = std::string(filepath);
size_t zw = directory.find("/");
size_t found = zw;
while (zw != std::string::npos)
{
zw = directory.find("/", found+1);
if (zw != std::string::npos)
found = zw;
} }
int start_fn = strlen(((struct file_server_data *)req->user_ctx)->base_path); char *pos = strrchr(filename, '/');
ESP_LOGD(TAG, "Directory: %s, start_fn: %d, found: %d", directory.c_str(), start_fn, found); *pos = '\0'; // Cut off filename
directory = directory.substr(start_fn, found - start_fn + 1); directory = std::string(filename);
directory = "/fileserver" + directory; directory = "/fileserver" + directory + "/";
ESP_LOGD(TAG, "Directory danach 4: %s", directory.c_str());
} }
////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////
/* Redirect onto root to see the updated file list */ /* Redirect onto root to see the updated file list */