Mercurial
diff mrjunejune/main.c @ 263:ee04e4e69fed
Add functional JRPG frame and tools
Add full-screen background_2 apertures, functional frame chrome, card-driven details, dual-window tools, live conversion workflows, and bounded cleanup for generated downloads.
Co-authored-by: Copilot <[email protected]>
| author | MrJuneJune <mrjunejune@users.noreply.github.com> |
|---|---|
| date | Thu, 06 Aug 2026 11:31:30 -0700 |
| parents | 1f9877b637e9 |
| children | 04fee26ecce0 |
line wrap: on
line diff
--- a/mrjunejune/main.c Thu Aug 06 04:08:45 2026 -0700 +++ b/mrjunejune/main.c Thu Aug 06 11:31:30 2026 -0700 @@ -691,6 +691,67 @@ return resp; } +static boolean Converted_Filename_Is_Valid(const char *filename) +{ + if (!filename) + return FALSE; + const char *extension = strrchr(filename, '.'); + if (!extension || (strcmp(extension, ".webp") != 0 && + strcmp(extension, ".mp4") != 0)) + return FALSE; + if ((size_t)(extension - filename) != 36) + return FALSE; + for (size_t i = 0; i < 36; i++) + { + char value = filename[i]; + if (i == 8 || i == 13 || i == 18 || i == 23) + { + if (value != '-') + return FALSE; + continue; + } + if (!((value >= '0' && value <= '9') || + (value >= 'a' && value <= 'f') || + (value >= 'A' && value <= 'F'))) + return FALSE; + } + return TRUE; +} + +static Seobeo_Request_Entry *Converted_File_Error( + Dowa_Arena *arena, + char *status, + char *message) +{ + Seobeo_Request_Entry *resp = NULL; + Dowa_HashMap_Push_Arena(resp, "status", status, arena); + Dowa_HashMap_Push_Arena(resp, "content-type", "text/plain", arena); + Dowa_HashMap_Push_Arena(resp, "body", message, arena); + return resp; +} + +Seobeo_Request_Entry *DeleteConvertedFile( + Seobeo_Request_Entry *req, + Dowa_Arena *arena) +{ + void *filename_kv = Dowa_HashMap_Get_Ptr(req, ":filename"); + const char *filename = filename_kv + ? ((Seobeo_Request_Entry *)filename_kv)->value + : NULL; + if (!Converted_Filename_Is_Valid(filename)) + return Converted_File_Error(arena, "400", "Invalid converted filename"); + + char filepath[512]; + snprintf(filepath, sizeof(filepath), "/tmp/%s", filename); + if (unlink(filepath) != 0 && errno != ENOENT) + return Converted_File_Error(arena, "500", "Unable to delete converted file"); + + Seobeo_Request_Entry *resp = NULL; + Dowa_HashMap_Push_Arena(resp, "status", "204", arena); + Dowa_HashMap_Push_Arena(resp, "body", "", arena); + return resp; +} + Seobeo_Request_Entry *DownloadConvertedFile(Seobeo_Request_Entry *req, Dowa_Arena *arena) { Seobeo_Request_Entry *resp = NULL; @@ -706,26 +767,8 @@ } const char *filename = ((Seobeo_Request_Entry*)filename_kv)->value; - - // TODO: Maybe check if the uuid is allowed or not? - // if (strlen(filename) != TMP_FILE_LENGTH) - // { - // char *error_msg = "Not Allowed Filename"; - // Dowa_HashMap_Push_Arena(resp, "status", "404", arena); - // Dowa_HashMap_Push_Arena(resp, "content-type", "text/plain", arena); - // Dowa_HashMap_Push_Arena(resp, "body", error_msg, arena); - // return resp; - // } - // boolean allowed = FALSE; - // for (int i = 0; i < Dowa_Array_Length(g_uuid4_array); i++) - // { - // if strcmp(g_uuid4_array, filename) - // { - // allowed = TRUE; - // break; - // } - // g_uuid4_array++; - // } + if (!Converted_Filename_Is_Valid(filename)) + return Converted_File_Error(arena, "400", "Invalid converted filename"); char filepath[512]; snprintf(filepath, sizeof(filepath), "/tmp/%s", filename); @@ -759,8 +802,8 @@ file_data[file_size] = '\0'; fclose(file); - const char *content_type = "application/octet-stream"; - if (strstr(filename, ".webp")) + char *content_type = "application/octet-stream"; + if (strcmp(strrchr(filename, '.'), ".webp") == 0) content_type = "image/webp"; else if (strstr(filename, ".mp4")) content_type = "video/mp4"; @@ -2067,6 +2110,7 @@ Seobeo_Router_Register("POST", "/api/convert/image-to-webp", ConvertImageToWebP); Seobeo_Router_Register("POST", "/api/convert/video-to-mp4", ConvertVideoToMP4); Seobeo_Router_Register("GET", "/api/download/:filename", DownloadConvertedFile); + Seobeo_Router_Register("DELETE", "/api/download/:filename", DeleteConvertedFile); Seobeo_Router_Register("POST", "/api/latex/render", RenderLatexPdf); // -- S3 Upload --/