Mercurial
annotate mrjunejune/main.c @ 200:90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
| author | MrJuneJune <me@mrjunejune.com> |
|---|---|
| date | Sat, 14 Feb 2026 16:32:24 -0800 |
| parents | 295ac2e5ec00 |
| children | 6cdee35a7ba9 |
| 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" |
|
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
|
4 #include <time.h> |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
5 |
|
655ea0b661fd
[Seobeo] Added 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 // 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
|
7 #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
|
8 #define UUID_LEN 37 |
|
7
114cad94008f
[Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
9 |
|
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
|
10 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
|
11 static _Atomic uint32_t counter = 0; |
|
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
|
12 |
|
200
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
13 // Server configuration (loaded from .config) |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
14 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
|
15 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
|
16 static char g_s3_bucket[128] = "mrjunejune"; |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
17 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
|
18 static S3_Config g_s3_config = {0}; |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
19 |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
20 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
|
21 { |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
22 FILE *f = fopen(config_path, "r"); |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
23 if (!f) |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
24 { |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
25 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
|
26 return; |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
27 } |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
28 |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
29 char line[512]; |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
30 while (fgets(line, sizeof(line), f)) |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
31 { |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
32 // Skip comments and empty lines |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
33 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
|
34 |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
35 char *eq = strchr(line, '='); |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
36 if (!eq) continue; |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
37 |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
38 *eq = '\0'; |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
39 char *key = line; |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
40 char *value = eq + 1; |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
41 |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
42 // Trim newline from value |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
43 size_t vlen = strlen(value); |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
44 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
|
45 value[--vlen] = '\0'; |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
46 |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
47 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
|
48 { |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
49 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
|
50 } |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
51 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
|
52 { |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
53 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
|
54 } |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
55 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
|
56 { |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
57 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
|
58 } |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
59 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
|
60 { |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
61 g_s3_url_expires = atoi(value); |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
62 } |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
63 } |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
64 fclose(f); |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
65 |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
66 printf("[CONFIG] Loaded: token=%s..., region=%s, bucket=%s, expires=%d\n", |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
67 g_upload_auth_token[0] ? "***" : "(empty)", |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
68 g_s3_region, g_s3_bucket, g_s3_url_expires); |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
69 } |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
70 |
|
72
4532ce6d9eb8
[Seobeo] Added router to the server logic. Few dowa string manipulation logics.
June Park <parkjune1995@gmail.com>
parents:
36
diff
changeset
|
71 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
|
72 { |
|
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
|
73 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
|
74 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
|
75 } |
|
7
114cad94008f
[Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
76 |
|
158
1c0878eb17de
[MrJuneJune] Readme file gets compiled in server side.
June Park <parkjune1995@gmail.com>
parents:
132
diff
changeset
|
77 void Seobeo_Render_Html( |
| 78 | 78 char *final_body, |
|
158
1c0878eb17de
[MrJuneJune] Readme file gets compiled in server side.
June Park <parkjune1995@gmail.com>
parents:
132
diff
changeset
|
79 char *template, |
| 78 | 80 Dowa_Arena *arena |
|
158
1c0878eb17de
[MrJuneJune] Readme file gets compiled in server side.
June Park <parkjune1995@gmail.com>
parents:
132
diff
changeset
|
81 ) |
|
1c0878eb17de
[MrJuneJune] Readme file gets compiled in server side.
June Park <parkjune1995@gmail.com>
parents:
132
diff
changeset
|
82 { |
| 78 | 83 size_t current_offset = 0; |
| 84 char *cursor = template; | |
| 85 | |
| 86 int32 token_len = 2; | |
| 87 | |
| 96 | 88 while (1) |
| 78 | 89 { |
| 90 char *start_tag = strstr(cursor, "{{"); | |
| 91 if (!start_tag) break; | |
| 92 | |
| 93 char *end_tag = strstr(start_tag, "}}"); | |
| 94 if (!end_tag) break; | |
| 95 | |
| 96 size_t leading_len = start_tag - cursor; | |
| 97 memcpy(final_body + current_offset, cursor, leading_len); | |
| 98 current_offset += leading_len; | |
| 99 | |
| 100 size_t name_len = end_tag - (start_tag + token_len); | |
| 101 char *include_name = Dowa_Arena_Allocate(arena, name_len + 1); | |
| 102 memcpy(include_name, start_tag + token_len, name_len); | |
| 103 include_name[name_len] = '\0'; | |
| 104 | |
| 105 size_t sub_file_size = 0; | |
| 106 char *sub_content = Seobeo_Web_LoadFile(include_name, &sub_file_size); | |
|
100
65e5a5b89a4e
[Seobeo] Migrated everything to this page.
June Park <parkjune1995@gmail.com>
parents:
96
diff
changeset
|
107 Seobeo_Log(SEOBEO_DEBUG, "[Curr] Sub content: %s\n", sub_content); |
| 78 | 108 if (sub_content) |
| 109 { | |
| 110 memcpy(final_body + current_offset, sub_content, sub_file_size); | |
| 111 current_offset += sub_file_size; | |
| 112 free(sub_content); | |
| 113 } | |
| 114 | |
| 115 cursor = end_tag + 2; | |
| 116 } | |
| 117 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
|
118 } |
|
1c0878eb17de
[MrJuneJune] Readme file gets compiled in server side.
June Park <parkjune1995@gmail.com>
parents:
132
diff
changeset
|
119 |
|
1c0878eb17de
[MrJuneJune] Readme file gets compiled in server side.
June Park <parkjune1995@gmail.com>
parents:
132
diff
changeset
|
120 |
|
1c0878eb17de
[MrJuneJune] Readme file gets compiled in server side.
June Park <parkjune1995@gmail.com>
parents:
132
diff
changeset
|
121 void Seobeo_Render_Html_FilePath( |
|
1c0878eb17de
[MrJuneJune] Readme file gets compiled in server side.
June Park <parkjune1995@gmail.com>
parents:
132
diff
changeset
|
122 char *final_body, |
|
1c0878eb17de
[MrJuneJune] Readme file gets compiled in server side.
June Park <parkjune1995@gmail.com>
parents:
132
diff
changeset
|
123 char *path, |
|
1c0878eb17de
[MrJuneJune] Readme file gets compiled in server side.
June Park <parkjune1995@gmail.com>
parents:
132
diff
changeset
|
124 Dowa_Arena *arena |
|
1c0878eb17de
[MrJuneJune] Readme file gets compiled in server side.
June Park <parkjune1995@gmail.com>
parents:
132
diff
changeset
|
125 ) { |
|
1c0878eb17de
[MrJuneJune] Readme file gets compiled in server side.
June Park <parkjune1995@gmail.com>
parents:
132
diff
changeset
|
126 Seobeo_Log(SEOBEO_DEBUG, "[Curr] %s\n", path); |
|
1c0878eb17de
[MrJuneJune] Readme file gets compiled in server side.
June Park <parkjune1995@gmail.com>
parents:
132
diff
changeset
|
127 size_t html_size = 0; |
|
1c0878eb17de
[MrJuneJune] Readme file gets compiled in server side.
June Park <parkjune1995@gmail.com>
parents:
132
diff
changeset
|
128 char *template = Seobeo_Web_LoadFile(path, &html_size); |
|
1c0878eb17de
[MrJuneJune] Readme file gets compiled in server side.
June Park <parkjune1995@gmail.com>
parents:
132
diff
changeset
|
129 if (!template) return; |
|
1c0878eb17de
[MrJuneJune] Readme file gets compiled in server side.
June Park <parkjune1995@gmail.com>
parents:
132
diff
changeset
|
130 Seobeo_Render_Html(final_body, template, arena); |
| 78 | 131 } |
| 132 | |
|
77
c348ac875294
[Seobeo] Server side rendering.
June Park <parkjune1995@gmail.com>
parents:
72
diff
changeset
|
133 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
|
134 { |
| 78 | 135 Seobeo_Request_Entry *resp = NULL; |
|
80
d55157451947
[MrJuneJune] Updating my homepage.
June Park <parkjune1995@gmail.com>
parents:
79
diff
changeset
|
136 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
|
137 Seobeo_Render_Html_FilePath(final_body, "/index.html", arena); |
| 78 | 138 Dowa_HashMap_Push_Arena(resp, "body", final_body, arena); |
| 139 return resp; | |
| 140 } | |
|
77
c348ac875294
[Seobeo] Server side rendering.
June Park <parkjune1995@gmail.com>
parents:
72
diff
changeset
|
141 |
| 78 | 142 Seobeo_Request_Entry* GetResume(Seobeo_Request_Entry *req, Dowa_Arena *arena) |
| 143 { | |
| 144 Seobeo_Request_Entry *resp = NULL; | |
| 145 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
|
146 Seobeo_Render_Html_FilePath(final_body, "/resume/index.html", arena); |
| 78 | 147 Dowa_HashMap_Push_Arena(resp, "body", final_body, arena); |
| 148 return resp; | |
| 149 } | |
| 150 | |
| 151 Seobeo_Request_Entry* GetTools(Seobeo_Request_Entry *req, Dowa_Arena *arena) | |
| 152 { | |
| 153 Seobeo_Request_Entry *resp = NULL; | |
| 154 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
|
155 Seobeo_Render_Html_FilePath(final_body, "/tools/index.html", arena); |
| 78 | 156 Dowa_HashMap_Push_Arena(resp, "body", final_body, arena); |
|
77
c348ac875294
[Seobeo] Server side rendering.
June Park <parkjune1995@gmail.com>
parents:
72
diff
changeset
|
157 return resp; |
|
c348ac875294
[Seobeo] Server side rendering.
June Park <parkjune1995@gmail.com>
parents:
72
diff
changeset
|
158 } |
|
c348ac875294
[Seobeo] Server side rendering.
June Park <parkjune1995@gmail.com>
parents:
72
diff
changeset
|
159 |
|
c348ac875294
[Seobeo] Server side rendering.
June Park <parkjune1995@gmail.com>
parents:
72
diff
changeset
|
160 |
| 78 | 161 Seobeo_Request_Entry* GetMDToHTML(Seobeo_Request_Entry *req, Dowa_Arena *arena) |
| 162 { | |
|
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
|
163 Seobeo_Request_Entry *resp = NULL; |
| 78 | 164 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
|
165 Seobeo_Render_Html_FilePath(final_body, "/tools/markdown_to_html/index.html", arena); |
| 78 | 166 Dowa_HashMap_Push_Arena(resp, "body", final_body, arena); |
| 167 return resp; | |
| 168 } | |
|
77
c348ac875294
[Seobeo] Server side rendering.
June Park <parkjune1995@gmail.com>
parents:
72
diff
changeset
|
169 |
|
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
|
170 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
|
171 { |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
172 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
|
173 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
|
174 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
|
175 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
|
176 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
|
177 } |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
178 |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
179 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
|
180 { |
|
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
|
181 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
|
182 |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
183 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
|
184 { |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
185 printf("ERROR: Request is NULL\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
|
186 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
|
187 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
|
188 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
|
189 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
|
190 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
|
191 } |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
192 |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
193 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
|
194 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
|
195 |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
196 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
|
197 { |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
198 printf(" Key[%zu]: '%s'\n", i, req[i].key); |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
199 } |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
200 |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
201 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
|
202 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
|
203 { |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
204 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
|
205 |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
206 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
|
207 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
|
208 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
|
209 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
|
210 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
|
211 } |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
212 |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
213 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
|
214 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
|
215 { |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
216 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
|
217 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
|
218 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
|
219 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
|
220 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
|
221 } |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
222 |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
223 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
|
224 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
|
225 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
|
226 |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
227 printf("DEBUG: Converting image, 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
|
228 |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
229 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
|
230 |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
231 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
|
232 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
|
233 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
|
234 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
|
235 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
|
236 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
|
237 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
|
238 { |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
239 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
|
240 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
|
241 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
|
242 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
|
243 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
|
244 } |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
245 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
|
246 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
|
247 |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
248 |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
249 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
|
250 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
|
251 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
|
252 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
|
253 snprintf(output_path, TMP_FILE_LENGTH, "/tmp/%s.webp", 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
|
254 printf("[DEBUG] output_path %s\n", 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
|
255 printf("[DEBUG] open_flags: 0x%x\n", open_flags); |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
256 printf("[DEBUG] input_path: %s\n", 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
|
257 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
|
258 printf("[DEBUG] output_fd: %d\n", 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
|
259 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
|
260 { |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
261 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
|
262 printf("[DEBUG] errno: %d (%s)\n", errno, strerror(errno)); |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
263 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
|
264 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
|
265 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
|
266 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
|
267 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
|
268 } |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
269 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
|
270 |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
271 char cmd[1024]; |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
272 snprintf(cmd, sizeof(cmd), "ffmpeg -y -i %s -quality 80 %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
|
273 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
|
274 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
|
275 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
|
276 { |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
277 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
|
278 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
|
279 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
|
280 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
|
281 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
|
282 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
|
283 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
|
284 } |
|
655ea0b661fd
[Seobeo] Added 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 |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
286 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
|
287 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
|
288 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
|
289 { |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
290 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
|
291 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
|
292 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
|
293 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
|
294 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
|
295 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
|
296 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
|
297 } |
|
655ea0b661fd
[Seobeo] Added 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 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
|
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 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
|
301 |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
302 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
|
303 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
|
304 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
|
305 "{\"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
|
306 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
|
307 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
|
308 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
|
309 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
|
310 |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
311 printf("DEBUG: Image 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
|
312 |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
313 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
|
314 } |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
315 |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
316 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
|
317 { |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
318 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
|
319 |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
320 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
|
321 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
|
322 { |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
323 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
|
324 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
|
325 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
|
326 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
|
327 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
|
328 } |
|
72
4532ce6d9eb8
[Seobeo] Added router to the server logic. Few dowa string manipulation logics.
June Park <parkjune1995@gmail.com>
parents:
36
diff
changeset
|
329 |
|
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
|
330 // 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
|
331 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
|
332 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
|
333 { |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
334 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
|
335 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
|
336 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
|
337 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
|
338 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
|
339 } |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
340 |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
341 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
|
342 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
|
343 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
|
344 |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
345 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
|
346 |
|
168
f3084bca7317
[Misc] Fixed all errors and all tests should pass now.
MrJuneJune <me@mrjunejune.com>
parents:
158
diff
changeset
|
347 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
|
348 |
|
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
|
349 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
|
350 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
|
351 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
|
352 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
|
353 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
|
354 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
|
355 |
|
f3084bca7317
[Misc] Fixed all errors and all tests should pass now.
MrJuneJune <me@mrjunejune.com>
parents:
158
diff
changeset
|
356 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
|
357 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
|
358 { |
|
168
f3084bca7317
[Misc] Fixed all errors and all tests should pass now.
MrJuneJune <me@mrjunejune.com>
parents:
158
diff
changeset
|
359 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
|
360 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
|
361 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
|
362 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
|
363 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
|
364 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
|
365 } |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
366 |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
367 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
|
368 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
|
369 |
|
168
f3084bca7317
[Misc] Fixed all errors and all tests should pass now.
MrJuneJune <me@mrjunejune.com>
parents:
158
diff
changeset
|
370 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
|
371 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
|
372 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
|
373 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
|
374 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
|
375 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
|
376 { |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
377 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
|
378 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
|
379 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
|
380 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
|
381 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
|
382 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
|
383 } |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
384 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
|
385 |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
386 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
|
387 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
|
388 "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
|
389 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
|
390 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
|
391 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
|
392 { |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
393 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
|
394 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
|
395 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
|
396 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
|
397 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
|
398 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
|
399 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
|
400 } |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
401 |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
402 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
|
403 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
|
404 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
|
405 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
|
406 "{\"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
|
407 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
|
408 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
|
409 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
|
410 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
|
411 |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
412 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
|
413 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
|
414 } |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
415 |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
416 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
|
417 { |
|
72
4532ce6d9eb8
[Seobeo] Added router to the server logic. Few dowa string manipulation logics.
June Park <parkjune1995@gmail.com>
parents:
36
diff
changeset
|
418 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
|
419 |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
420 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
|
421 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
|
422 { |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
423 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
|
424 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
|
425 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
|
426 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
|
427 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
|
428 } |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
429 |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
430 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
|
431 |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
432 // 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
|
433 // 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
|
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 // 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
|
436 // 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
|
437 // 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
|
438 // 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
|
439 // 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
|
440 // } |
|
655ea0b661fd
[Seobeo] Added 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 // 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
|
442 // 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
|
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 // 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
|
445 // { |
|
655ea0b661fd
[Seobeo] Added 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 // 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
|
447 // 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
|
448 // } |
|
655ea0b661fd
[Seobeo] Added 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 // 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
|
450 // } |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
451 |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
452 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
|
453 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
|
454 |
|
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
|
455 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
|
456 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
|
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 = "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
|
459 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
|
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 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
|
466 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
|
467 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
|
468 |
|
655ea0b661fd
[Seobeo] Added 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 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
|
470 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
|
471 { |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
472 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
|
473 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
|
474 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
|
475 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
|
476 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
|
477 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
|
478 } |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
479 |
|
655ea0b661fd
[Seobeo] Added 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 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
|
481 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
|
482 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
|
483 |
|
655ea0b661fd
[Seobeo] Added 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 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
|
485 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
|
486 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
|
487 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
|
488 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
|
489 |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
490 char *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
|
491 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
|
492 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
|
493 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
|
494 |
|
655ea0b661fd
[Seobeo] Added 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 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
|
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 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
|
498 |
|
655ea0b661fd
[Seobeo] Added 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 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
|
500 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
|
501 |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
502 Dowa_HashMap_Push_Arena(resp, "status", "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
|
503 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
|
504 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
|
505 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
|
506 |
|
72
4532ce6d9eb8
[Seobeo] Added router to the server logic. Few dowa string manipulation logics.
June Park <parkjune1995@gmail.com>
parents:
36
diff
changeset
|
507 return resp; |
|
4532ce6d9eb8
[Seobeo] Added router to the server logic. Few dowa string manipulation logics.
June Park <parkjune1995@gmail.com>
parents:
36
diff
changeset
|
508 } |
|
4532ce6d9eb8
[Seobeo] Added router to the server logic. Few dowa string manipulation logics.
June Park <parkjune1995@gmail.com>
parents:
36
diff
changeset
|
509 |
|
100
65e5a5b89a4e
[Seobeo] Migrated everything to this page.
June Park <parkjune1995@gmail.com>
parents:
96
diff
changeset
|
510 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
|
511 { |
|
65e5a5b89a4e
[Seobeo] Migrated everything to this page.
June Park <parkjune1995@gmail.com>
parents:
96
diff
changeset
|
512 Seobeo_Request_Entry *resp = NULL; |
|
65e5a5b89a4e
[Seobeo] Migrated everything to this page.
June Park <parkjune1995@gmail.com>
parents:
96
diff
changeset
|
513 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
|
514 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
|
515 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
|
516 return resp; |
|
65e5a5b89a4e
[Seobeo] Migrated everything to this page.
June Park <parkjune1995@gmail.com>
parents:
96
diff
changeset
|
517 } |
|
65e5a5b89a4e
[Seobeo] Migrated everything to this page.
June Park <parkjune1995@gmail.com>
parents:
96
diff
changeset
|
518 |
|
65e5a5b89a4e
[Seobeo] Migrated everything to this page.
June Park <parkjune1995@gmail.com>
parents:
96
diff
changeset
|
519 |
|
65e5a5b89a4e
[Seobeo] Migrated everything to this page.
June Park <parkjune1995@gmail.com>
parents:
96
diff
changeset
|
520 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
|
521 { |
|
65e5a5b89a4e
[Seobeo] Migrated everything to this page.
June Park <parkjune1995@gmail.com>
parents:
96
diff
changeset
|
522 Seobeo_Request_Entry *resp = NULL; |
|
65e5a5b89a4e
[Seobeo] Migrated everything to this page.
June Park <parkjune1995@gmail.com>
parents:
96
diff
changeset
|
523 |
|
169
295ac2e5ec00
[MrJuneJune] Created separate target for generating html from md.
MrJuneJune <me@mrjunejune.com>
parents:
168
diff
changeset
|
524 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
|
525 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
|
526 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
|
527 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
|
528 |
|
169
295ac2e5ec00
[MrJuneJune] Created separate target for generating html from md.
MrJuneJune <me@mrjunejune.com>
parents:
168
diff
changeset
|
529 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
|
530 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
|
531 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
|
532 return resp; |
|
65e5a5b89a4e
[Seobeo] Migrated everything to this page.
June Park <parkjune1995@gmail.com>
parents:
96
diff
changeset
|
533 } |
|
65e5a5b89a4e
[Seobeo] Migrated everything to this page.
June Park <parkjune1995@gmail.com>
parents:
96
diff
changeset
|
534 |
|
125
f236c895604e
[MrJuneJune] Added web socket for chat to this.
June Park <parkjune1995@gmail.com>
parents:
124
diff
changeset
|
535 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
|
536 { |
|
f236c895604e
[MrJuneJune] Added web socket for chat to this.
June Park <parkjune1995@gmail.com>
parents:
124
diff
changeset
|
537 (void)p_user_data; |
|
f236c895604e
[MrJuneJune] Added web socket for chat to this.
June Park <parkjune1995@gmail.com>
parents:
124
diff
changeset
|
538 |
|
f236c895604e
[MrJuneJune] Added web socket for chat to this.
June Park <parkjune1995@gmail.com>
parents:
124
diff
changeset
|
539 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
|
540 { |
|
f236c895604e
[MrJuneJune] Added web socket for chat to this.
June Park <parkjune1995@gmail.com>
parents:
124
diff
changeset
|
541 char message[2048]; |
|
f236c895604e
[MrJuneJune] Added web socket for chat to this.
June Park <parkjune1995@gmail.com>
parents:
124
diff
changeset
|
542 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
|
543 |
|
f236c895604e
[MrJuneJune] Added web socket for chat to this.
June Park <parkjune1995@gmail.com>
parents:
124
diff
changeset
|
544 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
|
545 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
|
546 } |
|
f236c895604e
[MrJuneJune] Added web socket for chat to this.
June Park <parkjune1995@gmail.com>
parents:
124
diff
changeset
|
547 } |
|
f236c895604e
[MrJuneJune] Added web socket for chat to this.
June Park <parkjune1995@gmail.com>
parents:
124
diff
changeset
|
548 |
|
f236c895604e
[MrJuneJune] Added web socket for chat to this.
June Park <parkjune1995@gmail.com>
parents:
124
diff
changeset
|
549 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
|
550 { |
|
f236c895604e
[MrJuneJune] Added web socket for chat to this.
June Park <parkjune1995@gmail.com>
parents:
124
diff
changeset
|
551 Seobeo_Request_Entry *resp = NULL; |
|
f236c895604e
[MrJuneJune] Added web socket for chat to this.
June Park <parkjune1995@gmail.com>
parents:
124
diff
changeset
|
552 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
|
553 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
|
554 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
|
555 return resp; |
|
f236c895604e
[MrJuneJune] Added web socket for chat to this.
June Park <parkjune1995@gmail.com>
parents:
124
diff
changeset
|
556 } |
|
100
65e5a5b89a4e
[Seobeo] Migrated everything to this page.
June Park <parkjune1995@gmail.com>
parents:
96
diff
changeset
|
557 |
| 88 | 558 CREATE_REDIRECT_HANDLER(HomePage, "/") |
| 559 CREATE_REDIRECT_HANDLER(Resume, "/resume") | |
| 560 CREATE_REDIRECT_HANDLER(Tools, "/tools") | |
| 561 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
|
562 CREATE_REDIRECT_HANDLER(FileConverter, "/tools/file_converter") |
|
125
f236c895604e
[MrJuneJune] Added web socket for chat to this.
June Park <parkjune1995@gmail.com>
parents:
124
diff
changeset
|
563 CREATE_REDIRECT_HANDLER(Talk, "/talk") |
|
79
5710108c949e
[Seobeo] Added Redirect logic.
June Park <parkjune1995@gmail.com>
parents:
78
diff
changeset
|
564 |
|
200
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
565 // S3 Upload URL API |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
566 // POST /api/s3/upload-url |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
567 // 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
|
568 // 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
|
569 // Returns: {"upload_url": "https://...", "key": "uploads/..."} |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
570 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
|
571 { |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
572 Seobeo_Request_Entry *resp = NULL; |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
573 |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
574 // Check auth token |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
575 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
|
576 if (!auth_kv) |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
577 { |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
578 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
|
579 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
|
580 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
|
581 return resp; |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
582 } |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
583 |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
584 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
|
585 |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
586 // Expect "Bearer <token>" |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
587 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
|
588 { |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
589 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
|
590 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
|
591 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
|
592 return resp; |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
593 } |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
594 |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
595 const char *token = auth_header + 7; |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
596 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
|
597 { |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
598 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
|
599 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
|
600 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
|
601 return resp; |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
602 } |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
603 |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
604 // 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
|
605 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
|
606 if (!body_kv) |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
607 { |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
608 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
|
609 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
|
610 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
|
611 return resp; |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
612 } |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
613 |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
614 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
|
615 |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
616 // 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
|
617 char filename[256] = {0}; |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
618 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
|
619 |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
620 // Find "filename":"value" |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
621 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
|
622 if (fn_key) |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
623 { |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
624 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
|
625 if (fn_start) |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
626 { |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
627 fn_start++; |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
628 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
|
629 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
|
630 { |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
631 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
|
632 filename[fn_end - fn_start] = '\0'; |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
633 } |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
634 } |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
635 } |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
636 |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
637 // Find "content_type":"value" |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
638 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
|
639 if (ct_key) |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
640 { |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
641 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
|
642 if (ct_start) |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
643 { |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
644 ct_start++; |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
645 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
|
646 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
|
647 { |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
648 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
|
649 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
|
650 } |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
651 } |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
652 } |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
653 |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
654 if (strlen(filename) == 0) |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
655 { |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
656 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
|
657 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
|
658 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
|
659 return resp; |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
660 } |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
661 |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
662 // Generate unique S3 key with timestamp |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
663 char s3_key[512]; |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
664 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
|
665 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
|
666 Dowa_String_UUID(seed, uuid); |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
667 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
|
668 |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
669 // Generate presigned URL |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
670 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
|
671 |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
672 if (!presigned.success) |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
673 { |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
674 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
|
675 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
|
676 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
|
677 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
|
678 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
|
679 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
|
680 S3_Presigned_URL_Destroy(&presigned); |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
681 return resp; |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
682 } |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
683 |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
684 // Build response |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
685 char *response_body = Dowa_Arena_Allocate(arena, 2048 + strlen(presigned.url)); |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
686 snprintf(response_body, 2048 + strlen(presigned.url), |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
687 "{\"upload_url\":\"%s\",\"key\":\"%s\",\"expires\":%d}", |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
688 presigned.url, s3_key, g_s3_url_expires); |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
689 |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
690 S3_Presigned_URL_Destroy(&presigned); |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
691 |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
692 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
|
693 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
|
694 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
|
695 |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
696 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
|
697 |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
698 return resp; |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
699 } |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
700 |
|
7
114cad94008f
[Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
701 int main(void) |
|
114cad94008f
[Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
702 { |
|
200
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
703 // Load server config |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
704 load_config("mrjunejune/.config"); |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
705 |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
706 // Load S3 credentials from .env |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
707 FILE *env_file = fopen(".env", "r"); |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
708 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
|
709 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
|
710 |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
711 if (env_file) |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
712 { |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
713 char line[512]; |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
714 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
|
715 { |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
716 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
|
717 { |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
718 char *val = line + 26; |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
719 size_t len = strlen(val); |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
720 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
|
721 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
|
722 } |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
723 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
|
724 { |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
725 char *val = line + 33; |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
726 size_t len = strlen(val); |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
727 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
|
728 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
|
729 } |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
730 } |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
731 fclose(env_file); |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
732 } |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
733 |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
734 // Initialize S3 config |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
735 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
|
736 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
|
737 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
|
738 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
|
739 g_s3_config.endpoint = NULL; |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
740 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
|
741 |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
742 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
|
743 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
|
744 |
|
72
4532ce6d9eb8
[Seobeo] Added router to the server logic. Few dowa string manipulation logics.
June Park <parkjune1995@gmail.com>
parents:
36
diff
changeset
|
745 Seobeo_Router_Init(); |
|
125
f236c895604e
[MrJuneJune] Added web socket for chat to this.
June Park <parkjune1995@gmail.com>
parents:
124
diff
changeset
|
746 |
|
77
c348ac875294
[Seobeo] Server side rendering.
June Park <parkjune1995@gmail.com>
parents:
72
diff
changeset
|
747 Seobeo_Router_Register("GET", "/", GetHomePage); |
|
79
5710108c949e
[Seobeo] Added Redirect logic.
June Park <parkjune1995@gmail.com>
parents:
78
diff
changeset
|
748 Seobeo_Router_Register("GET", "/index.html", GetRedirectHomePage); |
| 78 | 749 |
| 750 Seobeo_Router_Register("GET", "/resume", GetResume); | |
|
79
5710108c949e
[Seobeo] Added Redirect logic.
June Park <parkjune1995@gmail.com>
parents:
78
diff
changeset
|
751 Seobeo_Router_Register("GET", "/resume/index.html", GetRedirectResume); |
| 78 | 752 |
| 753 Seobeo_Router_Register("GET", "/tools", GetTools); | |
|
79
5710108c949e
[Seobeo] Added Redirect logic.
June Park <parkjune1995@gmail.com>
parents:
78
diff
changeset
|
754 Seobeo_Router_Register("GET", "/tools/index.html", GetRedirectTools); |
| 78 | 755 |
| 756 Seobeo_Router_Register("GET", "/tools/markdown_to_html", GetMDToHTML); | |
|
79
5710108c949e
[Seobeo] Added Redirect logic.
June Park <parkjune1995@gmail.com>
parents:
78
diff
changeset
|
757 Seobeo_Router_Register("GET", "/tools/markdown_to_html/index.html", GetRedirectMarkDownToHtml); |
| 78 | 758 |
|
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
|
759 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
|
760 Seobeo_Router_Register("GET", "/tools/file_converter/index.html", GetRedirectFileConverter); |
|
655ea0b661fd
[Seobeo] Added 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 |
|
100
65e5a5b89a4e
[Seobeo] Migrated everything to this page.
June Park <parkjune1995@gmail.com>
parents:
96
diff
changeset
|
762 // -- 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
|
763 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
|
764 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
|
765 Seobeo_Router_Register("GET", "/api/download/:filename", DownloadConvertedFile); |
|
655ea0b661fd
[Seobeo] Added 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 |
|
200
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
767 // -- S3 Upload --/ |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
768 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
|
769 |
|
100
65e5a5b89a4e
[Seobeo] Migrated everything to this page.
June Park <parkjune1995@gmail.com>
parents:
96
diff
changeset
|
770 // -- Blog --/ |
|
65e5a5b89a4e
[Seobeo] Migrated everything to this page.
June Park <parkjune1995@gmail.com>
parents:
96
diff
changeset
|
771 Seobeo_Router_Register("GET", "/blog", RenderBlogList); |
|
65e5a5b89a4e
[Seobeo] Migrated everything to this page.
June Park <parkjune1995@gmail.com>
parents:
96
diff
changeset
|
772 Seobeo_Router_Register("GET", "/blog/:blog_id", RenderBlog); |
|
65e5a5b89a4e
[Seobeo] Migrated everything to this page.
June Park <parkjune1995@gmail.com>
parents:
96
diff
changeset
|
773 |
|
125
f236c895604e
[MrJuneJune] Added web socket for chat to this.
June Park <parkjune1995@gmail.com>
parents:
124
diff
changeset
|
774 // -- Talk --/ |
|
f236c895604e
[MrJuneJune] Added web socket for chat to this.
June Park <parkjune1995@gmail.com>
parents:
124
diff
changeset
|
775 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
|
776 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
|
777 |
|
f236c895604e
[MrJuneJune] Added web socket for chat to this.
June Park <parkjune1995@gmail.com>
parents:
124
diff
changeset
|
778 Seobeo_WebSocket_Server_Init(); |
|
f236c895604e
[MrJuneJune] Added web socket for chat to this.
June Park <parkjune1995@gmail.com>
parents:
124
diff
changeset
|
779 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
|
780 |
| 89 | 781 Seobeo_Web_Server_Start("mrjunejune/src", "6969", SEOBEO_MODE_EDGE, 3); |
|
7
114cad94008f
[Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
782 } |