Mercurial
annotate mrjunejune/main.c @ 254:2b6e732087ff
[ui] Add complete native component catalog
Co-authored-by: Copilot <[email protected]>
| author | MrJuneJune <me@mrjunejune.com> |
|---|---|
| date | Tue, 04 Aug 2026 15:12:09 -0700 |
| parents | 745fd127b2a1 |
| children | 667156fcd3e3 |
| rev | line source |
|---|---|
|
7
114cad94008f
[Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
1 #include "seobeo/seobeo.h" |
|
158
1c0878eb17de
[MrJuneJune] Readme file gets compiled in server side.
June Park <parkjune1995@gmail.com>
parents:
132
diff
changeset
|
2 #include "markdown_converter/markdown_to_html.h" |
|
200
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
3 #include "s3/s3_uploader.h" |
| 201 | 4 #include "deita/deita.h" |
|
244
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
5 #include "mrjunejune/latex_renderer.h" |
|
92
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
6 #include <time.h> |
| 201 | 7 #include <sys/stat.h> |
| 8 #include <stdarg.h> | |
|
244
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
9 #include <stdatomic.h> |
| 201 | 10 #include <pthread.h> |
|
92
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
11 |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
12 // UUID + /tmp/ + format (max 4) |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
13 #define TMP_FILE_LENGTH 47 |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
14 #define UUID_LEN 37 |
|
7
114cad94008f
[Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
15 |
|
36
84672efec192
[Zenbu] WIP fixing issues regarding to using edge only. I think there is a problem where socket closes before sending back the info.
MrJuneJune <me@mrjunejune.com>
parents:
19
diff
changeset
|
16 volatile sig_atomic_t stop_server = 0; |
|
248
b8b6e726964a
[style] Use Dowa type aliases in C
MrJuneJune <me@mrjunejune.com>
parents:
244
diff
changeset
|
17 static _Atomic uint32 counter = 0; |
|
244
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
18 static _Atomic boolean g_latex_rendering = FALSE; |
|
250
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
19 static Seobeo_Worker_Pool *g_media_worker_pool = NULL; |
|
36
84672efec192
[Zenbu] WIP fixing issues regarding to using edge only. I think there is a problem where socket closes before sending back the info.
MrJuneJune <me@mrjunejune.com>
parents:
19
diff
changeset
|
20 |
|
250
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
21 // Media processing context owned by a background worker. |
| 201 | 22 typedef struct { |
| 23 int64 media_id; | |
| 24 char s3_key_original[512]; | |
| 25 char s3_key_processed[512]; | |
| 26 char content_type[128]; | |
| 27 char access_token[256]; | |
|
202
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
28 char db_path[256]; |
| 201 | 29 S3_Config s3_config; |
| 30 } Media_Processing_Context; | |
| 31 | |
|
216
e82b80b24012
[MrJuneJune] Make webp translate background job.
June Park <parkjune1995@gmail.com>
parents:
206
diff
changeset
|
32 typedef struct { |
|
e82b80b24012
[MrJuneJune] Make webp translate background job.
June Park <parkjune1995@gmail.com>
parents:
206
diff
changeset
|
33 char *input_path; |
|
e82b80b24012
[MrJuneJune] Make webp translate background job.
June Park <parkjune1995@gmail.com>
parents:
206
diff
changeset
|
34 char *output_path; |
|
250
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
35 int result; |
|
216
e82b80b24012
[MrJuneJune] Make webp translate background job.
June Park <parkjune1995@gmail.com>
parents:
206
diff
changeset
|
36 } File_Converter_Config; |
|
e82b80b24012
[MrJuneJune] Make webp translate background job.
June Park <parkjune1995@gmail.com>
parents:
206
diff
changeset
|
37 |
|
200
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
38 // Server configuration (loaded from .config) |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
39 static char g_upload_auth_token[256] = {0}; |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
40 static char g_s3_region[64] = "us-west-2"; |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
41 static char g_s3_bucket[128] = "mrjunejune"; |
| 201 | 42 static char g_s3_cloudfront_url[256] = {0}; |
| 43 static char g_db_path[256] = "mrjunejune/data/mrjunejune.db"; | |
|
200
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
44 static int g_s3_url_expires = 3600; |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
45 static S3_Config g_s3_config = {0}; |
| 201 | 46 static Deita_Connection *g_db_connection = NULL; |
|
200
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
47 |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
48 static void load_config(const char *config_path) |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
49 { |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
50 FILE *f = fopen(config_path, "r"); |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
51 if (!f) |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
52 { |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
53 printf("[CONFIG] Warning: Could not open %s, using defaults\n", config_path); |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
54 return; |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
55 } |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
56 |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
57 char line[512]; |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
58 while (fgets(line, sizeof(line), f)) |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
59 { |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
60 // Skip comments and empty lines |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
61 if (line[0] == '#' || line[0] == '\n' || line[0] == '\r') continue; |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
62 |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
63 char *eq = strchr(line, '='); |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
64 if (!eq) continue; |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
65 |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
66 *eq = '\0'; |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
67 char *key = line; |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
68 char *value = eq + 1; |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
69 |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
70 // Trim newline from value |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
71 size_t vlen = strlen(value); |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
72 while (vlen > 0 && (value[vlen-1] == '\n' || value[vlen-1] == '\r')) |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
73 value[--vlen] = '\0'; |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
74 |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
75 if (strcmp(key, "UPLOAD_AUTH_TOKEN") == 0) |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
76 { |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
77 strncpy(g_upload_auth_token, value, sizeof(g_upload_auth_token) - 1); |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
78 } |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
79 else if (strcmp(key, "S3_REGION") == 0) |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
80 { |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
81 strncpy(g_s3_region, value, sizeof(g_s3_region) - 1); |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
82 } |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
83 else if (strcmp(key, "S3_BUCKET") == 0) |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
84 { |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
85 strncpy(g_s3_bucket, value, sizeof(g_s3_bucket) - 1); |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
86 } |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
87 else if (strcmp(key, "S3_URL_EXPIRES") == 0) |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
88 { |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
89 g_s3_url_expires = atoi(value); |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
90 } |
| 201 | 91 else if (strcmp(key, "S3_CLOUDFRONT_URL") == 0) |
| 92 { | |
| 93 strncpy(g_s3_cloudfront_url, value, sizeof(g_s3_cloudfront_url) - 1); | |
| 94 } | |
| 95 else if (strcmp(key, "DB_PATH") == 0) | |
| 96 { | |
| 97 strncpy(g_db_path, value, sizeof(g_db_path) - 1); | |
| 98 } | |
|
200
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
99 } |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
100 fclose(f); |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
101 |
| 201 | 102 printf("[CONFIG] Loaded: token=%s..., region=%s, bucket=%s, expires=%d, cloudfront=%s, db=%s\n", |
|
200
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
103 g_upload_auth_token[0] ? "***" : "(empty)", |
| 201 | 104 g_s3_region, g_s3_bucket, g_s3_url_expires, |
| 105 g_s3_cloudfront_url[0] ? g_s3_cloudfront_url : "(none)", | |
| 106 g_db_path); | |
| 107 } | |
| 108 | |
| 109 static void init_database(void) | |
| 110 { | |
| 111 // Create data directory if needed | |
| 112 char *last_slash = strrchr(g_db_path, '/'); | |
| 113 if (last_slash) | |
| 114 { | |
| 115 char dir_path[256]; | |
| 116 size_t dir_len = last_slash - g_db_path; | |
| 117 strncpy(dir_path, g_db_path, dir_len); | |
| 118 dir_path[dir_len] = '\0'; | |
| 119 mkdir(dir_path, 0755); | |
| 120 } | |
| 121 | |
| 122 g_db_connection = Deita_Connection_Create(DEITA_DATABASE_TYPE_SQLITE3, g_db_path); | |
| 123 if (!g_db_connection || !Deita_Connection_Is_Open(g_db_connection)) | |
| 124 { | |
| 125 printf("[DB] ERROR: Failed to open database at %s\n", g_db_path); | |
| 126 return; | |
| 127 } | |
| 128 | |
| 129 // Create editor_content table | |
| 130 const char *create_table = | |
| 131 "CREATE TABLE IF NOT EXISTS editor_content (" | |
| 132 " id INTEGER PRIMARY KEY AUTOINCREMENT," | |
| 133 " access_token TEXT NOT NULL," | |
| 134 " doc_id TEXT NOT NULL," | |
| 135 " content TEXT," | |
| 136 " created_at INTEGER DEFAULT (strftime('%s', 'now'))," | |
| 137 " updated_at INTEGER DEFAULT (strftime('%s', 'now'))," | |
| 138 " UNIQUE(access_token, doc_id)" | |
| 139 ")"; | |
| 140 | |
| 141 int32 result = Deita_Query_Execute_Update(g_db_connection, create_table); | |
| 142 if (result < 0) | |
| 143 { | |
| 144 printf("[DB] ERROR: Failed to create editor_content table\n"); | |
| 145 } | |
| 146 | |
| 147 // Create media_uploads table | |
| 148 const char *create_media_uploads = | |
| 149 "CREATE TABLE IF NOT EXISTS media_uploads (" | |
| 150 " id INTEGER PRIMARY KEY AUTOINCREMENT," | |
| 151 " access_token TEXT NOT NULL," | |
| 152 " original_filename TEXT NOT NULL," | |
| 153 " content_type TEXT NOT NULL," | |
| 154 " s3_key_original TEXT NOT NULL," | |
| 155 " s3_key_processed TEXT," | |
| 156 " file_size INTEGER," | |
| 157 " status TEXT NOT NULL DEFAULT 'pending'," | |
| 158 " error_message TEXT," | |
| 159 " created_at INTEGER DEFAULT (strftime('%s', 'now'))," | |
| 160 " updated_at INTEGER DEFAULT (strftime('%s', 'now'))" | |
| 161 ")"; | |
| 162 | |
| 163 result = Deita_Query_Execute_Update(g_db_connection, create_media_uploads); | |
| 164 if (result < 0) | |
| 165 { | |
| 166 printf("[DB] ERROR: Failed to create media_uploads table\n"); | |
| 167 } | |
| 168 | |
| 169 // Create indices for media_uploads | |
| 170 const char *create_status_idx = | |
| 171 "CREATE INDEX IF NOT EXISTS idx_media_uploads_status ON media_uploads(status)"; | |
| 172 result = Deita_Query_Execute_Update(g_db_connection, create_status_idx); | |
| 173 if (result < 0) | |
| 174 { | |
| 175 printf("[DB] ERROR: Failed to create status index\n"); | |
| 176 } | |
| 177 | |
| 178 const char *create_token_status_idx = | |
| 179 "CREATE INDEX IF NOT EXISTS idx_media_uploads_token_status " | |
| 180 "ON media_uploads(access_token, status)"; | |
| 181 result = Deita_Query_Execute_Update(g_db_connection, create_token_status_idx); | |
| 182 if (result < 0) | |
| 183 { | |
| 184 printf("[DB] ERROR: Failed to create token_status index\n"); | |
| 185 } | |
| 186 else | |
| 187 { | |
| 188 printf("[DB] Initialized: %s\n", g_db_path); | |
| 189 } | |
|
200
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
190 } |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
191 |
|
72
4532ce6d9eb8
[Seobeo] Added router to the server logic. Few dowa string manipulation logics.
June Park <parkjune1995@gmail.com>
parents:
36
diff
changeset
|
192 void handle_sigint(int sig) |
|
4532ce6d9eb8
[Seobeo] Added router to the server logic. Few dowa string manipulation logics.
June Park <parkjune1995@gmail.com>
parents:
36
diff
changeset
|
193 { |
|
36
84672efec192
[Zenbu] WIP fixing issues regarding to using edge only. I think there is a problem where socket closes before sending back the info.
MrJuneJune <me@mrjunejune.com>
parents:
19
diff
changeset
|
194 printf("Failed\n"); |
|
84672efec192
[Zenbu] WIP fixing issues regarding to using edge only. I think there is a problem where socket closes before sending back the info.
MrJuneJune <me@mrjunejune.com>
parents:
19
diff
changeset
|
195 stop_server = 1; |
|
84672efec192
[Zenbu] WIP fixing issues regarding to using edge only. I think there is a problem where socket closes before sending back the info.
MrJuneJune <me@mrjunejune.com>
parents:
19
diff
changeset
|
196 } |
|
7
114cad94008f
[Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
197 |
|
158
1c0878eb17de
[MrJuneJune] Readme file gets compiled in server side.
June Park <parkjune1995@gmail.com>
parents:
132
diff
changeset
|
198 void Seobeo_Render_Html( |
| 78 | 199 char *final_body, |
|
158
1c0878eb17de
[MrJuneJune] Readme file gets compiled in server side.
June Park <parkjune1995@gmail.com>
parents:
132
diff
changeset
|
200 char *template, |
| 78 | 201 Dowa_Arena *arena |
|
158
1c0878eb17de
[MrJuneJune] Readme file gets compiled in server side.
June Park <parkjune1995@gmail.com>
parents:
132
diff
changeset
|
202 ) |
|
1c0878eb17de
[MrJuneJune] Readme file gets compiled in server side.
June Park <parkjune1995@gmail.com>
parents:
132
diff
changeset
|
203 { |
| 78 | 204 size_t current_offset = 0; |
| 205 char *cursor = template; | |
| 206 | |
| 207 int32 token_len = 2; | |
| 208 | |
| 96 | 209 while (1) |
| 78 | 210 { |
| 211 char *start_tag = strstr(cursor, "{{"); | |
| 212 if (!start_tag) break; | |
| 213 | |
| 214 char *end_tag = strstr(start_tag, "}}"); | |
| 215 if (!end_tag) break; | |
| 216 | |
| 201 | 217 Seobeo_Log(SEOBEO_INFO, "[Curr] Life\n"); |
| 218 | |
| 78 | 219 size_t leading_len = start_tag - cursor; |
| 220 memcpy(final_body + current_offset, cursor, leading_len); | |
| 221 current_offset += leading_len; | |
| 222 | |
| 223 size_t name_len = end_tag - (start_tag + token_len); | |
| 224 char *include_name = Dowa_Arena_Allocate(arena, name_len + 1); | |
| 225 memcpy(include_name, start_tag + token_len, name_len); | |
| 226 include_name[name_len] = '\0'; | |
| 227 | |
| 228 size_t sub_file_size = 0; | |
| 229 char *sub_content = Seobeo_Web_LoadFile(include_name, &sub_file_size); | |
| 201 | 230 Seobeo_Log(SEOBEO_DEBUG, "[TEMPLATE] Loading include: '%s' -> %s (size=%zu)\n", |
| 231 include_name, sub_content ? "OK" : "FAILED", sub_file_size); | |
| 78 | 232 if (sub_content) |
| 233 { | |
| 234 memcpy(final_body + current_offset, sub_content, sub_file_size); | |
| 235 current_offset += sub_file_size; | |
| 236 free(sub_content); | |
| 237 } | |
| 238 | |
| 239 cursor = end_tag + 2; | |
| 240 } | |
| 241 strcpy(final_body + current_offset, cursor); | |
|
158
1c0878eb17de
[MrJuneJune] Readme file gets compiled in server side.
June Park <parkjune1995@gmail.com>
parents:
132
diff
changeset
|
242 } |
|
1c0878eb17de
[MrJuneJune] Readme file gets compiled in server side.
June Park <parkjune1995@gmail.com>
parents:
132
diff
changeset
|
243 |
|
1c0878eb17de
[MrJuneJune] Readme file gets compiled in server side.
June Park <parkjune1995@gmail.com>
parents:
132
diff
changeset
|
244 void Seobeo_Render_Html_FilePath( |
|
1c0878eb17de
[MrJuneJune] Readme file gets compiled in server side.
June Park <parkjune1995@gmail.com>
parents:
132
diff
changeset
|
245 char *final_body, |
|
1c0878eb17de
[MrJuneJune] Readme file gets compiled in server side.
June Park <parkjune1995@gmail.com>
parents:
132
diff
changeset
|
246 char *path, |
|
1c0878eb17de
[MrJuneJune] Readme file gets compiled in server side.
June Park <parkjune1995@gmail.com>
parents:
132
diff
changeset
|
247 Dowa_Arena *arena |
|
1c0878eb17de
[MrJuneJune] Readme file gets compiled in server side.
June Park <parkjune1995@gmail.com>
parents:
132
diff
changeset
|
248 ) { |
| 201 | 249 Seobeo_Log(SEOBEO_DEBUG, "[TEMPLATE] Loading main template: '%s'\n", path); |
|
158
1c0878eb17de
[MrJuneJune] Readme file gets compiled in server side.
June Park <parkjune1995@gmail.com>
parents:
132
diff
changeset
|
250 size_t html_size = 0; |
|
1c0878eb17de
[MrJuneJune] Readme file gets compiled in server side.
June Park <parkjune1995@gmail.com>
parents:
132
diff
changeset
|
251 char *template = Seobeo_Web_LoadFile(path, &html_size); |
| 201 | 252 Seobeo_Log(SEOBEO_DEBUG, "[TEMPLATE] Main template loaded: %s (size=%zu)\n", template ? "OK" : "FAILED", html_size); |
|
158
1c0878eb17de
[MrJuneJune] Readme file gets compiled in server side.
June Park <parkjune1995@gmail.com>
parents:
132
diff
changeset
|
253 if (!template) return; |
|
1c0878eb17de
[MrJuneJune] Readme file gets compiled in server side.
June Park <parkjune1995@gmail.com>
parents:
132
diff
changeset
|
254 Seobeo_Render_Html(final_body, template, arena); |
| 78 | 255 } |
| 256 | |
|
77
c348ac875294
[Seobeo] Server side rendering.
June Park <parkjune1995@gmail.com>
parents:
72
diff
changeset
|
257 Seobeo_Request_Entry* GetHomePage(Seobeo_Request_Entry *req, Dowa_Arena *arena) |
|
c348ac875294
[Seobeo] Server side rendering.
June Park <parkjune1995@gmail.com>
parents:
72
diff
changeset
|
258 { |
| 78 | 259 Seobeo_Request_Entry *resp = NULL; |
|
80
d55157451947
[MrJuneJune] Updating my homepage.
June Park <parkjune1995@gmail.com>
parents:
79
diff
changeset
|
260 char *final_body = Dowa_Arena_Allocate(arena, 50 * 1024); |
|
158
1c0878eb17de
[MrJuneJune] Readme file gets compiled in server side.
June Park <parkjune1995@gmail.com>
parents:
132
diff
changeset
|
261 Seobeo_Render_Html_FilePath(final_body, "/index.html", arena); |
| 78 | 262 Dowa_HashMap_Push_Arena(resp, "body", final_body, arena); |
| 263 return resp; | |
| 264 } | |
|
77
c348ac875294
[Seobeo] Server side rendering.
June Park <parkjune1995@gmail.com>
parents:
72
diff
changeset
|
265 |
| 78 | 266 Seobeo_Request_Entry* GetResume(Seobeo_Request_Entry *req, Dowa_Arena *arena) |
| 267 { | |
| 268 Seobeo_Request_Entry *resp = NULL; | |
| 269 char *final_body = Dowa_Arena_Allocate(arena, 50 * 1024); | |
|
158
1c0878eb17de
[MrJuneJune] Readme file gets compiled in server side.
June Park <parkjune1995@gmail.com>
parents:
132
diff
changeset
|
270 Seobeo_Render_Html_FilePath(final_body, "/resume/index.html", arena); |
| 78 | 271 Dowa_HashMap_Push_Arena(resp, "body", final_body, arena); |
| 272 return resp; | |
| 273 } | |
| 274 | |
| 275 Seobeo_Request_Entry* GetTools(Seobeo_Request_Entry *req, Dowa_Arena *arena) | |
| 276 { | |
| 277 Seobeo_Request_Entry *resp = NULL; | |
| 278 char *final_body = Dowa_Arena_Allocate(arena, 50 * 1024); | |
|
158
1c0878eb17de
[MrJuneJune] Readme file gets compiled in server side.
June Park <parkjune1995@gmail.com>
parents:
132
diff
changeset
|
279 Seobeo_Render_Html_FilePath(final_body, "/tools/index.html", arena); |
| 78 | 280 Dowa_HashMap_Push_Arena(resp, "body", final_body, arena); |
|
77
c348ac875294
[Seobeo] Server side rendering.
June Park <parkjune1995@gmail.com>
parents:
72
diff
changeset
|
281 return resp; |
|
c348ac875294
[Seobeo] Server side rendering.
June Park <parkjune1995@gmail.com>
parents:
72
diff
changeset
|
282 } |
|
c348ac875294
[Seobeo] Server side rendering.
June Park <parkjune1995@gmail.com>
parents:
72
diff
changeset
|
283 |
|
c348ac875294
[Seobeo] Server side rendering.
June Park <parkjune1995@gmail.com>
parents:
72
diff
changeset
|
284 |
| 78 | 285 Seobeo_Request_Entry* GetMDToHTML(Seobeo_Request_Entry *req, Dowa_Arena *arena) |
| 286 { | |
|
92
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
287 Seobeo_Request_Entry *resp = NULL; |
| 78 | 288 char *final_body = Dowa_Arena_Allocate(arena, 50 * 1024); |
|
158
1c0878eb17de
[MrJuneJune] Readme file gets compiled in server side.
June Park <parkjune1995@gmail.com>
parents:
132
diff
changeset
|
289 Seobeo_Render_Html_FilePath(final_body, "/tools/markdown_to_html/index.html", arena); |
| 78 | 290 Dowa_HashMap_Push_Arena(resp, "body", final_body, arena); |
| 291 return resp; | |
| 292 } | |
|
77
c348ac875294
[Seobeo] Server side rendering.
June Park <parkjune1995@gmail.com>
parents:
72
diff
changeset
|
293 |
|
92
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
294 Seobeo_Request_Entry* GetFileConverter(Seobeo_Request_Entry *req, Dowa_Arena *arena) |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
295 { |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
296 Seobeo_Request_Entry *resp = NULL; |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
297 char *final_body = Dowa_Arena_Allocate(arena, 50 * 1024); |
|
158
1c0878eb17de
[MrJuneJune] Readme file gets compiled in server side.
June Park <parkjune1995@gmail.com>
parents:
132
diff
changeset
|
298 Seobeo_Render_Html_FilePath(final_body, "/tools/file_converter/index.html", arena); |
|
92
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
299 Dowa_HashMap_Push_Arena(resp, "body", final_body, arena); |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
300 return resp; |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
301 } |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
302 |
|
242
543df0fe7168
[tools] Add full HLS player support
MrJuneJune <me@mrjunejune.com>
parents:
241
diff
changeset
|
303 Seobeo_Request_Entry* GetHlsPlayer(Seobeo_Request_Entry *req, Dowa_Arena *arena) |
|
543df0fe7168
[tools] Add full HLS player support
MrJuneJune <me@mrjunejune.com>
parents:
241
diff
changeset
|
304 { |
|
543df0fe7168
[tools] Add full HLS player support
MrJuneJune <me@mrjunejune.com>
parents:
241
diff
changeset
|
305 Seobeo_Request_Entry *resp = NULL; |
|
543df0fe7168
[tools] Add full HLS player support
MrJuneJune <me@mrjunejune.com>
parents:
241
diff
changeset
|
306 char *final_body = Dowa_Arena_Allocate(arena, 50 * 1024); |
|
543df0fe7168
[tools] Add full HLS player support
MrJuneJune <me@mrjunejune.com>
parents:
241
diff
changeset
|
307 Seobeo_Render_Html_FilePath(final_body, "/tools/hls_player/index.html", arena); |
|
543df0fe7168
[tools] Add full HLS player support
MrJuneJune <me@mrjunejune.com>
parents:
241
diff
changeset
|
308 Dowa_HashMap_Push_Arena(resp, "body", final_body, arena); |
|
543df0fe7168
[tools] Add full HLS player support
MrJuneJune <me@mrjunejune.com>
parents:
241
diff
changeset
|
309 return resp; |
|
543df0fe7168
[tools] Add full HLS player support
MrJuneJune <me@mrjunejune.com>
parents:
241
diff
changeset
|
310 } |
|
543df0fe7168
[tools] Add full HLS player support
MrJuneJune <me@mrjunejune.com>
parents:
241
diff
changeset
|
311 |
|
244
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
312 Seobeo_Request_Entry* GetLatexEditor(Seobeo_Request_Entry *req, Dowa_Arena *arena) |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
313 { |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
314 Seobeo_Request_Entry *resp = NULL; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
315 char *final_body = Dowa_Arena_Allocate(arena, 50 * 1024); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
316 Seobeo_Render_Html_FilePath(final_body, "/tools/latex_editor/index.html", arena); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
317 Dowa_HashMap_Push_Arena(resp, "body", final_body, arena); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
318 return resp; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
319 } |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
320 |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
321 static Seobeo_Request_Entry *LatexErrorResponse( |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
322 Dowa_Arena *arena, |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
323 int status, |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
324 const char *message) |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
325 { |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
326 Seobeo_Request_Entry *resp = NULL; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
327 char *status_value = Dowa_Arena_Allocate(arena, 16); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
328 snprintf(status_value, 16, "%d", status); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
329 char *body = Dowa_Arena_Allocate(arena, strlen(message) + 1); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
330 strcpy(body, message); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
331 Dowa_HashMap_Push_Arena(resp, "status", status_value, arena); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
332 Dowa_HashMap_Push_Arena(resp, "content-type", "text/plain; charset=utf-8", arena); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
333 Dowa_HashMap_Push_Arena(resp, "cache-control", "no-store", arena); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
334 Dowa_HashMap_Push_Arena(resp, "body", body, arena); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
335 return resp; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
336 } |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
337 |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
338 Seobeo_Request_Entry *RenderLatexPdf(Seobeo_Request_Entry *req, Dowa_Arena *arena) |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
339 { |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
340 void *body_kv = Dowa_HashMap_Get_Ptr(req, "Body"); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
341 void *length_kv = Dowa_HashMap_Get_Ptr(req, "Content-Length"); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
342 if (!body_kv || !length_kv) |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
343 return LatexErrorResponse(arena, 400, "LaTeX source and Content-Length are required."); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
344 |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
345 const char *length_value = ((Seobeo_Request_Entry *)length_kv)->value; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
346 char *length_end = NULL; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
347 errno = 0; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
348 unsigned long long parsed_length = strtoull(length_value, &length_end, 10); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
349 if (errno != 0 || |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
350 length_end == length_value || |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
351 *length_end != '\0' || |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
352 parsed_length == 0) |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
353 return LatexErrorResponse(arena, 400, "Content-Length is invalid."); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
354 if (parsed_length > LATEX_SOURCE_MAX_BYTES) |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
355 return LatexErrorResponse(arena, 413, "LaTeX source exceeds the 64 KiB limit."); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
356 |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
357 boolean expected = FALSE; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
358 if (!atomic_compare_exchange_strong( |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
359 &g_latex_rendering, |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
360 &expected, |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
361 TRUE)) |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
362 return LatexErrorResponse(arena, 429, "The LaTeX compiler is busy. Try again shortly."); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
363 |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
364 Latex_Render_Result render = Latex_Render( |
|
248
b8b6e726964a
[style] Use Dowa type aliases in C
MrJuneJune <me@mrjunejune.com>
parents:
244
diff
changeset
|
365 (const uint8 *)((Seobeo_Request_Entry *)body_kv)->value, |
|
244
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
366 (size_t)parsed_length); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
367 atomic_store(&g_latex_rendering, FALSE); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
368 if (render.status != LATEX_RENDER_OK) |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
369 { |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
370 int status = 500; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
371 if (render.status == LATEX_RENDER_INVALID_INPUT) |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
372 status = 400; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
373 else if (render.status == LATEX_RENDER_COMPILE_ERROR) |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
374 status = 422; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
375 else if (render.status == LATEX_RENDER_TIMEOUT) |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
376 status = 504; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
377 else if (render.status == LATEX_RENDER_COMPILER_UNAVAILABLE) |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
378 status = 503; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
379 |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
380 const char *diagnostics = render.diagnostics |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
381 ? render.diagnostics |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
382 : "LaTeX rendering failed."; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
383 char *body = Dowa_Arena_Allocate(arena, strlen(diagnostics) + 1); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
384 strcpy(body, diagnostics); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
385 Latex_Render_Result_Destroy(&render); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
386 return LatexErrorResponse(arena, status, body); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
387 } |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
388 |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
389 char *pdf = Dowa_Arena_Allocate(arena, render.pdf_size + 1); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
390 if (!pdf) |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
391 { |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
392 Latex_Render_Result_Destroy(&render); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
393 return LatexErrorResponse(arena, 500, "The generated PDF is too large to return."); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
394 } |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
395 memcpy(pdf, render.pdf_data, render.pdf_size); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
396 pdf[render.pdf_size] = '\0'; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
397 char *content_length = Dowa_Arena_Allocate(arena, 32); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
398 snprintf(content_length, 32, "%zu", render.pdf_size); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
399 Latex_Render_Result_Destroy(&render); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
400 |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
401 Seobeo_Request_Entry *resp = NULL; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
402 Dowa_HashMap_Push_Arena(resp, "status", "200", arena); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
403 Dowa_HashMap_Push_Arena(resp, "content-type", "application/pdf", arena); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
404 Dowa_HashMap_Push_Arena(resp, "content-length", content_length, arena); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
405 Dowa_HashMap_Push_Arena(resp, "content-disposition", "inline; filename=\"document.pdf\"", arena); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
406 Dowa_HashMap_Push_Arena(resp, "cache-control", "no-store", arena); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
407 Dowa_HashMap_Push_Arena(resp, "x-content-type-options", "nosniff", arena); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
408 Dowa_HashMap_Push_Arena(resp, "body", pdf, arena); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
409 return resp; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
410 } |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
411 |
|
250
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
412 // Joinable worker function for local image conversion. |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
413 void Simple_WebpConverter_Background(void *arg) |
|
216
e82b80b24012
[MrJuneJune] Make webp translate background job.
June Park <parkjune1995@gmail.com>
parents:
206
diff
changeset
|
414 { |
|
e82b80b24012
[MrJuneJune] Make webp translate background job.
June Park <parkjune1995@gmail.com>
parents:
206
diff
changeset
|
415 File_Converter_Config *configuration = (File_Converter_Config *)arg; |
|
e82b80b24012
[MrJuneJune] Make webp translate background job.
June Park <parkjune1995@gmail.com>
parents:
206
diff
changeset
|
416 |
|
e82b80b24012
[MrJuneJune] Make webp translate background job.
June Park <parkjune1995@gmail.com>
parents:
206
diff
changeset
|
417 char cmd[1024]; |
|
e82b80b24012
[MrJuneJune] Make webp translate background job.
June Park <parkjune1995@gmail.com>
parents:
206
diff
changeset
|
418 snprintf(cmd, sizeof(cmd), "ffmpeg -y -i %s -quality 80 %s 2>/tmp/error_log", |
|
e82b80b24012
[MrJuneJune] Make webp translate background job.
June Park <parkjune1995@gmail.com>
parents:
206
diff
changeset
|
419 configuration->input_path, configuration->output_path); |
|
e82b80b24012
[MrJuneJune] Make webp translate background job.
June Park <parkjune1995@gmail.com>
parents:
206
diff
changeset
|
420 Seobeo_Log(SEOBEO_INFO, "[MEDIA] Running FFmpeg: %s\n", cmd); |
|
250
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
421 configuration->result = system(cmd); |
|
216
e82b80b24012
[MrJuneJune] Make webp translate background job.
June Park <parkjune1995@gmail.com>
parents:
206
diff
changeset
|
422 |
|
250
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
423 Seobeo_Log( |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
424 SEOBEO_INFO, |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
425 "[MEDIA] FFmpeg result: %d\n", |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
426 configuration->result); |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
427 if (configuration->result != 0) |
|
216
e82b80b24012
[MrJuneJune] Make webp translate background job.
June Park <parkjune1995@gmail.com>
parents:
206
diff
changeset
|
428 { |
|
e82b80b24012
[MrJuneJune] Make webp translate background job.
June Park <parkjune1995@gmail.com>
parents:
206
diff
changeset
|
429 Seobeo_Log(SEOBEO_ERROR, "[MEDIA] ERROR: FFmpeg conversion failed\n"); |
|
250
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
430 return; |
|
216
e82b80b24012
[MrJuneJune] Make webp translate background job.
June Park <parkjune1995@gmail.com>
parents:
206
diff
changeset
|
431 } |
|
250
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
432 Seobeo_Log( |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
433 SEOBEO_INFO, |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
434 "[MEDIA] Successfully converted to webp: %s\n", |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
435 configuration->output_path); |
|
216
e82b80b24012
[MrJuneJune] Make webp translate background job.
June Park <parkjune1995@gmail.com>
parents:
206
diff
changeset
|
436 } |
|
e82b80b24012
[MrJuneJune] Make webp translate background job.
June Park <parkjune1995@gmail.com>
parents:
206
diff
changeset
|
437 |
|
92
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
438 Seobeo_Request_Entry *ConvertImageToWebP(Seobeo_Request_Entry *req, Dowa_Arena *arena) |
|
72
4532ce6d9eb8
[Seobeo] Added router to the server logic. Few dowa string manipulation logics.
June Park <parkjune1995@gmail.com>
parents:
36
diff
changeset
|
439 { |
|
92
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
440 Seobeo_Request_Entry *resp = NULL; |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
441 |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
442 if (!req) |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
443 { |
| 201 | 444 Seobeo_Log(SEOBEO_ERROR, "Request is NULL\n"); |
|
92
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
445 char *error_msg = "Internal error: no request data"; |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
446 Dowa_HashMap_Push_Arena(resp, "status", "500", arena); |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
447 Dowa_HashMap_Push_Arena(resp, "content-type", "text/plain", arena); |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
448 Dowa_HashMap_Push_Arena(resp, "body", error_msg, arena); |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
449 return resp; |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
450 } |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
451 |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
452 size_t req_length = Dowa_Array_Length(req); |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
453 printf("Request has %zu entries\n", req_length); |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
454 |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
455 for (size_t i = 0; i < req_length; i++) |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
456 { |
| 201 | 457 Seobeo_Log(SEOBEO_INFO, " Key[%zu]: '%s'\n", i, req[i].key); |
|
92
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
458 } |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
459 |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
460 void *body_kv = Dowa_HashMap_Get_Ptr(req, "Body"); |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
461 if (!body_kv) |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
462 { |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
463 printf("ERROR: No 'Body' key found in request\n"); |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
464 |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
465 char *error_msg = "No file data provided"; |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
466 Dowa_HashMap_Push_Arena(resp, "status", "400", arena); |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
467 Dowa_HashMap_Push_Arena(resp, "content-type", "text/plain", arena); |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
468 Dowa_HashMap_Push_Arena(resp, "body", error_msg, arena); |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
469 return resp; |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
470 } |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
471 |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
472 void *cl_kv = Dowa_HashMap_Get_Ptr(req, "Content-Length"); |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
473 if (!cl_kv) |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
474 { |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
475 char *error_msg = "No Content-Length header"; |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
476 Dowa_HashMap_Push_Arena(resp, "status", "400", arena); |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
477 Dowa_HashMap_Push_Arena(resp, "content-type", "text/plain", arena); |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
478 Dowa_HashMap_Push_Arena(resp, "body", error_msg, arena); |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
479 return resp; |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
480 } |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
481 |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
482 const char *file_data = ((Seobeo_Request_Entry*)body_kv)->value; |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
483 const char *content_length_str = ((Seobeo_Request_Entry*)cl_kv)->value; |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
484 size_t file_size = atoi(content_length_str); |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
485 |
|
216
e82b80b24012
[MrJuneJune] Make webp translate background job.
June Park <parkjune1995@gmail.com>
parents:
206
diff
changeset
|
486 Seobeo_Log(SEOBEO_DEBUG, "Converting image, file_size=%zu bytes\n", file_size); |
|
92
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
487 |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
488 int open_flags = O_RDWR | O_CREAT | O_EXCL; |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
489 |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
490 char *uuid4 = (char *)Dowa_Arena_Allocate(arena, UUID_LEN); |
|
250
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
491 uint32 seed = |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
492 (uint32)time(NULL) ^ |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
493 (uint32)Seobeo_Thread_Current_Id() ^ |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
494 counter++; |
|
92
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
495 Dowa_String_UUID(seed, uuid4); |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
496 char *input_path = Dowa_Arena_Allocate(arena, TMP_FILE_LENGTH);; |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
497 snprintf(input_path, TMP_FILE_LENGTH, "/tmp/%s", uuid4); |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
498 int input_fd = open(input_path, open_flags, 0600); |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
499 if (input_fd == -1) |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
500 { |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
501 char *error_msg = "Failed to create temporary file"; |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
502 Dowa_HashMap_Push_Arena(resp, "status", "500", arena); |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
503 Dowa_HashMap_Push_Arena(resp, "content-type", "text/plain", arena); |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
504 Dowa_HashMap_Push_Arena(resp, "body", error_msg, arena); |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
505 return resp; |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
506 } |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
507 write(input_fd, file_data, file_size); |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
508 close(input_fd); |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
509 |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
510 |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
511 uuid4 = (char *)Dowa_Arena_Allocate(arena, UUID_LEN); |
|
250
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
512 seed = |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
513 (uint32)time(NULL) ^ |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
514 (uint32)Seobeo_Thread_Current_Id() ^ |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
515 counter++; |
|
92
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
516 Dowa_String_UUID(seed, uuid4); |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
517 char *output_path = (char *)Dowa_Arena_Allocate(arena, TMP_FILE_LENGTH);; |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
518 snprintf(output_path, TMP_FILE_LENGTH, "/tmp/%s.webp", uuid4); |
|
216
e82b80b24012
[MrJuneJune] Make webp translate background job.
June Park <parkjune1995@gmail.com>
parents:
206
diff
changeset
|
519 Seobeo_Log(SEOBEO_DEBUG, "output_path %s\n", output_path); |
|
e82b80b24012
[MrJuneJune] Make webp translate background job.
June Park <parkjune1995@gmail.com>
parents:
206
diff
changeset
|
520 Seobeo_Log(SEOBEO_DEBUG, "open_flags: 0x%x\n", open_flags); |
|
e82b80b24012
[MrJuneJune] Make webp translate background job.
June Park <parkjune1995@gmail.com>
parents:
206
diff
changeset
|
521 Seobeo_Log(SEOBEO_DEBUG, "input_path: %s\n", input_path); |
|
92
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
522 int output_fd = open(output_path, open_flags, 0600); |
|
216
e82b80b24012
[MrJuneJune] Make webp translate background job.
June Park <parkjune1995@gmail.com>
parents:
206
diff
changeset
|
523 Seobeo_Log(SEOBEO_DEBUG, "output_fd: %d\n", output_fd); |
|
92
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
524 if (output_fd == -1) |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
525 { |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
526 unlink(input_path); |
|
216
e82b80b24012
[MrJuneJune] Make webp translate background job.
June Park <parkjune1995@gmail.com>
parents:
206
diff
changeset
|
527 Seobeo_Log(SEOBEO_DEBUG, "errno: %d (%s)\n", errno, strerror(errno)); |
|
92
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
528 char *error_msg = "Failed to create output file"; |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
529 Dowa_HashMap_Push_Arena(resp, "status", "500", arena); |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
530 Dowa_HashMap_Push_Arena(resp, "content-type", "text/plain", arena); |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
531 Dowa_HashMap_Push_Arena(resp, "body", error_msg, arena); |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
532 return resp; |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
533 } |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
534 close(output_fd); |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
535 |
|
216
e82b80b24012
[MrJuneJune] Make webp translate background job.
June Park <parkjune1995@gmail.com>
parents:
206
diff
changeset
|
536 File_Converter_Config *configuration = Dowa_Arena_Allocate(arena, sizeof(File_Converter_Config)); |
|
e82b80b24012
[MrJuneJune] Make webp translate background job.
June Park <parkjune1995@gmail.com>
parents:
206
diff
changeset
|
537 configuration->input_path = input_path; |
|
e82b80b24012
[MrJuneJune] Make webp translate background job.
June Park <parkjune1995@gmail.com>
parents:
206
diff
changeset
|
538 configuration->output_path = output_path; |
|
250
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
539 configuration->result = -1; |
|
216
e82b80b24012
[MrJuneJune] Make webp translate background job.
June Park <parkjune1995@gmail.com>
parents:
206
diff
changeset
|
540 |
|
250
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
541 Seobeo_Thread *p_worker = Seobeo_Thread_Start( |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
542 Simple_WebpConverter_Background, |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
543 configuration, |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
544 NULL); |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
545 if (!p_worker || |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
546 Seobeo_Thread_Join(p_worker) != SEOBEO_WORKER_OK || |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
547 configuration->result != 0) |
|
92
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
548 { |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
549 unlink(input_path); |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
550 unlink(output_path); |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
551 char *error_msg = "FFmpeg conversion failed"; |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
552 Dowa_HashMap_Push_Arena(resp, "status", "500", arena); |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
553 Dowa_HashMap_Push_Arena(resp, "content-type", "text/plain", arena); |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
554 Dowa_HashMap_Push_Arena(resp, "body", error_msg, arena); |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
555 return resp; |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
556 } |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
557 |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
558 FILE *out_file = fopen(output_path, "rb"); |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
559 if (!out_file) |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
560 { |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
561 unlink(input_path); |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
562 unlink(output_path); |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
563 char *error_msg = "Failed to read converted file"; |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
564 Dowa_HashMap_Push_Arena(resp, "status", "500", arena); |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
565 Dowa_HashMap_Push_Arena(resp, "content-type", "text/plain", arena); |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
566 Dowa_HashMap_Push_Arena(resp, "body", error_msg, arena); |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
567 return resp; |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
568 } |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
569 fclose(out_file); |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
570 |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
571 unlink(input_path); |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
572 char *filename = strrchr(output_path, '/') + 1; |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
573 char *response_body = Dowa_Arena_Allocate(arena, 512); |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
574 snprintf(response_body, 512, |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
575 "{\"success\":true,\"download_url\":\"/api/download/%s\",\"expires\":\"10 minutes\"}", |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
576 filename); |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
577 Dowa_HashMap_Push_Arena(resp, "status", "200", arena); |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
578 Dowa_HashMap_Push_Arena(resp, "content-type", "application/json", arena); |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
579 Dowa_HashMap_Push_Arena(resp, "body", response_body, arena); |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
580 |
|
216
e82b80b24012
[MrJuneJune] Make webp translate background job.
June Park <parkjune1995@gmail.com>
parents:
206
diff
changeset
|
581 Seobeo_Log(SEOBEO_DEBUG, "Image converted, available at /api/download/%s\n", filename); |
|
92
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
582 |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
583 return resp; |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
584 } |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
585 |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
586 Seobeo_Request_Entry *ConvertVideoToMP4(Seobeo_Request_Entry *req, Dowa_Arena *arena) |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
587 { |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
588 Seobeo_Request_Entry *resp = NULL; |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
589 |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
590 void *body_kv = Dowa_HashMap_Get_Ptr(req, "Body"); |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
591 if (!body_kv) |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
592 { |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
593 char *error_msg = "No file data provided"; |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
594 Dowa_HashMap_Push_Arena(resp, "status", "400", arena); |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
595 Dowa_HashMap_Push_Arena(resp, "content-type", "text/plain", arena); |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
596 Dowa_HashMap_Push_Arena(resp, "body", error_msg, arena); |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
597 return resp; |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
598 } |
|
72
4532ce6d9eb8
[Seobeo] Added router to the server logic. Few dowa string manipulation logics.
June Park <parkjune1995@gmail.com>
parents:
36
diff
changeset
|
599 |
|
92
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
600 // Get Content-Length to know the actual binary size |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
601 void *cl_kv = Dowa_HashMap_Get_Ptr(req, "Content-Length"); |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
602 if (!cl_kv) |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
603 { |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
604 char *error_msg = "No Content-Length header"; |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
605 Dowa_HashMap_Push_Arena(resp, "status", "400", arena); |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
606 Dowa_HashMap_Push_Arena(resp, "content-type", "text/plain", arena); |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
607 Dowa_HashMap_Push_Arena(resp, "body", error_msg, arena); |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
608 return resp; |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
609 } |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
610 |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
611 const char *file_data = ((Seobeo_Request_Entry*)body_kv)->value; |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
612 const char *content_length_str = ((Seobeo_Request_Entry*)cl_kv)->value; |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
613 size_t file_size = atoi(content_length_str); |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
614 |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
615 printf("DEBUG: Converting video, file_size=%zu bytes\n", file_size); |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
616 |
|
168
f3084bca7317
[Misc] Fixed all errors and all tests should pass now.
MrJuneJune <me@mrjunejune.com>
parents:
158
diff
changeset
|
617 int open_flags = O_RDWR | O_CREAT | O_EXCL; |
|
f3084bca7317
[Misc] Fixed all errors and all tests should pass now.
MrJuneJune <me@mrjunejune.com>
parents:
158
diff
changeset
|
618 |
|
92
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
619 char *uuid4 = (char *)Dowa_Arena_Allocate(arena, UUID_LEN); |
|
250
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
620 uint32 seed = |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
621 (uint32)time(NULL) ^ |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
622 (uint32)Seobeo_Thread_Current_Id() ^ |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
623 counter++; |
|
168
f3084bca7317
[Misc] Fixed all errors and all tests should pass now.
MrJuneJune <me@mrjunejune.com>
parents:
158
diff
changeset
|
624 Dowa_String_UUID(seed, uuid4); |
|
92
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
625 char *input_path = Dowa_Arena_Allocate(arena, TMP_FILE_LENGTH); |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
626 snprintf(input_path, TMP_FILE_LENGTH, "/tmp/%s", uuid4); |
|
168
f3084bca7317
[Misc] Fixed all errors and all tests should pass now.
MrJuneJune <me@mrjunejune.com>
parents:
158
diff
changeset
|
627 Seobeo_Log(SEOBEO_DEBUG, "Input path: %s\n", input_path); |
|
f3084bca7317
[Misc] Fixed all errors and all tests should pass now.
MrJuneJune <me@mrjunejune.com>
parents:
158
diff
changeset
|
628 |
|
f3084bca7317
[Misc] Fixed all errors and all tests should pass now.
MrJuneJune <me@mrjunejune.com>
parents:
158
diff
changeset
|
629 int input_fd = open(input_path, open_flags, 0600); |
|
92
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
630 if (input_fd == -1) |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
631 { |
|
168
f3084bca7317
[Misc] Fixed all errors and all tests should pass now.
MrJuneJune <me@mrjunejune.com>
parents:
158
diff
changeset
|
632 Seobeo_Log(SEOBEO_DEBUG, "errno: %d (%s)\n", errno, strerror(errno)); |
|
92
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
633 char *error_msg = "Failed to create temporary file"; |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
634 Dowa_HashMap_Push_Arena(resp, "status", "500", arena); |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
635 Dowa_HashMap_Push_Arena(resp, "content-type", "text/plain", arena); |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
636 Dowa_HashMap_Push_Arena(resp, "body", error_msg, arena); |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
637 return resp; |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
638 } |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
639 |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
640 write(input_fd, file_data, file_size); |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
641 close(input_fd); |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
642 |
|
250
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
643 seed = |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
644 (uint32)time(NULL) ^ |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
645 (uint32)Seobeo_Thread_Current_Id() ^ |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
646 counter++; |
|
92
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
647 Dowa_String_UUID(seed, uuid4); |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
648 char *output_path = (char *)Dowa_Arena_Allocate(arena, TMP_FILE_LENGTH);; |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
649 snprintf(output_path, TMP_FILE_LENGTH, "/tmp/%s.mp4", uuid4); |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
650 int output_fd = open(output_path, open_flags, 0600); |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
651 if (output_fd == -1) |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
652 { |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
653 unlink(input_path); |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
654 char *error_msg = "Failed to create output file"; |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
655 Dowa_HashMap_Push_Arena(resp, "status", "500", arena); |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
656 Dowa_HashMap_Push_Arena(resp, "content-type", "text/plain", arena); |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
657 Dowa_HashMap_Push_Arena(resp, "body", error_msg, arena); |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
658 return resp; |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
659 } |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
660 close(output_fd); |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
661 |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
662 char cmd[512]; |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
663 snprintf(cmd, sizeof(cmd), |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
664 "ffmpeg -y -i %s -c:v libx264 -preset fast -crf 23 -c:a aac %s 2>/tmp/error_log", |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
665 input_path, output_path); |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
666 int result = system(cmd); |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
667 if (result != 0) |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
668 { |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
669 unlink(input_path); |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
670 unlink(output_path); |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
671 char *error_msg = "FFmpeg conversion failed"; |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
672 Dowa_HashMap_Push_Arena(resp, "status", "500", arena); |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
673 Dowa_HashMap_Push_Arena(resp, "content-type", "text/plain", arena); |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
674 Dowa_HashMap_Push_Arena(resp, "body", error_msg, arena); |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
675 return resp; |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
676 } |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
677 |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
678 unlink(input_path); |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
679 char *filename = strrchr(output_path, '/') + 1; |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
680 char *response_body = Dowa_Arena_Allocate(arena, 512); |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
681 snprintf(response_body, 512, |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
682 "{\"success\":true,\"download_url\":\"/api/download/%s\",\"expires\":\"10 minutes\"}", |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
683 filename); |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
684 Dowa_HashMap_Push_Arena(resp, "status", "200", arena); |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
685 Dowa_HashMap_Push_Arena(resp, "content-type", "application/json", arena); |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
686 Dowa_HashMap_Push_Arena(resp, "body", response_body, arena); |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
687 |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
688 printf("DEBUG: Video converted, available at /api/download/%s\n", filename); |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
689 return resp; |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
690 } |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
691 |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
692 Seobeo_Request_Entry *DownloadConvertedFile(Seobeo_Request_Entry *req, Dowa_Arena *arena) |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
693 { |
|
72
4532ce6d9eb8
[Seobeo] Added router to the server logic. Few dowa string manipulation logics.
June Park <parkjune1995@gmail.com>
parents:
36
diff
changeset
|
694 Seobeo_Request_Entry *resp = NULL; |
|
92
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
695 |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
696 void *filename_kv = Dowa_HashMap_Get_Ptr(req, ":filename"); |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
697 if (!filename_kv) |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
698 { |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
699 char *error_msg = "No filename specified"; |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
700 Dowa_HashMap_Push_Arena(resp, "status", "404", arena); |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
701 Dowa_HashMap_Push_Arena(resp, "content-type", "text/plain", arena); |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
702 Dowa_HashMap_Push_Arena(resp, "body", error_msg, arena); |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
703 return resp; |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
704 } |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
705 |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
706 const char *filename = ((Seobeo_Request_Entry*)filename_kv)->value; |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
707 |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
708 // TODO: Maybe check if the uuid is allowed or not? |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
709 // if (strlen(filename) != TMP_FILE_LENGTH) |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
710 // { |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
711 // char *error_msg = "Not Allowed Filename"; |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
712 // Dowa_HashMap_Push_Arena(resp, "status", "404", arena); |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
713 // Dowa_HashMap_Push_Arena(resp, "content-type", "text/plain", arena); |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
714 // Dowa_HashMap_Push_Arena(resp, "body", error_msg, arena); |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
715 // return resp; |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
716 // } |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
717 // boolean allowed = FALSE; |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
718 // for (int i = 0; i < Dowa_Array_Length(g_uuid4_array); i++) |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
719 // { |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
720 // if strcmp(g_uuid4_array, filename) |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
721 // { |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
722 // allowed = TRUE; |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
723 // break; |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
724 // } |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
725 // g_uuid4_array++; |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
726 // } |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
727 |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
728 char filepath[512]; |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
729 snprintf(filepath, sizeof(filepath), "/tmp/%s", filename); |
|
72
4532ce6d9eb8
[Seobeo] Added router to the server logic. Few dowa string manipulation logics.
June Park <parkjune1995@gmail.com>
parents:
36
diff
changeset
|
730 |
|
92
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
731 FILE *file = fopen(filepath, "rb"); |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
732 if (!file) |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
733 { |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
734 char *error_msg = "File not found or expired"; |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
735 Dowa_HashMap_Push_Arena(resp, "status", "404", arena); |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
736 Dowa_HashMap_Push_Arena(resp, "content-type", "text/plain", arena); |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
737 Dowa_HashMap_Push_Arena(resp, "body", error_msg, arena); |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
738 return resp; |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
739 } |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
740 |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
741 fseek(file, 0, SEEK_END); |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
742 size_t file_size = ftell(file); |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
743 fseek(file, 0, SEEK_SET); |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
744 |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
745 char *file_data = malloc(file_size + 1); |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
746 if (!file_data) |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
747 { |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
748 fclose(file); |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
749 char *error_msg = "Memory allocation failed"; |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
750 Dowa_HashMap_Push_Arena(resp, "status", "500", arena); |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
751 Dowa_HashMap_Push_Arena(resp, "content-type", "text/plain", arena); |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
752 Dowa_HashMap_Push_Arena(resp, "body", error_msg, arena); |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
753 return resp; |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
754 } |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
755 |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
756 fread(file_data, 1, file_size, file); |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
757 file_data[file_size] = '\0'; |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
758 fclose(file); |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
759 |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
760 const char *content_type = "application/octet-stream"; |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
761 if (strstr(filename, ".webp")) |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
762 content_type = "image/webp"; |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
763 else if (strstr(filename, ".mp4")) |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
764 content_type = "video/mp4"; |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
765 |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
766 char *body = Dowa_Arena_Allocate(arena, file_size + 1); |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
767 memcpy(body, file_data, file_size); |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
768 body[file_size] = '\0'; |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
769 free(file_data); |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
770 |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
771 unlink(filepath); |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
772 |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
773 printf("DEBUG: Served and deleted file: %s (%zu bytes)\n", filename, file_size); |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
774 |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
775 char *content_length = Dowa_Arena_Allocate(arena, 32); |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
776 snprintf(content_length, 32, "%zu", file_size); |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
777 |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
778 Dowa_HashMap_Push_Arena(resp, "status", "200", arena); |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
779 Dowa_HashMap_Push_Arena(resp, "content-type", content_type, arena); |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
780 Dowa_HashMap_Push_Arena(resp, "content-length", content_length, arena); |
|
72
4532ce6d9eb8
[Seobeo] Added router to the server logic. Few dowa string manipulation logics.
June Park <parkjune1995@gmail.com>
parents:
36
diff
changeset
|
781 Dowa_HashMap_Push_Arena(resp, "body", body, arena); |
|
92
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
782 |
|
72
4532ce6d9eb8
[Seobeo] Added router to the server logic. Few dowa string manipulation logics.
June Park <parkjune1995@gmail.com>
parents:
36
diff
changeset
|
783 return resp; |
|
4532ce6d9eb8
[Seobeo] Added router to the server logic. Few dowa string manipulation logics.
June Park <parkjune1995@gmail.com>
parents:
36
diff
changeset
|
784 } |
|
4532ce6d9eb8
[Seobeo] Added router to the server logic. Few dowa string manipulation logics.
June Park <parkjune1995@gmail.com>
parents:
36
diff
changeset
|
785 |
|
100
65e5a5b89a4e
[Seobeo] Migrated everything to this page.
June Park <parkjune1995@gmail.com>
parents:
96
diff
changeset
|
786 Seobeo_Request_Entry *RenderBlogList(Seobeo_Request_Entry *req, Dowa_Arena *arena) |
|
65e5a5b89a4e
[Seobeo] Migrated everything to this page.
June Park <parkjune1995@gmail.com>
parents:
96
diff
changeset
|
787 { |
|
65e5a5b89a4e
[Seobeo] Migrated everything to this page.
June Park <parkjune1995@gmail.com>
parents:
96
diff
changeset
|
788 Seobeo_Request_Entry *resp = NULL; |
|
65e5a5b89a4e
[Seobeo] Migrated everything to this page.
June Park <parkjune1995@gmail.com>
parents:
96
diff
changeset
|
789 char *final_body = Dowa_Arena_Allocate(arena, 50 * 1024); |
|
158
1c0878eb17de
[MrJuneJune] Readme file gets compiled in server side.
June Park <parkjune1995@gmail.com>
parents:
132
diff
changeset
|
790 Seobeo_Render_Html_FilePath(final_body, "/blog/index.html", arena); |
|
100
65e5a5b89a4e
[Seobeo] Migrated everything to this page.
June Park <parkjune1995@gmail.com>
parents:
96
diff
changeset
|
791 Dowa_HashMap_Push_Arena(resp, "body", final_body, arena); |
|
65e5a5b89a4e
[Seobeo] Migrated everything to this page.
June Park <parkjune1995@gmail.com>
parents:
96
diff
changeset
|
792 return resp; |
|
65e5a5b89a4e
[Seobeo] Migrated everything to this page.
June Park <parkjune1995@gmail.com>
parents:
96
diff
changeset
|
793 } |
|
65e5a5b89a4e
[Seobeo] Migrated everything to this page.
June Park <parkjune1995@gmail.com>
parents:
96
diff
changeset
|
794 |
|
65e5a5b89a4e
[Seobeo] Migrated everything to this page.
June Park <parkjune1995@gmail.com>
parents:
96
diff
changeset
|
795 |
|
65e5a5b89a4e
[Seobeo] Migrated everything to this page.
June Park <parkjune1995@gmail.com>
parents:
96
diff
changeset
|
796 Seobeo_Request_Entry *RenderBlog(Seobeo_Request_Entry *req, Dowa_Arena *arena) |
|
65e5a5b89a4e
[Seobeo] Migrated everything to this page.
June Park <parkjune1995@gmail.com>
parents:
96
diff
changeset
|
797 { |
|
65e5a5b89a4e
[Seobeo] Migrated everything to this page.
June Park <parkjune1995@gmail.com>
parents:
96
diff
changeset
|
798 Seobeo_Request_Entry *resp = NULL; |
|
65e5a5b89a4e
[Seobeo] Migrated everything to this page.
June Park <parkjune1995@gmail.com>
parents:
96
diff
changeset
|
799 |
|
169
295ac2e5ec00
[MrJuneJune] Created separate target for generating html from md.
MrJuneJune <me@mrjunejune.com>
parents:
168
diff
changeset
|
800 char *file_path = Dowa_Arena_Allocate(arena, 1024); |
|
100
65e5a5b89a4e
[Seobeo] Migrated everything to this page.
June Park <parkjune1995@gmail.com>
parents:
96
diff
changeset
|
801 void *blog_id_kv = Dowa_HashMap_Get_Ptr(req, ":blog_id"); |
|
169
295ac2e5ec00
[MrJuneJune] Created separate target for generating html from md.
MrJuneJune <me@mrjunejune.com>
parents:
168
diff
changeset
|
802 char *blog_id = ((Seobeo_Request_Entry*)blog_id_kv)->value; |
|
295ac2e5ec00
[MrJuneJune] Created separate target for generating html from md.
MrJuneJune <me@mrjunejune.com>
parents:
168
diff
changeset
|
803 snprintf(file_path, 1024, "/blog/%s/index.html", blog_id); |
|
100
65e5a5b89a4e
[Seobeo] Migrated everything to this page.
June Park <parkjune1995@gmail.com>
parents:
96
diff
changeset
|
804 |
|
169
295ac2e5ec00
[MrJuneJune] Created separate target for generating html from md.
MrJuneJune <me@mrjunejune.com>
parents:
168
diff
changeset
|
805 char *final_body = Dowa_Arena_Allocate(arena, 50 * 1024); |
|
295ac2e5ec00
[MrJuneJune] Created separate target for generating html from md.
MrJuneJune <me@mrjunejune.com>
parents:
168
diff
changeset
|
806 Seobeo_Render_Html_FilePath(final_body, file_path, arena); |
|
100
65e5a5b89a4e
[Seobeo] Migrated everything to this page.
June Park <parkjune1995@gmail.com>
parents:
96
diff
changeset
|
807 Dowa_HashMap_Push_Arena(resp, "body", final_body, arena); |
|
65e5a5b89a4e
[Seobeo] Migrated everything to this page.
June Park <parkjune1995@gmail.com>
parents:
96
diff
changeset
|
808 return resp; |
|
65e5a5b89a4e
[Seobeo] Migrated everything to this page.
June Park <parkjune1995@gmail.com>
parents:
96
diff
changeset
|
809 } |
|
65e5a5b89a4e
[Seobeo] Migrated everything to this page.
June Park <parkjune1995@gmail.com>
parents:
96
diff
changeset
|
810 |
|
125
f236c895604e
[MrJuneJune] Added web socket for chat to this.
June Park <parkjune1995@gmail.com>
parents:
124
diff
changeset
|
811 void Chat_Handler(Seobeo_WebSocket_Server_Connection *p_conn, Seobeo_WebSocket_Message *p_msg, void *p_user_data) |
|
f236c895604e
[MrJuneJune] Added web socket for chat to this.
June Park <parkjune1995@gmail.com>
parents:
124
diff
changeset
|
812 { |
|
f236c895604e
[MrJuneJune] Added web socket for chat to this.
June Park <parkjune1995@gmail.com>
parents:
124
diff
changeset
|
813 (void)p_user_data; |
|
f236c895604e
[MrJuneJune] Added web socket for chat to this.
June Park <parkjune1995@gmail.com>
parents:
124
diff
changeset
|
814 |
|
f236c895604e
[MrJuneJune] Added web socket for chat to this.
June Park <parkjune1995@gmail.com>
parents:
124
diff
changeset
|
815 if (p_msg->opcode == SEOBEO_WS_OPCODE_TEXT) |
|
f236c895604e
[MrJuneJune] Added web socket for chat to this.
June Park <parkjune1995@gmail.com>
parents:
124
diff
changeset
|
816 { |
|
f236c895604e
[MrJuneJune] Added web socket for chat to this.
June Park <parkjune1995@gmail.com>
parents:
124
diff
changeset
|
817 char message[2048]; |
|
f236c895604e
[MrJuneJune] Added web socket for chat to this.
June Park <parkjune1995@gmail.com>
parents:
124
diff
changeset
|
818 snprintf(message, sizeof(message), "[%s]: %.*s", p_conn->client_id, (int)p_msg->length, (char*)p_msg->data); |
|
f236c895604e
[MrJuneJune] Added web socket for chat to this.
June Park <parkjune1995@gmail.com>
parents:
124
diff
changeset
|
819 |
|
f236c895604e
[MrJuneJune] Added web socket for chat to this.
June Park <parkjune1995@gmail.com>
parents:
124
diff
changeset
|
820 Seobeo_Log(SEOBEO_INFO, "[Chat] Broadcasting: %s\n", message); |
|
f236c895604e
[MrJuneJune] Added web socket for chat to this.
June Park <parkjune1995@gmail.com>
parents:
124
diff
changeset
|
821 Seobeo_WebSocket_Server_Broadcast_Text(message, p_conn); |
|
f236c895604e
[MrJuneJune] Added web socket for chat to this.
June Park <parkjune1995@gmail.com>
parents:
124
diff
changeset
|
822 } |
|
f236c895604e
[MrJuneJune] Added web socket for chat to this.
June Park <parkjune1995@gmail.com>
parents:
124
diff
changeset
|
823 } |
|
f236c895604e
[MrJuneJune] Added web socket for chat to this.
June Park <parkjune1995@gmail.com>
parents:
124
diff
changeset
|
824 |
|
f236c895604e
[MrJuneJune] Added web socket for chat to this.
June Park <parkjune1995@gmail.com>
parents:
124
diff
changeset
|
825 Seobeo_Request_Entry *GetTalk(Seobeo_Request_Entry *req, Dowa_Arena *arena) |
|
f236c895604e
[MrJuneJune] Added web socket for chat to this.
June Park <parkjune1995@gmail.com>
parents:
124
diff
changeset
|
826 { |
|
f236c895604e
[MrJuneJune] Added web socket for chat to this.
June Park <parkjune1995@gmail.com>
parents:
124
diff
changeset
|
827 Seobeo_Request_Entry *resp = NULL; |
|
f236c895604e
[MrJuneJune] Added web socket for chat to this.
June Park <parkjune1995@gmail.com>
parents:
124
diff
changeset
|
828 char *final_body = Dowa_Arena_Allocate(arena, 50 * 1024); |
|
158
1c0878eb17de
[MrJuneJune] Readme file gets compiled in server side.
June Park <parkjune1995@gmail.com>
parents:
132
diff
changeset
|
829 Seobeo_Render_Html_FilePath(final_body, "/talk/index.html", arena); |
|
125
f236c895604e
[MrJuneJune] Added web socket for chat to this.
June Park <parkjune1995@gmail.com>
parents:
124
diff
changeset
|
830 Dowa_HashMap_Push_Arena(resp, "body", final_body, arena); |
|
f236c895604e
[MrJuneJune] Added web socket for chat to this.
June Park <parkjune1995@gmail.com>
parents:
124
diff
changeset
|
831 return resp; |
|
f236c895604e
[MrJuneJune] Added web socket for chat to this.
June Park <parkjune1995@gmail.com>
parents:
124
diff
changeset
|
832 } |
|
100
65e5a5b89a4e
[Seobeo] Migrated everything to this page.
June Park <parkjune1995@gmail.com>
parents:
96
diff
changeset
|
833 |
| 201 | 834 Seobeo_Request_Entry *GetNotesLogin(Seobeo_Request_Entry *req, Dowa_Arena *arena) |
| 835 { | |
| 836 Seobeo_Request_Entry *resp = NULL; | |
| 837 char *final_body = Dowa_Arena_Allocate(arena, 50 * 1024); | |
| 838 Seobeo_Render_Html_FilePath(final_body, "/notes/login.html", arena); | |
| 839 Dowa_HashMap_Push_Arena(resp, "body", final_body, arena); | |
| 840 return resp; | |
| 841 } | |
| 842 | |
| 843 Seobeo_Request_Entry *GetNotes(Seobeo_Request_Entry *req, Dowa_Arena *arena) | |
| 844 { | |
| 845 Seobeo_Request_Entry *resp = NULL; | |
| 846 char *final_body = Dowa_Arena_Allocate(arena, 50 * 1024); | |
| 847 Seobeo_Render_Html_FilePath(final_body, "/notes/index.html", arena); | |
| 848 Dowa_HashMap_Push_Arena(resp, "body", final_body, arena); | |
| 849 return resp; | |
| 850 } | |
| 851 | |
| 852 Seobeo_Request_Entry *GetNoteById(Seobeo_Request_Entry *req, Dowa_Arena *arena) | |
| 853 { | |
| 854 Seobeo_Request_Entry *resp = NULL; | |
| 855 char *final_body = Dowa_Arena_Allocate(arena, 50 * 1024); | |
| 856 // Same template - JavaScript handles the note_id from URL | |
| 857 Seobeo_Render_Html_FilePath(final_body, "/notes/index.html", arena); | |
| 858 Dowa_HashMap_Push_Arena(resp, "body", final_body, arena); | |
| 859 return resp; | |
| 860 } | |
| 861 | |
| 88 | 862 CREATE_REDIRECT_HANDLER(HomePage, "/") |
| 863 CREATE_REDIRECT_HANDLER(Resume, "/resume") | |
| 864 CREATE_REDIRECT_HANDLER(Tools, "/tools") | |
| 865 CREATE_REDIRECT_HANDLER(MarkDownToHtml, "/tools/markdown_to_html") | |
|
92
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
866 CREATE_REDIRECT_HANDLER(FileConverter, "/tools/file_converter") |
|
242
543df0fe7168
[tools] Add full HLS player support
MrJuneJune <me@mrjunejune.com>
parents:
241
diff
changeset
|
867 CREATE_REDIRECT_HANDLER(HlsPlayer, "/tools/hls_player") |
|
244
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
868 CREATE_REDIRECT_HANDLER(LatexEditor, "/tools/latex_editor") |
|
125
f236c895604e
[MrJuneJune] Added web socket for chat to this.
June Park <parkjune1995@gmail.com>
parents:
124
diff
changeset
|
869 CREATE_REDIRECT_HANDLER(Talk, "/talk") |
| 201 | 870 CREATE_REDIRECT_HANDLER(Editor, "/editor") |
|
79
5710108c949e
[Seobeo] Added Redirect logic.
June Park <parkjune1995@gmail.com>
parents:
78
diff
changeset
|
871 |
|
200
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
872 // S3 Upload URL API |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
873 // POST /api/s3/upload-url |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
874 // Headers: Authorization: Bearer <token>, Content-Type: application/json |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
875 // Body: {"filename": "photo.png", "content_type": "image/png"} |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
876 // Returns: {"upload_url": "https://...", "key": "uploads/..."} |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
877 Seobeo_Request_Entry *GetS3UploadUrl(Seobeo_Request_Entry *req, Dowa_Arena *arena) |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
878 { |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
879 Seobeo_Request_Entry *resp = NULL; |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
880 |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
881 // Check auth token |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
882 void *auth_kv = Dowa_HashMap_Get_Ptr(req, "Authorization"); |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
883 if (!auth_kv) |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
884 { |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
885 Dowa_HashMap_Push_Arena(resp, "status", "401", arena); |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
886 Dowa_HashMap_Push_Arena(resp, "content-type", "application/json", arena); |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
887 Dowa_HashMap_Push_Arena(resp, "body", "{\"error\":\"Missing Authorization header\"}", arena); |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
888 return resp; |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
889 } |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
890 |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
891 const char *auth_header = ((Seobeo_Request_Entry*)auth_kv)->value; |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
892 |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
893 // Expect "Bearer <token>" |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
894 if (strncmp(auth_header, "Bearer ", 7) != 0) |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
895 { |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
896 Dowa_HashMap_Push_Arena(resp, "status", "401", arena); |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
897 Dowa_HashMap_Push_Arena(resp, "content-type", "application/json", arena); |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
898 Dowa_HashMap_Push_Arena(resp, "body", "{\"error\":\"Invalid Authorization format, use Bearer token\"}", arena); |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
899 return resp; |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
900 } |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
901 |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
902 const char *token = auth_header + 7; |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
903 if (strlen(g_upload_auth_token) == 0 || strcmp(token, g_upload_auth_token) != 0) |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
904 { |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
905 Dowa_HashMap_Push_Arena(resp, "status", "403", arena); |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
906 Dowa_HashMap_Push_Arena(resp, "content-type", "application/json", arena); |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
907 Dowa_HashMap_Push_Arena(resp, "body", "{\"error\":\"Invalid token\"}", arena); |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
908 return resp; |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
909 } |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
910 |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
911 // Parse request body for filename and content_type |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
912 void *body_kv = Dowa_HashMap_Get_Ptr(req, "Body"); |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
913 if (!body_kv) |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
914 { |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
915 Dowa_HashMap_Push_Arena(resp, "status", "400", arena); |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
916 Dowa_HashMap_Push_Arena(resp, "content-type", "application/json", arena); |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
917 Dowa_HashMap_Push_Arena(resp, "body", "{\"error\":\"Missing request body\"}", arena); |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
918 return resp; |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
919 } |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
920 |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
921 const char *body = ((Seobeo_Request_Entry*)body_kv)->value; |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
922 |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
923 // Simple JSON parsing for filename and content_type |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
924 char filename[256] = {0}; |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
925 char content_type[128] = "application/octet-stream"; |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
926 |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
927 // Find "filename":"value" |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
928 const char *fn_key = strstr(body, "\"filename\""); |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
929 if (fn_key) |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
930 { |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
931 const char *fn_start = strchr(fn_key + 10, '"'); |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
932 if (fn_start) |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
933 { |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
934 fn_start++; |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
935 const char *fn_end = strchr(fn_start, '"'); |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
936 if (fn_end && (size_t)(fn_end - fn_start) < sizeof(filename)) |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
937 { |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
938 memcpy(filename, fn_start, fn_end - fn_start); |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
939 filename[fn_end - fn_start] = '\0'; |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
940 } |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
941 } |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
942 } |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
943 |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
944 // Find "content_type":"value" |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
945 const char *ct_key = strstr(body, "\"content_type\""); |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
946 if (ct_key) |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
947 { |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
948 const char *ct_start = strchr(ct_key + 14, '"'); |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
949 if (ct_start) |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
950 { |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
951 ct_start++; |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
952 const char *ct_end = strchr(ct_start, '"'); |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
953 if (ct_end && (size_t)(ct_end - ct_start) < sizeof(content_type)) |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
954 { |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
955 memcpy(content_type, ct_start, ct_end - ct_start); |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
956 content_type[ct_end - ct_start] = '\0'; |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
957 } |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
958 } |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
959 } |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
960 |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
961 if (strlen(filename) == 0) |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
962 { |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
963 Dowa_HashMap_Push_Arena(resp, "status", "400", arena); |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
964 Dowa_HashMap_Push_Arena(resp, "content-type", "application/json", arena); |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
965 Dowa_HashMap_Push_Arena(resp, "body", "{\"error\":\"Missing filename in request body\"}", arena); |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
966 return resp; |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
967 } |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
968 |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
969 // Generate unique S3 key with timestamp |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
970 char s3_key[512]; |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
971 char *uuid = Dowa_Arena_Allocate(arena, UUID_LEN); |
|
250
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
972 uint32 seed = |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
973 (uint32)time(NULL) ^ |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
974 (uint32)Seobeo_Thread_Current_Id() ^ |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
975 counter++; |
|
200
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
976 Dowa_String_UUID(seed, uuid); |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
977 snprintf(s3_key, sizeof(s3_key), "uploads/%s/%s", uuid, filename); |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
978 |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
979 // Generate presigned URL |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
980 S3_Presigned_URL presigned = S3_Presign_Put(&g_s3_config, s3_key, content_type, g_s3_url_expires); |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
981 |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
982 if (!presigned.success) |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
983 { |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
984 Dowa_HashMap_Push_Arena(resp, "status", "500", arena); |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
985 Dowa_HashMap_Push_Arena(resp, "content-type", "application/json", arena); |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
986 char *error_body = Dowa_Arena_Allocate(arena, 256); |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
987 snprintf(error_body, 256, "{\"error\":\"Failed to generate upload URL: %s\"}", |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
988 presigned.error_message ? presigned.error_message : "unknown"); |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
989 Dowa_HashMap_Push_Arena(resp, "body", error_body, arena); |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
990 S3_Presigned_URL_Destroy(&presigned); |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
991 return resp; |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
992 } |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
993 |
| 201 | 994 // Build public URL using CloudFront |
| 995 char public_url[512]; | |
| 996 if (g_s3_cloudfront_url[0]) | |
| 997 { | |
| 998 snprintf(public_url, sizeof(public_url), "%s/%s", g_s3_cloudfront_url, s3_key); | |
| 999 } | |
| 1000 else | |
| 1001 { | |
| 1002 snprintf(public_url, sizeof(public_url), "https://%s.s3.%s.amazonaws.com/%s", | |
| 1003 g_s3_bucket, g_s3_region, s3_key); | |
| 1004 } | |
| 1005 | |
|
200
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1006 // Build response |
| 201 | 1007 char *response_body = Dowa_Arena_Allocate(arena, 4096 + strlen(presigned.url)); |
| 1008 snprintf(response_body, 4096 + strlen(presigned.url), | |
| 1009 "{\"upload_url\":\"%s\",\"public_url\":\"%s\",\"key\":\"%s\",\"expires\":%d}", | |
| 1010 presigned.url, public_url, s3_key, g_s3_url_expires); | |
|
200
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1011 |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1012 S3_Presigned_URL_Destroy(&presigned); |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1013 |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1014 Dowa_HashMap_Push_Arena(resp, "status", "200", arena); |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1015 Dowa_HashMap_Push_Arena(resp, "content-type", "application/json", arena); |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1016 Dowa_HashMap_Push_Arena(resp, "body", response_body, arena); |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1017 |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1018 printf("[S3] Generated upload URL for: %s\n", s3_key); |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1019 |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1020 return resp; |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1021 } |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1022 |
| 201 | 1023 // Editor Content Save API |
| 1024 // POST /api/editor/save | |
| 1025 // Headers: Authorization: Bearer <token> | |
| 1026 // Body: {"doc_id": "my-doc", "content": "<html content>"} | |
| 1027 Seobeo_Request_Entry *EditorSave(Seobeo_Request_Entry *req, Dowa_Arena *arena) | |
| 1028 { | |
| 1029 Seobeo_Request_Entry *resp = NULL; | |
| 1030 | |
| 1031 // Check auth token | |
| 1032 void *auth_kv = Dowa_HashMap_Get_Ptr(req, "Authorization"); | |
| 1033 if (!auth_kv) | |
| 1034 { | |
| 1035 Dowa_HashMap_Push_Arena(resp, "status", "401", arena); | |
| 1036 Dowa_HashMap_Push_Arena(resp, "content-type", "application/json", arena); | |
| 1037 Dowa_HashMap_Push_Arena(resp, "body", "{\"error\":\"Missing Authorization header\"}", arena); | |
| 1038 return resp; | |
| 1039 } | |
| 1040 | |
| 1041 const char *auth_header = ((Seobeo_Request_Entry*)auth_kv)->value; | |
| 1042 if (strncmp(auth_header, "Bearer ", 7) != 0) | |
| 1043 { | |
| 1044 Dowa_HashMap_Push_Arena(resp, "status", "401", arena); | |
| 1045 Dowa_HashMap_Push_Arena(resp, "content-type", "application/json", arena); | |
| 1046 Dowa_HashMap_Push_Arena(resp, "body", "{\"error\":\"Invalid Authorization format\"}", arena); | |
| 1047 return resp; | |
| 1048 } | |
| 1049 | |
| 1050 const char *token = auth_header + 7; | |
| 1051 if (strlen(g_upload_auth_token) == 0 || strcmp(token, g_upload_auth_token) != 0) | |
| 1052 { | |
| 1053 Dowa_HashMap_Push_Arena(resp, "status", "403", arena); | |
| 1054 Dowa_HashMap_Push_Arena(resp, "content-type", "application/json", arena); | |
| 1055 Dowa_HashMap_Push_Arena(resp, "body", "{\"error\":\"Invalid token\"}", arena); | |
| 1056 return resp; | |
| 1057 } | |
| 1058 | |
| 1059 if (!g_db_connection) | |
| 1060 { | |
| 1061 Dowa_HashMap_Push_Arena(resp, "status", "500", arena); | |
| 1062 Dowa_HashMap_Push_Arena(resp, "content-type", "application/json", arena); | |
| 1063 Dowa_HashMap_Push_Arena(resp, "body", "{\"error\":\"Database not available\"}", arena); | |
| 1064 return resp; | |
| 1065 } | |
| 1066 | |
| 1067 // Parse request body | |
| 1068 void *body_kv = Dowa_HashMap_Get_Ptr(req, "Body"); | |
| 1069 if (!body_kv) | |
| 1070 { | |
| 1071 Dowa_HashMap_Push_Arena(resp, "status", "400", arena); | |
| 1072 Dowa_HashMap_Push_Arena(resp, "content-type", "application/json", arena); | |
| 1073 Dowa_HashMap_Push_Arena(resp, "body", "{\"error\":\"Missing request body\"}", arena); | |
| 1074 return resp; | |
| 1075 } | |
| 1076 | |
| 1077 const char *body = ((Seobeo_Request_Entry*)body_kv)->value; | |
| 1078 | |
| 1079 // Parse doc_id and content from JSON | |
| 1080 char doc_id[256] = "default"; | |
| 1081 char *content = NULL; | |
| 1082 size_t content_len = 0; | |
| 1083 | |
| 1084 // Find "doc_id":"value" | |
| 1085 const char *doc_key = strstr(body, "\"doc_id\""); | |
| 1086 if (doc_key) | |
| 1087 { | |
| 1088 const char *doc_start = strchr(doc_key + 8, '"'); | |
| 1089 if (doc_start) | |
| 1090 { | |
| 1091 doc_start++; | |
| 1092 const char *doc_end = strchr(doc_start, '"'); | |
| 1093 if (doc_end && (size_t)(doc_end - doc_start) < sizeof(doc_id)) | |
| 1094 { | |
| 1095 memcpy(doc_id, doc_start, doc_end - doc_start); | |
| 1096 doc_id[doc_end - doc_start] = '\0'; | |
| 1097 } | |
| 1098 } | |
| 1099 } | |
| 1100 | |
| 1101 // Find "content":"value" - content can be large and contain escaped characters | |
| 1102 const char *content_key = strstr(body, "\"content\""); | |
| 1103 if (content_key) | |
| 1104 { | |
| 1105 const char *content_start = strchr(content_key + 9, '"'); | |
| 1106 if (content_start) | |
| 1107 { | |
| 1108 content_start++; | |
| 1109 // Find closing quote (accounting for escaped quotes) | |
| 1110 const char *p = content_start; | |
| 1111 while (*p) | |
| 1112 { | |
| 1113 if (*p == '\\' && *(p+1)) | |
| 1114 { | |
| 1115 p += 2; | |
| 1116 continue; | |
| 1117 } | |
| 1118 if (*p == '"') break; | |
| 1119 p++; | |
| 1120 } | |
| 1121 content_len = p - content_start; | |
| 1122 content = Dowa_Arena_Allocate(arena, content_len + 1); | |
| 1123 memcpy(content, content_start, content_len); | |
| 1124 content[content_len] = '\0'; | |
| 1125 } | |
| 1126 } | |
| 1127 | |
| 1128 if (!content) | |
| 1129 { | |
| 1130 Dowa_HashMap_Push_Arena(resp, "status", "400", arena); | |
| 1131 Dowa_HashMap_Push_Arena(resp, "content-type", "application/json", arena); | |
| 1132 Dowa_HashMap_Push_Arena(resp, "body", "{\"error\":\"Missing content\"}", arena); | |
| 1133 return resp; | |
| 1134 } | |
| 1135 | |
| 1136 // Upsert content | |
| 1137 const char *upsert_query = | |
| 1138 "INSERT INTO editor_content (access_token, doc_id, content, updated_at) " | |
| 1139 "VALUES (?, ?, ?, strftime('%s', 'now')) " | |
| 1140 "ON CONFLICT(access_token, doc_id) DO UPDATE SET " | |
| 1141 "content = excluded.content, updated_at = strftime('%s', 'now')"; | |
| 1142 | |
| 1143 const char *params[] = { token, doc_id, content }; | |
| 1144 int32 result = Deita_Query_Execute_Update_Prepared(g_db_connection, upsert_query, 3, params); | |
| 1145 | |
| 1146 if (result < 0) | |
| 1147 { | |
| 1148 Dowa_HashMap_Push_Arena(resp, "status", "500", arena); | |
| 1149 Dowa_HashMap_Push_Arena(resp, "content-type", "application/json", arena); | |
| 1150 Dowa_HashMap_Push_Arena(resp, "body", "{\"error\":\"Failed to save\"}", arena); | |
| 1151 return resp; | |
| 1152 } | |
| 1153 | |
| 1154 printf("[EDITOR] Saved doc_id=%s, content_len=%zu\n", doc_id, content_len); | |
| 1155 | |
| 1156 Dowa_HashMap_Push_Arena(resp, "status", "200", arena); | |
| 1157 Dowa_HashMap_Push_Arena(resp, "content-type", "application/json", arena); | |
| 1158 Dowa_HashMap_Push_Arena(resp, "body", "{\"success\":true}", arena); | |
| 1159 return resp; | |
| 1160 } | |
| 1161 | |
| 1162 // Editor Content Load API | |
| 1163 // GET /api/editor/load/:doc_id | |
| 1164 // Headers: Authorization: Bearer <token> | |
| 1165 Seobeo_Request_Entry *EditorLoad(Seobeo_Request_Entry *req, Dowa_Arena *arena) | |
| 1166 { | |
| 1167 Seobeo_Request_Entry *resp = NULL; | |
| 1168 | |
| 1169 // Check auth token | |
| 1170 void *auth_kv = Dowa_HashMap_Get_Ptr(req, "Authorization"); | |
| 1171 if (!auth_kv) | |
| 1172 { | |
| 1173 Dowa_HashMap_Push_Arena(resp, "status", "401", arena); | |
| 1174 Dowa_HashMap_Push_Arena(resp, "content-type", "application/json", arena); | |
| 1175 Dowa_HashMap_Push_Arena(resp, "body", "{\"error\":\"Missing Authorization header\"}", arena); | |
| 1176 return resp; | |
| 1177 } | |
| 1178 | |
| 1179 const char *auth_header = ((Seobeo_Request_Entry*)auth_kv)->value; | |
| 1180 if (strncmp(auth_header, "Bearer ", 7) != 0) | |
| 1181 { | |
| 1182 Dowa_HashMap_Push_Arena(resp, "status", "401", arena); | |
| 1183 Dowa_HashMap_Push_Arena(resp, "content-type", "application/json", arena); | |
| 1184 Dowa_HashMap_Push_Arena(resp, "body", "{\"error\":\"Invalid Authorization format\"}", arena); | |
| 1185 return resp; | |
| 1186 } | |
| 1187 | |
| 1188 const char *token = auth_header + 7; | |
| 1189 if (strlen(g_upload_auth_token) == 0 || strcmp(token, g_upload_auth_token) != 0) | |
| 1190 { | |
| 1191 Dowa_HashMap_Push_Arena(resp, "status", "403", arena); | |
| 1192 Dowa_HashMap_Push_Arena(resp, "content-type", "application/json", arena); | |
| 1193 Dowa_HashMap_Push_Arena(resp, "body", "{\"error\":\"Invalid token\"}", arena); | |
| 1194 return resp; | |
| 1195 } | |
| 1196 | |
| 1197 if (!g_db_connection) | |
| 1198 { | |
| 1199 Dowa_HashMap_Push_Arena(resp, "status", "500", arena); | |
| 1200 Dowa_HashMap_Push_Arena(resp, "content-type", "application/json", arena); | |
| 1201 Dowa_HashMap_Push_Arena(resp, "body", "{\"error\":\"Database not available\"}", arena); | |
| 1202 return resp; | |
| 1203 } | |
| 1204 | |
| 1205 // Get doc_id from URL parameter | |
| 1206 void *doc_id_kv = Dowa_HashMap_Get_Ptr(req, ":doc_id"); | |
| 1207 const char *doc_id = "default"; | |
| 1208 if (doc_id_kv) | |
| 1209 { | |
| 1210 doc_id = ((Seobeo_Request_Entry*)doc_id_kv)->value; | |
| 1211 } | |
| 1212 | |
| 1213 // Query content | |
| 1214 const char *select_query = | |
| 1215 "SELECT content, updated_at FROM editor_content WHERE access_token = ? AND doc_id = ?"; | |
| 1216 const char *params[] = { token, doc_id }; | |
| 1217 | |
| 1218 Deita_Result_Set *p_result = Deita_Query_Execute_Prepared(g_db_connection, select_query, 2, params, arena); | |
| 1219 | |
| 1220 if (p_result && Deita_Result_Set_Next(p_result)) | |
| 1221 { | |
| 1222 const char *content = Deita_Result_Set_Get_Text(p_result, 0); | |
| 1223 int64 updated_at = Deita_Result_Set_Get_Integer(p_result, 1); | |
| 1224 | |
| 1225 // Build JSON response - escape content | |
| 1226 size_t content_len = content ? strlen(content) : 0; | |
| 1227 char *response_body = Dowa_Arena_Allocate(arena, content_len + 256); | |
| 1228 snprintf(response_body, content_len + 256, | |
| 1229 "{\"doc_id\":\"%s\",\"content\":\"%s\",\"updated_at\":%lld}", | |
| 1230 doc_id, content ? content : "", (long long)updated_at); | |
| 1231 | |
| 1232 Dowa_HashMap_Push_Arena(resp, "status", "200", arena); | |
| 1233 Dowa_HashMap_Push_Arena(resp, "content-type", "application/json", arena); | |
| 1234 Dowa_HashMap_Push_Arena(resp, "body", response_body, arena); | |
| 1235 | |
| 1236 printf("[EDITOR] Loaded doc_id=%s\n", doc_id); | |
| 1237 } | |
| 1238 else | |
| 1239 { | |
| 1240 // No content found, return empty | |
| 1241 char *response_body = Dowa_Arena_Allocate(arena, 128); | |
| 1242 snprintf(response_body, 128, "{\"doc_id\":\"%s\",\"content\":\"\",\"updated_at\":0}", doc_id); | |
| 1243 | |
| 1244 Dowa_HashMap_Push_Arena(resp, "status", "200", arena); | |
| 1245 Dowa_HashMap_Push_Arena(resp, "content-type", "application/json", arena); | |
| 1246 Dowa_HashMap_Push_Arena(resp, "body", response_body, arena); | |
| 1247 } | |
| 1248 | |
| 1249 if (p_result) Deita_Result_Set_Free(p_result); | |
| 1250 return resp; | |
| 1251 } | |
| 1252 | |
| 1253 // Media Upload API - Create media record | |
| 1254 // POST /api/media/create | |
| 1255 // Headers: Authorization: Bearer <token>, Content-Type: application/json | |
| 1256 // Body: {"filename": "photo.jpg", "content_type": "image/jpeg"} | |
| 1257 // Returns: {"media_id": 123, "upload_url": "https://...", "expires": 3600} | |
| 1258 Seobeo_Request_Entry *MediaCreate(Seobeo_Request_Entry *req, Dowa_Arena *arena) | |
| 1259 { | |
| 1260 Seobeo_Request_Entry *resp = NULL; | |
| 1261 | |
| 1262 // Check auth token | |
| 1263 void *auth_kv = Dowa_HashMap_Get_Ptr(req, "Authorization"); | |
| 1264 if (!auth_kv) | |
| 1265 { | |
| 1266 Dowa_HashMap_Push_Arena(resp, "status", "401", arena); | |
| 1267 Dowa_HashMap_Push_Arena(resp, "content-type", "application/json", arena); | |
| 1268 Dowa_HashMap_Push_Arena(resp, "body", "{\"error\":\"Missing Authorization header\"}", arena); | |
| 1269 return resp; | |
| 1270 } | |
| 1271 | |
| 1272 const char *auth_header = ((Seobeo_Request_Entry*)auth_kv)->value; | |
| 1273 if (strncmp(auth_header, "Bearer ", 7) != 0) | |
| 1274 { | |
| 1275 Dowa_HashMap_Push_Arena(resp, "status", "401", arena); | |
| 1276 Dowa_HashMap_Push_Arena(resp, "content-type", "application/json", arena); | |
| 1277 Dowa_HashMap_Push_Arena(resp, "body", "{\"error\":\"Invalid Authorization format\"}", arena); | |
| 1278 return resp; | |
| 1279 } | |
| 1280 | |
| 1281 const char *token = auth_header + 7; | |
| 1282 if (strlen(g_upload_auth_token) == 0 || strcmp(token, g_upload_auth_token) != 0) | |
| 1283 { | |
| 1284 Dowa_HashMap_Push_Arena(resp, "status", "403", arena); | |
| 1285 Dowa_HashMap_Push_Arena(resp, "content-type", "application/json", arena); | |
| 1286 Dowa_HashMap_Push_Arena(resp, "body", "{\"error\":\"Invalid token\"}", arena); | |
| 1287 return resp; | |
| 1288 } | |
| 1289 | |
| 1290 if (!g_db_connection) | |
| 1291 { | |
| 1292 Dowa_HashMap_Push_Arena(resp, "status", "500", arena); | |
| 1293 Dowa_HashMap_Push_Arena(resp, "content-type", "application/json", arena); | |
| 1294 Dowa_HashMap_Push_Arena(resp, "body", "{\"error\":\"Database not available\"}", arena); | |
| 1295 return resp; | |
| 1296 } | |
| 1297 | |
| 1298 // Parse request body | |
| 1299 void *body_kv = Dowa_HashMap_Get_Ptr(req, "Body"); | |
| 1300 if (!body_kv) | |
| 1301 { | |
| 1302 Dowa_HashMap_Push_Arena(resp, "status", "400", arena); | |
| 1303 Dowa_HashMap_Push_Arena(resp, "content-type", "application/json", arena); | |
| 1304 Dowa_HashMap_Push_Arena(resp, "body", "{\"error\":\"Missing request body\"}", arena); | |
| 1305 return resp; | |
| 1306 } | |
| 1307 | |
| 1308 const char *body = ((Seobeo_Request_Entry*)body_kv)->value; | |
| 1309 | |
| 1310 // Parse filename and content_type | |
| 1311 char filename[256] = {0}; | |
| 1312 char content_type[128] = "application/octet-stream"; | |
| 1313 | |
| 1314 // Find "filename":"value" | |
| 1315 const char *fn_key = strstr(body, "\"filename\""); | |
| 1316 if (fn_key) | |
| 1317 { | |
| 1318 const char *fn_start = strchr(fn_key + 10, '"'); | |
| 1319 if (fn_start) | |
| 1320 { | |
| 1321 fn_start++; | |
| 1322 const char *fn_end = strchr(fn_start, '"'); | |
| 1323 if (fn_end && (size_t)(fn_end - fn_start) < sizeof(filename)) | |
| 1324 { | |
| 1325 memcpy(filename, fn_start, fn_end - fn_start); | |
| 1326 filename[fn_end - fn_start] = '\0'; | |
| 1327 } | |
| 1328 } | |
| 1329 } | |
| 1330 | |
| 1331 // Find "content_type":"value" | |
| 1332 const char *ct_key = strstr(body, "\"content_type\""); | |
| 1333 if (ct_key) | |
| 1334 { | |
| 1335 const char *ct_start = strchr(ct_key + 14, '"'); | |
| 1336 if (ct_start) | |
| 1337 { | |
| 1338 ct_start++; | |
| 1339 const char *ct_end = strchr(ct_start, '"'); | |
| 1340 if (ct_end && (size_t)(ct_end - ct_start) < sizeof(content_type)) | |
| 1341 { | |
| 1342 memcpy(content_type, ct_start, ct_end - ct_start); | |
| 1343 content_type[ct_end - ct_start] = '\0'; | |
| 1344 } | |
| 1345 } | |
| 1346 } | |
| 1347 | |
| 1348 if (strlen(filename) == 0) | |
| 1349 { | |
| 1350 Dowa_HashMap_Push_Arena(resp, "status", "400", arena); | |
| 1351 Dowa_HashMap_Push_Arena(resp, "content-type", "application/json", arena); | |
| 1352 Dowa_HashMap_Push_Arena(resp, "body", "{\"error\":\"Missing filename\"}", arena); | |
| 1353 return resp; | |
| 1354 } | |
| 1355 | |
| 1356 // Generate UUID for this upload | |
| 1357 char *uuid = Dowa_Arena_Allocate(arena, UUID_LEN); | |
|
250
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
1358 uint32 seed = |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
1359 (uint32)time(NULL) ^ |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
1360 (uint32)Seobeo_Thread_Current_Id() ^ |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
1361 counter++; |
| 201 | 1362 Dowa_String_UUID(seed, uuid); |
| 1363 | |
| 1364 // Generate S3 keys | |
| 1365 char s3_key_original[512]; | |
| 1366 char s3_key_processed[512]; | |
| 1367 snprintf(s3_key_original, sizeof(s3_key_original), "uploads/%s/%s", uuid, filename); | |
|
204
e5aed6c36672
[Notes] Added icons and updated styling a bit. Probalby usable now.
MrJuneJune <me@mrjunejune.com>
parents:
202
diff
changeset
|
1368 |
|
e5aed6c36672
[Notes] Added icons and updated styling a bit. Probalby usable now.
MrJuneJune <me@mrjunejune.com>
parents:
202
diff
changeset
|
1369 // Only use .webp for images |
|
e5aed6c36672
[Notes] Added icons and updated styling a bit. Probalby usable now.
MrJuneJune <me@mrjunejune.com>
parents:
202
diff
changeset
|
1370 int is_image = (strncmp(content_type, "image/", 6) == 0); |
|
e5aed6c36672
[Notes] Added icons and updated styling a bit. Probalby usable now.
MrJuneJune <me@mrjunejune.com>
parents:
202
diff
changeset
|
1371 if (is_image) |
|
e5aed6c36672
[Notes] Added icons and updated styling a bit. Probalby usable now.
MrJuneJune <me@mrjunejune.com>
parents:
202
diff
changeset
|
1372 { |
|
e5aed6c36672
[Notes] Added icons and updated styling a bit. Probalby usable now.
MrJuneJune <me@mrjunejune.com>
parents:
202
diff
changeset
|
1373 snprintf(s3_key_processed, sizeof(s3_key_processed), "uploads/%s/processed.webp", uuid); |
|
e5aed6c36672
[Notes] Added icons and updated styling a bit. Probalby usable now.
MrJuneJune <me@mrjunejune.com>
parents:
202
diff
changeset
|
1374 } |
|
e5aed6c36672
[Notes] Added icons and updated styling a bit. Probalby usable now.
MrJuneJune <me@mrjunejune.com>
parents:
202
diff
changeset
|
1375 else |
|
e5aed6c36672
[Notes] Added icons and updated styling a bit. Probalby usable now.
MrJuneJune <me@mrjunejune.com>
parents:
202
diff
changeset
|
1376 { |
|
e5aed6c36672
[Notes] Added icons and updated styling a bit. Probalby usable now.
MrJuneJune <me@mrjunejune.com>
parents:
202
diff
changeset
|
1377 s3_key_processed[0] = '\0'; // No processed version for non-images |
|
e5aed6c36672
[Notes] Added icons and updated styling a bit. Probalby usable now.
MrJuneJune <me@mrjunejune.com>
parents:
202
diff
changeset
|
1378 } |
| 201 | 1379 |
| 1380 // Insert into database | |
| 1381 const char *insert_query = | |
| 1382 "INSERT INTO media_uploads (access_token, original_filename, content_type, s3_key_original, s3_key_processed, status) " | |
|
244
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
1383 "VALUES (?, ?, ?, ?, ?, 'pending') RETURNING id"; |
| 201 | 1384 |
| 1385 const char *params[] = { token, filename, content_type, s3_key_original, s3_key_processed }; | |
|
244
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
1386 Deita_Result_Set *id_result = Deita_Query_Execute_Prepared( |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
1387 g_db_connection, |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
1388 insert_query, |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
1389 5, |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
1390 params, |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
1391 arena); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
1392 if (!id_result || !Deita_Result_Set_Next(id_result)) |
| 201 | 1393 { |
|
244
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
1394 if (id_result) Deita_Result_Set_Free(id_result); |
| 201 | 1395 Dowa_HashMap_Push_Arena(resp, "status", "500", arena); |
| 1396 Dowa_HashMap_Push_Arena(resp, "content-type", "application/json", arena); | |
| 1397 Dowa_HashMap_Push_Arena(resp, "body", "{\"error\":\"Failed to create media record\"}", arena); | |
| 1398 return resp; | |
| 1399 } | |
| 1400 | |
|
244
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
1401 int64 media_id = Deita_Result_Set_Get_Integer(id_result, 0); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
1402 Deita_Result_Set_Free(id_result); |
| 201 | 1403 |
| 1404 // Generate presigned PUT URL | |
| 1405 S3_Presigned_URL presigned = S3_Presign_Put(&g_s3_config, s3_key_original, content_type, g_s3_url_expires); | |
| 1406 | |
| 1407 if (!presigned.success) | |
| 1408 { | |
| 1409 Dowa_HashMap_Push_Arena(resp, "status", "500", arena); | |
| 1410 Dowa_HashMap_Push_Arena(resp, "content-type", "application/json", arena); | |
| 1411 char *error_body = Dowa_Arena_Allocate(arena, 256); | |
| 1412 snprintf(error_body, 256, "{\"error\":\"Failed to generate upload URL: %s\"}", | |
| 1413 presigned.error_message ? presigned.error_message : "unknown"); | |
| 1414 Dowa_HashMap_Push_Arena(resp, "body", error_body, arena); | |
| 1415 S3_Presigned_URL_Destroy(&presigned); | |
| 1416 return resp; | |
| 1417 } | |
| 1418 | |
|
204
e5aed6c36672
[Notes] Added icons and updated styling a bit. Probalby usable now.
MrJuneJune <me@mrjunejune.com>
parents:
202
diff
changeset
|
1419 // Build public URL using CloudFront or S3 |
|
e5aed6c36672
[Notes] Added icons and updated styling a bit. Probalby usable now.
MrJuneJune <me@mrjunejune.com>
parents:
202
diff
changeset
|
1420 char public_url[512]; |
|
e5aed6c36672
[Notes] Added icons and updated styling a bit. Probalby usable now.
MrJuneJune <me@mrjunejune.com>
parents:
202
diff
changeset
|
1421 if (g_s3_cloudfront_url[0]) |
|
e5aed6c36672
[Notes] Added icons and updated styling a bit. Probalby usable now.
MrJuneJune <me@mrjunejune.com>
parents:
202
diff
changeset
|
1422 { |
|
e5aed6c36672
[Notes] Added icons and updated styling a bit. Probalby usable now.
MrJuneJune <me@mrjunejune.com>
parents:
202
diff
changeset
|
1423 snprintf(public_url, sizeof(public_url), "%s/%s", g_s3_cloudfront_url, s3_key_original); |
|
e5aed6c36672
[Notes] Added icons and updated styling a bit. Probalby usable now.
MrJuneJune <me@mrjunejune.com>
parents:
202
diff
changeset
|
1424 } |
|
e5aed6c36672
[Notes] Added icons and updated styling a bit. Probalby usable now.
MrJuneJune <me@mrjunejune.com>
parents:
202
diff
changeset
|
1425 else |
|
e5aed6c36672
[Notes] Added icons and updated styling a bit. Probalby usable now.
MrJuneJune <me@mrjunejune.com>
parents:
202
diff
changeset
|
1426 { |
|
e5aed6c36672
[Notes] Added icons and updated styling a bit. Probalby usable now.
MrJuneJune <me@mrjunejune.com>
parents:
202
diff
changeset
|
1427 snprintf(public_url, sizeof(public_url), "https://%s.s3.%s.amazonaws.com/%s", |
|
e5aed6c36672
[Notes] Added icons and updated styling a bit. Probalby usable now.
MrJuneJune <me@mrjunejune.com>
parents:
202
diff
changeset
|
1428 g_s3_bucket, g_s3_region, s3_key_original); |
|
e5aed6c36672
[Notes] Added icons and updated styling a bit. Probalby usable now.
MrJuneJune <me@mrjunejune.com>
parents:
202
diff
changeset
|
1429 } |
|
e5aed6c36672
[Notes] Added icons and updated styling a bit. Probalby usable now.
MrJuneJune <me@mrjunejune.com>
parents:
202
diff
changeset
|
1430 |
| 201 | 1431 // Build response |
|
204
e5aed6c36672
[Notes] Added icons and updated styling a bit. Probalby usable now.
MrJuneJune <me@mrjunejune.com>
parents:
202
diff
changeset
|
1432 char *response_body = Dowa_Arena_Allocate(arena, 4096 + strlen(presigned.url) + strlen(public_url)); |
|
e5aed6c36672
[Notes] Added icons and updated styling a bit. Probalby usable now.
MrJuneJune <me@mrjunejune.com>
parents:
202
diff
changeset
|
1433 snprintf(response_body, 4096 + strlen(presigned.url) + strlen(public_url), |
|
e5aed6c36672
[Notes] Added icons and updated styling a bit. Probalby usable now.
MrJuneJune <me@mrjunejune.com>
parents:
202
diff
changeset
|
1434 "{\"media_id\":%lld,\"upload_url\":\"%s\",\"public_url\":\"%s\",\"expires\":%d}", |
|
e5aed6c36672
[Notes] Added icons and updated styling a bit. Probalby usable now.
MrJuneJune <me@mrjunejune.com>
parents:
202
diff
changeset
|
1435 (long long)media_id, presigned.url, public_url, g_s3_url_expires); |
| 201 | 1436 |
| 1437 S3_Presigned_URL_Destroy(&presigned); | |
| 1438 | |
| 1439 Dowa_HashMap_Push_Arena(resp, "status", "200", arena); | |
| 1440 Dowa_HashMap_Push_Arena(resp, "content-type", "application/json", arena); | |
| 1441 Dowa_HashMap_Push_Arena(resp, "body", response_body, arena); | |
| 1442 | |
| 1443 printf("[MEDIA] Created media_id=%lld, file=%s\n", (long long)media_id, filename); | |
| 1444 | |
| 1445 return resp; | |
| 1446 } | |
| 1447 | |
|
250
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
1448 // Worker-pool task for S3 media processing. |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
1449 void Media_Process_Background(void *arg) |
| 201 | 1450 { |
| 1451 Media_Processing_Context *ctx = (Media_Processing_Context *)arg; | |
| 1452 | |
|
250
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
1453 Seobeo_Log(SEOBEO_INFO, "[MEDIA] Background worker started for media_id=%lld\n", (long long)ctx->media_id); |
|
202
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
1454 Seobeo_Log(SEOBEO_INFO, "[MEDIA] S3 key original: %s\n", ctx->s3_key_original); |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
1455 Seobeo_Log(SEOBEO_INFO, "[MEDIA] S3 key processed: %s\n", ctx->s3_key_processed); |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
1456 Seobeo_Log(SEOBEO_INFO, "[MEDIA] DB path: %s\n", ctx->db_path); |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
1457 |
|
250
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
1458 // Open a worker-local DB connection. |
|
202
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
1459 Deita_Connection *db_conn = Deita_Connection_Create(DEITA_DATABASE_TYPE_SQLITE3, ctx->db_path); |
| 201 | 1460 if (!db_conn || !Deita_Connection_Is_Open(db_conn)) |
| 1461 { | |
|
250
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
1462 Seobeo_Log(SEOBEO_ERROR, "[MEDIA] Worker ERROR: Failed to open database for media_id=%lld\n", (long long)ctx->media_id); |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
1463 return; |
| 201 | 1464 } |
| 1465 | |
| 1466 // Update status to 'processing' | |
| 1467 const char *update_processing = | |
| 1468 "UPDATE media_uploads SET status='processing', updated_at=strftime('%s','now') WHERE id=?"; | |
| 1469 char media_id_str[32]; | |
| 1470 snprintf(media_id_str, sizeof(media_id_str), "%lld", (long long)ctx->media_id); | |
| 1471 const char *params[] = { media_id_str }; | |
|
202
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
1472 int32 update_result = Deita_Query_Execute_Update_Prepared(db_conn, update_processing, 1, params); |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
1473 Seobeo_Log(SEOBEO_INFO, "[MEDIA] Updated status to 'processing' for media_id=%lld (result=%d)\n", (long long)ctx->media_id, update_result); |
| 201 | 1474 |
| 1475 // Generate presigned GET URL for download (10 min expiry) | |
|
202
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
1476 Seobeo_Log(SEOBEO_INFO, "[MEDIA] Generating presigned GET URL for media_id=%lld\n", (long long)ctx->media_id); |
| 201 | 1477 S3_Presigned_URL download_url = S3_Presign_Get(&ctx->s3_config, ctx->s3_key_original, 600); |
| 1478 if (!download_url.success) | |
| 1479 { | |
|
202
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
1480 const char *error_msg = download_url.error_message ? download_url.error_message : "Failed to generate download URL"; |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
1481 Seobeo_Log(SEOBEO_ERROR, "[MEDIA] ERROR: Failed to generate download URL for media_id=%lld: %s\n", |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
1482 (long long)ctx->media_id, error_msg); |
| 201 | 1483 const char *update_error = |
| 1484 "UPDATE media_uploads SET status='error', error_message=?, updated_at=strftime('%s','now') WHERE id=?"; | |
|
202
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
1485 const char *error_params[] = { error_msg, media_id_str }; |
| 201 | 1486 Deita_Query_Execute_Update_Prepared(db_conn, update_error, 2, error_params); |
| 1487 S3_Presigned_URL_Destroy(&download_url); | |
| 1488 Deita_Connection_Close(db_conn); | |
|
250
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
1489 return; |
| 201 | 1490 } |
|
202
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
1491 Seobeo_Log(SEOBEO_INFO, "[MEDIA] Generated presigned URL: %.100s...\n", download_url.url); |
| 201 | 1492 |
| 1493 // Generate temp file paths | |
| 1494 char tmp_input[256]; | |
| 1495 char tmp_output[256]; | |
| 1496 char *uuid_input = malloc(UUID_LEN); | |
| 1497 char *uuid_output = malloc(UUID_LEN); | |
|
250
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
1498 uint32 seed1 = |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
1499 (uint32)time(NULL) ^ |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
1500 (uint32)Seobeo_Thread_Current_Id() ^ |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
1501 counter++; |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
1502 uint32 seed2 = |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
1503 (uint32)time(NULL) ^ |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
1504 (uint32)Seobeo_Thread_Current_Id() ^ |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
1505 counter++; |
| 201 | 1506 Dowa_String_UUID(seed1, uuid_input); |
| 1507 Dowa_String_UUID(seed2, uuid_output); | |
| 1508 snprintf(tmp_input, sizeof(tmp_input), "/tmp/%s", uuid_input); | |
| 1509 snprintf(tmp_output, sizeof(tmp_output), "/tmp/%s.webp", uuid_output); | |
| 1510 free(uuid_input); | |
| 1511 free(uuid_output); | |
| 1512 | |
| 1513 // Download from S3 | |
|
202
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
1514 Seobeo_Log(SEOBEO_INFO, "[MEDIA] Downloading from S3 to %s for media_id=%lld\n", tmp_input, (long long)ctx->media_id); |
| 201 | 1515 Seobeo_Client_Request *download_req = Seobeo_Client_Request_Create(download_url.url); |
| 1516 Seobeo_Client_Request_Set_Download_Path(download_req, tmp_input); | |
| 1517 Seobeo_Client_Response *download_resp = Seobeo_Client_Request_Execute(download_req); | |
| 1518 | |
| 1519 S3_Presigned_URL_Destroy(&download_url); | |
| 1520 | |
| 1521 if (!download_resp || download_resp->status_code != 200) | |
| 1522 { | |
|
202
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
1523 int status = download_resp ? download_resp->status_code : 0; |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
1524 Seobeo_Log(SEOBEO_ERROR, "[MEDIA] ERROR: Failed to download from S3 for media_id=%lld (status=%d)\n", |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
1525 (long long)ctx->media_id, status); |
| 201 | 1526 const char *update_error = |
| 1527 "UPDATE media_uploads SET status='error', error_message=?, updated_at=strftime('%s','now') WHERE id=?"; | |
| 1528 const char *error_params[] = { "Failed to download from S3", media_id_str }; | |
| 1529 Deita_Query_Execute_Update_Prepared(db_conn, update_error, 2, error_params); | |
| 1530 if (download_req) Seobeo_Client_Request_Destroy(download_req); | |
| 1531 if (download_resp) Seobeo_Client_Response_Destroy(download_resp); | |
| 1532 unlink(tmp_input); | |
| 1533 Deita_Connection_Close(db_conn); | |
|
250
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
1534 return; |
| 201 | 1535 } |
| 1536 | |
|
202
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
1537 Seobeo_Log(SEOBEO_INFO, "[MEDIA] Successfully downloaded file to %s\n", tmp_input); |
| 201 | 1538 Seobeo_Client_Request_Destroy(download_req); |
| 1539 Seobeo_Client_Response_Destroy(download_resp); | |
| 1540 | |
| 1541 // Convert to webp using FFmpeg | |
| 1542 char cmd[1024]; | |
| 1543 char log_file[256]; | |
| 1544 snprintf(log_file, sizeof(log_file), "/tmp/ffmpeg_%lld.log", (long long)ctx->media_id); | |
| 1545 snprintf(cmd, sizeof(cmd), "ffmpeg -y -i %s -quality 80 %s 2>%s", | |
| 1546 tmp_input, tmp_output, log_file); | |
| 1547 | |
|
202
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
1548 Seobeo_Log(SEOBEO_INFO, "[MEDIA] Running FFmpeg: %s\n", cmd); |
| 201 | 1549 int ffmpeg_result = system(cmd); |
|
202
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
1550 Seobeo_Log(SEOBEO_INFO, "[MEDIA] FFmpeg result: %d for media_id=%lld\n", ffmpeg_result, (long long)ctx->media_id); |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
1551 |
| 201 | 1552 if (ffmpeg_result != 0) |
| 1553 { | |
|
202
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
1554 Seobeo_Log(SEOBEO_ERROR, "[MEDIA] ERROR: FFmpeg conversion failed for media_id=%lld (exit code %d). Check log: %s\n", |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
1555 (long long)ctx->media_id, ffmpeg_result, log_file); |
| 201 | 1556 const char *update_error = |
| 1557 "UPDATE media_uploads SET status='error', error_message=?, updated_at=strftime('%s','now') WHERE id=?"; | |
| 1558 const char *error_params[] = { "Image conversion failed", media_id_str }; | |
| 1559 Deita_Query_Execute_Update_Prepared(db_conn, update_error, 2, error_params); | |
|
202
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
1560 unlink(tmp_input); |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
1561 unlink(tmp_output); |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
1562 Deita_Connection_Close(db_conn); |
|
250
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
1563 return; |
|
202
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
1564 } |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
1565 Seobeo_Log(SEOBEO_INFO, "[MEDIA] Successfully converted to webp: %s\n", tmp_output); |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
1566 |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
1567 // Upload processed file to S3 |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
1568 Seobeo_Log(SEOBEO_INFO, "[MEDIA] Uploading processed file to S3: %s -> %s\n", tmp_output, ctx->s3_key_processed); |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
1569 S3_Result upload_result = S3_Upload_File_With_Content_Type( |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
1570 &ctx->s3_config, tmp_output, ctx->s3_key_processed, "image/webp"); |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
1571 |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
1572 if (!upload_result.success) |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
1573 { |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
1574 const char *error_msg = upload_result.error_message ? upload_result.error_message : "Failed to upload processed file"; |
|
206
240337164a80
[Seobeo] SSL should be used for large file as well lol.
MrJuneJune <me@mrjunejune.com>
parents:
204
diff
changeset
|
1575 Seobeo_Log(SEOBEO_ERROR, "[MEDIA] ERROR: Failed to upload processed file for media_id=%lld: %s (HTTP status: %d)\n", |
|
240337164a80
[Seobeo] SSL should be used for large file as well lol.
MrJuneJune <me@mrjunejune.com>
parents:
204
diff
changeset
|
1576 (long long)ctx->media_id, error_msg, upload_result.status_code); |
|
202
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
1577 const char *update_error = |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
1578 "UPDATE media_uploads SET status='error', error_message=?, updated_at=strftime('%s','now') WHERE id=?"; |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
1579 const char *error_params[] = { error_msg, media_id_str }; |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
1580 Deita_Query_Execute_Update_Prepared(db_conn, update_error, 2, error_params); |
| 201 | 1581 unlink(tmp_input); |
| 1582 unlink(tmp_output); | |
| 1583 Deita_Connection_Close(db_conn); | |
|
250
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
1584 return; |
| 201 | 1585 } |
| 1586 | |
|
202
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
1587 Seobeo_Log(SEOBEO_INFO, "[MEDIA] Successfully uploaded processed file to S3\n"); |
| 201 | 1588 |
| 1589 // Update status to 'finished' | |
| 1590 const char *update_finished = | |
| 1591 "UPDATE media_uploads SET status='finished', updated_at=strftime('%s','now') WHERE id=?"; | |
| 1592 Deita_Query_Execute_Update_Prepared(db_conn, update_finished, 1, params); | |
| 1593 | |
|
202
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
1594 Seobeo_Log(SEOBEO_INFO, "[MEDIA] Successfully processed media_id=%lld - COMPLETE\n", (long long)ctx->media_id); |
| 201 | 1595 |
| 1596 // Cleanup | |
| 1597 unlink(tmp_input); | |
| 1598 unlink(tmp_output); | |
| 1599 Deita_Connection_Close(db_conn); | |
| 1600 } | |
| 1601 | |
| 1602 // Media Upload API - Mark uploaded | |
| 1603 // POST /api/media/:id/uploaded | |
| 1604 // Headers: Authorization: Bearer <token> | |
| 1605 Seobeo_Request_Entry *MediaUploaded(Seobeo_Request_Entry *req, Dowa_Arena *arena) | |
| 1606 { | |
| 1607 Seobeo_Request_Entry *resp = NULL; | |
| 1608 | |
| 1609 // Check auth token | |
| 1610 void *auth_kv = Dowa_HashMap_Get_Ptr(req, "Authorization"); | |
| 1611 if (!auth_kv) | |
| 1612 { | |
| 1613 Dowa_HashMap_Push_Arena(resp, "status", "401", arena); | |
| 1614 Dowa_HashMap_Push_Arena(resp, "content-type", "application/json", arena); | |
| 1615 Dowa_HashMap_Push_Arena(resp, "body", "{\"error\":\"Missing Authorization header\"}", arena); | |
| 1616 return resp; | |
| 1617 } | |
| 1618 | |
| 1619 const char *auth_header = ((Seobeo_Request_Entry*)auth_kv)->value; | |
| 1620 if (strncmp(auth_header, "Bearer ", 7) != 0) | |
| 1621 { | |
| 1622 Dowa_HashMap_Push_Arena(resp, "status", "401", arena); | |
| 1623 Dowa_HashMap_Push_Arena(resp, "content-type", "application/json", arena); | |
| 1624 Dowa_HashMap_Push_Arena(resp, "body", "{\"error\":\"Invalid Authorization format\"}", arena); | |
| 1625 return resp; | |
| 1626 } | |
| 1627 | |
| 1628 const char *token = auth_header + 7; | |
| 1629 if (strlen(g_upload_auth_token) == 0 || strcmp(token, g_upload_auth_token) != 0) | |
| 1630 { | |
| 1631 Dowa_HashMap_Push_Arena(resp, "status", "403", arena); | |
| 1632 Dowa_HashMap_Push_Arena(resp, "content-type", "application/json", arena); | |
| 1633 Dowa_HashMap_Push_Arena(resp, "body", "{\"error\":\"Invalid token\"}", arena); | |
| 1634 return resp; | |
| 1635 } | |
| 1636 | |
| 1637 if (!g_db_connection) | |
| 1638 { | |
| 1639 Dowa_HashMap_Push_Arena(resp, "status", "500", arena); | |
| 1640 Dowa_HashMap_Push_Arena(resp, "content-type", "application/json", arena); | |
| 1641 Dowa_HashMap_Push_Arena(resp, "body", "{\"error\":\"Database not available\"}", arena); | |
| 1642 return resp; | |
| 1643 } | |
| 1644 | |
| 1645 // Extract media_id from URL params | |
| 1646 void *id_kv = Dowa_HashMap_Get_Ptr(req, ":id"); | |
| 1647 if (!id_kv) | |
| 1648 { | |
| 1649 Dowa_HashMap_Push_Arena(resp, "status", "400", arena); | |
| 1650 Dowa_HashMap_Push_Arena(resp, "content-type", "application/json", arena); | |
| 1651 Dowa_HashMap_Push_Arena(resp, "body", "{\"error\":\"Missing media ID\"}", arena); | |
| 1652 return resp; | |
| 1653 } | |
| 1654 | |
| 1655 const char *media_id_str = ((Seobeo_Request_Entry*)id_kv)->value; | |
| 1656 int64 media_id = atoll(media_id_str); | |
| 1657 | |
| 1658 // Verify access_token matches and get content_type | |
| 1659 const char *select_query = | |
| 1660 "SELECT content_type, s3_key_original, s3_key_processed FROM media_uploads WHERE id = ? AND access_token = ?"; | |
| 1661 const char *select_params[] = { media_id_str, token }; | |
| 1662 | |
| 1663 Deita_Result_Set *p_result = Deita_Query_Execute_Prepared(g_db_connection, select_query, 2, select_params, arena); | |
| 1664 | |
| 1665 if (!p_result || !Deita_Result_Set_Next(p_result)) | |
| 1666 { | |
| 1667 if (p_result) Deita_Result_Set_Free(p_result); | |
| 1668 Dowa_HashMap_Push_Arena(resp, "status", "404", arena); | |
| 1669 Dowa_HashMap_Push_Arena(resp, "content-type", "application/json", arena); | |
| 1670 Dowa_HashMap_Push_Arena(resp, "body", "{\"error\":\"Media not found or access denied\"}", arena); | |
| 1671 return resp; | |
| 1672 } | |
| 1673 | |
| 1674 const char *content_type = Deita_Result_Set_Get_Text(p_result, 0); | |
| 1675 const char *s3_key_original = Deita_Result_Set_Get_Text(p_result, 1); | |
| 1676 const char *s3_key_processed = Deita_Result_Set_Get_Text(p_result, 2); | |
| 1677 | |
| 1678 // Copy values before freeing result set | |
| 1679 char content_type_copy[128]; | |
| 1680 char s3_key_original_copy[512]; | |
| 1681 char s3_key_processed_copy[512]; | |
| 1682 strncpy(content_type_copy, content_type, sizeof(content_type_copy) - 1); | |
| 1683 strncpy(s3_key_original_copy, s3_key_original, sizeof(s3_key_original_copy) - 1); | |
| 1684 strncpy(s3_key_processed_copy, s3_key_processed, sizeof(s3_key_processed_copy) - 1); | |
| 1685 content_type_copy[sizeof(content_type_copy) - 1] = '\0'; | |
| 1686 s3_key_original_copy[sizeof(s3_key_original_copy) - 1] = '\0'; | |
| 1687 s3_key_processed_copy[sizeof(s3_key_processed_copy) - 1] = '\0'; | |
| 1688 | |
| 1689 Deita_Result_Set_Free(p_result); | |
| 1690 | |
| 1691 // Update status to 'uploaded' | |
| 1692 const char *update_query = | |
| 1693 "UPDATE media_uploads SET status='uploaded', updated_at=strftime('%s','now') WHERE id=?"; | |
| 1694 const char *update_params[] = { media_id_str }; | |
| 1695 int32 result = Deita_Query_Execute_Update_Prepared(g_db_connection, update_query, 1, update_params); | |
| 1696 | |
| 1697 if (result < 0) | |
| 1698 { | |
| 1699 Dowa_HashMap_Push_Arena(resp, "status", "500", arena); | |
| 1700 Dowa_HashMap_Push_Arena(resp, "content-type", "application/json", arena); | |
| 1701 Dowa_HashMap_Push_Arena(resp, "body", "{\"error\":\"Failed to update status\"}", arena); | |
| 1702 return resp; | |
| 1703 } | |
| 1704 | |
|
202
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
1705 Seobeo_Log(SEOBEO_INFO, "[MEDIA] Content type for media_id=%lld: '%s'\n", (long long)media_id, content_type_copy); |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
1706 |
|
250
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
1707 // Images are processed asynchronously by the bounded media pool. |
| 201 | 1708 if (strncmp(content_type_copy, "image/", 6) == 0) |
| 1709 { | |
|
250
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
1710 Seobeo_Log(SEOBEO_INFO, "[MEDIA] Queueing image processing for media_id=%lld\n", (long long)media_id); |
|
202
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
1711 |
|
250
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
1712 // The pool owns this context after a successful submission. |
| 201 | 1713 Media_Processing_Context *ctx = malloc(sizeof(Media_Processing_Context)); |
|
250
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
1714 if (!ctx) |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
1715 { |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
1716 Dowa_HashMap_Push_Arena(resp, "status", "500", arena); |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
1717 Dowa_HashMap_Push_Arena(resp, "content-type", "application/json", arena); |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
1718 Dowa_HashMap_Push_Arena(resp, "body", "{\"error\":\"Unable to allocate media work\"}", arena); |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
1719 return resp; |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
1720 } |
| 201 | 1721 ctx->media_id = media_id; |
| 1722 strncpy(ctx->s3_key_original, s3_key_original_copy, sizeof(ctx->s3_key_original) - 1); | |
| 1723 strncpy(ctx->s3_key_processed, s3_key_processed_copy, sizeof(ctx->s3_key_processed) - 1); | |
| 1724 strncpy(ctx->content_type, content_type_copy, sizeof(ctx->content_type) - 1); | |
| 1725 strncpy(ctx->access_token, token, sizeof(ctx->access_token) - 1); | |
|
202
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
1726 strncpy(ctx->db_path, g_db_path, sizeof(ctx->db_path) - 1); |
| 201 | 1727 ctx->s3_key_original[sizeof(ctx->s3_key_original) - 1] = '\0'; |
| 1728 ctx->s3_key_processed[sizeof(ctx->s3_key_processed) - 1] = '\0'; | |
| 1729 ctx->content_type[sizeof(ctx->content_type) - 1] = '\0'; | |
| 1730 ctx->access_token[sizeof(ctx->access_token) - 1] = '\0'; | |
|
202
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
1731 ctx->db_path[sizeof(ctx->db_path) - 1] = '\0'; |
| 201 | 1732 ctx->s3_config = g_s3_config; |
| 1733 | |
|
250
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
1734 Seobeo_Worker_Result worker_result = |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
1735 g_media_worker_pool |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
1736 ? Seobeo_Worker_Pool_Submit( |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
1737 g_media_worker_pool, |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
1738 Media_Process_Background, |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
1739 ctx, |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
1740 free) |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
1741 : SEOBEO_WORKER_STOPPED; |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
1742 if (worker_result != SEOBEO_WORKER_OK) |
| 201 | 1743 { |
|
250
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
1744 Seobeo_Log( |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
1745 SEOBEO_ERROR, |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
1746 "[MEDIA] Worker submission failed with result=%d for media_id=%lld\n", |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
1747 worker_result, |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
1748 (long long)media_id); |
| 201 | 1749 free(ctx); |
|
250
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
1750 const char *update_error = |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
1751 "UPDATE media_uploads SET status='error', error_message=?, updated_at=strftime('%s','now') WHERE id=?"; |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
1752 const char *error_params[] = { |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
1753 "Media worker queue is unavailable", |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
1754 media_id_str, |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
1755 }; |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
1756 Deita_Query_Execute_Update_Prepared( |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
1757 g_db_connection, |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
1758 update_error, |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
1759 2, |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
1760 error_params); |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
1761 Dowa_HashMap_Push_Arena(resp, "status", "503", arena); |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
1762 Dowa_HashMap_Push_Arena(resp, "content-type", "application/json", arena); |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
1763 Dowa_HashMap_Push_Arena(resp, "body", "{\"error\":\"Media worker queue is unavailable\"}", arena); |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
1764 return resp; |
| 201 | 1765 } |
|
250
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
1766 Seobeo_Log( |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
1767 SEOBEO_INFO, |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
1768 "[MEDIA] Submitted media_id=%lld to the worker pool\n", |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
1769 (long long)media_id); |
| 201 | 1770 } |
|
202
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
1771 else |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
1772 { |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
1773 Seobeo_Log(SEOBEO_INFO, "[MEDIA] Non-image file, skipping background processing for media_id=%lld\n", (long long)media_id); |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
1774 } |
| 201 | 1775 |
| 1776 Dowa_HashMap_Push_Arena(resp, "status", "200", arena); | |
| 1777 Dowa_HashMap_Push_Arena(resp, "content-type", "application/json", arena); | |
| 1778 Dowa_HashMap_Push_Arena(resp, "body", "{\"success\":true,\"status\":\"uploaded\"}", arena); | |
| 1779 | |
| 1780 Seobeo_Log(SEOBEO_INFO, "[MEDIA] Marked uploaded media_id=%lld\n", (long long)media_id); | |
| 1781 | |
| 1782 return resp; | |
| 1783 } | |
| 1784 | |
| 1785 // Media Upload API - Get status | |
| 1786 // GET /api/media/:id/status | |
| 1787 // Headers: Authorization: Bearer <token> | |
| 1788 // Returns: {"id": 123, "status": "finished", "processed_url": "https://...", "error_message": null} | |
| 1789 Seobeo_Request_Entry *MediaStatus(Seobeo_Request_Entry *req, Dowa_Arena *arena) | |
| 1790 { | |
| 1791 Seobeo_Request_Entry *resp = NULL; | |
| 1792 | |
| 1793 // Check auth token | |
| 1794 void *auth_kv = Dowa_HashMap_Get_Ptr(req, "Authorization"); | |
| 1795 if (!auth_kv) | |
| 1796 { | |
| 1797 Dowa_HashMap_Push_Arena(resp, "status", "401", arena); | |
| 1798 Dowa_HashMap_Push_Arena(resp, "content-type", "application/json", arena); | |
| 1799 Dowa_HashMap_Push_Arena(resp, "body", "{\"error\":\"Missing Authorization header\"}", arena); | |
| 1800 return resp; | |
| 1801 } | |
| 1802 | |
| 1803 const char *auth_header = ((Seobeo_Request_Entry*)auth_kv)->value; | |
| 1804 if (strncmp(auth_header, "Bearer ", 7) != 0) | |
| 1805 { | |
| 1806 Dowa_HashMap_Push_Arena(resp, "status", "401", arena); | |
| 1807 Dowa_HashMap_Push_Arena(resp, "content-type", "application/json", arena); | |
| 1808 Dowa_HashMap_Push_Arena(resp, "body", "{\"error\":\"Invalid Authorization format\"}", arena); | |
| 1809 return resp; | |
| 1810 } | |
| 1811 | |
| 1812 const char *token = auth_header + 7; | |
| 1813 if (strlen(g_upload_auth_token) == 0 || strcmp(token, g_upload_auth_token) != 0) | |
| 1814 { | |
| 1815 Dowa_HashMap_Push_Arena(resp, "status", "403", arena); | |
| 1816 Dowa_HashMap_Push_Arena(resp, "content-type", "application/json", arena); | |
| 1817 Dowa_HashMap_Push_Arena(resp, "body", "{\"error\":\"Invalid token\"}", arena); | |
| 1818 return resp; | |
| 1819 } | |
| 1820 | |
| 1821 if (!g_db_connection) | |
| 1822 { | |
| 1823 Dowa_HashMap_Push_Arena(resp, "status", "500", arena); | |
| 1824 Dowa_HashMap_Push_Arena(resp, "content-type", "application/json", arena); | |
| 1825 Dowa_HashMap_Push_Arena(resp, "body", "{\"error\":\"Database not available\"}", arena); | |
| 1826 return resp; | |
| 1827 } | |
| 1828 | |
| 1829 // Extract media_id from URL params | |
| 1830 void *id_kv = Dowa_HashMap_Get_Ptr(req, ":id"); | |
| 1831 if (!id_kv) | |
| 1832 { | |
| 1833 Dowa_HashMap_Push_Arena(resp, "status", "400", arena); | |
| 1834 Dowa_HashMap_Push_Arena(resp, "content-type", "application/json", arena); | |
| 1835 Dowa_HashMap_Push_Arena(resp, "body", "{\"error\":\"Missing media ID\"}", arena); | |
| 1836 return resp; | |
| 1837 } | |
| 1838 | |
| 1839 const char *media_id_str = ((Seobeo_Request_Entry*)id_kv)->value; | |
| 1840 | |
| 1841 // Query media status | |
| 1842 const char *select_query = | |
|
202
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
1843 "SELECT id, status, s3_key_original, s3_key_processed, error_message FROM media_uploads WHERE id = ? AND access_token = ?"; |
| 201 | 1844 const char *select_params[] = { media_id_str, token }; |
| 1845 | |
| 1846 Deita_Result_Set *p_result = Deita_Query_Execute_Prepared(g_db_connection, select_query, 2, select_params, arena); | |
| 1847 | |
| 1848 if (!p_result || !Deita_Result_Set_Next(p_result)) | |
| 1849 { | |
| 1850 if (p_result) Deita_Result_Set_Free(p_result); | |
| 1851 Dowa_HashMap_Push_Arena(resp, "status", "404", arena); | |
| 1852 Dowa_HashMap_Push_Arena(resp, "content-type", "application/json", arena); | |
| 1853 Dowa_HashMap_Push_Arena(resp, "body", "{\"error\":\"Media not found\"}", arena); | |
| 1854 return resp; | |
| 1855 } | |
| 1856 | |
| 1857 int64 id = Deita_Result_Set_Get_Integer(p_result, 0); | |
| 1858 const char *status = Deita_Result_Set_Get_Text(p_result, 1); | |
|
202
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
1859 const char *s3_key_original = Deita_Result_Set_Get_Text(p_result, 2); |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
1860 const char *s3_key_processed = Deita_Result_Set_Get_Text(p_result, 3); |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
1861 const char *error_message = Deita_Result_Set_Get_Text(p_result, 4); |
| 201 | 1862 |
|
202
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
1863 // Build CloudFront URL for processed file if status is 'finished' |
| 201 | 1864 char processed_url[1024] = {0}; |
| 1865 if (strcmp(status, "finished") == 0 && s3_key_processed && strlen(s3_key_processed) > 0) | |
| 1866 { | |
| 1867 if (g_s3_cloudfront_url[0]) | |
| 1868 { | |
| 1869 snprintf(processed_url, sizeof(processed_url), "%s/%s", g_s3_cloudfront_url, s3_key_processed); | |
| 1870 } | |
| 1871 else | |
| 1872 { | |
| 1873 snprintf(processed_url, sizeof(processed_url), "https://%s.s3.%s.amazonaws.com/%s", | |
| 1874 g_s3_bucket, g_s3_region, s3_key_processed); | |
| 1875 } | |
| 1876 } | |
| 1877 | |
|
202
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
1878 // Build CloudFront URL for original file (for non-images or before processing completes) |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
1879 char original_url[1024] = {0}; |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
1880 if (s3_key_original && strlen(s3_key_original) > 0) |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
1881 { |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
1882 if (g_s3_cloudfront_url[0]) |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
1883 { |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
1884 snprintf(original_url, sizeof(original_url), "%s/%s", g_s3_cloudfront_url, s3_key_original); |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
1885 } |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
1886 else |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
1887 { |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
1888 snprintf(original_url, sizeof(original_url), "https://%s.s3.%s.amazonaws.com/%s", |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
1889 g_s3_bucket, g_s3_region, s3_key_original); |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
1890 } |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
1891 } |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
1892 |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
1893 // Build JSON response with both processed_url and original_url |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
1894 char *response_body = Dowa_Arena_Allocate(arena, 3072); |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
1895 |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
1896 // Build the base response |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
1897 int offset = snprintf(response_body, 3072, |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
1898 "{\"id\":%lld,\"status\":\"%s\",", |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
1899 (long long)id, status); |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
1900 |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
1901 // Add processed_url |
| 201 | 1902 if (strlen(processed_url) > 0) |
| 1903 { | |
|
202
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
1904 offset += snprintf(response_body + offset, 3072 - offset, |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
1905 "\"processed_url\":\"%s\",", processed_url); |
| 201 | 1906 } |
| 1907 else | |
| 1908 { | |
|
202
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
1909 offset += snprintf(response_body + offset, 3072 - offset, |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
1910 "\"processed_url\":null,"); |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
1911 } |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
1912 |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
1913 // Add original_url |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
1914 if (strlen(original_url) > 0) |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
1915 { |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
1916 offset += snprintf(response_body + offset, 3072 - offset, |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
1917 "\"original_url\":\"%s\",", original_url); |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
1918 } |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
1919 else |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
1920 { |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
1921 offset += snprintf(response_body + offset, 3072 - offset, |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
1922 "\"original_url\":null,"); |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
1923 } |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
1924 |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
1925 // Add error_message |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
1926 if (error_message && strlen(error_message) > 0) |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
1927 { |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
1928 snprintf(response_body + offset, 3072 - offset, |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
1929 "\"error_message\":\"%s\"}", error_message); |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
1930 } |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
1931 else |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
1932 { |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
1933 snprintf(response_body + offset, 3072 - offset, |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
1934 "\"error_message\":null}"); |
| 201 | 1935 } |
| 1936 | |
| 1937 Deita_Result_Set_Free(p_result); | |
| 1938 | |
| 1939 Dowa_HashMap_Push_Arena(resp, "status", "200", arena); | |
| 1940 Dowa_HashMap_Push_Arena(resp, "content-type", "application/json", arena); | |
| 1941 Dowa_HashMap_Push_Arena(resp, "body", response_body, arena); | |
| 1942 | |
| 1943 return resp; | |
| 1944 } | |
| 1945 | |
|
7
114cad94008f
[Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
1946 int main(void) |
|
114cad94008f
[Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
1947 { |
|
200
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1948 // Load server config |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1949 load_config("mrjunejune/.config"); |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1950 |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1951 // Load S3 credentials from .env |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1952 FILE *env_file = fopen(".env", "r"); |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1953 static char s3_access_key[128] = {0}; |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1954 static char s3_secret_key[128] = {0}; |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1955 |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1956 if (env_file) |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1957 { |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1958 char line[512]; |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1959 while (fgets(line, sizeof(line), env_file)) |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1960 { |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1961 if (strncmp(line, "AWS_MRJUNEJUNE_ACCESS_KEY=", 26) == 0) |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1962 { |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1963 char *val = line + 26; |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1964 size_t len = strlen(val); |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1965 while (len > 0 && (val[len-1] == '\n' || val[len-1] == '\r')) val[--len] = '\0'; |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1966 strncpy(s3_access_key, val, sizeof(s3_access_key) - 1); |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1967 } |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1968 else if (strncmp(line, "AWS_MRJUNEJUNE_SECRET_ACCESS_KEY=", 33) == 0) |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1969 { |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1970 char *val = line + 33; |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1971 size_t len = strlen(val); |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1972 while (len > 0 && (val[len-1] == '\n' || val[len-1] == '\r')) val[--len] = '\0'; |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1973 strncpy(s3_secret_key, val, sizeof(s3_secret_key) - 1); |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1974 } |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1975 } |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1976 fclose(env_file); |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1977 } |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1978 |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1979 // Initialize S3 config |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1980 g_s3_config.access_key_id = s3_access_key; |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1981 g_s3_config.secret_access_key = s3_secret_key; |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1982 g_s3_config.region = g_s3_region; |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1983 g_s3_config.bucket = g_s3_bucket; |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1984 g_s3_config.endpoint = NULL; |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1985 g_s3_config.use_path_style = FALSE; |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1986 |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1987 printf("[S3] Configured: region=%s, bucket=%s, key=%s...\n", |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1988 g_s3_region, g_s3_bucket, s3_access_key[0] ? "***" : "(missing)"); |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1989 |
|
202
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
1990 // Show current working directory |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
1991 char cwd[1024]; |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
1992 if (getcwd(cwd, sizeof(cwd)) != NULL) |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
1993 { |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
1994 printf("[STARTUP] Current working directory: %s\n", cwd); |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
1995 printf("[STARTUP] Database path (relative): %s\n", g_db_path); |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
1996 } |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
1997 |
| 201 | 1998 // Initialize database |
| 1999 init_database(); | |
| 2000 | |
|
250
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
2001 g_media_worker_pool = Seobeo_Worker_Pool_Create(2, 16); |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
2002 if (!g_media_worker_pool) |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
2003 { |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
2004 Seobeo_Log( |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
2005 SEOBEO_ERROR, |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
2006 "[MEDIA] Unable to initialize the media worker pool\n"); |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
2007 } |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
2008 |
|
72
4532ce6d9eb8
[Seobeo] Added router to the server logic. Few dowa string manipulation logics.
June Park <parkjune1995@gmail.com>
parents:
36
diff
changeset
|
2009 Seobeo_Router_Init(); |
|
125
f236c895604e
[MrJuneJune] Added web socket for chat to this.
June Park <parkjune1995@gmail.com>
parents:
124
diff
changeset
|
2010 |
|
77
c348ac875294
[Seobeo] Server side rendering.
June Park <parkjune1995@gmail.com>
parents:
72
diff
changeset
|
2011 Seobeo_Router_Register("GET", "/", GetHomePage); |
|
79
5710108c949e
[Seobeo] Added Redirect logic.
June Park <parkjune1995@gmail.com>
parents:
78
diff
changeset
|
2012 Seobeo_Router_Register("GET", "/index.html", GetRedirectHomePage); |
| 78 | 2013 |
| 2014 Seobeo_Router_Register("GET", "/resume", GetResume); | |
|
79
5710108c949e
[Seobeo] Added Redirect logic.
June Park <parkjune1995@gmail.com>
parents:
78
diff
changeset
|
2015 Seobeo_Router_Register("GET", "/resume/index.html", GetRedirectResume); |
| 78 | 2016 |
| 2017 Seobeo_Router_Register("GET", "/tools", GetTools); | |
|
79
5710108c949e
[Seobeo] Added Redirect logic.
June Park <parkjune1995@gmail.com>
parents:
78
diff
changeset
|
2018 Seobeo_Router_Register("GET", "/tools/index.html", GetRedirectTools); |
| 78 | 2019 |
| 2020 Seobeo_Router_Register("GET", "/tools/markdown_to_html", GetMDToHTML); | |
|
79
5710108c949e
[Seobeo] Added Redirect logic.
June Park <parkjune1995@gmail.com>
parents:
78
diff
changeset
|
2021 Seobeo_Router_Register("GET", "/tools/markdown_to_html/index.html", GetRedirectMarkDownToHtml); |
| 78 | 2022 |
|
92
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
2023 Seobeo_Router_Register("GET", "/tools/file_converter", GetFileConverter); |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
2024 Seobeo_Router_Register("GET", "/tools/file_converter/index.html", GetRedirectFileConverter); |
|
242
543df0fe7168
[tools] Add full HLS player support
MrJuneJune <me@mrjunejune.com>
parents:
241
diff
changeset
|
2025 Seobeo_Router_Register("GET", "/tools/hls_player", GetHlsPlayer); |
|
543df0fe7168
[tools] Add full HLS player support
MrJuneJune <me@mrjunejune.com>
parents:
241
diff
changeset
|
2026 Seobeo_Router_Register("GET", "/tools/hls_player/index.html", GetRedirectHlsPlayer); |
|
244
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
2027 Seobeo_Router_Register("GET", "/tools/latex_editor", GetLatexEditor); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
2028 Seobeo_Router_Register("GET", "/tools/latex_editor/index.html", GetRedirectLatexEditor); |
|
92
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
2029 |
|
100
65e5a5b89a4e
[Seobeo] Migrated everything to this page.
June Park <parkjune1995@gmail.com>
parents:
96
diff
changeset
|
2030 // -- File converter --/ |
|
92
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
2031 Seobeo_Router_Register("POST", "/api/convert/image-to-webp", ConvertImageToWebP); |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
2032 Seobeo_Router_Register("POST", "/api/convert/video-to-mp4", ConvertVideoToMP4); |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
2033 Seobeo_Router_Register("GET", "/api/download/:filename", DownloadConvertedFile); |
|
244
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
2034 Seobeo_Router_Register("POST", "/api/latex/render", RenderLatexPdf); |
|
92
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
2035 |
|
200
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
2036 // -- S3 Upload --/ |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
2037 Seobeo_Router_Register("POST", "/api/s3/upload-url", GetS3UploadUrl); |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
2038 |
| 201 | 2039 // -- Media Upload --/ |
| 2040 Seobeo_Router_Register("POST", "/api/media/create", MediaCreate); | |
| 2041 Seobeo_Router_Register("POST", "/api/media/:id/uploaded", MediaUploaded); | |
| 2042 Seobeo_Router_Register("GET", "/api/media/:id/status", MediaStatus); | |
| 2043 | |
| 2044 // -- Editor --/ | |
| 2045 Seobeo_Router_Register("POST", "/api/editor/save", EditorSave); | |
| 2046 Seobeo_Router_Register("GET", "/api/editor/load/:doc_id", EditorLoad); | |
| 2047 | |
|
100
65e5a5b89a4e
[Seobeo] Migrated everything to this page.
June Park <parkjune1995@gmail.com>
parents:
96
diff
changeset
|
2048 // -- Blog --/ |
|
65e5a5b89a4e
[Seobeo] Migrated everything to this page.
June Park <parkjune1995@gmail.com>
parents:
96
diff
changeset
|
2049 Seobeo_Router_Register("GET", "/blog", RenderBlogList); |
|
65e5a5b89a4e
[Seobeo] Migrated everything to this page.
June Park <parkjune1995@gmail.com>
parents:
96
diff
changeset
|
2050 Seobeo_Router_Register("GET", "/blog/:blog_id", RenderBlog); |
|
65e5a5b89a4e
[Seobeo] Migrated everything to this page.
June Park <parkjune1995@gmail.com>
parents:
96
diff
changeset
|
2051 |
|
125
f236c895604e
[MrJuneJune] Added web socket for chat to this.
June Park <parkjune1995@gmail.com>
parents:
124
diff
changeset
|
2052 // -- Talk --/ |
|
f236c895604e
[MrJuneJune] Added web socket for chat to this.
June Park <parkjune1995@gmail.com>
parents:
124
diff
changeset
|
2053 Seobeo_Router_Register("GET", "/talk", GetTalk); |
|
128
7eb79fd91c7e
[Misc] Fixed all bazel targets. I should creat a separate scripts for these lol.
June Park <parkjune1995@gmail.com>
parents:
125
diff
changeset
|
2054 Seobeo_Router_Register("GET", "/talk/index.html", GetRedirectTalk); |
|
125
f236c895604e
[MrJuneJune] Added web socket for chat to this.
June Park <parkjune1995@gmail.com>
parents:
124
diff
changeset
|
2055 |
| 201 | 2056 // -- Notes --/ |
| 2057 Seobeo_Router_Register("GET", "/notes", GetNotes); | |
| 2058 Seobeo_Router_Register("GET", "/notes/", GetNotes); | |
| 2059 Seobeo_Router_Register("GET", "/notes/index.html", GetNotes); | |
| 2060 Seobeo_Router_Register("GET", "/notes/login", GetNotesLogin); | |
| 2061 Seobeo_Router_Register("GET", "/notes/login/", GetNotesLogin); | |
| 2062 Seobeo_Router_Register("GET", "/notes/:note_id", GetNoteById); | |
| 2063 | |
|
125
f236c895604e
[MrJuneJune] Added web socket for chat to this.
June Park <parkjune1995@gmail.com>
parents:
124
diff
changeset
|
2064 Seobeo_WebSocket_Server_Init(); |
|
f236c895604e
[MrJuneJune] Added web socket for chat to this.
June Park <parkjune1995@gmail.com>
parents:
124
diff
changeset
|
2065 Seobeo_WebSocket_Server_Register("/chat", Chat_Handler, NULL); |
|
f236c895604e
[MrJuneJune] Added web socket for chat to this.
June Park <parkjune1995@gmail.com>
parents:
124
diff
changeset
|
2066 |
| 201 | 2067 Seobeo_Log(SEOBEO_INFO, "WTF is going on\n"); |
|
241
9c2eec61a152
[assets] Generate site images as WebP with Bazel
MrJuneJune <me@mrjunejune.com>
parents:
216
diff
changeset
|
2068 const char *server_port = getenv("MRJUNEJUNE_PORT"); |
|
9c2eec61a152
[assets] Generate site images as WebP with Bazel
MrJuneJune <me@mrjunejune.com>
parents:
216
diff
changeset
|
2069 if (!server_port || server_port[0] == '\0') |
|
9c2eec61a152
[assets] Generate site images as WebP with Bazel
MrJuneJune <me@mrjunejune.com>
parents:
216
diff
changeset
|
2070 server_port = "6969"; |
|
244
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
2071 Seobeo_Web_Server_Start("mrjunejune/src", server_port, SEOBEO_MODE_EDGE, 4); |
|
250
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
2072 Seobeo_Worker_Pool_Destroy(g_media_worker_pool); |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
2073 g_media_worker_pool = NULL; |
|
7
114cad94008f
[Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
2074 } |