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