Mercurial
diff mrjunejune/main.c @ 216:e82b80b24012 default tip
[MrJuneJune] Make webp translate background job.
| author | June Park <parkjune1995@gmail.com> |
|---|---|
| date | Sat, 28 Feb 2026 21:04:43 -0800 |
| parents | 240337164a80 |
| children |
line wrap: on
line diff
--- a/mrjunejune/main.c Sat Feb 28 20:34:18 2026 -0800 +++ b/mrjunejune/main.c Sat Feb 28 21:04:43 2026 -0800 @@ -25,6 +25,11 @@ S3_Config s3_config; } Media_Processing_Context; +typedef struct { + char *input_path; + char *output_path; +} File_Converter_Config; + // Server configuration (loaded from .config) static char g_upload_auth_token[256] = {0}; static char g_s3_region[64] = "us-west-2"; @@ -290,6 +295,27 @@ return resp; } +// Background thread function for media processing +void *Simple_WebpConverter_Background(void *arg) +{ + File_Converter_Config *configuration = (File_Converter_Config *)arg; + + char cmd[1024]; + snprintf(cmd, sizeof(cmd), "ffmpeg -y -i %s -quality 80 %s 2>/tmp/error_log", + configuration->input_path, configuration->output_path); + Seobeo_Log(SEOBEO_INFO, "[MEDIA] Running FFmpeg: %s\n", cmd); + int ffmpeg_result = system(cmd); + + Seobeo_Log(SEOBEO_INFO, "[MEDIA] FFmpeg result: %d\n", ffmpeg_result); + if (ffmpeg_result != 0) + { + Seobeo_Log(SEOBEO_ERROR, "[MEDIA] ERROR: FFmpeg conversion failed\n"); + return NULL; + } + Seobeo_Log(SEOBEO_INFO, "[MEDIA] Successfully converted to webp: %s\n"); + return NULL; +} + Seobeo_Request_Entry *ConvertImageToWebP(Seobeo_Request_Entry *req, Dowa_Arena *arena) { Seobeo_Request_Entry *resp = NULL; @@ -338,7 +364,7 @@ const char *content_length_str = ((Seobeo_Request_Entry*)cl_kv)->value; size_t file_size = atoi(content_length_str); - printf("DEBUG: Converting image, file_size=%zu bytes\n", file_size); + Seobeo_Log(SEOBEO_DEBUG, "Converting image, file_size=%zu bytes\n", file_size); int open_flags = O_RDWR | O_CREAT | O_EXCL; @@ -365,15 +391,15 @@ Dowa_String_UUID(seed, uuid4); char *output_path = (char *)Dowa_Arena_Allocate(arena, TMP_FILE_LENGTH);; snprintf(output_path, TMP_FILE_LENGTH, "/tmp/%s.webp", uuid4); - printf("[DEBUG] output_path %s\n", output_path); - printf("[DEBUG] open_flags: 0x%x\n", open_flags); - printf("[DEBUG] input_path: %s\n", input_path); + Seobeo_Log(SEOBEO_DEBUG, "output_path %s\n", output_path); + Seobeo_Log(SEOBEO_DEBUG, "open_flags: 0x%x\n", open_flags); + Seobeo_Log(SEOBEO_DEBUG, "input_path: %s\n", input_path); int output_fd = open(output_path, open_flags, 0600); - printf("[DEBUG] output_fd: %d\n", output_fd); + Seobeo_Log(SEOBEO_DEBUG, "output_fd: %d\n", output_fd); if (output_fd == -1) { unlink(input_path); - printf("[DEBUG] errno: %d (%s)\n", errno, strerror(errno)); + Seobeo_Log(SEOBEO_DEBUG, "errno: %d (%s)\n", errno, strerror(errno)); char *error_msg = "Failed to create output file"; Dowa_HashMap_Push_Arena(resp, "status", "500", arena); Dowa_HashMap_Push_Arena(resp, "content-type", "text/plain", arena); @@ -382,11 +408,14 @@ } close(output_fd); - char cmd[1024]; - snprintf(cmd, sizeof(cmd), "ffmpeg -y -i %s -quality 80 %s 2>/tmp/error_log", - input_path, output_path); - int result = system(cmd); - if (result != 0) + File_Converter_Config *configuration = Dowa_Arena_Allocate(arena, sizeof(File_Converter_Config)); + configuration->input_path = input_path; + configuration->output_path = output_path; + + pthread_t thread_id; + int thread_result = pthread_create(&thread_id, NULL, Simple_WebpConverter_Background, (void *)configuration); + + if (thread_result != 0) { unlink(input_path); unlink(output_path); @@ -396,6 +425,12 @@ Dowa_HashMap_Push_Arena(resp, "body", error_msg, arena); return resp; } + else + { + // Detach thread so it cleans up automatically when done + pthread_detach(thread_id); + Seobeo_Log(SEOBEO_INFO, "[MEDIA] Successfully spawned and detached thread\n"); + } size_t converted_size = 0; FILE *out_file = fopen(output_path, "rb"); @@ -412,7 +447,6 @@ fclose(out_file); unlink(input_path); - char *filename = strrchr(output_path, '/') + 1; char *response_body = Dowa_Arena_Allocate(arena, 512); snprintf(response_body, 512, @@ -422,7 +456,7 @@ Dowa_HashMap_Push_Arena(resp, "content-type", "application/json", arena); Dowa_HashMap_Push_Arena(resp, "body", response_body, arena); - printf("DEBUG: Image converted, available at /api/download/%s\n", filename); + Seobeo_Log(SEOBEO_DEBUG, "Image converted, available at /api/download/%s\n", filename); return resp; }