diff --git a/include/cache.h b/include/cache.h index 2df7cb28..9023701a 100644 --- a/include/cache.h +++ b/include/cache.h @@ -39,8 +39,10 @@ struct cache_entry_s * The function also logs the outcome of each file deletion attempt. * * @note This operation does not remove cache entries from memory; it only deletes the files from the file system. + * + * @return The number of files that were successfully deleted. */ -void cache_flush(); +uint32_t cache_flush(); /** * @brief Gathers statistics about the current cache. diff --git a/include/handler_api.h b/include/handler_api.h index 6dec5210..32cca95b 100644 --- a/include/handler_api.h +++ b/include/handler_api.h @@ -60,4 +60,8 @@ error_t handleApiAuthRefreshToken(HttpConnection *connection, const char_t *uri, error_t handleApiMigrateContent2Lib(HttpConnection *connection, const char_t *uri, const char_t *queryString, client_ctx_t *client_ctx); error_t handleDeleteOverlay(HttpConnection *connection, const char_t *uri, const char_t *queryString, client_ctx_t *client_ctx); + +error_t handleApiCacheFlush(HttpConnection *connection, const char_t *uri, const char_t *queryString, client_ctx_t *client_ctx); +error_t handleApiCacheStats(HttpConnection *connection, const char_t *uri, const char_t *queryString, client_ctx_t *client_ctx); + #endif \ No newline at end of file diff --git a/src/cache.c b/src/cache.c index ab038e5a..535bc89e 100644 --- a/src/cache.c +++ b/src/cache.c @@ -10,8 +10,9 @@ cache_entry_t cache_table = {.next = NULL, .hash = 0, .original_url = NULL, .cached_url = NULL, .file_path = NULL}; uint32_t cache_entries = 0; -void cache_flush() +uint32_t cache_flush() { + uint32_t deleted = 0; cache_entry_t *pos = &cache_table; while (pos != NULL) @@ -19,8 +20,9 @@ void cache_flush() if (pos->exists) { // Attempt to delete the local file - if (pos->file_path && remove(pos->file_path) == 0) + if (pos->file_path && fsDeleteFile(pos->file_path) == NO_ERROR) { + deleted++; TRACE_INFO("Deleted cached file: %s\n", pos->file_path); } else @@ -34,6 +36,8 @@ void cache_flush() pos = pos->next; } + + return deleted; } /** diff --git a/src/handler_api.c b/src/handler_api.c index d974da1e..3910a6fd 100644 --- a/src/handler_api.c +++ b/src/handler_api.c @@ -22,6 +22,7 @@ #include "fs_ext.h" #include "cert.h" #include "esp32.h" +#include "cache.h" error_t parsePostData(HttpConnection *connection, char_t *post_data, size_t buffer_size) { @@ -2667,4 +2668,40 @@ error_t handleDeleteOverlay(HttpConnection *connection, const char_t *uri, const TRACE_INFO("Removed overlay %s\n", overlay); return httpOkResponse(connection); +} + +error_t handleApiCacheFlush(HttpConnection *connection, const char_t *uri, const char_t *queryString, client_ctx_t *client_ctx) +{ + /* RESTful API-based cache flush request */ + uint32_t deleted = cache_flush(); + + char json_resp[128]; + snprintf(json_resp, sizeof(json_resp), "{\"message\": \"Cache successfully flushed.\", \"deleted_files\": %u}", deleted); + + httpPrepareHeader(connection, "application/json; charset=utf-8", strlen(json_resp)); + return httpWriteResponseString(connection, json_resp, false); +} + +error_t handleApiCacheStats(HttpConnection *connection, const char_t *uri, const char_t *queryString, client_ctx_t *client_ctx) +{ + cache_stats_t stats; + cache_stats(&stats); + + char stats_json[512]; + snprintf(stats_json, sizeof(stats_json), + "{" + "\"total_entries\": %zu," + "\"exists_entries\": %zu," + "\"total_files\": %zu," + "\"total_size\": %zu," + "\"memory_used\": %zu" + "}", + stats.total_entries, + stats.exists_entries, + stats.total_files, + stats.total_size, + stats.memory_used); + + httpPrepareHeader(connection, "application/json; charset=utf-8", osStrlen(stats_json)); + return httpWriteResponseString(connection, stats_json, false); } \ No newline at end of file diff --git a/src/server.c b/src/server.c index b7ca54af..10e699b7 100644 --- a/src/server.c +++ b/src/server.c @@ -146,6 +146,8 @@ request_type_t request_paths[] = { {REQ_POST, "/api/settings/reset/", SERTY_WEB, &handleApiSettingsReset}, {REQ_POST, "/api/settings/removeOverlay", SERTY_WEB, &handleDeleteOverlay}, {REQ_POST, "/api/migrateContent2Lib", SERTY_WEB, &handleApiMigrateContent2Lib}, + {REQ_POST, "/api/cacheFlush", SERTY_WEB, &handleApiCacheFlush}, + {REQ_GET, "/api/cacheStats", SERTY_WEB, &handleApiCacheStats}, {REQ_GET, "/api/sse", SERTY_WEB, &handleApiSse}, {REQ_GET, "/robots.txt", SERTY_WEB, &handleSecMitRobotsTxt}, /* official tonies API */ @@ -160,28 +162,20 @@ request_type_t request_paths[] = { error_t handleCacheDownload(HttpConnection *connection, const char_t *uri, const char_t *queryString, client_ctx_t *client_ctx) { - if (strcmp(uri, "/cache/flush") == 0) - { - cache_flush(); - - char *resp = "

Cache Flushed

The cache has been successfully flushed.

Return to Cache Stats"; - httpPrepareHeader(connection, "text/html; charset=utf-8", osStrlen(resp)); - return httpWriteResponseString(connection, resp, false); - } - - if (strcmp(uri, "/cache/stats.html") == 0) + /* guerilla-style stats page for internal tests, to be removed when web ui is finished */ + if (osStrcmp(uri, "/cache/stats.html") == 0) { cache_stats_t stats; cache_stats(&stats); - char stats_page[4096]; // Buffer for the HTML page + char stats_page[4096]; snprintf(stats_page, sizeof(stats_page), "" "" "" "" "" - "" + "" "Cache Statistics" "" + "" "" "" "
" @@ -203,7 +212,7 @@ error_t handleCacheDownload(HttpConnection *connection, const char_t *uri, const "Total Cache Size%zu bytes" "Memory Used%zu bytes" "" - "Flush Cache" + "" "
" "" "",