annotate mrjunejune/main.c @ 98:5c47eab8032d

Updated deploy script
author June Park <parkjune1995@gmail.com>
date Fri, 02 Jan 2026 20:25:47 -0800
parents 70401cf61e97
children 65e5a5b89a4e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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"
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
2 #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
3
655ea0b661fd [Seobeo] Added 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 // 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
5 #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
6 #define UUID_LEN 37
7
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
7
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
8 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
9 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
10
72
4532ce6d9eb8 [Seobeo] Added router to the server logic. Few dowa string manipulation logics.
June Park <parkjune1995@gmail.com>
parents: 36
diff changeset
11 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
12 {
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
13 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
14 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
15 }
7
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
16
78
June Park <parkjune1995@gmail.com>
parents: 77
diff changeset
17 void Seobeo_ServerSideRender(
June Park <parkjune1995@gmail.com>
parents: 77
diff changeset
18 char *final_body,
June Park <parkjune1995@gmail.com>
parents: 77
diff changeset
19 char *path,
June Park <parkjune1995@gmail.com>
parents: 77
diff changeset
20 Dowa_Arena *arena
June Park <parkjune1995@gmail.com>
parents: 77
diff changeset
21 ) {
June Park <parkjune1995@gmail.com>
parents: 77
diff changeset
22 size_t html_size = 0;
June Park <parkjune1995@gmail.com>
parents: 77
diff changeset
23 char *template = Seobeo_Web_LoadFile(path, &html_size);
June Park <parkjune1995@gmail.com>
parents: 77
diff changeset
24 if (!template) return;
June Park <parkjune1995@gmail.com>
parents: 77
diff changeset
25
June Park <parkjune1995@gmail.com>
parents: 77
diff changeset
26 size_t current_offset = 0;
June Park <parkjune1995@gmail.com>
parents: 77
diff changeset
27 char *cursor = template;
June Park <parkjune1995@gmail.com>
parents: 77
diff changeset
28
June Park <parkjune1995@gmail.com>
parents: 77
diff changeset
29 int32 token_len = 2;
June Park <parkjune1995@gmail.com>
parents: 77
diff changeset
30
96
70401cf61e97 [Seobeo] Added logging.
June Park <parkjune1995@gmail.com>
parents: 92
diff changeset
31 while (1)
78
June Park <parkjune1995@gmail.com>
parents: 77
diff changeset
32 {
June Park <parkjune1995@gmail.com>
parents: 77
diff changeset
33 char *start_tag = strstr(cursor, "{{");
June Park <parkjune1995@gmail.com>
parents: 77
diff changeset
34 if (!start_tag) break;
June Park <parkjune1995@gmail.com>
parents: 77
diff changeset
35
June Park <parkjune1995@gmail.com>
parents: 77
diff changeset
36 char *end_tag = strstr(start_tag, "}}");
June Park <parkjune1995@gmail.com>
parents: 77
diff changeset
37 if (!end_tag) break;
June Park <parkjune1995@gmail.com>
parents: 77
diff changeset
38
June Park <parkjune1995@gmail.com>
parents: 77
diff changeset
39 size_t leading_len = start_tag - cursor;
June Park <parkjune1995@gmail.com>
parents: 77
diff changeset
40 memcpy(final_body + current_offset, cursor, leading_len);
June Park <parkjune1995@gmail.com>
parents: 77
diff changeset
41 current_offset += leading_len;
June Park <parkjune1995@gmail.com>
parents: 77
diff changeset
42
June Park <parkjune1995@gmail.com>
parents: 77
diff changeset
43 size_t name_len = end_tag - (start_tag + token_len);
June Park <parkjune1995@gmail.com>
parents: 77
diff changeset
44 char *include_name = Dowa_Arena_Allocate(arena, name_len + 1);
June Park <parkjune1995@gmail.com>
parents: 77
diff changeset
45 memcpy(include_name, start_tag + token_len, name_len);
June Park <parkjune1995@gmail.com>
parents: 77
diff changeset
46 include_name[name_len] = '\0';
June Park <parkjune1995@gmail.com>
parents: 77
diff changeset
47
June Park <parkjune1995@gmail.com>
parents: 77
diff changeset
48 size_t sub_file_size = 0;
June Park <parkjune1995@gmail.com>
parents: 77
diff changeset
49 char *sub_content = Seobeo_Web_LoadFile(include_name, &sub_file_size);
June Park <parkjune1995@gmail.com>
parents: 77
diff changeset
50 if (sub_content)
June Park <parkjune1995@gmail.com>
parents: 77
diff changeset
51 {
June Park <parkjune1995@gmail.com>
parents: 77
diff changeset
52 memcpy(final_body + current_offset, sub_content, sub_file_size);
June Park <parkjune1995@gmail.com>
parents: 77
diff changeset
53 current_offset += sub_file_size;
June Park <parkjune1995@gmail.com>
parents: 77
diff changeset
54 free(sub_content);
June Park <parkjune1995@gmail.com>
parents: 77
diff changeset
55 }
June Park <parkjune1995@gmail.com>
parents: 77
diff changeset
56
June Park <parkjune1995@gmail.com>
parents: 77
diff changeset
57 cursor = end_tag + 2;
June Park <parkjune1995@gmail.com>
parents: 77
diff changeset
58 }
June Park <parkjune1995@gmail.com>
parents: 77
diff changeset
59 strcpy(final_body + current_offset, cursor);
June Park <parkjune1995@gmail.com>
parents: 77
diff changeset
60 free(template);
June Park <parkjune1995@gmail.com>
parents: 77
diff changeset
61 }
June Park <parkjune1995@gmail.com>
parents: 77
diff changeset
62
77
c348ac875294 [Seobeo] Server side rendering.
June Park <parkjune1995@gmail.com>
parents: 72
diff changeset
63 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
64 {
78
June Park <parkjune1995@gmail.com>
parents: 77
diff changeset
65 Seobeo_Request_Entry *resp = NULL;
80
d55157451947 [MrJuneJune] Updating my homepage.
June Park <parkjune1995@gmail.com>
parents: 79
diff changeset
66 char *final_body = Dowa_Arena_Allocate(arena, 50 * 1024);
78
June Park <parkjune1995@gmail.com>
parents: 77
diff changeset
67 Seobeo_ServerSideRender(final_body, "/index.html", arena);
June Park <parkjune1995@gmail.com>
parents: 77
diff changeset
68 Dowa_HashMap_Push_Arena(resp, "body", final_body, arena);
June Park <parkjune1995@gmail.com>
parents: 77
diff changeset
69 return resp;
June Park <parkjune1995@gmail.com>
parents: 77
diff changeset
70 }
77
c348ac875294 [Seobeo] Server side rendering.
June Park <parkjune1995@gmail.com>
parents: 72
diff changeset
71
78
June Park <parkjune1995@gmail.com>
parents: 77
diff changeset
72 Seobeo_Request_Entry* GetResume(Seobeo_Request_Entry *req, Dowa_Arena *arena)
June Park <parkjune1995@gmail.com>
parents: 77
diff changeset
73 {
June Park <parkjune1995@gmail.com>
parents: 77
diff changeset
74 Seobeo_Request_Entry *resp = NULL;
June Park <parkjune1995@gmail.com>
parents: 77
diff changeset
75 char *final_body = Dowa_Arena_Allocate(arena, 50 * 1024);
June Park <parkjune1995@gmail.com>
parents: 77
diff changeset
76 Seobeo_ServerSideRender(final_body, "/resume/index.html", arena);
June Park <parkjune1995@gmail.com>
parents: 77
diff changeset
77 Dowa_HashMap_Push_Arena(resp, "body", final_body, arena);
June Park <parkjune1995@gmail.com>
parents: 77
diff changeset
78 return resp;
June Park <parkjune1995@gmail.com>
parents: 77
diff changeset
79 }
June Park <parkjune1995@gmail.com>
parents: 77
diff changeset
80
June Park <parkjune1995@gmail.com>
parents: 77
diff changeset
81 Seobeo_Request_Entry* GetTools(Seobeo_Request_Entry *req, Dowa_Arena *arena)
June Park <parkjune1995@gmail.com>
parents: 77
diff changeset
82 {
June Park <parkjune1995@gmail.com>
parents: 77
diff changeset
83 Seobeo_Request_Entry *resp = NULL;
June Park <parkjune1995@gmail.com>
parents: 77
diff changeset
84 char *final_body = Dowa_Arena_Allocate(arena, 50 * 1024);
June Park <parkjune1995@gmail.com>
parents: 77
diff changeset
85 Seobeo_ServerSideRender(final_body, "/tools/index.html", arena);
June Park <parkjune1995@gmail.com>
parents: 77
diff changeset
86 Dowa_HashMap_Push_Arena(resp, "body", final_body, arena);
77
c348ac875294 [Seobeo] Server side rendering.
June Park <parkjune1995@gmail.com>
parents: 72
diff changeset
87 return resp;
c348ac875294 [Seobeo] Server side rendering.
June Park <parkjune1995@gmail.com>
parents: 72
diff changeset
88 }
c348ac875294 [Seobeo] Server side rendering.
June Park <parkjune1995@gmail.com>
parents: 72
diff changeset
89
c348ac875294 [Seobeo] Server side rendering.
June Park <parkjune1995@gmail.com>
parents: 72
diff changeset
90
78
June Park <parkjune1995@gmail.com>
parents: 77
diff changeset
91 Seobeo_Request_Entry* GetMDToHTML(Seobeo_Request_Entry *req, Dowa_Arena *arena)
June Park <parkjune1995@gmail.com>
parents: 77
diff changeset
92 {
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
93 Seobeo_Request_Entry *resp = NULL;
78
June Park <parkjune1995@gmail.com>
parents: 77
diff changeset
94 char *final_body = Dowa_Arena_Allocate(arena, 50 * 1024);
June Park <parkjune1995@gmail.com>
parents: 77
diff changeset
95 Seobeo_ServerSideRender(final_body, "/tools/markdown_to_html/index.html", arena);
June Park <parkjune1995@gmail.com>
parents: 77
diff changeset
96 Dowa_HashMap_Push_Arena(resp, "body", final_body, arena);
June Park <parkjune1995@gmail.com>
parents: 77
diff changeset
97 return resp;
June Park <parkjune1995@gmail.com>
parents: 77
diff changeset
98 }
77
c348ac875294 [Seobeo] Server side rendering.
June Park <parkjune1995@gmail.com>
parents: 72
diff changeset
99
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
100 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
101 {
655ea0b661fd [Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents: 89
diff changeset
102 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
103 char *final_body = Dowa_Arena_Allocate(arena, 50 * 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
104 Seobeo_ServerSideRender(final_body, "/tools/file_converter/index.html", 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
105 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
106 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
107 }
655ea0b661fd [Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents: 89
diff changeset
108
655ea0b661fd [Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents: 89
diff changeset
109 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
110 {
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
111 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
112
655ea0b661fd [Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents: 89
diff changeset
113 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
114 {
655ea0b661fd [Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents: 89
diff changeset
115 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
116 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
117 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
118 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
119 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
120 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
121 }
655ea0b661fd [Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents: 89
diff changeset
122
655ea0b661fd [Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents: 89
diff changeset
123 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
124 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
125
655ea0b661fd [Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents: 89
diff changeset
126 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
127 {
655ea0b661fd [Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents: 89
diff changeset
128 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
129 }
655ea0b661fd [Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents: 89
diff changeset
130
655ea0b661fd [Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents: 89
diff changeset
131 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
132 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
133 {
655ea0b661fd [Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents: 89
diff changeset
134 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
135
655ea0b661fd [Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents: 89
diff changeset
136 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
137 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
138 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
139 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
140 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
141 }
655ea0b661fd [Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents: 89
diff changeset
142
655ea0b661fd [Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents: 89
diff changeset
143 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
144 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
145 {
655ea0b661fd [Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents: 89
diff changeset
146 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
147 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
148 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
149 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
150 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
151 }
655ea0b661fd [Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents: 89
diff changeset
152
655ea0b661fd [Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents: 89
diff changeset
153 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
154 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
155 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
156
655ea0b661fd [Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents: 89
diff changeset
157 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
158
655ea0b661fd [Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents: 89
diff changeset
159
655ea0b661fd [Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents: 89
diff changeset
160 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
161
655ea0b661fd [Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents: 89
diff changeset
162 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
163 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
164 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
165 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
166 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
167 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
168 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
169 {
655ea0b661fd [Seobeo] Added 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 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
171 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
172 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
173 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
174 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
175 }
655ea0b661fd [Seobeo] Added 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 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
177 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
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
655ea0b661fd [Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents: 89
diff changeset
180 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
181 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
182 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
183 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
184 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
185 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
186 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
187 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
188 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
189 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
190 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
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 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
193 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
194 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
195 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
196 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
197 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
198 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
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 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
201
655ea0b661fd [Seobeo] Added 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 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
203 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
204 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
205 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
206 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
207 {
655ea0b661fd [Seobeo] Added 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 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
209 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
210 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
211 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
212 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
213 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
214 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
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
655ea0b661fd [Seobeo] Added 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 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
218 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
219 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
220 {
655ea0b661fd [Seobeo] Added 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 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
222 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
223 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
224 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
225 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
226 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
227 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
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 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
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 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
232
655ea0b661fd [Seobeo] Added 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 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
234 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
235 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
236 "{\"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
237 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
238 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
239 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
240 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
241
655ea0b661fd [Seobeo] Added 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 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
243
655ea0b661fd [Seobeo] Added 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 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
245 }
655ea0b661fd [Seobeo] Added 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
655ea0b661fd [Seobeo] Added 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 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
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 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
250
655ea0b661fd [Seobeo] Added 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 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
252 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
253 {
655ea0b661fd [Seobeo] Added 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 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
255 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
256 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
257 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
258 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
259 }
72
4532ce6d9eb8 [Seobeo] Added router to the server logic. Few dowa string manipulation logics.
June Park <parkjune1995@gmail.com>
parents: 36
diff changeset
260
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
261 // 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
262 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
263 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
264 {
655ea0b661fd [Seobeo] Added 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 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
266 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
267 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
268 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
269 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
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
655ea0b661fd [Seobeo] Added 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 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
273 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
274 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
275
655ea0b661fd [Seobeo] Added 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 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
277
655ea0b661fd [Seobeo] Added 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 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
279 Dowa_String_UUID((uint32)time(NULL), 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
280 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
281 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
282 int input_fd = mkstemp(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
283 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
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 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
286 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
287 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
288 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
289 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
290 }
655ea0b661fd [Seobeo] Added 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
655ea0b661fd [Seobeo] Added 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 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
293 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
294
655ea0b661fd [Seobeo] Added 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 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
296
655ea0b661fd [Seobeo] Added 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 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
298 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
299 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
300 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
301 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
302 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
303 {
655ea0b661fd [Seobeo] Added 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 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
305 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
306 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
307 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
308 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
309 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
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 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
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 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
314 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
315 "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
316 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
317 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
318 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
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 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
321 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
322 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
323 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
324 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
325 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
326 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
327 }
655ea0b661fd [Seobeo] Added 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
655ea0b661fd [Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents: 89
diff changeset
329 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
330 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
331 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
332 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
333 "{\"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
334 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
335 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
336 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
337 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
338
655ea0b661fd [Seobeo] Added 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 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
340 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
341 }
655ea0b661fd [Seobeo] Added 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
655ea0b661fd [Seobeo] Added 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 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
344 {
72
4532ce6d9eb8 [Seobeo] Added router to the server logic. Few dowa string manipulation logics.
June Park <parkjune1995@gmail.com>
parents: 36
diff changeset
345 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
346
655ea0b661fd [Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents: 89
diff changeset
347 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
348 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
349 {
655ea0b661fd [Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents: 89
diff changeset
350 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
351 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
352 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
353 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
354 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
355 }
655ea0b661fd [Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents: 89
diff changeset
356
655ea0b661fd [Seobeo] Added 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 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
358
655ea0b661fd [Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents: 89
diff changeset
359 // 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
360 // 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
361 // {
655ea0b661fd [Seobeo] Added 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 // 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
363 // 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
364 // 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
365 // 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
366 // 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
367 // }
655ea0b661fd [Seobeo] Added 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 // 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
369 // 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
370 // {
655ea0b661fd [Seobeo] Added 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 // 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
372 // {
655ea0b661fd [Seobeo] Added 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 // 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
374 // 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
375 // }
655ea0b661fd [Seobeo] Added 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 // 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
377 // }
655ea0b661fd [Seobeo] Added 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
655ea0b661fd [Seobeo] Added 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 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
380 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
381
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
382 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
383 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
384 {
655ea0b661fd [Seobeo] Added 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 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
386 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
387 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
388 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
389 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
390 }
655ea0b661fd [Seobeo] Added 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
655ea0b661fd [Seobeo] Added 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 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
393 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
394 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
395
655ea0b661fd [Seobeo] Added 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 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
397 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
398 {
655ea0b661fd [Seobeo] Added 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 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
400 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
401 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
402 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
403 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
404 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
405 }
655ea0b661fd [Seobeo] Added 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
655ea0b661fd [Seobeo] Added 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 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
408 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
409 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
410
655ea0b661fd [Seobeo] Added 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 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
412 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
413 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
414 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
415 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
416
655ea0b661fd [Seobeo] Added 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 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
418 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
419 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
420 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
421
655ea0b661fd [Seobeo] Added 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 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
423
655ea0b661fd [Seobeo] Added 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 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
425
655ea0b661fd [Seobeo] Added 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 // Set proper Content-Length for binary 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
427 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
428 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
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 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
431 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
432 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
433 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
434
72
4532ce6d9eb8 [Seobeo] Added router to the server logic. Few dowa string manipulation logics.
June Park <parkjune1995@gmail.com>
parents: 36
diff changeset
435 return resp;
4532ce6d9eb8 [Seobeo] Added router to the server logic. Few dowa string manipulation logics.
June Park <parkjune1995@gmail.com>
parents: 36
diff changeset
436 }
4532ce6d9eb8 [Seobeo] Added router to the server logic. Few dowa string manipulation logics.
June Park <parkjune1995@gmail.com>
parents: 36
diff changeset
437
88
a3962e681490 Fixed routing issues.
June Park <parkjune1995@gmail.com>
parents: 84
diff changeset
438 CREATE_REDIRECT_HANDLER(HomePage, "/")
a3962e681490 Fixed routing issues.
June Park <parkjune1995@gmail.com>
parents: 84
diff changeset
439 CREATE_REDIRECT_HANDLER(Resume, "/resume")
a3962e681490 Fixed routing issues.
June Park <parkjune1995@gmail.com>
parents: 84
diff changeset
440 CREATE_REDIRECT_HANDLER(Tools, "/tools")
a3962e681490 Fixed routing issues.
June Park <parkjune1995@gmail.com>
parents: 84
diff changeset
441 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
442 CREATE_REDIRECT_HANDLER(FileConverter, "/tools/file_converter")
79
5710108c949e [Seobeo] Added Redirect logic.
June Park <parkjune1995@gmail.com>
parents: 78
diff changeset
443
7
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
444 int main(void)
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
445 {
72
4532ce6d9eb8 [Seobeo] Added router to the server logic. Few dowa string manipulation logics.
June Park <parkjune1995@gmail.com>
parents: 36
diff changeset
446 Seobeo_Router_Init();
77
c348ac875294 [Seobeo] Server side rendering.
June Park <parkjune1995@gmail.com>
parents: 72
diff changeset
447 Seobeo_Router_Register("GET", "/", GetHomePage);
79
5710108c949e [Seobeo] Added Redirect logic.
June Park <parkjune1995@gmail.com>
parents: 78
diff changeset
448 Seobeo_Router_Register("GET", "/index.html", GetRedirectHomePage);
78
June Park <parkjune1995@gmail.com>
parents: 77
diff changeset
449
June Park <parkjune1995@gmail.com>
parents: 77
diff changeset
450 Seobeo_Router_Register("GET", "/resume", GetResume);
79
5710108c949e [Seobeo] Added Redirect logic.
June Park <parkjune1995@gmail.com>
parents: 78
diff changeset
451 Seobeo_Router_Register("GET", "/resume/index.html", GetRedirectResume);
78
June Park <parkjune1995@gmail.com>
parents: 77
diff changeset
452
June Park <parkjune1995@gmail.com>
parents: 77
diff changeset
453 Seobeo_Router_Register("GET", "/tools", GetTools);
79
5710108c949e [Seobeo] Added Redirect logic.
June Park <parkjune1995@gmail.com>
parents: 78
diff changeset
454 Seobeo_Router_Register("GET", "/tools/index.html", GetRedirectTools);
78
June Park <parkjune1995@gmail.com>
parents: 77
diff changeset
455
June Park <parkjune1995@gmail.com>
parents: 77
diff changeset
456 Seobeo_Router_Register("GET", "/tools/markdown_to_html", GetMDToHTML);
79
5710108c949e [Seobeo] Added Redirect logic.
June Park <parkjune1995@gmail.com>
parents: 78
diff changeset
457 Seobeo_Router_Register("GET", "/tools/markdown_to_html/index.html", GetRedirectMarkDownToHtml);
78
June Park <parkjune1995@gmail.com>
parents: 77
diff changeset
458
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
459 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
460 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
461
655ea0b661fd [Seobeo] Added 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 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
463 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
464 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
465
89
b5e65b01f0a3 Seobeo Tests.
June Park <parkjune1995@gmail.com>
parents: 88
diff changeset
466 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
467 }