Mercurial
annotate mrjunejune/main.c @ 273:e02e2036ef84 default tip
add Layer 2 JRPG component system
Add reusable content and window modals, an isolated component sandbox, shared cyberpunk scroll areas, production-safe cache freshness, and server-rendered JRPG panel state.
Co-authored-by: Copilot <[email protected]>
| author | MrJuneJune <me@mrjunejune.com> |
|---|---|
| date | Sat, 08 Aug 2026 02:08:08 -0700 |
| parents | 056790c4fb0d |
| children |
| rev | line source |
|---|---|
|
7
114cad94008f
[Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
1 #include "seobeo/seobeo.h" |
|
158
1c0878eb17de
[MrJuneJune] Readme file gets compiled in server side.
June Park <parkjune1995@gmail.com>
parents:
132
diff
changeset
|
2 #include "markdown_converter/markdown_to_html.h" |
|
200
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
3 #include "s3/s3_uploader.h" |
| 201 | 4 #include "deita/deita.h" |
|
244
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
5 #include "mrjunejune/latex_renderer.h" |
|
260
1f9877b637e9
Add Copilot-powered cyberpunk JRPG chat
MrJuneJune <mrjunejune@users.noreply.github.com>
parents:
259
diff
changeset
|
6 #include "mrjunejune/conversation_api.h" |
|
264
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
7 #include "mrjunejune/auth_api.h" |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
8 #include "mrjunejune/admin_api.h" |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
9 #include "mrjunejune/template_renderer.h" |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
10 #include "auth/auth_crypto.h" |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
11 #include <stdio.h> |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
12 #include <stdlib.h> |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
13 #include <string.h> |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
14 #include <strings.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
|
15 #include <time.h> |
| 201 | 16 #include <sys/stat.h> |
| 17 #include <stdarg.h> | |
|
244
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
18 #include <stdatomic.h> |
| 201 | 19 #include <pthread.h> |
|
264
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
20 #include <arpa/inet.h> |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
21 #include <openssl/crypto.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
|
22 |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
23 // 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
|
24 #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
|
25 #define UUID_LEN 37 |
|
7
114cad94008f
[Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
26 |
|
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
|
27 volatile sig_atomic_t stop_server = 0; |
|
248
b8b6e726964a
[style] Use Dowa type aliases in C
MrJuneJune <me@mrjunejune.com>
parents:
244
diff
changeset
|
28 static _Atomic uint32 counter = 0; |
|
244
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
29 static _Atomic boolean g_latex_rendering = FALSE; |
|
250
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
30 static Seobeo_Worker_Pool *g_media_worker_pool = NULL; |
|
36
84672efec192
[Zenbu] WIP fixing issues regarding to using edge only. I think there is a problem where socket closes before sending back the info.
MrJuneJune <me@mrjunejune.com>
parents:
19
diff
changeset
|
31 |
|
250
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
32 // Media processing context owned by a background worker. |
| 201 | 33 typedef struct { |
| 34 int64 media_id; | |
| 35 char s3_key_original[512]; | |
| 36 char s3_key_processed[512]; | |
| 37 char content_type[128]; | |
| 38 char access_token[256]; | |
|
202
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
39 char db_path[256]; |
| 201 | 40 S3_Config s3_config; |
| 41 } Media_Processing_Context; | |
| 42 | |
|
216
e82b80b24012
[MrJuneJune] Make webp translate background job.
June Park <parkjune1995@gmail.com>
parents:
206
diff
changeset
|
43 typedef struct { |
|
e82b80b24012
[MrJuneJune] Make webp translate background job.
June Park <parkjune1995@gmail.com>
parents:
206
diff
changeset
|
44 char *input_path; |
|
e82b80b24012
[MrJuneJune] Make webp translate background job.
June Park <parkjune1995@gmail.com>
parents:
206
diff
changeset
|
45 char *output_path; |
|
250
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
46 int result; |
|
216
e82b80b24012
[MrJuneJune] Make webp translate background job.
June Park <parkjune1995@gmail.com>
parents:
206
diff
changeset
|
47 } File_Converter_Config; |
|
e82b80b24012
[MrJuneJune] Make webp translate background job.
June Park <parkjune1995@gmail.com>
parents:
206
diff
changeset
|
48 |
|
200
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
49 // Server configuration (loaded from .config) |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
50 static char g_upload_auth_token[256] = {0}; |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
51 static char g_s3_region[64] = "us-west-2"; |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
52 static char g_s3_bucket[128] = "mrjunejune"; |
| 201 | 53 static char g_s3_cloudfront_url[256] = {0}; |
| 54 static char g_db_path[256] = "mrjunejune/data/mrjunejune.db"; | |
|
200
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
55 static int g_s3_url_expires = 3600; |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
56 static S3_Config g_s3_config = {0}; |
| 201 | 57 static Deita_Connection *g_db_connection = NULL; |
|
264
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
58 /* S3 credentials — never logged; zero after use in init. */ |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
59 static char g_s3_access_key[128] = {0}; |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
60 static char g_s3_secret_key[128] = {0}; |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
61 |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
62 /* Auth configuration */ |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
63 static char g_auth_cookie_secret[AUTH_CRYPTO_COOKIE_SECRET_MAX_BYTES * 2 + 1] = {0}; |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
64 static char g_auth_bootstrap_username[64] = {0}; |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
65 static char g_auth_bootstrap_password_hash[AUTH_CRYPTO_PASSWORD_HASH_ENCODED_SIZE] = {0}; |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
66 static char g_auth_trusted_proxy[AUTH_CRYPTO_IP_MAX_BYTES] = {0}; |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
67 static int64 g_auth_session_idle_ttl = AUTH_API_SESSION_IDLE_TTL_DEFAULT; |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
68 static int64 g_auth_session_abs_ttl = AUTH_API_SESSION_ABS_TTL_DEFAULT; |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
69 static int64 g_auth_guest_ttl = AUTH_API_GUEST_TTL_DEFAULT; |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
70 static boolean g_auth_dev_insecure = FALSE; |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
71 static char g_server_host[128] = {0}; /* SERVER_HOST; empty → 0.0.0.0 */ |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
72 |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
73 /* Guest inference / quota configuration */ |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
74 #define G_GUEST_DAILY_TURNS_DEFAULT 10 |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
75 #define G_GUEST_DAILY_OUTPUT_TOKENS_DEFAULT 20000 |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
76 #define G_GUEST_REQUEST_OUTPUT_TOKENS_MIN 1 |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
77 #define G_GUEST_DAILY_TURNS_MIN 1 |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
78 #define G_GUEST_DAILY_TURNS_MAX 10000 |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
79 #define G_GUEST_DAILY_OUTPUT_TOKENS_MIN 1 |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
80 #define G_GUEST_DAILY_OUTPUT_TOKENS_MAX 1000000 |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
81 #define G_GUEST_REQUEST_OUTPUT_TOKENS_DEFAULT 2048 |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
82 static boolean g_guest_inference_enabled = FALSE; |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
83 static int64 g_guest_daily_turns = G_GUEST_DAILY_TURNS_DEFAULT; |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
84 static int64 g_guest_daily_output_tokens = G_GUEST_DAILY_OUTPUT_TOKENS_DEFAULT; |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
85 static int64 g_guest_request_output_tokens = G_GUEST_REQUEST_OUTPUT_TOKENS_DEFAULT; |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
86 |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
87 /* |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
88 * Strict full-string integer parse: returns FALSE if value is empty, |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
89 * has trailing non-digit characters, or overflows a long. |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
90 */ |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
91 static boolean config__parse_int64_strict(const char *value, int64 *out) |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
92 { |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
93 if (!value || value[0] == '\0') |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
94 return FALSE; |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
95 char *end = NULL; |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
96 long v = strtol(value, &end, 10); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
97 if (end == value || *end != '\0') |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
98 return FALSE; |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
99 *out = (int64)v; |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
100 return TRUE; |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
101 } |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
102 |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
103 /* |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
104 * Decode a lowercase or uppercase hex string into raw bytes. |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
105 * Returns the number of decoded bytes, or 0 on any error. |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
106 * hex_len must be even; each pair of hex chars produces one byte. |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
107 */ |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
108 static size_t config__hex_decode(const char *hex, uint8 *out, size_t out_capacity) |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
109 { |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
110 if (!hex || !out) return 0; |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
111 size_t hex_len = strlen(hex); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
112 if (hex_len == 0 || hex_len % 2 != 0) return 0; |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
113 size_t byte_count = hex_len / 2; |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
114 if (byte_count > out_capacity) return 0; |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
115 for (size_t i = 0; i < byte_count; i++) |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
116 { |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
117 int h, l; |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
118 char hi = hex[i * 2]; |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
119 char lo = hex[i * 2 + 1]; |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
120 if (hi >= '0' && hi <= '9') h = hi - '0'; |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
121 else if (hi >= 'a' && hi <= 'f') h = hi - 'a' + 10; |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
122 else if (hi >= 'A' && hi <= 'F') h = hi - 'A' + 10; |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
123 else return 0; |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
124 if (lo >= '0' && lo <= '9') l = lo - '0'; |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
125 else if (lo >= 'a' && lo <= 'f') l = lo - 'a' + 10; |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
126 else if (lo >= 'A' && lo <= 'F') l = lo - 'A' + 10; |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
127 else return 0; |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
128 out[i] = (uint8)((h << 4) | l); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
129 } |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
130 return byte_count; |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
131 } |
|
200
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
132 |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
133 static void load_config(const char *config_path) |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
134 { |
|
265
056790c4fb0d
add role-aware Epi assistant prompts
MrJuneJune <me@mrjunejune.com>
parents:
264
diff
changeset
|
135 boolean config_loaded = FALSE; |
|
200
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
136 FILE *f = fopen(config_path, "r"); |
|
264
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
137 char workspace_config_path[1024] = {0}; |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
138 if (!f) |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
139 { |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
140 const char *workspace = getenv("BUILD_WORKSPACE_DIRECTORY"); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
141 if (workspace && workspace[0] != '\0') |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
142 { |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
143 int written = snprintf( |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
144 workspace_config_path, |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
145 sizeof(workspace_config_path), |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
146 "%s/%s", |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
147 workspace, |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
148 config_path); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
149 if (written > 0 && (size_t)written < sizeof(workspace_config_path)) |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
150 f = fopen(workspace_config_path, "r"); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
151 } |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
152 } |
|
200
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
153 if (!f) |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
154 { |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
155 printf("[CONFIG] Warning: Could not open %s, using defaults\n", config_path); |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
156 } |
|
264
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
157 else |
|
200
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
158 { |
|
265
056790c4fb0d
add role-aware Epi assistant prompts
MrJuneJune <me@mrjunejune.com>
parents:
264
diff
changeset
|
159 config_loaded = TRUE; |
|
264
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
160 char line[512]; |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
161 while (fgets(line, sizeof(line), f)) |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
162 { |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
163 // Skip comments and empty lines |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
164 if (line[0] == '#' || line[0] == '\n' || line[0] == '\r') continue; |
|
200
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
165 |
|
264
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
166 char *eq = strchr(line, '='); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
167 if (!eq) continue; |
|
200
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
168 |
|
264
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
169 *eq = '\0'; |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
170 char *key = line; |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
171 char *value = eq + 1; |
|
200
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
172 |
|
264
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
173 // Trim newline from value |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
174 size_t vlen = strlen(value); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
175 while (vlen > 0 && (value[vlen-1] == '\n' || value[vlen-1] == '\r')) |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
176 value[--vlen] = '\0'; |
|
200
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
177 |
|
264
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
178 if (strcmp(key, "UPLOAD_AUTH_TOKEN") == 0) |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
179 { |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
180 strncpy(g_upload_auth_token, value, sizeof(g_upload_auth_token) - 1); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
181 } |
|
200
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
182 else if (strcmp(key, "S3_REGION") == 0) |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
183 { |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
184 strncpy(g_s3_region, value, sizeof(g_s3_region) - 1); |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
185 } |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
186 else if (strcmp(key, "S3_BUCKET") == 0) |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
187 { |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
188 strncpy(g_s3_bucket, value, sizeof(g_s3_bucket) - 1); |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
189 } |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
190 else if (strcmp(key, "S3_URL_EXPIRES") == 0) |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
191 { |
|
264
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
192 int64 v; |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
193 if (!config__parse_int64_strict(value, &v) || v <= 0) |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
194 { |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
195 fprintf(stderr, "[CONFIG] ERROR: S3_URL_EXPIRES must be a positive integer\n"); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
196 exit(1); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
197 } |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
198 g_s3_url_expires = (int)v; |
|
200
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
199 } |
| 201 | 200 else if (strcmp(key, "S3_CLOUDFRONT_URL") == 0) |
| 201 { | |
| 202 strncpy(g_s3_cloudfront_url, value, sizeof(g_s3_cloudfront_url) - 1); | |
| 203 } | |
| 204 else if (strcmp(key, "DB_PATH") == 0) | |
| 205 { | |
| 206 strncpy(g_db_path, value, sizeof(g_db_path) - 1); | |
| 207 } | |
|
264
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
208 else if (strcmp(key, "AWS_MRJUNEJUNE_ACCESS_KEY") == 0) |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
209 { |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
210 strncpy(g_s3_access_key, value, sizeof(g_s3_access_key) - 1); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
211 } |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
212 else if (strcmp(key, "AWS_MRJUNEJUNE_SECRET_ACCESS_KEY") == 0) |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
213 { |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
214 strncpy(g_s3_secret_key, value, sizeof(g_s3_secret_key) - 1); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
215 } |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
216 else if (strcmp(key, "AUTH_COOKIE_SECRET") == 0) |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
217 { |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
218 /* Never log this value */ |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
219 strncpy(g_auth_cookie_secret, value, sizeof(g_auth_cookie_secret) - 1); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
220 } |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
221 else if (strcmp(key, "AUTH_BOOTSTRAP_USERNAME") == 0) |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
222 { |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
223 strncpy(g_auth_bootstrap_username, value, |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
224 sizeof(g_auth_bootstrap_username) - 1); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
225 } |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
226 else if (strcmp(key, "AUTH_BOOTSTRAP_PASSWORD_HASH") == 0) |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
227 { |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
228 strncpy(g_auth_bootstrap_password_hash, value, |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
229 sizeof(g_auth_bootstrap_password_hash) - 1); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
230 } |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
231 else if (strcmp(key, "AUTH_TRUSTED_PROXY") == 0) |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
232 { |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
233 strncpy(g_auth_trusted_proxy, value, sizeof(g_auth_trusted_proxy) - 1); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
234 } |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
235 else if (strcmp(key, "AUTH_SESSION_IDLE_TTL") == 0) |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
236 { |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
237 int64 v; |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
238 if (!config__parse_int64_strict(value, &v) || v <= 0) |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
239 { |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
240 fprintf(stderr, "[CONFIG] ERROR: AUTH_SESSION_IDLE_TTL must be a positive integer\n"); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
241 exit(1); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
242 } |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
243 g_auth_session_idle_ttl = v; |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
244 } |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
245 else if (strcmp(key, "AUTH_SESSION_ABS_TTL") == 0) |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
246 { |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
247 int64 v; |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
248 if (!config__parse_int64_strict(value, &v) || v <= 0) |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
249 { |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
250 fprintf(stderr, "[CONFIG] ERROR: AUTH_SESSION_ABS_TTL must be a positive integer\n"); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
251 exit(1); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
252 } |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
253 g_auth_session_abs_ttl = v; |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
254 } |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
255 else if (strcmp(key, "AUTH_GUEST_TTL") == 0) |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
256 { |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
257 int64 v; |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
258 if (!config__parse_int64_strict(value, &v) || v <= 0) |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
259 { |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
260 fprintf(stderr, "[CONFIG] ERROR: AUTH_GUEST_TTL must be a positive integer\n"); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
261 exit(1); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
262 } |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
263 g_auth_guest_ttl = v; |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
264 } |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
265 else if (strcmp(key, "AUTH_DEV_INSECURE_COOKIE") == 0) |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
266 { |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
267 g_auth_dev_insecure = |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
268 (strcmp(value, "1") == 0 || strcmp(value, "true") == 0); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
269 } |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
270 else if (strcmp(key, "SERVER_HOST") == 0) |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
271 { |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
272 strncpy(g_server_host, value, sizeof(g_server_host) - 1); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
273 } |
|
265
056790c4fb0d
add role-aware Epi assistant prompts
MrJuneJune <me@mrjunejune.com>
parents:
264
diff
changeset
|
274 else if (strcmp(key, "MRJUNEJUNE_ALLOW_GUEST_INFERENCE") == 0) |
|
056790c4fb0d
add role-aware Epi assistant prompts
MrJuneJune <me@mrjunejune.com>
parents:
264
diff
changeset
|
275 { |
|
056790c4fb0d
add role-aware Epi assistant prompts
MrJuneJune <me@mrjunejune.com>
parents:
264
diff
changeset
|
276 if (strcmp(value, "1") == 0 || strcasecmp(value, "true") == 0) |
|
056790c4fb0d
add role-aware Epi assistant prompts
MrJuneJune <me@mrjunejune.com>
parents:
264
diff
changeset
|
277 g_guest_inference_enabled = TRUE; |
|
056790c4fb0d
add role-aware Epi assistant prompts
MrJuneJune <me@mrjunejune.com>
parents:
264
diff
changeset
|
278 else if (strcmp(value, "0") == 0 || strcasecmp(value, "false") == 0) |
|
056790c4fb0d
add role-aware Epi assistant prompts
MrJuneJune <me@mrjunejune.com>
parents:
264
diff
changeset
|
279 g_guest_inference_enabled = FALSE; |
|
056790c4fb0d
add role-aware Epi assistant prompts
MrJuneJune <me@mrjunejune.com>
parents:
264
diff
changeset
|
280 else |
|
056790c4fb0d
add role-aware Epi assistant prompts
MrJuneJune <me@mrjunejune.com>
parents:
264
diff
changeset
|
281 { |
|
056790c4fb0d
add role-aware Epi assistant prompts
MrJuneJune <me@mrjunejune.com>
parents:
264
diff
changeset
|
282 fprintf( |
|
056790c4fb0d
add role-aware Epi assistant prompts
MrJuneJune <me@mrjunejune.com>
parents:
264
diff
changeset
|
283 stderr, |
|
056790c4fb0d
add role-aware Epi assistant prompts
MrJuneJune <me@mrjunejune.com>
parents:
264
diff
changeset
|
284 "[CONFIG] ERROR: MRJUNEJUNE_ALLOW_GUEST_INFERENCE must be true or false\n"); |
|
056790c4fb0d
add role-aware Epi assistant prompts
MrJuneJune <me@mrjunejune.com>
parents:
264
diff
changeset
|
285 exit(1); |
|
056790c4fb0d
add role-aware Epi assistant prompts
MrJuneJune <me@mrjunejune.com>
parents:
264
diff
changeset
|
286 } |
|
056790c4fb0d
add role-aware Epi assistant prompts
MrJuneJune <me@mrjunejune.com>
parents:
264
diff
changeset
|
287 } |
|
264
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
288 else if (strcmp(key, "AUTH_GUEST_DAILY_TURNS") == 0) |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
289 { |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
290 int64 v; |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
291 if (!config__parse_int64_strict(value, &v) || |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
292 v < G_GUEST_DAILY_TURNS_MIN || v > G_GUEST_DAILY_TURNS_MAX) |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
293 { |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
294 printf("[CONFIG] ERROR: AUTH_GUEST_DAILY_TURNS must be %d..%d\n", |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
295 G_GUEST_DAILY_TURNS_MIN, G_GUEST_DAILY_TURNS_MAX); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
296 exit(1); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
297 } |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
298 g_guest_daily_turns = v; |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
299 } |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
300 else if (strcmp(key, "AUTH_GUEST_DAILY_OUTPUT_TOKENS") == 0) |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
301 { |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
302 int64 v; |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
303 if (!config__parse_int64_strict(value, &v) || |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
304 v < G_GUEST_DAILY_OUTPUT_TOKENS_MIN || |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
305 v > G_GUEST_DAILY_OUTPUT_TOKENS_MAX) |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
306 { |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
307 printf("[CONFIG] ERROR: AUTH_GUEST_DAILY_OUTPUT_TOKENS must be %d..%d\n", |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
308 G_GUEST_DAILY_OUTPUT_TOKENS_MIN, G_GUEST_DAILY_OUTPUT_TOKENS_MAX); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
309 exit(1); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
310 } |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
311 g_guest_daily_output_tokens = v; |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
312 } |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
313 else if (strcmp(key, "AUTH_GUEST_REQUEST_OUTPUT_TOKENS") == 0) |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
314 { |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
315 int64 v; |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
316 if (!config__parse_int64_strict(value, &v) || |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
317 v < G_GUEST_REQUEST_OUTPUT_TOKENS_MIN) |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
318 { |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
319 printf("[CONFIG] ERROR: AUTH_GUEST_REQUEST_OUTPUT_TOKENS must be >= %d\n", |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
320 G_GUEST_REQUEST_OUTPUT_TOKENS_MIN); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
321 exit(1); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
322 } |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
323 g_guest_request_output_tokens = v; |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
324 } |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
325 } |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
326 fclose(f); |
|
200
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
327 } |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
328 |
| 201 | 329 printf("[CONFIG] Loaded: token=%s..., region=%s, bucket=%s, expires=%d, cloudfront=%s, db=%s\n", |
|
200
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
330 g_upload_auth_token[0] ? "***" : "(empty)", |
| 201 | 331 g_s3_region, g_s3_bucket, g_s3_url_expires, |
| 332 g_s3_cloudfront_url[0] ? g_s3_cloudfront_url : "(none)", | |
| 333 g_db_path); | |
|
264
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
334 printf("[CONFIG] Auth: secret=%s, bootstrap_user=%s, trusted_proxy=%s\n", |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
335 g_auth_cookie_secret[0] ? "(set)" : "(not set)", |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
336 g_auth_bootstrap_username[0] ? g_auth_bootstrap_username : "(none)", |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
337 g_auth_trusted_proxy[0] ? "(set)" : "(not set)"); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
338 |
|
265
056790c4fb0d
add role-aware Epi assistant prompts
MrJuneJune <me@mrjunejune.com>
parents:
264
diff
changeset
|
339 const char *database_path_override = getenv("MRJUNEJUNE_DB_PATH"); |
|
264
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
340 const char *test_tmpdir = getenv("TEST_TMPDIR"); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
341 if (database_path_override && database_path_override[0] != '\0') |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
342 { |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
343 strncpy(g_db_path, database_path_override, sizeof(g_db_path) - 1); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
344 } |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
345 else if (test_tmpdir && test_tmpdir[0] != '\0') |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
346 { |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
347 snprintf(g_db_path, sizeof(g_db_path), "%s/mrjunejune.db", test_tmpdir); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
348 } |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
349 |
|
265
056790c4fb0d
add role-aware Epi assistant prompts
MrJuneJune <me@mrjunejune.com>
parents:
264
diff
changeset
|
350 /* Environment configuration is a compatibility/testing fallback only when |
|
056790c4fb0d
add role-aware Epi assistant prompts
MrJuneJune <me@mrjunejune.com>
parents:
264
diff
changeset
|
351 * no config file is present. Dynamic supervisor wiring is read separately. */ |
|
056790c4fb0d
add role-aware Epi assistant prompts
MrJuneJune <me@mrjunejune.com>
parents:
264
diff
changeset
|
352 if (!config_loaded) |
|
264
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
353 { |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
354 const char *env; |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
355 |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
356 /* S3 / server config env overrides */ |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
357 if ((env = getenv("UPLOAD_AUTH_TOKEN")) && env[0] != '\0') |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
358 strncpy(g_upload_auth_token, env, sizeof(g_upload_auth_token) - 1); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
359 if ((env = getenv("S3_REGION")) && env[0] != '\0') |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
360 strncpy(g_s3_region, env, sizeof(g_s3_region) - 1); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
361 if ((env = getenv("S3_BUCKET")) && env[0] != '\0') |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
362 strncpy(g_s3_bucket, env, sizeof(g_s3_bucket) - 1); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
363 if ((env = getenv("S3_CLOUDFRONT_URL")) && env[0] != '\0') |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
364 strncpy(g_s3_cloudfront_url, env, sizeof(g_s3_cloudfront_url) - 1); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
365 if ((env = getenv("S3_URL_EXPIRES")) && env[0] != '\0') |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
366 { |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
367 int64 v; |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
368 if (!config__parse_int64_strict(env, &v) || v <= 0) |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
369 { |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
370 fprintf(stderr, "[CONFIG] ERROR: S3_URL_EXPIRES must be a positive integer\n"); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
371 exit(1); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
372 } |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
373 g_s3_url_expires = (int)v; |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
374 } |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
375 if ((env = getenv("AWS_MRJUNEJUNE_ACCESS_KEY")) && env[0] != '\0') |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
376 strncpy(g_s3_access_key, env, sizeof(g_s3_access_key) - 1); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
377 if ((env = getenv("AWS_MRJUNEJUNE_SECRET_ACCESS_KEY")) && env[0] != '\0') |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
378 strncpy(g_s3_secret_key, env, sizeof(g_s3_secret_key) - 1); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
379 |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
380 /* Auth env overrides */ |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
381 if ((env = getenv("AUTH_COOKIE_SECRET")) && env[0] != '\0') |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
382 strncpy(g_auth_cookie_secret, env, sizeof(g_auth_cookie_secret) - 1); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
383 if ((env = getenv("AUTH_BOOTSTRAP_USERNAME")) && env[0] != '\0') |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
384 strncpy(g_auth_bootstrap_username, env, |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
385 sizeof(g_auth_bootstrap_username) - 1); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
386 if ((env = getenv("AUTH_BOOTSTRAP_PASSWORD_HASH")) && env[0] != '\0') |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
387 strncpy(g_auth_bootstrap_password_hash, env, |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
388 sizeof(g_auth_bootstrap_password_hash) - 1); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
389 if ((env = getenv("AUTH_TRUSTED_PROXY")) && env[0] != '\0') |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
390 strncpy(g_auth_trusted_proxy, env, sizeof(g_auth_trusted_proxy) - 1); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
391 if ((env = getenv("AUTH_SESSION_IDLE_TTL")) && env[0] != '\0') |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
392 { |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
393 int64 v; |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
394 if (!config__parse_int64_strict(env, &v) || v <= 0) |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
395 { |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
396 fprintf(stderr, "[CONFIG] ERROR: AUTH_SESSION_IDLE_TTL must be a positive integer\n"); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
397 exit(1); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
398 } |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
399 g_auth_session_idle_ttl = v; |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
400 } |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
401 if ((env = getenv("AUTH_SESSION_ABS_TTL")) && env[0] != '\0') |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
402 { |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
403 int64 v; |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
404 if (!config__parse_int64_strict(env, &v) || v <= 0) |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
405 { |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
406 fprintf(stderr, "[CONFIG] ERROR: AUTH_SESSION_ABS_TTL must be a positive integer\n"); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
407 exit(1); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
408 } |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
409 g_auth_session_abs_ttl = v; |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
410 } |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
411 if ((env = getenv("AUTH_GUEST_TTL")) && env[0] != '\0') |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
412 { |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
413 int64 v; |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
414 if (!config__parse_int64_strict(env, &v) || v <= 0) |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
415 { |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
416 fprintf(stderr, "[CONFIG] ERROR: AUTH_GUEST_TTL must be a positive integer\n"); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
417 exit(1); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
418 } |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
419 g_auth_guest_ttl = v; |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
420 } |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
421 if ((env = getenv("AUTH_DEV_INSECURE_COOKIE")) && env[0] != '\0') |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
422 g_auth_dev_insecure = |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
423 (strcmp(env, "1") == 0 || strcmp(env, "true") == 0); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
424 if ((env = getenv("SERVER_HOST")) && env[0] != '\0') |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
425 strncpy(g_server_host, env, sizeof(g_server_host) - 1); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
426 |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
427 /* Guest inference enable: runtime-only, boolean parsed strictly. */ |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
428 if ((env = getenv("MRJUNEJUNE_ALLOW_GUEST_INFERENCE")) && env[0] != '\0') |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
429 g_guest_inference_enabled = |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
430 (strcmp(env, "1") == 0 || strcasecmp(env, "true") == 0); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
431 else if ((env = getenv("MRJUNEJUNE_ALLOW_ANONYMOUS_INFERENCE")) && env[0] != '\0') |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
432 g_guest_inference_enabled = |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
433 (strcmp(env, "1") == 0 || strcasecmp(env, "true") == 0); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
434 |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
435 /* Guest quota env overrides: strict full-string integer, fail on malformed. */ |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
436 if ((env = getenv("AUTH_GUEST_DAILY_TURNS")) && env[0] != '\0') |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
437 { |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
438 int64 v; |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
439 if (!config__parse_int64_strict(env, &v) || |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
440 v < G_GUEST_DAILY_TURNS_MIN || v > G_GUEST_DAILY_TURNS_MAX) |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
441 { |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
442 fprintf(stderr, |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
443 "[CONFIG] ERROR: AUTH_GUEST_DAILY_TURNS must be %d..%d\n", |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
444 G_GUEST_DAILY_TURNS_MIN, G_GUEST_DAILY_TURNS_MAX); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
445 exit(1); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
446 } |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
447 g_guest_daily_turns = v; |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
448 } |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
449 if ((env = getenv("AUTH_GUEST_DAILY_OUTPUT_TOKENS")) && env[0] != '\0') |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
450 { |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
451 int64 v; |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
452 if (!config__parse_int64_strict(env, &v) || |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
453 v < G_GUEST_DAILY_OUTPUT_TOKENS_MIN || |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
454 v > G_GUEST_DAILY_OUTPUT_TOKENS_MAX) |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
455 { |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
456 fprintf(stderr, |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
457 "[CONFIG] ERROR: AUTH_GUEST_DAILY_OUTPUT_TOKENS must be %d..%d\n", |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
458 G_GUEST_DAILY_OUTPUT_TOKENS_MIN, G_GUEST_DAILY_OUTPUT_TOKENS_MAX); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
459 exit(1); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
460 } |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
461 g_guest_daily_output_tokens = v; |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
462 } |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
463 if ((env = getenv("AUTH_GUEST_REQUEST_OUTPUT_TOKENS")) && env[0] != '\0') |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
464 { |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
465 int64 v; |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
466 if (!config__parse_int64_strict(env, &v) || |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
467 v < G_GUEST_REQUEST_OUTPUT_TOKENS_MIN) |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
468 { |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
469 fprintf(stderr, |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
470 "[CONFIG] ERROR: AUTH_GUEST_REQUEST_OUTPUT_TOKENS must be >= %d\n", |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
471 G_GUEST_REQUEST_OUTPUT_TOKENS_MIN); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
472 exit(1); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
473 } |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
474 g_guest_request_output_tokens = v; |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
475 } |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
476 } |
| 201 | 477 } |
| 478 | |
| 479 static void init_database(void) | |
| 480 { | |
| 481 // Create data directory if needed | |
| 482 char *last_slash = strrchr(g_db_path, '/'); | |
| 483 if (last_slash) | |
| 484 { | |
| 485 char dir_path[256]; | |
| 486 size_t dir_len = last_slash - g_db_path; | |
| 487 strncpy(dir_path, g_db_path, dir_len); | |
| 488 dir_path[dir_len] = '\0'; | |
| 489 mkdir(dir_path, 0755); | |
| 490 } | |
| 491 | |
| 492 g_db_connection = Deita_Connection_Create(DEITA_DATABASE_TYPE_SQLITE3, g_db_path); | |
| 493 if (!g_db_connection || !Deita_Connection_Is_Open(g_db_connection)) | |
| 494 { | |
| 495 printf("[DB] ERROR: Failed to open database at %s\n", g_db_path); | |
| 496 return; | |
| 497 } | |
| 498 | |
| 499 // Create editor_content table | |
| 500 const char *create_table = | |
| 501 "CREATE TABLE IF NOT EXISTS editor_content (" | |
| 502 " id INTEGER PRIMARY KEY AUTOINCREMENT," | |
| 503 " access_token TEXT NOT NULL," | |
| 504 " doc_id TEXT NOT NULL," | |
| 505 " content TEXT," | |
| 506 " created_at INTEGER DEFAULT (strftime('%s', 'now'))," | |
| 507 " updated_at INTEGER DEFAULT (strftime('%s', 'now'))," | |
| 508 " UNIQUE(access_token, doc_id)" | |
| 509 ")"; | |
| 510 | |
| 511 int32 result = Deita_Query_Execute_Update(g_db_connection, create_table); | |
| 512 if (result < 0) | |
| 513 { | |
| 514 printf("[DB] ERROR: Failed to create editor_content table\n"); | |
| 515 } | |
| 516 | |
| 517 // Create media_uploads table | |
| 518 const char *create_media_uploads = | |
| 519 "CREATE TABLE IF NOT EXISTS media_uploads (" | |
| 520 " id INTEGER PRIMARY KEY AUTOINCREMENT," | |
| 521 " access_token TEXT NOT NULL," | |
| 522 " original_filename TEXT NOT NULL," | |
| 523 " content_type TEXT NOT NULL," | |
| 524 " s3_key_original TEXT NOT NULL," | |
| 525 " s3_key_processed TEXT," | |
| 526 " file_size INTEGER," | |
| 527 " status TEXT NOT NULL DEFAULT 'pending'," | |
| 528 " error_message TEXT," | |
| 529 " created_at INTEGER DEFAULT (strftime('%s', 'now'))," | |
| 530 " updated_at INTEGER DEFAULT (strftime('%s', 'now'))" | |
| 531 ")"; | |
| 532 | |
| 533 result = Deita_Query_Execute_Update(g_db_connection, create_media_uploads); | |
| 534 if (result < 0) | |
| 535 { | |
| 536 printf("[DB] ERROR: Failed to create media_uploads table\n"); | |
| 537 } | |
| 538 | |
| 539 // Create indices for media_uploads | |
| 540 const char *create_status_idx = | |
| 541 "CREATE INDEX IF NOT EXISTS idx_media_uploads_status ON media_uploads(status)"; | |
| 542 result = Deita_Query_Execute_Update(g_db_connection, create_status_idx); | |
| 543 if (result < 0) | |
| 544 { | |
| 545 printf("[DB] ERROR: Failed to create status index\n"); | |
| 546 } | |
| 547 | |
| 548 const char *create_token_status_idx = | |
| 549 "CREATE INDEX IF NOT EXISTS idx_media_uploads_token_status " | |
| 550 "ON media_uploads(access_token, status)"; | |
| 551 result = Deita_Query_Execute_Update(g_db_connection, create_token_status_idx); | |
| 552 if (result < 0) | |
| 553 { | |
| 554 printf("[DB] ERROR: Failed to create token_status index\n"); | |
| 555 } | |
| 556 else | |
| 557 { | |
| 558 printf("[DB] Initialized: %s\n", g_db_path); | |
| 559 } | |
|
200
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
560 } |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
561 |
|
72
4532ce6d9eb8
[Seobeo] Added router to the server logic. Few dowa string manipulation logics.
June Park <parkjune1995@gmail.com>
parents:
36
diff
changeset
|
562 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
|
563 { |
|
260
1f9877b637e9
Add Copilot-powered cyberpunk JRPG chat
MrJuneJune <mrjunejune@users.noreply.github.com>
parents:
259
diff
changeset
|
564 (void)sig; |
|
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
|
565 stop_server = 1; |
|
260
1f9877b637e9
Add Copilot-powered cyberpunk JRPG chat
MrJuneJune <mrjunejune@users.noreply.github.com>
parents:
259
diff
changeset
|
566 Seobeo_Web_Server_Stop(); |
|
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
|
567 } |
|
7
114cad94008f
[Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
568 |
|
264
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
569 static Seobeo_Request_Entry *html_render_error(Dowa_Arena *arena, const char *msg) |
|
158
1c0878eb17de
[MrJuneJune] Readme file gets compiled in server side.
June Park <parkjune1995@gmail.com>
parents:
132
diff
changeset
|
570 { |
|
264
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
571 Seobeo_Request_Entry *resp = NULL; |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
572 Dowa_HashMap_Push_Arena(resp, "status", "500", arena); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
573 Dowa_HashMap_Push_Arena(resp, "content-type", "text/plain; charset=utf-8", arena); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
574 Dowa_HashMap_Push_Arena(resp, "body", (char *)msg, arena); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
575 return resp; |
|
158
1c0878eb17de
[MrJuneJune] Readme file gets compiled in server side.
June Park <parkjune1995@gmail.com>
parents:
132
diff
changeset
|
576 } |
|
1c0878eb17de
[MrJuneJune] Readme file gets compiled in server side.
June Park <parkjune1995@gmail.com>
parents:
132
diff
changeset
|
577 |
|
264
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
578 #define HTML_PAGE_CAP (128 * 1024) |
| 78 | 579 |
|
77
c348ac875294
[Seobeo] Server side rendering.
June Park <parkjune1995@gmail.com>
parents:
72
diff
changeset
|
580 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
|
581 { |
|
264
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
582 Seobeo_Request_Entry *resp = NULL; |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
583 char *final_body = Dowa_Arena_Allocate(arena, HTML_PAGE_CAP); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
584 if (!final_body || !Mjj_Template_Render_File(final_body, HTML_PAGE_CAP, "/index.html", arena)) |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
585 return html_render_error(arena, "Internal Server Error"); |
| 78 | 586 Dowa_HashMap_Push_Arena(resp, "body", final_body, arena); |
| 587 return resp; | |
| 588 } | |
|
77
c348ac875294
[Seobeo] Server side rendering.
June Park <parkjune1995@gmail.com>
parents:
72
diff
changeset
|
589 |
| 78 | 590 Seobeo_Request_Entry* GetResume(Seobeo_Request_Entry *req, Dowa_Arena *arena) |
| 591 { | |
|
264
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
592 Seobeo_Request_Entry *resp = NULL; |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
593 char *final_body = Dowa_Arena_Allocate(arena, HTML_PAGE_CAP); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
594 if (!final_body || !Mjj_Template_Render_File(final_body, HTML_PAGE_CAP, "/resume/index.html", arena)) |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
595 return html_render_error(arena, "Internal Server Error"); |
| 78 | 596 Dowa_HashMap_Push_Arena(resp, "body", final_body, arena); |
| 597 return resp; | |
| 598 } | |
| 599 | |
| 600 Seobeo_Request_Entry* GetTools(Seobeo_Request_Entry *req, Dowa_Arena *arena) | |
| 601 { | |
|
264
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
602 Seobeo_Request_Entry *resp = NULL; |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
603 char *final_body = Dowa_Arena_Allocate(arena, HTML_PAGE_CAP); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
604 if (!final_body || !Mjj_Template_Render_File(final_body, HTML_PAGE_CAP, "/tools/index.html", arena)) |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
605 return html_render_error(arena, "Internal Server Error"); |
| 78 | 606 Dowa_HashMap_Push_Arena(resp, "body", final_body, arena); |
|
77
c348ac875294
[Seobeo] Server side rendering.
June Park <parkjune1995@gmail.com>
parents:
72
diff
changeset
|
607 return resp; |
|
c348ac875294
[Seobeo] Server side rendering.
June Park <parkjune1995@gmail.com>
parents:
72
diff
changeset
|
608 } |
|
c348ac875294
[Seobeo] Server side rendering.
June Park <parkjune1995@gmail.com>
parents:
72
diff
changeset
|
609 |
|
c348ac875294
[Seobeo] Server side rendering.
June Park <parkjune1995@gmail.com>
parents:
72
diff
changeset
|
610 |
| 78 | 611 Seobeo_Request_Entry* GetMDToHTML(Seobeo_Request_Entry *req, Dowa_Arena *arena) |
| 612 { | |
|
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
|
613 Seobeo_Request_Entry *resp = NULL; |
|
264
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
614 char *final_body = Dowa_Arena_Allocate(arena, HTML_PAGE_CAP); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
615 if (!final_body || !Mjj_Template_Render_File(final_body, HTML_PAGE_CAP, "/tools/markdown_to_html/index.html", arena)) |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
616 return html_render_error(arena, "Internal Server Error"); |
| 78 | 617 Dowa_HashMap_Push_Arena(resp, "body", final_body, arena); |
| 618 return resp; | |
| 619 } | |
|
77
c348ac875294
[Seobeo] Server side rendering.
June Park <parkjune1995@gmail.com>
parents:
72
diff
changeset
|
620 |
|
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
|
621 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
|
622 { |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
623 Seobeo_Request_Entry *resp = NULL; |
|
264
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
624 char *final_body = Dowa_Arena_Allocate(arena, HTML_PAGE_CAP); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
625 if (!final_body || !Mjj_Template_Render_File(final_body, HTML_PAGE_CAP, "/tools/file_converter/index.html", arena)) |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
626 return html_render_error(arena, "Internal Server Error"); |
|
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
|
627 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
|
628 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
|
629 } |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
630 |
|
242
543df0fe7168
[tools] Add full HLS player support
MrJuneJune <me@mrjunejune.com>
parents:
241
diff
changeset
|
631 Seobeo_Request_Entry* GetHlsPlayer(Seobeo_Request_Entry *req, Dowa_Arena *arena) |
|
543df0fe7168
[tools] Add full HLS player support
MrJuneJune <me@mrjunejune.com>
parents:
241
diff
changeset
|
632 { |
|
543df0fe7168
[tools] Add full HLS player support
MrJuneJune <me@mrjunejune.com>
parents:
241
diff
changeset
|
633 Seobeo_Request_Entry *resp = NULL; |
|
264
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
634 char *final_body = Dowa_Arena_Allocate(arena, HTML_PAGE_CAP); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
635 if (!final_body || !Mjj_Template_Render_File(final_body, HTML_PAGE_CAP, "/tools/hls_player/index.html", arena)) |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
636 return html_render_error(arena, "Internal Server Error"); |
|
242
543df0fe7168
[tools] Add full HLS player support
MrJuneJune <me@mrjunejune.com>
parents:
241
diff
changeset
|
637 Dowa_HashMap_Push_Arena(resp, "body", final_body, arena); |
|
543df0fe7168
[tools] Add full HLS player support
MrJuneJune <me@mrjunejune.com>
parents:
241
diff
changeset
|
638 return resp; |
|
543df0fe7168
[tools] Add full HLS player support
MrJuneJune <me@mrjunejune.com>
parents:
241
diff
changeset
|
639 } |
|
543df0fe7168
[tools] Add full HLS player support
MrJuneJune <me@mrjunejune.com>
parents:
241
diff
changeset
|
640 |
|
244
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
641 Seobeo_Request_Entry* GetLatexEditor(Seobeo_Request_Entry *req, Dowa_Arena *arena) |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
642 { |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
643 Seobeo_Request_Entry *resp = NULL; |
|
264
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
644 char *final_body = Dowa_Arena_Allocate(arena, HTML_PAGE_CAP); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
645 if (!final_body || !Mjj_Template_Render_File(final_body, HTML_PAGE_CAP, "/tools/latex_editor/index.html", arena)) |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
646 return html_render_error(arena, "Internal Server Error"); |
|
244
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
647 Dowa_HashMap_Push_Arena(resp, "body", final_body, arena); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
648 return resp; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
649 } |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
650 |
|
264
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
651 |
|
244
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
652 static Seobeo_Request_Entry *LatexErrorResponse( |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
653 Dowa_Arena *arena, |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
654 int status, |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
655 const char *message) |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
656 { |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
657 Seobeo_Request_Entry *resp = NULL; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
658 char *status_value = Dowa_Arena_Allocate(arena, 16); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
659 snprintf(status_value, 16, "%d", status); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
660 char *body = Dowa_Arena_Allocate(arena, strlen(message) + 1); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
661 strcpy(body, message); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
662 Dowa_HashMap_Push_Arena(resp, "status", status_value, arena); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
663 Dowa_HashMap_Push_Arena(resp, "content-type", "text/plain; charset=utf-8", arena); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
664 Dowa_HashMap_Push_Arena(resp, "cache-control", "no-store", arena); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
665 Dowa_HashMap_Push_Arena(resp, "body", body, arena); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
666 return resp; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
667 } |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
668 |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
669 Seobeo_Request_Entry *RenderLatexPdf(Seobeo_Request_Entry *req, Dowa_Arena *arena) |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
670 { |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
671 void *body_kv = Dowa_HashMap_Get_Ptr(req, "Body"); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
672 void *length_kv = Dowa_HashMap_Get_Ptr(req, "Content-Length"); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
673 if (!body_kv || !length_kv) |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
674 return LatexErrorResponse(arena, 400, "LaTeX source and Content-Length are required."); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
675 |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
676 const char *length_value = ((Seobeo_Request_Entry *)length_kv)->value; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
677 char *length_end = NULL; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
678 errno = 0; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
679 unsigned long long parsed_length = strtoull(length_value, &length_end, 10); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
680 if (errno != 0 || |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
681 length_end == length_value || |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
682 *length_end != '\0' || |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
683 parsed_length == 0) |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
684 return LatexErrorResponse(arena, 400, "Content-Length is invalid."); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
685 if (parsed_length > LATEX_SOURCE_MAX_BYTES) |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
686 return LatexErrorResponse(arena, 413, "LaTeX source exceeds the 64 KiB limit."); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
687 |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
688 boolean expected = FALSE; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
689 if (!atomic_compare_exchange_strong( |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
690 &g_latex_rendering, |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
691 &expected, |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
692 TRUE)) |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
693 return LatexErrorResponse(arena, 429, "The LaTeX compiler is busy. Try again shortly."); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
694 |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
695 Latex_Render_Result render = Latex_Render( |
|
248
b8b6e726964a
[style] Use Dowa type aliases in C
MrJuneJune <me@mrjunejune.com>
parents:
244
diff
changeset
|
696 (const uint8 *)((Seobeo_Request_Entry *)body_kv)->value, |
|
244
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
697 (size_t)parsed_length); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
698 atomic_store(&g_latex_rendering, FALSE); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
699 if (render.status != LATEX_RENDER_OK) |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
700 { |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
701 int status = 500; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
702 if (render.status == LATEX_RENDER_INVALID_INPUT) |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
703 status = 400; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
704 else if (render.status == LATEX_RENDER_COMPILE_ERROR) |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
705 status = 422; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
706 else if (render.status == LATEX_RENDER_TIMEOUT) |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
707 status = 504; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
708 else if (render.status == LATEX_RENDER_COMPILER_UNAVAILABLE) |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
709 status = 503; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
710 |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
711 const char *diagnostics = render.diagnostics |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
712 ? render.diagnostics |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
713 : "LaTeX rendering failed."; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
714 char *body = Dowa_Arena_Allocate(arena, strlen(diagnostics) + 1); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
715 strcpy(body, diagnostics); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
716 Latex_Render_Result_Destroy(&render); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
717 return LatexErrorResponse(arena, status, body); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
718 } |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
719 |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
720 char *pdf = Dowa_Arena_Allocate(arena, render.pdf_size + 1); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
721 if (!pdf) |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
722 { |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
723 Latex_Render_Result_Destroy(&render); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
724 return LatexErrorResponse(arena, 500, "The generated PDF is too large to return."); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
725 } |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
726 memcpy(pdf, render.pdf_data, render.pdf_size); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
727 pdf[render.pdf_size] = '\0'; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
728 char *content_length = Dowa_Arena_Allocate(arena, 32); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
729 snprintf(content_length, 32, "%zu", render.pdf_size); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
730 Latex_Render_Result_Destroy(&render); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
731 |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
732 Seobeo_Request_Entry *resp = NULL; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
733 Dowa_HashMap_Push_Arena(resp, "status", "200", arena); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
734 Dowa_HashMap_Push_Arena(resp, "content-type", "application/pdf", arena); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
735 Dowa_HashMap_Push_Arena(resp, "content-length", content_length, arena); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
736 Dowa_HashMap_Push_Arena(resp, "content-disposition", "inline; filename=\"document.pdf\"", arena); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
737 Dowa_HashMap_Push_Arena(resp, "cache-control", "no-store", arena); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
738 Dowa_HashMap_Push_Arena(resp, "x-content-type-options", "nosniff", arena); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
739 Dowa_HashMap_Push_Arena(resp, "body", pdf, arena); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
740 return resp; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
741 } |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
742 |
|
250
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
743 // Joinable worker function for local image conversion. |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
744 void Simple_WebpConverter_Background(void *arg) |
|
216
e82b80b24012
[MrJuneJune] Make webp translate background job.
June Park <parkjune1995@gmail.com>
parents:
206
diff
changeset
|
745 { |
|
e82b80b24012
[MrJuneJune] Make webp translate background job.
June Park <parkjune1995@gmail.com>
parents:
206
diff
changeset
|
746 File_Converter_Config *configuration = (File_Converter_Config *)arg; |
|
e82b80b24012
[MrJuneJune] Make webp translate background job.
June Park <parkjune1995@gmail.com>
parents:
206
diff
changeset
|
747 |
|
e82b80b24012
[MrJuneJune] Make webp translate background job.
June Park <parkjune1995@gmail.com>
parents:
206
diff
changeset
|
748 char cmd[1024]; |
|
e82b80b24012
[MrJuneJune] Make webp translate background job.
June Park <parkjune1995@gmail.com>
parents:
206
diff
changeset
|
749 snprintf(cmd, sizeof(cmd), "ffmpeg -y -i %s -quality 80 %s 2>/tmp/error_log", |
|
e82b80b24012
[MrJuneJune] Make webp translate background job.
June Park <parkjune1995@gmail.com>
parents:
206
diff
changeset
|
750 configuration->input_path, configuration->output_path); |
|
e82b80b24012
[MrJuneJune] Make webp translate background job.
June Park <parkjune1995@gmail.com>
parents:
206
diff
changeset
|
751 Seobeo_Log(SEOBEO_INFO, "[MEDIA] Running FFmpeg: %s\n", cmd); |
|
250
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
752 configuration->result = system(cmd); |
|
216
e82b80b24012
[MrJuneJune] Make webp translate background job.
June Park <parkjune1995@gmail.com>
parents:
206
diff
changeset
|
753 |
|
250
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
754 Seobeo_Log( |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
755 SEOBEO_INFO, |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
756 "[MEDIA] FFmpeg result: %d\n", |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
757 configuration->result); |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
758 if (configuration->result != 0) |
|
216
e82b80b24012
[MrJuneJune] Make webp translate background job.
June Park <parkjune1995@gmail.com>
parents:
206
diff
changeset
|
759 { |
|
e82b80b24012
[MrJuneJune] Make webp translate background job.
June Park <parkjune1995@gmail.com>
parents:
206
diff
changeset
|
760 Seobeo_Log(SEOBEO_ERROR, "[MEDIA] ERROR: FFmpeg conversion failed\n"); |
|
250
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
761 return; |
|
216
e82b80b24012
[MrJuneJune] Make webp translate background job.
June Park <parkjune1995@gmail.com>
parents:
206
diff
changeset
|
762 } |
|
250
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
763 Seobeo_Log( |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
764 SEOBEO_INFO, |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
765 "[MEDIA] Successfully converted to webp: %s\n", |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
766 configuration->output_path); |
|
216
e82b80b24012
[MrJuneJune] Make webp translate background job.
June Park <parkjune1995@gmail.com>
parents:
206
diff
changeset
|
767 } |
|
e82b80b24012
[MrJuneJune] Make webp translate background job.
June Park <parkjune1995@gmail.com>
parents:
206
diff
changeset
|
768 |
|
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
|
769 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
|
770 { |
|
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
|
771 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
|
772 |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
773 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
|
774 { |
| 201 | 775 Seobeo_Log(SEOBEO_ERROR, "Request is NULL\n"); |
|
92
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
776 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
|
777 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
|
778 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
|
779 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
|
780 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
|
781 } |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
782 |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
783 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
|
784 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
|
785 |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
786 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
|
787 { |
| 201 | 788 Seobeo_Log(SEOBEO_INFO, " Key[%zu]: '%s'\n", i, req[i].key); |
|
92
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
789 } |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
790 |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
791 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
|
792 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
|
793 { |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
794 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
|
795 |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
796 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
|
797 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
|
798 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
|
799 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
|
800 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
|
801 } |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
802 |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
803 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
|
804 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
|
805 { |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
806 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
|
807 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
|
808 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
|
809 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
|
810 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
|
811 } |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
812 |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
813 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
|
814 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
|
815 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
|
816 |
|
216
e82b80b24012
[MrJuneJune] Make webp translate background job.
June Park <parkjune1995@gmail.com>
parents:
206
diff
changeset
|
817 Seobeo_Log(SEOBEO_DEBUG, "Converting image, file_size=%zu bytes\n", file_size); |
|
92
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
818 |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
819 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
|
820 |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
821 char *uuid4 = (char *)Dowa_Arena_Allocate(arena, UUID_LEN); |
|
250
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
822 uint32 seed = |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
823 (uint32)time(NULL) ^ |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
824 (uint32)Seobeo_Thread_Current_Id() ^ |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
825 counter++; |
|
92
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
826 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
|
827 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
|
828 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
|
829 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
|
830 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
|
831 { |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
832 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
|
833 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
|
834 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
|
835 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
|
836 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
|
837 } |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
838 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
|
839 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
|
840 |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
841 |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
842 uuid4 = (char *)Dowa_Arena_Allocate(arena, UUID_LEN); |
|
250
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
843 seed = |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
844 (uint32)time(NULL) ^ |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
845 (uint32)Seobeo_Thread_Current_Id() ^ |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
846 counter++; |
|
92
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
847 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
|
848 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
|
849 snprintf(output_path, TMP_FILE_LENGTH, "/tmp/%s.webp", uuid4); |
|
216
e82b80b24012
[MrJuneJune] Make webp translate background job.
June Park <parkjune1995@gmail.com>
parents:
206
diff
changeset
|
850 Seobeo_Log(SEOBEO_DEBUG, "output_path %s\n", output_path); |
|
e82b80b24012
[MrJuneJune] Make webp translate background job.
June Park <parkjune1995@gmail.com>
parents:
206
diff
changeset
|
851 Seobeo_Log(SEOBEO_DEBUG, "open_flags: 0x%x\n", open_flags); |
|
e82b80b24012
[MrJuneJune] Make webp translate background job.
June Park <parkjune1995@gmail.com>
parents:
206
diff
changeset
|
852 Seobeo_Log(SEOBEO_DEBUG, "input_path: %s\n", input_path); |
|
92
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
853 int output_fd = open(output_path, open_flags, 0600); |
|
216
e82b80b24012
[MrJuneJune] Make webp translate background job.
June Park <parkjune1995@gmail.com>
parents:
206
diff
changeset
|
854 Seobeo_Log(SEOBEO_DEBUG, "output_fd: %d\n", output_fd); |
|
92
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
855 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
|
856 { |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
857 unlink(input_path); |
|
216
e82b80b24012
[MrJuneJune] Make webp translate background job.
June Park <parkjune1995@gmail.com>
parents:
206
diff
changeset
|
858 Seobeo_Log(SEOBEO_DEBUG, "errno: %d (%s)\n", errno, strerror(errno)); |
|
92
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
859 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
|
860 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
|
861 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
|
862 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
|
863 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
|
864 } |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
865 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
|
866 |
|
216
e82b80b24012
[MrJuneJune] Make webp translate background job.
June Park <parkjune1995@gmail.com>
parents:
206
diff
changeset
|
867 File_Converter_Config *configuration = Dowa_Arena_Allocate(arena, sizeof(File_Converter_Config)); |
|
e82b80b24012
[MrJuneJune] Make webp translate background job.
June Park <parkjune1995@gmail.com>
parents:
206
diff
changeset
|
868 configuration->input_path = input_path; |
|
e82b80b24012
[MrJuneJune] Make webp translate background job.
June Park <parkjune1995@gmail.com>
parents:
206
diff
changeset
|
869 configuration->output_path = output_path; |
|
250
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
870 configuration->result = -1; |
|
216
e82b80b24012
[MrJuneJune] Make webp translate background job.
June Park <parkjune1995@gmail.com>
parents:
206
diff
changeset
|
871 |
|
250
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
872 Seobeo_Thread *p_worker = Seobeo_Thread_Start( |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
873 Simple_WebpConverter_Background, |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
874 configuration, |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
875 NULL); |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
876 if (!p_worker || |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
877 Seobeo_Thread_Join(p_worker) != SEOBEO_WORKER_OK || |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
878 configuration->result != 0) |
|
92
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
879 { |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
880 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
|
881 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
|
882 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
|
883 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
|
884 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
|
885 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
|
886 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
|
887 } |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
888 |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
889 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
|
890 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
|
891 { |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
892 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
|
893 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
|
894 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
|
895 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
|
896 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
|
897 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
|
898 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
|
899 } |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
900 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
|
901 |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
902 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
|
903 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
|
904 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
|
905 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
|
906 "{\"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
|
907 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
|
908 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
|
909 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
|
910 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
|
911 |
|
216
e82b80b24012
[MrJuneJune] Make webp translate background job.
June Park <parkjune1995@gmail.com>
parents:
206
diff
changeset
|
912 Seobeo_Log(SEOBEO_DEBUG, "Image converted, available at /api/download/%s\n", filename); |
|
92
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
913 |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
914 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
|
915 } |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
916 |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
917 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
|
918 { |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
919 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
|
920 |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
921 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
|
922 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
|
923 { |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
924 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
|
925 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
|
926 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
|
927 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
|
928 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
|
929 } |
|
72
4532ce6d9eb8
[Seobeo] Added router to the server logic. Few dowa string manipulation logics.
June Park <parkjune1995@gmail.com>
parents:
36
diff
changeset
|
930 |
|
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
|
931 // 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
|
932 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
|
933 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
|
934 { |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
935 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
|
936 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
|
937 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
|
938 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
|
939 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
|
940 } |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
941 |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
942 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
|
943 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
|
944 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
|
945 |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
946 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
|
947 |
|
168
f3084bca7317
[Misc] Fixed all errors and all tests should pass now.
MrJuneJune <me@mrjunejune.com>
parents:
158
diff
changeset
|
948 int open_flags = O_RDWR | O_CREAT | O_EXCL; |
|
f3084bca7317
[Misc] Fixed all errors and all tests should pass now.
MrJuneJune <me@mrjunejune.com>
parents:
158
diff
changeset
|
949 |
|
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
|
950 char *uuid4 = (char *)Dowa_Arena_Allocate(arena, UUID_LEN); |
|
250
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
951 uint32 seed = |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
952 (uint32)time(NULL) ^ |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
953 (uint32)Seobeo_Thread_Current_Id() ^ |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
954 counter++; |
|
168
f3084bca7317
[Misc] Fixed all errors and all tests should pass now.
MrJuneJune <me@mrjunejune.com>
parents:
158
diff
changeset
|
955 Dowa_String_UUID(seed, uuid4); |
|
92
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
956 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
|
957 snprintf(input_path, TMP_FILE_LENGTH, "/tmp/%s", uuid4); |
|
168
f3084bca7317
[Misc] Fixed all errors and all tests should pass now.
MrJuneJune <me@mrjunejune.com>
parents:
158
diff
changeset
|
958 Seobeo_Log(SEOBEO_DEBUG, "Input path: %s\n", input_path); |
|
f3084bca7317
[Misc] Fixed all errors and all tests should pass now.
MrJuneJune <me@mrjunejune.com>
parents:
158
diff
changeset
|
959 |
|
f3084bca7317
[Misc] Fixed all errors and all tests should pass now.
MrJuneJune <me@mrjunejune.com>
parents:
158
diff
changeset
|
960 int input_fd = open(input_path, open_flags, 0600); |
|
92
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
961 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
|
962 { |
|
168
f3084bca7317
[Misc] Fixed all errors and all tests should pass now.
MrJuneJune <me@mrjunejune.com>
parents:
158
diff
changeset
|
963 Seobeo_Log(SEOBEO_DEBUG, "errno: %d (%s)\n", errno, strerror(errno)); |
|
92
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
964 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
|
965 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
|
966 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
|
967 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
|
968 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
|
969 } |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
970 |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
971 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
|
972 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
|
973 |
|
250
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
974 seed = |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
975 (uint32)time(NULL) ^ |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
976 (uint32)Seobeo_Thread_Current_Id() ^ |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
977 counter++; |
|
92
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
978 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
|
979 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
|
980 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
|
981 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
|
982 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
|
983 { |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
984 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
|
985 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
|
986 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
|
987 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
|
988 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
|
989 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
|
990 } |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
991 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
|
992 |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
993 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
|
994 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
|
995 "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
|
996 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
|
997 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
|
998 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
|
999 { |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
1000 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
|
1001 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
|
1002 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
|
1003 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
|
1004 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
|
1005 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
|
1006 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
|
1007 } |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
1008 |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
1009 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
|
1010 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
|
1011 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
|
1012 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
|
1013 "{\"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
|
1014 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
|
1015 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
|
1016 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
|
1017 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
|
1018 |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
1019 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
|
1020 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
|
1021 } |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
1022 |
|
263
ee04e4e69fed
Add functional JRPG frame and tools
MrJuneJune <mrjunejune@users.noreply.github.com>
parents:
260
diff
changeset
|
1023 static boolean Converted_Filename_Is_Valid(const char *filename) |
|
ee04e4e69fed
Add functional JRPG frame and tools
MrJuneJune <mrjunejune@users.noreply.github.com>
parents:
260
diff
changeset
|
1024 { |
|
ee04e4e69fed
Add functional JRPG frame and tools
MrJuneJune <mrjunejune@users.noreply.github.com>
parents:
260
diff
changeset
|
1025 if (!filename) |
|
ee04e4e69fed
Add functional JRPG frame and tools
MrJuneJune <mrjunejune@users.noreply.github.com>
parents:
260
diff
changeset
|
1026 return FALSE; |
|
ee04e4e69fed
Add functional JRPG frame and tools
MrJuneJune <mrjunejune@users.noreply.github.com>
parents:
260
diff
changeset
|
1027 const char *extension = strrchr(filename, '.'); |
|
ee04e4e69fed
Add functional JRPG frame and tools
MrJuneJune <mrjunejune@users.noreply.github.com>
parents:
260
diff
changeset
|
1028 if (!extension || (strcmp(extension, ".webp") != 0 && |
|
ee04e4e69fed
Add functional JRPG frame and tools
MrJuneJune <mrjunejune@users.noreply.github.com>
parents:
260
diff
changeset
|
1029 strcmp(extension, ".mp4") != 0)) |
|
ee04e4e69fed
Add functional JRPG frame and tools
MrJuneJune <mrjunejune@users.noreply.github.com>
parents:
260
diff
changeset
|
1030 return FALSE; |
|
ee04e4e69fed
Add functional JRPG frame and tools
MrJuneJune <mrjunejune@users.noreply.github.com>
parents:
260
diff
changeset
|
1031 if ((size_t)(extension - filename) != 36) |
|
ee04e4e69fed
Add functional JRPG frame and tools
MrJuneJune <mrjunejune@users.noreply.github.com>
parents:
260
diff
changeset
|
1032 return FALSE; |
|
ee04e4e69fed
Add functional JRPG frame and tools
MrJuneJune <mrjunejune@users.noreply.github.com>
parents:
260
diff
changeset
|
1033 for (size_t i = 0; i < 36; i++) |
|
ee04e4e69fed
Add functional JRPG frame and tools
MrJuneJune <mrjunejune@users.noreply.github.com>
parents:
260
diff
changeset
|
1034 { |
|
ee04e4e69fed
Add functional JRPG frame and tools
MrJuneJune <mrjunejune@users.noreply.github.com>
parents:
260
diff
changeset
|
1035 char value = filename[i]; |
|
ee04e4e69fed
Add functional JRPG frame and tools
MrJuneJune <mrjunejune@users.noreply.github.com>
parents:
260
diff
changeset
|
1036 if (i == 8 || i == 13 || i == 18 || i == 23) |
|
ee04e4e69fed
Add functional JRPG frame and tools
MrJuneJune <mrjunejune@users.noreply.github.com>
parents:
260
diff
changeset
|
1037 { |
|
ee04e4e69fed
Add functional JRPG frame and tools
MrJuneJune <mrjunejune@users.noreply.github.com>
parents:
260
diff
changeset
|
1038 if (value != '-') |
|
ee04e4e69fed
Add functional JRPG frame and tools
MrJuneJune <mrjunejune@users.noreply.github.com>
parents:
260
diff
changeset
|
1039 return FALSE; |
|
ee04e4e69fed
Add functional JRPG frame and tools
MrJuneJune <mrjunejune@users.noreply.github.com>
parents:
260
diff
changeset
|
1040 continue; |
|
ee04e4e69fed
Add functional JRPG frame and tools
MrJuneJune <mrjunejune@users.noreply.github.com>
parents:
260
diff
changeset
|
1041 } |
|
ee04e4e69fed
Add functional JRPG frame and tools
MrJuneJune <mrjunejune@users.noreply.github.com>
parents:
260
diff
changeset
|
1042 if (!((value >= '0' && value <= '9') || |
|
ee04e4e69fed
Add functional JRPG frame and tools
MrJuneJune <mrjunejune@users.noreply.github.com>
parents:
260
diff
changeset
|
1043 (value >= 'a' && value <= 'f') || |
|
ee04e4e69fed
Add functional JRPG frame and tools
MrJuneJune <mrjunejune@users.noreply.github.com>
parents:
260
diff
changeset
|
1044 (value >= 'A' && value <= 'F'))) |
|
ee04e4e69fed
Add functional JRPG frame and tools
MrJuneJune <mrjunejune@users.noreply.github.com>
parents:
260
diff
changeset
|
1045 return FALSE; |
|
ee04e4e69fed
Add functional JRPG frame and tools
MrJuneJune <mrjunejune@users.noreply.github.com>
parents:
260
diff
changeset
|
1046 } |
|
ee04e4e69fed
Add functional JRPG frame and tools
MrJuneJune <mrjunejune@users.noreply.github.com>
parents:
260
diff
changeset
|
1047 return TRUE; |
|
ee04e4e69fed
Add functional JRPG frame and tools
MrJuneJune <mrjunejune@users.noreply.github.com>
parents:
260
diff
changeset
|
1048 } |
|
ee04e4e69fed
Add functional JRPG frame and tools
MrJuneJune <mrjunejune@users.noreply.github.com>
parents:
260
diff
changeset
|
1049 |
|
ee04e4e69fed
Add functional JRPG frame and tools
MrJuneJune <mrjunejune@users.noreply.github.com>
parents:
260
diff
changeset
|
1050 static Seobeo_Request_Entry *Converted_File_Error( |
|
ee04e4e69fed
Add functional JRPG frame and tools
MrJuneJune <mrjunejune@users.noreply.github.com>
parents:
260
diff
changeset
|
1051 Dowa_Arena *arena, |
|
ee04e4e69fed
Add functional JRPG frame and tools
MrJuneJune <mrjunejune@users.noreply.github.com>
parents:
260
diff
changeset
|
1052 char *status, |
|
ee04e4e69fed
Add functional JRPG frame and tools
MrJuneJune <mrjunejune@users.noreply.github.com>
parents:
260
diff
changeset
|
1053 char *message) |
|
ee04e4e69fed
Add functional JRPG frame and tools
MrJuneJune <mrjunejune@users.noreply.github.com>
parents:
260
diff
changeset
|
1054 { |
|
ee04e4e69fed
Add functional JRPG frame and tools
MrJuneJune <mrjunejune@users.noreply.github.com>
parents:
260
diff
changeset
|
1055 Seobeo_Request_Entry *resp = NULL; |
|
ee04e4e69fed
Add functional JRPG frame and tools
MrJuneJune <mrjunejune@users.noreply.github.com>
parents:
260
diff
changeset
|
1056 Dowa_HashMap_Push_Arena(resp, "status", status, arena); |
|
ee04e4e69fed
Add functional JRPG frame and tools
MrJuneJune <mrjunejune@users.noreply.github.com>
parents:
260
diff
changeset
|
1057 Dowa_HashMap_Push_Arena(resp, "content-type", "text/plain", arena); |
|
ee04e4e69fed
Add functional JRPG frame and tools
MrJuneJune <mrjunejune@users.noreply.github.com>
parents:
260
diff
changeset
|
1058 Dowa_HashMap_Push_Arena(resp, "body", message, arena); |
|
ee04e4e69fed
Add functional JRPG frame and tools
MrJuneJune <mrjunejune@users.noreply.github.com>
parents:
260
diff
changeset
|
1059 return resp; |
|
ee04e4e69fed
Add functional JRPG frame and tools
MrJuneJune <mrjunejune@users.noreply.github.com>
parents:
260
diff
changeset
|
1060 } |
|
ee04e4e69fed
Add functional JRPG frame and tools
MrJuneJune <mrjunejune@users.noreply.github.com>
parents:
260
diff
changeset
|
1061 |
|
ee04e4e69fed
Add functional JRPG frame and tools
MrJuneJune <mrjunejune@users.noreply.github.com>
parents:
260
diff
changeset
|
1062 Seobeo_Request_Entry *DeleteConvertedFile( |
|
ee04e4e69fed
Add functional JRPG frame and tools
MrJuneJune <mrjunejune@users.noreply.github.com>
parents:
260
diff
changeset
|
1063 Seobeo_Request_Entry *req, |
|
ee04e4e69fed
Add functional JRPG frame and tools
MrJuneJune <mrjunejune@users.noreply.github.com>
parents:
260
diff
changeset
|
1064 Dowa_Arena *arena) |
|
ee04e4e69fed
Add functional JRPG frame and tools
MrJuneJune <mrjunejune@users.noreply.github.com>
parents:
260
diff
changeset
|
1065 { |
|
ee04e4e69fed
Add functional JRPG frame and tools
MrJuneJune <mrjunejune@users.noreply.github.com>
parents:
260
diff
changeset
|
1066 void *filename_kv = Dowa_HashMap_Get_Ptr(req, ":filename"); |
|
ee04e4e69fed
Add functional JRPG frame and tools
MrJuneJune <mrjunejune@users.noreply.github.com>
parents:
260
diff
changeset
|
1067 const char *filename = filename_kv |
|
ee04e4e69fed
Add functional JRPG frame and tools
MrJuneJune <mrjunejune@users.noreply.github.com>
parents:
260
diff
changeset
|
1068 ? ((Seobeo_Request_Entry *)filename_kv)->value |
|
ee04e4e69fed
Add functional JRPG frame and tools
MrJuneJune <mrjunejune@users.noreply.github.com>
parents:
260
diff
changeset
|
1069 : NULL; |
|
ee04e4e69fed
Add functional JRPG frame and tools
MrJuneJune <mrjunejune@users.noreply.github.com>
parents:
260
diff
changeset
|
1070 if (!Converted_Filename_Is_Valid(filename)) |
|
ee04e4e69fed
Add functional JRPG frame and tools
MrJuneJune <mrjunejune@users.noreply.github.com>
parents:
260
diff
changeset
|
1071 return Converted_File_Error(arena, "400", "Invalid converted filename"); |
|
ee04e4e69fed
Add functional JRPG frame and tools
MrJuneJune <mrjunejune@users.noreply.github.com>
parents:
260
diff
changeset
|
1072 |
|
ee04e4e69fed
Add functional JRPG frame and tools
MrJuneJune <mrjunejune@users.noreply.github.com>
parents:
260
diff
changeset
|
1073 char filepath[512]; |
|
ee04e4e69fed
Add functional JRPG frame and tools
MrJuneJune <mrjunejune@users.noreply.github.com>
parents:
260
diff
changeset
|
1074 snprintf(filepath, sizeof(filepath), "/tmp/%s", filename); |
|
ee04e4e69fed
Add functional JRPG frame and tools
MrJuneJune <mrjunejune@users.noreply.github.com>
parents:
260
diff
changeset
|
1075 if (unlink(filepath) != 0 && errno != ENOENT) |
|
ee04e4e69fed
Add functional JRPG frame and tools
MrJuneJune <mrjunejune@users.noreply.github.com>
parents:
260
diff
changeset
|
1076 return Converted_File_Error(arena, "500", "Unable to delete converted file"); |
|
ee04e4e69fed
Add functional JRPG frame and tools
MrJuneJune <mrjunejune@users.noreply.github.com>
parents:
260
diff
changeset
|
1077 |
|
ee04e4e69fed
Add functional JRPG frame and tools
MrJuneJune <mrjunejune@users.noreply.github.com>
parents:
260
diff
changeset
|
1078 Seobeo_Request_Entry *resp = NULL; |
|
ee04e4e69fed
Add functional JRPG frame and tools
MrJuneJune <mrjunejune@users.noreply.github.com>
parents:
260
diff
changeset
|
1079 Dowa_HashMap_Push_Arena(resp, "status", "204", arena); |
|
ee04e4e69fed
Add functional JRPG frame and tools
MrJuneJune <mrjunejune@users.noreply.github.com>
parents:
260
diff
changeset
|
1080 Dowa_HashMap_Push_Arena(resp, "body", "", arena); |
|
ee04e4e69fed
Add functional JRPG frame and tools
MrJuneJune <mrjunejune@users.noreply.github.com>
parents:
260
diff
changeset
|
1081 return resp; |
|
ee04e4e69fed
Add functional JRPG frame and tools
MrJuneJune <mrjunejune@users.noreply.github.com>
parents:
260
diff
changeset
|
1082 } |
|
ee04e4e69fed
Add functional JRPG frame and tools
MrJuneJune <mrjunejune@users.noreply.github.com>
parents:
260
diff
changeset
|
1083 |
|
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
|
1084 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
|
1085 { |
|
72
4532ce6d9eb8
[Seobeo] Added router to the server logic. Few dowa string manipulation logics.
June Park <parkjune1995@gmail.com>
parents:
36
diff
changeset
|
1086 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
|
1087 |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
1088 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
|
1089 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
|
1090 { |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
1091 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
|
1092 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
|
1093 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
|
1094 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
|
1095 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
|
1096 } |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
1097 |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
1098 const char *filename = ((Seobeo_Request_Entry*)filename_kv)->value; |
|
263
ee04e4e69fed
Add functional JRPG frame and tools
MrJuneJune <mrjunejune@users.noreply.github.com>
parents:
260
diff
changeset
|
1099 if (!Converted_Filename_Is_Valid(filename)) |
|
ee04e4e69fed
Add functional JRPG frame and tools
MrJuneJune <mrjunejune@users.noreply.github.com>
parents:
260
diff
changeset
|
1100 return Converted_File_Error(arena, "400", "Invalid converted filename"); |
|
92
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
1101 |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
1102 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
|
1103 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
|
1104 |
|
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
|
1105 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
|
1106 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
|
1107 { |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
1108 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
|
1109 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
|
1110 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
|
1111 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
|
1112 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
|
1113 } |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
1114 |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
1115 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
|
1116 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
|
1117 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
|
1118 |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
1119 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
|
1120 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
|
1121 { |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
1122 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
|
1123 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
|
1124 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
|
1125 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
|
1126 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
|
1127 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
|
1128 } |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
1129 |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
1130 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
|
1131 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
|
1132 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
|
1133 |
|
263
ee04e4e69fed
Add functional JRPG frame and tools
MrJuneJune <mrjunejune@users.noreply.github.com>
parents:
260
diff
changeset
|
1134 char *content_type = "application/octet-stream"; |
|
ee04e4e69fed
Add functional JRPG frame and tools
MrJuneJune <mrjunejune@users.noreply.github.com>
parents:
260
diff
changeset
|
1135 if (strcmp(strrchr(filename, '.'), ".webp") == 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
|
1136 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
|
1137 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
|
1138 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
|
1139 |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
1140 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
|
1141 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
|
1142 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
|
1143 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
|
1144 |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
1145 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
|
1146 |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
1147 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
|
1148 |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
1149 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
|
1150 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
|
1151 |
|
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
1152 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
|
1153 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
|
1154 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
|
1155 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
|
1156 |
|
72
4532ce6d9eb8
[Seobeo] Added router to the server logic. Few dowa string manipulation logics.
June Park <parkjune1995@gmail.com>
parents:
36
diff
changeset
|
1157 return resp; |
|
4532ce6d9eb8
[Seobeo] Added router to the server logic. Few dowa string manipulation logics.
June Park <parkjune1995@gmail.com>
parents:
36
diff
changeset
|
1158 } |
|
4532ce6d9eb8
[Seobeo] Added router to the server logic. Few dowa string manipulation logics.
June Park <parkjune1995@gmail.com>
parents:
36
diff
changeset
|
1159 |
|
100
65e5a5b89a4e
[Seobeo] Migrated everything to this page.
June Park <parkjune1995@gmail.com>
parents:
96
diff
changeset
|
1160 Seobeo_Request_Entry *RenderBlogList(Seobeo_Request_Entry *req, Dowa_Arena *arena) |
|
65e5a5b89a4e
[Seobeo] Migrated everything to this page.
June Park <parkjune1995@gmail.com>
parents:
96
diff
changeset
|
1161 { |
|
65e5a5b89a4e
[Seobeo] Migrated everything to this page.
June Park <parkjune1995@gmail.com>
parents:
96
diff
changeset
|
1162 Seobeo_Request_Entry *resp = NULL; |
|
264
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
1163 char *final_body = Dowa_Arena_Allocate(arena, HTML_PAGE_CAP); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
1164 if (!final_body || !Mjj_Template_Render_File(final_body, HTML_PAGE_CAP, "/blog/index.html", arena)) |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
1165 return html_render_error(arena, "Internal Server Error"); |
|
100
65e5a5b89a4e
[Seobeo] Migrated everything to this page.
June Park <parkjune1995@gmail.com>
parents:
96
diff
changeset
|
1166 Dowa_HashMap_Push_Arena(resp, "body", final_body, arena); |
|
65e5a5b89a4e
[Seobeo] Migrated everything to this page.
June Park <parkjune1995@gmail.com>
parents:
96
diff
changeset
|
1167 return resp; |
|
65e5a5b89a4e
[Seobeo] Migrated everything to this page.
June Park <parkjune1995@gmail.com>
parents:
96
diff
changeset
|
1168 } |
|
65e5a5b89a4e
[Seobeo] Migrated everything to this page.
June Park <parkjune1995@gmail.com>
parents:
96
diff
changeset
|
1169 |
|
65e5a5b89a4e
[Seobeo] Migrated everything to this page.
June Park <parkjune1995@gmail.com>
parents:
96
diff
changeset
|
1170 |
|
65e5a5b89a4e
[Seobeo] Migrated everything to this page.
June Park <parkjune1995@gmail.com>
parents:
96
diff
changeset
|
1171 Seobeo_Request_Entry *RenderBlog(Seobeo_Request_Entry *req, Dowa_Arena *arena) |
|
65e5a5b89a4e
[Seobeo] Migrated everything to this page.
June Park <parkjune1995@gmail.com>
parents:
96
diff
changeset
|
1172 { |
|
65e5a5b89a4e
[Seobeo] Migrated everything to this page.
June Park <parkjune1995@gmail.com>
parents:
96
diff
changeset
|
1173 Seobeo_Request_Entry *resp = NULL; |
|
65e5a5b89a4e
[Seobeo] Migrated everything to this page.
June Park <parkjune1995@gmail.com>
parents:
96
diff
changeset
|
1174 |
|
169
295ac2e5ec00
[MrJuneJune] Created separate target for generating html from md.
MrJuneJune <me@mrjunejune.com>
parents:
168
diff
changeset
|
1175 char *file_path = Dowa_Arena_Allocate(arena, 1024); |
|
100
65e5a5b89a4e
[Seobeo] Migrated everything to this page.
June Park <parkjune1995@gmail.com>
parents:
96
diff
changeset
|
1176 void *blog_id_kv = Dowa_HashMap_Get_Ptr(req, ":blog_id"); |
|
169
295ac2e5ec00
[MrJuneJune] Created separate target for generating html from md.
MrJuneJune <me@mrjunejune.com>
parents:
168
diff
changeset
|
1177 char *blog_id = ((Seobeo_Request_Entry*)blog_id_kv)->value; |
|
295ac2e5ec00
[MrJuneJune] Created separate target for generating html from md.
MrJuneJune <me@mrjunejune.com>
parents:
168
diff
changeset
|
1178 snprintf(file_path, 1024, "/blog/%s/index.html", blog_id); |
|
100
65e5a5b89a4e
[Seobeo] Migrated everything to this page.
June Park <parkjune1995@gmail.com>
parents:
96
diff
changeset
|
1179 |
|
264
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
1180 char *final_body = Dowa_Arena_Allocate(arena, HTML_PAGE_CAP); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
1181 if (!final_body || !Mjj_Template_Render_File(final_body, HTML_PAGE_CAP, file_path, arena)) |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
1182 { |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
1183 Seobeo_Request_Entry *err = NULL; |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
1184 Dowa_HashMap_Push_Arena(err, "status", "404", arena); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
1185 Dowa_HashMap_Push_Arena(err, "content-type", "text/plain; charset=utf-8", arena); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
1186 Dowa_HashMap_Push_Arena(err, "body", "Not found", arena); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
1187 return err; |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
1188 } |
|
100
65e5a5b89a4e
[Seobeo] Migrated everything to this page.
June Park <parkjune1995@gmail.com>
parents:
96
diff
changeset
|
1189 Dowa_HashMap_Push_Arena(resp, "body", final_body, arena); |
|
65e5a5b89a4e
[Seobeo] Migrated everything to this page.
June Park <parkjune1995@gmail.com>
parents:
96
diff
changeset
|
1190 return resp; |
|
65e5a5b89a4e
[Seobeo] Migrated everything to this page.
June Park <parkjune1995@gmail.com>
parents:
96
diff
changeset
|
1191 } |
|
65e5a5b89a4e
[Seobeo] Migrated everything to this page.
June Park <parkjune1995@gmail.com>
parents:
96
diff
changeset
|
1192 |
|
125
f236c895604e
[MrJuneJune] Added web socket for chat to this.
June Park <parkjune1995@gmail.com>
parents:
124
diff
changeset
|
1193 void Chat_Handler(Seobeo_WebSocket_Server_Connection *p_conn, Seobeo_WebSocket_Message *p_msg, void *p_user_data) |
|
f236c895604e
[MrJuneJune] Added web socket for chat to this.
June Park <parkjune1995@gmail.com>
parents:
124
diff
changeset
|
1194 { |
|
f236c895604e
[MrJuneJune] Added web socket for chat to this.
June Park <parkjune1995@gmail.com>
parents:
124
diff
changeset
|
1195 (void)p_user_data; |
|
f236c895604e
[MrJuneJune] Added web socket for chat to this.
June Park <parkjune1995@gmail.com>
parents:
124
diff
changeset
|
1196 |
|
f236c895604e
[MrJuneJune] Added web socket for chat to this.
June Park <parkjune1995@gmail.com>
parents:
124
diff
changeset
|
1197 if (p_msg->opcode == SEOBEO_WS_OPCODE_TEXT) |
|
f236c895604e
[MrJuneJune] Added web socket for chat to this.
June Park <parkjune1995@gmail.com>
parents:
124
diff
changeset
|
1198 { |
|
f236c895604e
[MrJuneJune] Added web socket for chat to this.
June Park <parkjune1995@gmail.com>
parents:
124
diff
changeset
|
1199 char message[2048]; |
|
f236c895604e
[MrJuneJune] Added web socket for chat to this.
June Park <parkjune1995@gmail.com>
parents:
124
diff
changeset
|
1200 snprintf(message, sizeof(message), "[%s]: %.*s", p_conn->client_id, (int)p_msg->length, (char*)p_msg->data); |
|
f236c895604e
[MrJuneJune] Added web socket for chat to this.
June Park <parkjune1995@gmail.com>
parents:
124
diff
changeset
|
1201 |
|
f236c895604e
[MrJuneJune] Added web socket for chat to this.
June Park <parkjune1995@gmail.com>
parents:
124
diff
changeset
|
1202 Seobeo_Log(SEOBEO_INFO, "[Chat] Broadcasting: %s\n", message); |
|
f236c895604e
[MrJuneJune] Added web socket for chat to this.
June Park <parkjune1995@gmail.com>
parents:
124
diff
changeset
|
1203 Seobeo_WebSocket_Server_Broadcast_Text(message, p_conn); |
|
f236c895604e
[MrJuneJune] Added web socket for chat to this.
June Park <parkjune1995@gmail.com>
parents:
124
diff
changeset
|
1204 } |
|
f236c895604e
[MrJuneJune] Added web socket for chat to this.
June Park <parkjune1995@gmail.com>
parents:
124
diff
changeset
|
1205 } |
|
f236c895604e
[MrJuneJune] Added web socket for chat to this.
June Park <parkjune1995@gmail.com>
parents:
124
diff
changeset
|
1206 |
|
f236c895604e
[MrJuneJune] Added web socket for chat to this.
June Park <parkjune1995@gmail.com>
parents:
124
diff
changeset
|
1207 Seobeo_Request_Entry *GetTalk(Seobeo_Request_Entry *req, Dowa_Arena *arena) |
|
f236c895604e
[MrJuneJune] Added web socket for chat to this.
June Park <parkjune1995@gmail.com>
parents:
124
diff
changeset
|
1208 { |
|
f236c895604e
[MrJuneJune] Added web socket for chat to this.
June Park <parkjune1995@gmail.com>
parents:
124
diff
changeset
|
1209 Seobeo_Request_Entry *resp = NULL; |
|
264
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
1210 char *final_body = Dowa_Arena_Allocate(arena, HTML_PAGE_CAP); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
1211 if (!final_body || !Mjj_Template_Render_File(final_body, HTML_PAGE_CAP, "/talk/index.html", arena)) |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
1212 return html_render_error(arena, "Internal Server Error"); |
|
125
f236c895604e
[MrJuneJune] Added web socket for chat to this.
June Park <parkjune1995@gmail.com>
parents:
124
diff
changeset
|
1213 Dowa_HashMap_Push_Arena(resp, "body", final_body, arena); |
|
f236c895604e
[MrJuneJune] Added web socket for chat to this.
June Park <parkjune1995@gmail.com>
parents:
124
diff
changeset
|
1214 return resp; |
|
f236c895604e
[MrJuneJune] Added web socket for chat to this.
June Park <parkjune1995@gmail.com>
parents:
124
diff
changeset
|
1215 } |
|
100
65e5a5b89a4e
[Seobeo] Migrated everything to this page.
June Park <parkjune1995@gmail.com>
parents:
96
diff
changeset
|
1216 |
|
273
e02e2036ef84
add Layer 2 JRPG component system
MrJuneJune <me@mrjunejune.com>
parents:
265
diff
changeset
|
1217 typedef struct { |
|
e02e2036ef84
add Layer 2 JRPG component system
MrJuneJune <me@mrjunejune.com>
parents:
265
diff
changeset
|
1218 const char *name; |
|
e02e2036ef84
add Layer 2 JRPG component system
MrJuneJune <me@mrjunejune.com>
parents:
265
diff
changeset
|
1219 const char *title; |
|
e02e2036ef84
add Layer 2 JRPG component system
MrJuneJune <me@mrjunejune.com>
parents:
265
diff
changeset
|
1220 const char *copy; |
|
e02e2036ef84
add Layer 2 JRPG component system
MrJuneJune <me@mrjunejune.com>
parents:
265
diff
changeset
|
1221 const char *works; |
|
e02e2036ef84
add Layer 2 JRPG component system
MrJuneJune <me@mrjunejune.com>
parents:
265
diff
changeset
|
1222 } Jrpg_Initial_Panel; |
|
e02e2036ef84
add Layer 2 JRPG component system
MrJuneJune <me@mrjunejune.com>
parents:
265
diff
changeset
|
1223 |
|
e02e2036ef84
add Layer 2 JRPG component system
MrJuneJune <me@mrjunejune.com>
parents:
265
diff
changeset
|
1224 static boolean Mjj_Replace_All( |
|
e02e2036ef84
add Layer 2 JRPG component system
MrJuneJune <me@mrjunejune.com>
parents:
265
diff
changeset
|
1225 char *buffer, |
|
e02e2036ef84
add Layer 2 JRPG component system
MrJuneJune <me@mrjunejune.com>
parents:
265
diff
changeset
|
1226 size_t capacity, |
|
e02e2036ef84
add Layer 2 JRPG component system
MrJuneJune <me@mrjunejune.com>
parents:
265
diff
changeset
|
1227 const char *needle, |
|
e02e2036ef84
add Layer 2 JRPG component system
MrJuneJune <me@mrjunejune.com>
parents:
265
diff
changeset
|
1228 const char *replacement) |
|
e02e2036ef84
add Layer 2 JRPG component system
MrJuneJune <me@mrjunejune.com>
parents:
265
diff
changeset
|
1229 { |
|
e02e2036ef84
add Layer 2 JRPG component system
MrJuneJune <me@mrjunejune.com>
parents:
265
diff
changeset
|
1230 if (!buffer || !needle || !replacement || needle[0] == '\0') |
|
e02e2036ef84
add Layer 2 JRPG component system
MrJuneJune <me@mrjunejune.com>
parents:
265
diff
changeset
|
1231 return FALSE; |
|
e02e2036ef84
add Layer 2 JRPG component system
MrJuneJune <me@mrjunejune.com>
parents:
265
diff
changeset
|
1232 |
|
e02e2036ef84
add Layer 2 JRPG component system
MrJuneJune <me@mrjunejune.com>
parents:
265
diff
changeset
|
1233 size_t needle_length = strlen(needle); |
|
e02e2036ef84
add Layer 2 JRPG component system
MrJuneJune <me@mrjunejune.com>
parents:
265
diff
changeset
|
1234 size_t replacement_length = strlen(replacement); |
|
e02e2036ef84
add Layer 2 JRPG component system
MrJuneJune <me@mrjunejune.com>
parents:
265
diff
changeset
|
1235 char *match = strstr(buffer, needle); |
|
e02e2036ef84
add Layer 2 JRPG component system
MrJuneJune <me@mrjunejune.com>
parents:
265
diff
changeset
|
1236 while (match) |
|
e02e2036ef84
add Layer 2 JRPG component system
MrJuneJune <me@mrjunejune.com>
parents:
265
diff
changeset
|
1237 { |
|
e02e2036ef84
add Layer 2 JRPG component system
MrJuneJune <me@mrjunejune.com>
parents:
265
diff
changeset
|
1238 size_t current_length = strlen(buffer); |
|
e02e2036ef84
add Layer 2 JRPG component system
MrJuneJune <me@mrjunejune.com>
parents:
265
diff
changeset
|
1239 size_t offset = (size_t)(match - buffer); |
|
e02e2036ef84
add Layer 2 JRPG component system
MrJuneJune <me@mrjunejune.com>
parents:
265
diff
changeset
|
1240 size_t new_length = |
|
e02e2036ef84
add Layer 2 JRPG component system
MrJuneJune <me@mrjunejune.com>
parents:
265
diff
changeset
|
1241 current_length - needle_length + replacement_length; |
|
e02e2036ef84
add Layer 2 JRPG component system
MrJuneJune <me@mrjunejune.com>
parents:
265
diff
changeset
|
1242 if (new_length >= capacity) |
|
e02e2036ef84
add Layer 2 JRPG component system
MrJuneJune <me@mrjunejune.com>
parents:
265
diff
changeset
|
1243 return FALSE; |
|
e02e2036ef84
add Layer 2 JRPG component system
MrJuneJune <me@mrjunejune.com>
parents:
265
diff
changeset
|
1244 |
|
e02e2036ef84
add Layer 2 JRPG component system
MrJuneJune <me@mrjunejune.com>
parents:
265
diff
changeset
|
1245 memmove( |
|
e02e2036ef84
add Layer 2 JRPG component system
MrJuneJune <me@mrjunejune.com>
parents:
265
diff
changeset
|
1246 match + replacement_length, |
|
e02e2036ef84
add Layer 2 JRPG component system
MrJuneJune <me@mrjunejune.com>
parents:
265
diff
changeset
|
1247 match + needle_length, |
|
e02e2036ef84
add Layer 2 JRPG component system
MrJuneJune <me@mrjunejune.com>
parents:
265
diff
changeset
|
1248 current_length - offset - needle_length + 1); |
|
e02e2036ef84
add Layer 2 JRPG component system
MrJuneJune <me@mrjunejune.com>
parents:
265
diff
changeset
|
1249 memcpy(match, replacement, replacement_length); |
|
e02e2036ef84
add Layer 2 JRPG component system
MrJuneJune <me@mrjunejune.com>
parents:
265
diff
changeset
|
1250 match = strstr(match + replacement_length, needle); |
|
e02e2036ef84
add Layer 2 JRPG component system
MrJuneJune <me@mrjunejune.com>
parents:
265
diff
changeset
|
1251 } |
|
e02e2036ef84
add Layer 2 JRPG component system
MrJuneJune <me@mrjunejune.com>
parents:
265
diff
changeset
|
1252 return TRUE; |
|
e02e2036ef84
add Layer 2 JRPG component system
MrJuneJune <me@mrjunejune.com>
parents:
265
diff
changeset
|
1253 } |
|
e02e2036ef84
add Layer 2 JRPG component system
MrJuneJune <me@mrjunejune.com>
parents:
265
diff
changeset
|
1254 |
|
e02e2036ef84
add Layer 2 JRPG component system
MrJuneJune <me@mrjunejune.com>
parents:
265
diff
changeset
|
1255 static const Jrpg_Initial_Panel *Jrpg_Resolve_Initial_Panel( |
|
e02e2036ef84
add Layer 2 JRPG component system
MrJuneJune <me@mrjunejune.com>
parents:
265
diff
changeset
|
1256 Seobeo_Request_Entry *req) |
|
e02e2036ef84
add Layer 2 JRPG component system
MrJuneJune <me@mrjunejune.com>
parents:
265
diff
changeset
|
1257 { |
|
e02e2036ef84
add Layer 2 JRPG component system
MrJuneJune <me@mrjunejune.com>
parents:
265
diff
changeset
|
1258 static const Jrpg_Initial_Panel panels[] = { |
|
e02e2036ef84
add Layer 2 JRPG component system
MrJuneJune <me@mrjunejune.com>
parents:
265
diff
changeset
|
1259 { |
|
e02e2036ef84
add Layer 2 JRPG component system
MrJuneJune <me@mrjunejune.com>
parents:
265
diff
changeset
|
1260 "resume", |
|
e02e2036ef84
add Layer 2 JRPG component system
MrJuneJune <me@mrjunejune.com>
parents:
265
diff
changeset
|
1261 "Resume", |
|
e02e2036ef84
add Layer 2 JRPG component system
MrJuneJune <me@mrjunejune.com>
parents:
265
diff
changeset
|
1262 "Member of Technical Staff and engineering leader with 10+ years " |
|
e02e2036ef84
add Layer 2 JRPG component system
MrJuneJune <me@mrjunejune.com>
parents:
265
diff
changeset
|
1263 "building AI agent platforms and production systems across Microsoft, " |
|
e02e2036ef84
add Layer 2 JRPG component system
MrJuneJune <me@mrjunejune.com>
parents:
265
diff
changeset
|
1264 "Meta, Google, and growth-stage companies.", |
|
e02e2036ef84
add Layer 2 JRPG component system
MrJuneJune <me@mrjunejune.com>
parents:
265
diff
changeset
|
1265 "<li><a href=\"/resume\" data-resume-modal>" |
|
e02e2036ef84
add Layer 2 JRPG component system
MrJuneJune <me@mrjunejune.com>
parents:
265
diff
changeset
|
1266 "<span>Full resume</span><small>Career dossier</small></a></li>" |
|
e02e2036ef84
add Layer 2 JRPG component system
MrJuneJune <me@mrjunejune.com>
parents:
265
diff
changeset
|
1267 "<li><a href=\"https://www.microsoft.com/en-us/microsoft-copilot/blog/" |
|
e02e2036ef84
add Layer 2 JRPG component system
MrJuneJune <me@mrjunejune.com>
parents:
265
diff
changeset
|
1268 "2026/02/26/copilot-tasks-from-answers-to-actions/\">" |
|
e02e2036ef84
add Layer 2 JRPG component system
MrJuneJune <me@mrjunejune.com>
parents:
265
diff
changeset
|
1269 "<span>Copilot Tasks</span><small>Agentic execution</small></a></li>" |
|
e02e2036ef84
add Layer 2 JRPG component system
MrJuneJune <me@mrjunejune.com>
parents:
265
diff
changeset
|
1270 }, |
|
e02e2036ef84
add Layer 2 JRPG component system
MrJuneJune <me@mrjunejune.com>
parents:
265
diff
changeset
|
1271 { |
|
e02e2036ef84
add Layer 2 JRPG component system
MrJuneJune <me@mrjunejune.com>
parents:
265
diff
changeset
|
1272 "tools", |
|
e02e2036ef84
add Layer 2 JRPG component system
MrJuneJune <me@mrjunejune.com>
parents:
265
diff
changeset
|
1273 "Tools", |
|
e02e2036ef84
add Layer 2 JRPG component system
MrJuneJune <me@mrjunejune.com>
parents:
265
diff
changeset
|
1274 "Useful browser tools backed by first-party C, WASM, media, and " |
|
e02e2036ef84
add Layer 2 JRPG component system
MrJuneJune <me@mrjunejune.com>
parents:
265
diff
changeset
|
1275 "document-processing systems.", |
|
e02e2036ef84
add Layer 2 JRPG component system
MrJuneJune <me@mrjunejune.com>
parents:
265
diff
changeset
|
1276 "<li><a href=\"/tools/markdown_to_html\" data-latest-tool " |
|
e02e2036ef84
add Layer 2 JRPG component system
MrJuneJune <me@mrjunejune.com>
parents:
265
diff
changeset
|
1277 "data-tool-url=\"/tools/markdown_to_html\">" |
|
e02e2036ef84
add Layer 2 JRPG component system
MrJuneJune <me@mrjunejune.com>
parents:
265
diff
changeset
|
1278 "<span>Markdown</span><small>Writing</small></a></li>" |
|
e02e2036ef84
add Layer 2 JRPG component system
MrJuneJune <me@mrjunejune.com>
parents:
265
diff
changeset
|
1279 "<li><a href=\"/tools/file_converter\" data-latest-tool " |
|
e02e2036ef84
add Layer 2 JRPG component system
MrJuneJune <me@mrjunejune.com>
parents:
265
diff
changeset
|
1280 "data-tool-url=\"/tools/file_converter\">" |
|
e02e2036ef84
add Layer 2 JRPG component system
MrJuneJune <me@mrjunejune.com>
parents:
265
diff
changeset
|
1281 "<span>Converter</span><small>Media</small></a></li>" |
|
e02e2036ef84
add Layer 2 JRPG component system
MrJuneJune <me@mrjunejune.com>
parents:
265
diff
changeset
|
1282 "<li><a href=\"/tools/hls_player\" data-latest-tool " |
|
e02e2036ef84
add Layer 2 JRPG component system
MrJuneJune <me@mrjunejune.com>
parents:
265
diff
changeset
|
1283 "data-tool-url=\"/tools/hls_player\">" |
|
e02e2036ef84
add Layer 2 JRPG component system
MrJuneJune <me@mrjunejune.com>
parents:
265
diff
changeset
|
1284 "<span>HLS Player</span><small>Streaming</small></a></li>" |
|
e02e2036ef84
add Layer 2 JRPG component system
MrJuneJune <me@mrjunejune.com>
parents:
265
diff
changeset
|
1285 }, |
|
e02e2036ef84
add Layer 2 JRPG component system
MrJuneJune <me@mrjunejune.com>
parents:
265
diff
changeset
|
1286 { |
|
e02e2036ef84
add Layer 2 JRPG component system
MrJuneJune <me@mrjunejune.com>
parents:
265
diff
changeset
|
1287 "blog", |
|
e02e2036ef84
add Layer 2 JRPG component system
MrJuneJune <me@mrjunejune.com>
parents:
265
diff
changeset
|
1288 "Blogs", |
|
e02e2036ef84
add Layer 2 JRPG component system
MrJuneJune <me@mrjunejune.com>
parents:
265
diff
changeset
|
1289 "Technical writing about networking, rendering, performance, developer " |
|
e02e2036ef84
add Layer 2 JRPG component system
MrJuneJune <me@mrjunejune.com>
parents:
265
diff
changeset
|
1290 "tooling, and experiments.", |
|
e02e2036ef84
add Layer 2 JRPG component system
MrJuneJune <me@mrjunejune.com>
parents:
265
diff
changeset
|
1291 "<li><a href=\"/blog\" data-blog-archive>" |
|
e02e2036ef84
add Layer 2 JRPG component system
MrJuneJune <me@mrjunejune.com>
parents:
265
diff
changeset
|
1292 "<span>All posts</span><small>Archive</small></a></li>" |
|
e02e2036ef84
add Layer 2 JRPG component system
MrJuneJune <me@mrjunejune.com>
parents:
265
diff
changeset
|
1293 }, |
|
e02e2036ef84
add Layer 2 JRPG component system
MrJuneJune <me@mrjunejune.com>
parents:
265
diff
changeset
|
1294 { |
|
e02e2036ef84
add Layer 2 JRPG component system
MrJuneJune <me@mrjunejune.com>
parents:
265
diff
changeset
|
1295 "conversations", |
|
e02e2036ef84
add Layer 2 JRPG component system
MrJuneJune <me@mrjunejune.com>
parents:
265
diff
changeset
|
1296 "Resume", |
|
e02e2036ef84
add Layer 2 JRPG component system
MrJuneJune <me@mrjunejune.com>
parents:
265
diff
changeset
|
1297 "", |
|
e02e2036ef84
add Layer 2 JRPG component system
MrJuneJune <me@mrjunejune.com>
parents:
265
diff
changeset
|
1298 "" |
|
e02e2036ef84
add Layer 2 JRPG component system
MrJuneJune <me@mrjunejune.com>
parents:
265
diff
changeset
|
1299 }, |
|
e02e2036ef84
add Layer 2 JRPG component system
MrJuneJune <me@mrjunejune.com>
parents:
265
diff
changeset
|
1300 }; |
|
e02e2036ef84
add Layer 2 JRPG component system
MrJuneJune <me@mrjunejune.com>
parents:
265
diff
changeset
|
1301 |
|
e02e2036ef84
add Layer 2 JRPG component system
MrJuneJune <me@mrjunejune.com>
parents:
265
diff
changeset
|
1302 const char *requested = NULL; |
|
e02e2036ef84
add Layer 2 JRPG component system
MrJuneJune <me@mrjunejune.com>
parents:
265
diff
changeset
|
1303 void *panel_kv = Dowa_HashMap_Get_Ptr(req, "query_panel"); |
|
e02e2036ef84
add Layer 2 JRPG component system
MrJuneJune <me@mrjunejune.com>
parents:
265
diff
changeset
|
1304 if (panel_kv) |
|
e02e2036ef84
add Layer 2 JRPG component system
MrJuneJune <me@mrjunejune.com>
parents:
265
diff
changeset
|
1305 requested = ((Seobeo_Request_Entry *)panel_kv)->value; |
|
e02e2036ef84
add Layer 2 JRPG component system
MrJuneJune <me@mrjunejune.com>
parents:
265
diff
changeset
|
1306 |
|
e02e2036ef84
add Layer 2 JRPG component system
MrJuneJune <me@mrjunejune.com>
parents:
265
diff
changeset
|
1307 for (size_t i = 0; i < sizeof(panels) / sizeof(panels[0]); ++i) |
|
e02e2036ef84
add Layer 2 JRPG component system
MrJuneJune <me@mrjunejune.com>
parents:
265
diff
changeset
|
1308 { |
|
e02e2036ef84
add Layer 2 JRPG component system
MrJuneJune <me@mrjunejune.com>
parents:
265
diff
changeset
|
1309 if (requested && strcmp(requested, panels[i].name) == 0) |
|
e02e2036ef84
add Layer 2 JRPG component system
MrJuneJune <me@mrjunejune.com>
parents:
265
diff
changeset
|
1310 return &panels[i]; |
|
e02e2036ef84
add Layer 2 JRPG component system
MrJuneJune <me@mrjunejune.com>
parents:
265
diff
changeset
|
1311 } |
|
e02e2036ef84
add Layer 2 JRPG component system
MrJuneJune <me@mrjunejune.com>
parents:
265
diff
changeset
|
1312 return &panels[0]; |
|
e02e2036ef84
add Layer 2 JRPG component system
MrJuneJune <me@mrjunejune.com>
parents:
265
diff
changeset
|
1313 } |
|
e02e2036ef84
add Layer 2 JRPG component system
MrJuneJune <me@mrjunejune.com>
parents:
265
diff
changeset
|
1314 |
|
259
667156fcd3e3
Add dev-only cyberpunk JRPG page
MrJuneJune <me@mrjunejune.com>
parents:
250
diff
changeset
|
1315 Seobeo_Request_Entry *GetJrpg(Seobeo_Request_Entry *req, Dowa_Arena *arena) |
|
667156fcd3e3
Add dev-only cyberpunk JRPG page
MrJuneJune <me@mrjunejune.com>
parents:
250
diff
changeset
|
1316 { |
|
667156fcd3e3
Add dev-only cyberpunk JRPG page
MrJuneJune <me@mrjunejune.com>
parents:
250
diff
changeset
|
1317 Seobeo_Request_Entry *resp = NULL; |
|
264
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
1318 char *final_body = Dowa_Arena_Allocate(arena, HTML_PAGE_CAP); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
1319 if (!final_body || !Mjj_Template_Render_File(final_body, HTML_PAGE_CAP, "/jrpg/index.html", arena)) |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
1320 return html_render_error(arena, "Internal Server Error"); |
|
273
e02e2036ef84
add Layer 2 JRPG component system
MrJuneJune <me@mrjunejune.com>
parents:
265
diff
changeset
|
1321 |
|
e02e2036ef84
add Layer 2 JRPG component system
MrJuneJune <me@mrjunejune.com>
parents:
265
diff
changeset
|
1322 const Jrpg_Initial_Panel *panel = Jrpg_Resolve_Initial_Panel(req); |
|
e02e2036ef84
add Layer 2 JRPG component system
MrJuneJune <me@mrjunejune.com>
parents:
265
diff
changeset
|
1323 boolean conversations = |
|
e02e2036ef84
add Layer 2 JRPG component system
MrJuneJune <me@mrjunejune.com>
parents:
265
diff
changeset
|
1324 strcmp(panel->name, "conversations") == 0 ? TRUE : FALSE; |
|
e02e2036ef84
add Layer 2 JRPG component system
MrJuneJune <me@mrjunejune.com>
parents:
265
diff
changeset
|
1325 const char *preview_selection = conversations ? "resume" : panel->name; |
|
e02e2036ef84
add Layer 2 JRPG component system
MrJuneJune <me@mrjunejune.com>
parents:
265
diff
changeset
|
1326 struct { |
|
e02e2036ef84
add Layer 2 JRPG component system
MrJuneJune <me@mrjunejune.com>
parents:
265
diff
changeset
|
1327 const char *token; |
|
e02e2036ef84
add Layer 2 JRPG component system
MrJuneJune <me@mrjunejune.com>
parents:
265
diff
changeset
|
1328 const char *value; |
|
e02e2036ef84
add Layer 2 JRPG component system
MrJuneJune <me@mrjunejune.com>
parents:
265
diff
changeset
|
1329 } replacements[] = { |
|
e02e2036ef84
add Layer 2 JRPG component system
MrJuneJune <me@mrjunejune.com>
parents:
265
diff
changeset
|
1330 {"__MJJ_PANEL__", panel->name}, |
|
e02e2036ef84
add Layer 2 JRPG component system
MrJuneJune <me@mrjunejune.com>
parents:
265
diff
changeset
|
1331 {"__MJJ_PREVIEW_SELECTION__", preview_selection}, |
|
e02e2036ef84
add Layer 2 JRPG component system
MrJuneJune <me@mrjunejune.com>
parents:
265
diff
changeset
|
1332 {"__MJJ_PREVIEW_TITLE__", panel->title}, |
|
e02e2036ef84
add Layer 2 JRPG component system
MrJuneJune <me@mrjunejune.com>
parents:
265
diff
changeset
|
1333 {"__MJJ_PREVIEW_COPY__", panel->copy}, |
|
e02e2036ef84
add Layer 2 JRPG component system
MrJuneJune <me@mrjunejune.com>
parents:
265
diff
changeset
|
1334 {"__MJJ_PREVIEW_WORKS__", panel->works}, |
|
e02e2036ef84
add Layer 2 JRPG component system
MrJuneJune <me@mrjunejune.com>
parents:
265
diff
changeset
|
1335 {"__MJJ_PREVIEW_HIDDEN__", conversations ? "hidden" : ""}, |
|
e02e2036ef84
add Layer 2 JRPG component system
MrJuneJune <me@mrjunejune.com>
parents:
265
diff
changeset
|
1336 {"__MJJ_ARCHIVE_HIDDEN__", conversations ? "" : "hidden"}, |
|
e02e2036ef84
add Layer 2 JRPG component system
MrJuneJune <me@mrjunejune.com>
parents:
265
diff
changeset
|
1337 {"__MJJ_RESUME_PRESSED__", |
|
e02e2036ef84
add Layer 2 JRPG component system
MrJuneJune <me@mrjunejune.com>
parents:
265
diff
changeset
|
1338 strcmp(panel->name, "resume") == 0 ? "true" : "false"}, |
|
e02e2036ef84
add Layer 2 JRPG component system
MrJuneJune <me@mrjunejune.com>
parents:
265
diff
changeset
|
1339 {"__MJJ_TOOLS_PRESSED__", |
|
e02e2036ef84
add Layer 2 JRPG component system
MrJuneJune <me@mrjunejune.com>
parents:
265
diff
changeset
|
1340 strcmp(panel->name, "tools") == 0 ? "true" : "false"}, |
|
e02e2036ef84
add Layer 2 JRPG component system
MrJuneJune <me@mrjunejune.com>
parents:
265
diff
changeset
|
1341 {"__MJJ_BLOG_PRESSED__", |
|
e02e2036ef84
add Layer 2 JRPG component system
MrJuneJune <me@mrjunejune.com>
parents:
265
diff
changeset
|
1342 strcmp(panel->name, "blog") == 0 ? "true" : "false"}, |
|
e02e2036ef84
add Layer 2 JRPG component system
MrJuneJune <me@mrjunejune.com>
parents:
265
diff
changeset
|
1343 {"__MJJ_CONVERSATIONS_PRESSED__", conversations ? "true" : "false"}, |
|
e02e2036ef84
add Layer 2 JRPG component system
MrJuneJune <me@mrjunejune.com>
parents:
265
diff
changeset
|
1344 }; |
|
e02e2036ef84
add Layer 2 JRPG component system
MrJuneJune <me@mrjunejune.com>
parents:
265
diff
changeset
|
1345 for (size_t i = 0; |
|
e02e2036ef84
add Layer 2 JRPG component system
MrJuneJune <me@mrjunejune.com>
parents:
265
diff
changeset
|
1346 i < sizeof(replacements) / sizeof(replacements[0]); |
|
e02e2036ef84
add Layer 2 JRPG component system
MrJuneJune <me@mrjunejune.com>
parents:
265
diff
changeset
|
1347 ++i) |
|
e02e2036ef84
add Layer 2 JRPG component system
MrJuneJune <me@mrjunejune.com>
parents:
265
diff
changeset
|
1348 { |
|
e02e2036ef84
add Layer 2 JRPG component system
MrJuneJune <me@mrjunejune.com>
parents:
265
diff
changeset
|
1349 if (!Mjj_Replace_All( |
|
e02e2036ef84
add Layer 2 JRPG component system
MrJuneJune <me@mrjunejune.com>
parents:
265
diff
changeset
|
1350 final_body, |
|
e02e2036ef84
add Layer 2 JRPG component system
MrJuneJune <me@mrjunejune.com>
parents:
265
diff
changeset
|
1351 HTML_PAGE_CAP, |
|
e02e2036ef84
add Layer 2 JRPG component system
MrJuneJune <me@mrjunejune.com>
parents:
265
diff
changeset
|
1352 replacements[i].token, |
|
e02e2036ef84
add Layer 2 JRPG component system
MrJuneJune <me@mrjunejune.com>
parents:
265
diff
changeset
|
1353 replacements[i].value)) |
|
e02e2036ef84
add Layer 2 JRPG component system
MrJuneJune <me@mrjunejune.com>
parents:
265
diff
changeset
|
1354 return html_render_error(arena, "Internal Server Error"); |
|
e02e2036ef84
add Layer 2 JRPG component system
MrJuneJune <me@mrjunejune.com>
parents:
265
diff
changeset
|
1355 } |
|
259
667156fcd3e3
Add dev-only cyberpunk JRPG page
MrJuneJune <me@mrjunejune.com>
parents:
250
diff
changeset
|
1356 Dowa_HashMap_Push_Arena(resp, "body", final_body, arena); |
|
264
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
1357 Dowa_HashMap_Push_Arena(resp, "referrer-policy", "no-referrer", arena); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
1358 return resp; |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
1359 } |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
1360 |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
1361 Seobeo_Request_Entry *GetLogin(Seobeo_Request_Entry *req, Dowa_Arena *arena) |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
1362 { |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
1363 (void)req; |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
1364 Seobeo_Request_Entry *resp = NULL; |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
1365 char *final_body = Dowa_Arena_Allocate(arena, HTML_PAGE_CAP); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
1366 if (!final_body || !Mjj_Template_Render_File(final_body, HTML_PAGE_CAP, "/login/index.html", arena)) |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
1367 { |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
1368 Seobeo_Request_Entry *err = NULL; |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
1369 Dowa_HashMap_Push_Arena(err, "status", "500", arena); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
1370 Dowa_HashMap_Push_Arena(err, "content-type", "text/plain; charset=utf-8", arena); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
1371 Dowa_HashMap_Push_Arena(err, "cache-control", "no-store", arena); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
1372 Dowa_HashMap_Push_Arena(err, "body", "Internal Server Error", arena); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
1373 return err; |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
1374 } |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
1375 Dowa_HashMap_Push_Arena(resp, "body", final_body, arena); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
1376 Dowa_HashMap_Push_Arena( |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
1377 resp, "content-type", "text/html; charset=utf-8", arena); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
1378 Dowa_HashMap_Push_Arena(resp, "cache-control", "no-store", arena); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
1379 Dowa_HashMap_Push_Arena(resp, "pragma", "no-cache", arena); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
1380 Dowa_HashMap_Push_Arena(resp, "x-content-type-options", "nosniff", arena); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
1381 Dowa_HashMap_Push_Arena(resp, "referrer-policy", "no-referrer", arena); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
1382 Dowa_HashMap_Push_Arena( |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
1383 resp, "content-security-policy", "frame-ancestors 'none'", arena); |
|
259
667156fcd3e3
Add dev-only cyberpunk JRPG page
MrJuneJune <me@mrjunejune.com>
parents:
250
diff
changeset
|
1384 return resp; |
|
667156fcd3e3
Add dev-only cyberpunk JRPG page
MrJuneJune <me@mrjunejune.com>
parents:
250
diff
changeset
|
1385 } |
|
667156fcd3e3
Add dev-only cyberpunk JRPG page
MrJuneJune <me@mrjunejune.com>
parents:
250
diff
changeset
|
1386 |
| 201 | 1387 Seobeo_Request_Entry *GetNotesLogin(Seobeo_Request_Entry *req, Dowa_Arena *arena) |
| 1388 { | |
| 1389 Seobeo_Request_Entry *resp = NULL; | |
|
264
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
1390 char *final_body = Dowa_Arena_Allocate(arena, HTML_PAGE_CAP); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
1391 if (!final_body || !Mjj_Template_Render_File(final_body, HTML_PAGE_CAP, "/notes/login.html", arena)) |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
1392 return html_render_error(arena, "Internal Server Error"); |
| 201 | 1393 Dowa_HashMap_Push_Arena(resp, "body", final_body, arena); |
| 1394 return resp; | |
| 1395 } | |
| 1396 | |
| 1397 Seobeo_Request_Entry *GetNotes(Seobeo_Request_Entry *req, Dowa_Arena *arena) | |
| 1398 { | |
| 1399 Seobeo_Request_Entry *resp = NULL; | |
|
264
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
1400 char *final_body = Dowa_Arena_Allocate(arena, HTML_PAGE_CAP); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
1401 if (!final_body || !Mjj_Template_Render_File(final_body, HTML_PAGE_CAP, "/notes/index.html", arena)) |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
1402 return html_render_error(arena, "Internal Server Error"); |
| 201 | 1403 Dowa_HashMap_Push_Arena(resp, "body", final_body, arena); |
| 1404 return resp; | |
| 1405 } | |
| 1406 | |
| 1407 Seobeo_Request_Entry *GetNoteById(Seobeo_Request_Entry *req, Dowa_Arena *arena) | |
| 1408 { | |
| 1409 Seobeo_Request_Entry *resp = NULL; | |
|
264
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
1410 char *final_body = Dowa_Arena_Allocate(arena, HTML_PAGE_CAP); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
1411 if (!final_body || !Mjj_Template_Render_File(final_body, HTML_PAGE_CAP, "/notes/index.html", arena)) |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
1412 return html_render_error(arena, "Internal Server Error"); |
| 201 | 1413 Dowa_HashMap_Push_Arena(resp, "body", final_body, arena); |
| 1414 return resp; | |
| 1415 } | |
| 1416 | |
| 88 | 1417 CREATE_REDIRECT_HANDLER(HomePage, "/") |
| 1418 CREATE_REDIRECT_HANDLER(Resume, "/resume") | |
| 1419 CREATE_REDIRECT_HANDLER(Tools, "/tools") | |
| 1420 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
|
1421 CREATE_REDIRECT_HANDLER(FileConverter, "/tools/file_converter") |
|
242
543df0fe7168
[tools] Add full HLS player support
MrJuneJune <me@mrjunejune.com>
parents:
241
diff
changeset
|
1422 CREATE_REDIRECT_HANDLER(HlsPlayer, "/tools/hls_player") |
|
244
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
1423 CREATE_REDIRECT_HANDLER(LatexEditor, "/tools/latex_editor") |
|
125
f236c895604e
[MrJuneJune] Added web socket for chat to this.
June Park <parkjune1995@gmail.com>
parents:
124
diff
changeset
|
1424 CREATE_REDIRECT_HANDLER(Talk, "/talk") |
|
259
667156fcd3e3
Add dev-only cyberpunk JRPG page
MrJuneJune <me@mrjunejune.com>
parents:
250
diff
changeset
|
1425 CREATE_REDIRECT_HANDLER(Jrpg, "/jrpg") |
| 201 | 1426 CREATE_REDIRECT_HANDLER(Editor, "/editor") |
|
79
5710108c949e
[Seobeo] Added Redirect logic.
June Park <parkjune1995@gmail.com>
parents:
78
diff
changeset
|
1427 |
|
200
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1428 // S3 Upload URL API |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1429 // POST /api/s3/upload-url |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1430 // Headers: Authorization: Bearer <token>, Content-Type: application/json |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1431 // Body: {"filename": "photo.png", "content_type": "image/png"} |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1432 // Returns: {"upload_url": "https://...", "key": "uploads/..."} |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1433 Seobeo_Request_Entry *GetS3UploadUrl(Seobeo_Request_Entry *req, Dowa_Arena *arena) |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1434 { |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1435 Seobeo_Request_Entry *resp = NULL; |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1436 |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1437 // Check auth token |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1438 void *auth_kv = Dowa_HashMap_Get_Ptr(req, "Authorization"); |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1439 if (!auth_kv) |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1440 { |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1441 Dowa_HashMap_Push_Arena(resp, "status", "401", arena); |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1442 Dowa_HashMap_Push_Arena(resp, "content-type", "application/json", arena); |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1443 Dowa_HashMap_Push_Arena(resp, "body", "{\"error\":\"Missing Authorization header\"}", arena); |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1444 return resp; |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1445 } |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1446 |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1447 const char *auth_header = ((Seobeo_Request_Entry*)auth_kv)->value; |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1448 |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1449 // Expect "Bearer <token>" |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1450 if (strncmp(auth_header, "Bearer ", 7) != 0) |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1451 { |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1452 Dowa_HashMap_Push_Arena(resp, "status", "401", arena); |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1453 Dowa_HashMap_Push_Arena(resp, "content-type", "application/json", arena); |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1454 Dowa_HashMap_Push_Arena(resp, "body", "{\"error\":\"Invalid Authorization format, use Bearer token\"}", arena); |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1455 return resp; |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1456 } |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1457 |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1458 const char *token = auth_header + 7; |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1459 if (strlen(g_upload_auth_token) == 0 || strcmp(token, g_upload_auth_token) != 0) |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1460 { |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1461 Dowa_HashMap_Push_Arena(resp, "status", "403", arena); |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1462 Dowa_HashMap_Push_Arena(resp, "content-type", "application/json", arena); |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1463 Dowa_HashMap_Push_Arena(resp, "body", "{\"error\":\"Invalid token\"}", arena); |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1464 return resp; |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1465 } |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1466 |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1467 // Parse request body for filename and content_type |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1468 void *body_kv = Dowa_HashMap_Get_Ptr(req, "Body"); |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1469 if (!body_kv) |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1470 { |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1471 Dowa_HashMap_Push_Arena(resp, "status", "400", arena); |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1472 Dowa_HashMap_Push_Arena(resp, "content-type", "application/json", arena); |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1473 Dowa_HashMap_Push_Arena(resp, "body", "{\"error\":\"Missing request body\"}", arena); |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1474 return resp; |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1475 } |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1476 |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1477 const char *body = ((Seobeo_Request_Entry*)body_kv)->value; |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1478 |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1479 // Simple JSON parsing for filename and content_type |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1480 char filename[256] = {0}; |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1481 char content_type[128] = "application/octet-stream"; |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1482 |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1483 // Find "filename":"value" |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1484 const char *fn_key = strstr(body, "\"filename\""); |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1485 if (fn_key) |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1486 { |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1487 const char *fn_start = strchr(fn_key + 10, '"'); |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1488 if (fn_start) |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1489 { |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1490 fn_start++; |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1491 const char *fn_end = strchr(fn_start, '"'); |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1492 if (fn_end && (size_t)(fn_end - fn_start) < sizeof(filename)) |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1493 { |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1494 memcpy(filename, fn_start, fn_end - fn_start); |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1495 filename[fn_end - fn_start] = '\0'; |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1496 } |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1497 } |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1498 } |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1499 |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1500 // Find "content_type":"value" |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1501 const char *ct_key = strstr(body, "\"content_type\""); |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1502 if (ct_key) |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1503 { |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1504 const char *ct_start = strchr(ct_key + 14, '"'); |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1505 if (ct_start) |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1506 { |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1507 ct_start++; |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1508 const char *ct_end = strchr(ct_start, '"'); |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1509 if (ct_end && (size_t)(ct_end - ct_start) < sizeof(content_type)) |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1510 { |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1511 memcpy(content_type, ct_start, ct_end - ct_start); |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1512 content_type[ct_end - ct_start] = '\0'; |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1513 } |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1514 } |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1515 } |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1516 |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1517 if (strlen(filename) == 0) |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1518 { |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1519 Dowa_HashMap_Push_Arena(resp, "status", "400", arena); |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1520 Dowa_HashMap_Push_Arena(resp, "content-type", "application/json", arena); |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1521 Dowa_HashMap_Push_Arena(resp, "body", "{\"error\":\"Missing filename in request body\"}", arena); |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1522 return resp; |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1523 } |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1524 |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1525 // Generate unique S3 key with timestamp |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1526 char s3_key[512]; |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1527 char *uuid = Dowa_Arena_Allocate(arena, UUID_LEN); |
|
250
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
1528 uint32 seed = |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
1529 (uint32)time(NULL) ^ |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
1530 (uint32)Seobeo_Thread_Current_Id() ^ |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
1531 counter++; |
|
200
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1532 Dowa_String_UUID(seed, uuid); |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1533 snprintf(s3_key, sizeof(s3_key), "uploads/%s/%s", uuid, filename); |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1534 |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1535 // Generate presigned URL |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1536 S3_Presigned_URL presigned = S3_Presign_Put(&g_s3_config, s3_key, content_type, g_s3_url_expires); |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1537 |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1538 if (!presigned.success) |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1539 { |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1540 Dowa_HashMap_Push_Arena(resp, "status", "500", arena); |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1541 Dowa_HashMap_Push_Arena(resp, "content-type", "application/json", arena); |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1542 char *error_body = Dowa_Arena_Allocate(arena, 256); |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1543 snprintf(error_body, 256, "{\"error\":\"Failed to generate upload URL: %s\"}", |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1544 presigned.error_message ? presigned.error_message : "unknown"); |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1545 Dowa_HashMap_Push_Arena(resp, "body", error_body, arena); |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1546 S3_Presigned_URL_Destroy(&presigned); |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1547 return resp; |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1548 } |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1549 |
| 201 | 1550 // Build public URL using CloudFront |
| 1551 char public_url[512]; | |
| 1552 if (g_s3_cloudfront_url[0]) | |
| 1553 { | |
| 1554 snprintf(public_url, sizeof(public_url), "%s/%s", g_s3_cloudfront_url, s3_key); | |
| 1555 } | |
| 1556 else | |
| 1557 { | |
| 1558 snprintf(public_url, sizeof(public_url), "https://%s.s3.%s.amazonaws.com/%s", | |
| 1559 g_s3_bucket, g_s3_region, s3_key); | |
| 1560 } | |
| 1561 | |
|
200
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1562 // Build response |
| 201 | 1563 char *response_body = Dowa_Arena_Allocate(arena, 4096 + strlen(presigned.url)); |
| 1564 snprintf(response_body, 4096 + strlen(presigned.url), | |
| 1565 "{\"upload_url\":\"%s\",\"public_url\":\"%s\",\"key\":\"%s\",\"expires\":%d}", | |
| 1566 presigned.url, public_url, s3_key, g_s3_url_expires); | |
|
200
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1567 |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1568 S3_Presigned_URL_Destroy(&presigned); |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1569 |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1570 Dowa_HashMap_Push_Arena(resp, "status", "200", arena); |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1571 Dowa_HashMap_Push_Arena(resp, "content-type", "application/json", arena); |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1572 Dowa_HashMap_Push_Arena(resp, "body", response_body, arena); |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1573 |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1574 printf("[S3] Generated upload URL for: %s\n", s3_key); |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1575 |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1576 return resp; |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1577 } |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1578 |
| 201 | 1579 // Editor Content Save API |
| 1580 // POST /api/editor/save | |
| 1581 // Headers: Authorization: Bearer <token> | |
| 1582 // Body: {"doc_id": "my-doc", "content": "<html content>"} | |
| 1583 Seobeo_Request_Entry *EditorSave(Seobeo_Request_Entry *req, Dowa_Arena *arena) | |
| 1584 { | |
| 1585 Seobeo_Request_Entry *resp = NULL; | |
| 1586 | |
| 1587 // Check auth token | |
| 1588 void *auth_kv = Dowa_HashMap_Get_Ptr(req, "Authorization"); | |
| 1589 if (!auth_kv) | |
| 1590 { | |
| 1591 Dowa_HashMap_Push_Arena(resp, "status", "401", arena); | |
| 1592 Dowa_HashMap_Push_Arena(resp, "content-type", "application/json", arena); | |
| 1593 Dowa_HashMap_Push_Arena(resp, "body", "{\"error\":\"Missing Authorization header\"}", arena); | |
| 1594 return resp; | |
| 1595 } | |
| 1596 | |
| 1597 const char *auth_header = ((Seobeo_Request_Entry*)auth_kv)->value; | |
| 1598 if (strncmp(auth_header, "Bearer ", 7) != 0) | |
| 1599 { | |
| 1600 Dowa_HashMap_Push_Arena(resp, "status", "401", arena); | |
| 1601 Dowa_HashMap_Push_Arena(resp, "content-type", "application/json", arena); | |
| 1602 Dowa_HashMap_Push_Arena(resp, "body", "{\"error\":\"Invalid Authorization format\"}", arena); | |
| 1603 return resp; | |
| 1604 } | |
| 1605 | |
| 1606 const char *token = auth_header + 7; | |
| 1607 if (strlen(g_upload_auth_token) == 0 || strcmp(token, g_upload_auth_token) != 0) | |
| 1608 { | |
| 1609 Dowa_HashMap_Push_Arena(resp, "status", "403", arena); | |
| 1610 Dowa_HashMap_Push_Arena(resp, "content-type", "application/json", arena); | |
| 1611 Dowa_HashMap_Push_Arena(resp, "body", "{\"error\":\"Invalid token\"}", arena); | |
| 1612 return resp; | |
| 1613 } | |
| 1614 | |
| 1615 if (!g_db_connection) | |
| 1616 { | |
| 1617 Dowa_HashMap_Push_Arena(resp, "status", "500", arena); | |
| 1618 Dowa_HashMap_Push_Arena(resp, "content-type", "application/json", arena); | |
| 1619 Dowa_HashMap_Push_Arena(resp, "body", "{\"error\":\"Database not available\"}", arena); | |
| 1620 return resp; | |
| 1621 } | |
| 1622 | |
| 1623 // Parse request body | |
| 1624 void *body_kv = Dowa_HashMap_Get_Ptr(req, "Body"); | |
| 1625 if (!body_kv) | |
| 1626 { | |
| 1627 Dowa_HashMap_Push_Arena(resp, "status", "400", arena); | |
| 1628 Dowa_HashMap_Push_Arena(resp, "content-type", "application/json", arena); | |
| 1629 Dowa_HashMap_Push_Arena(resp, "body", "{\"error\":\"Missing request body\"}", arena); | |
| 1630 return resp; | |
| 1631 } | |
| 1632 | |
| 1633 const char *body = ((Seobeo_Request_Entry*)body_kv)->value; | |
| 1634 | |
| 1635 // Parse doc_id and content from JSON | |
| 1636 char doc_id[256] = "default"; | |
| 1637 char *content = NULL; | |
| 1638 size_t content_len = 0; | |
| 1639 | |
| 1640 // Find "doc_id":"value" | |
| 1641 const char *doc_key = strstr(body, "\"doc_id\""); | |
| 1642 if (doc_key) | |
| 1643 { | |
| 1644 const char *doc_start = strchr(doc_key + 8, '"'); | |
| 1645 if (doc_start) | |
| 1646 { | |
| 1647 doc_start++; | |
| 1648 const char *doc_end = strchr(doc_start, '"'); | |
| 1649 if (doc_end && (size_t)(doc_end - doc_start) < sizeof(doc_id)) | |
| 1650 { | |
| 1651 memcpy(doc_id, doc_start, doc_end - doc_start); | |
| 1652 doc_id[doc_end - doc_start] = '\0'; | |
| 1653 } | |
| 1654 } | |
| 1655 } | |
| 1656 | |
| 1657 // Find "content":"value" - content can be large and contain escaped characters | |
| 1658 const char *content_key = strstr(body, "\"content\""); | |
| 1659 if (content_key) | |
| 1660 { | |
| 1661 const char *content_start = strchr(content_key + 9, '"'); | |
| 1662 if (content_start) | |
| 1663 { | |
| 1664 content_start++; | |
| 1665 // Find closing quote (accounting for escaped quotes) | |
| 1666 const char *p = content_start; | |
| 1667 while (*p) | |
| 1668 { | |
| 1669 if (*p == '\\' && *(p+1)) | |
| 1670 { | |
| 1671 p += 2; | |
| 1672 continue; | |
| 1673 } | |
| 1674 if (*p == '"') break; | |
| 1675 p++; | |
| 1676 } | |
| 1677 content_len = p - content_start; | |
| 1678 content = Dowa_Arena_Allocate(arena, content_len + 1); | |
| 1679 memcpy(content, content_start, content_len); | |
| 1680 content[content_len] = '\0'; | |
| 1681 } | |
| 1682 } | |
| 1683 | |
| 1684 if (!content) | |
| 1685 { | |
| 1686 Dowa_HashMap_Push_Arena(resp, "status", "400", arena); | |
| 1687 Dowa_HashMap_Push_Arena(resp, "content-type", "application/json", arena); | |
| 1688 Dowa_HashMap_Push_Arena(resp, "body", "{\"error\":\"Missing content\"}", arena); | |
| 1689 return resp; | |
| 1690 } | |
| 1691 | |
| 1692 // Upsert content | |
| 1693 const char *upsert_query = | |
| 1694 "INSERT INTO editor_content (access_token, doc_id, content, updated_at) " | |
| 1695 "VALUES (?, ?, ?, strftime('%s', 'now')) " | |
| 1696 "ON CONFLICT(access_token, doc_id) DO UPDATE SET " | |
| 1697 "content = excluded.content, updated_at = strftime('%s', 'now')"; | |
| 1698 | |
| 1699 const char *params[] = { token, doc_id, content }; | |
| 1700 int32 result = Deita_Query_Execute_Update_Prepared(g_db_connection, upsert_query, 3, params); | |
| 1701 | |
| 1702 if (result < 0) | |
| 1703 { | |
| 1704 Dowa_HashMap_Push_Arena(resp, "status", "500", arena); | |
| 1705 Dowa_HashMap_Push_Arena(resp, "content-type", "application/json", arena); | |
| 1706 Dowa_HashMap_Push_Arena(resp, "body", "{\"error\":\"Failed to save\"}", arena); | |
| 1707 return resp; | |
| 1708 } | |
| 1709 | |
| 1710 printf("[EDITOR] Saved doc_id=%s, content_len=%zu\n", doc_id, content_len); | |
| 1711 | |
| 1712 Dowa_HashMap_Push_Arena(resp, "status", "200", arena); | |
| 1713 Dowa_HashMap_Push_Arena(resp, "content-type", "application/json", arena); | |
| 1714 Dowa_HashMap_Push_Arena(resp, "body", "{\"success\":true}", arena); | |
| 1715 return resp; | |
| 1716 } | |
| 1717 | |
| 1718 // Editor Content Load API | |
| 1719 // GET /api/editor/load/:doc_id | |
| 1720 // Headers: Authorization: Bearer <token> | |
| 1721 Seobeo_Request_Entry *EditorLoad(Seobeo_Request_Entry *req, Dowa_Arena *arena) | |
| 1722 { | |
| 1723 Seobeo_Request_Entry *resp = NULL; | |
| 1724 | |
| 1725 // Check auth token | |
| 1726 void *auth_kv = Dowa_HashMap_Get_Ptr(req, "Authorization"); | |
| 1727 if (!auth_kv) | |
| 1728 { | |
| 1729 Dowa_HashMap_Push_Arena(resp, "status", "401", arena); | |
| 1730 Dowa_HashMap_Push_Arena(resp, "content-type", "application/json", arena); | |
| 1731 Dowa_HashMap_Push_Arena(resp, "body", "{\"error\":\"Missing Authorization header\"}", arena); | |
| 1732 return resp; | |
| 1733 } | |
| 1734 | |
| 1735 const char *auth_header = ((Seobeo_Request_Entry*)auth_kv)->value; | |
| 1736 if (strncmp(auth_header, "Bearer ", 7) != 0) | |
| 1737 { | |
| 1738 Dowa_HashMap_Push_Arena(resp, "status", "401", arena); | |
| 1739 Dowa_HashMap_Push_Arena(resp, "content-type", "application/json", arena); | |
| 1740 Dowa_HashMap_Push_Arena(resp, "body", "{\"error\":\"Invalid Authorization format\"}", arena); | |
| 1741 return resp; | |
| 1742 } | |
| 1743 | |
| 1744 const char *token = auth_header + 7; | |
| 1745 if (strlen(g_upload_auth_token) == 0 || strcmp(token, g_upload_auth_token) != 0) | |
| 1746 { | |
| 1747 Dowa_HashMap_Push_Arena(resp, "status", "403", arena); | |
| 1748 Dowa_HashMap_Push_Arena(resp, "content-type", "application/json", arena); | |
| 1749 Dowa_HashMap_Push_Arena(resp, "body", "{\"error\":\"Invalid token\"}", arena); | |
| 1750 return resp; | |
| 1751 } | |
| 1752 | |
| 1753 if (!g_db_connection) | |
| 1754 { | |
| 1755 Dowa_HashMap_Push_Arena(resp, "status", "500", arena); | |
| 1756 Dowa_HashMap_Push_Arena(resp, "content-type", "application/json", arena); | |
| 1757 Dowa_HashMap_Push_Arena(resp, "body", "{\"error\":\"Database not available\"}", arena); | |
| 1758 return resp; | |
| 1759 } | |
| 1760 | |
| 1761 // Get doc_id from URL parameter | |
| 1762 void *doc_id_kv = Dowa_HashMap_Get_Ptr(req, ":doc_id"); | |
| 1763 const char *doc_id = "default"; | |
| 1764 if (doc_id_kv) | |
| 1765 { | |
| 1766 doc_id = ((Seobeo_Request_Entry*)doc_id_kv)->value; | |
| 1767 } | |
| 1768 | |
| 1769 // Query content | |
| 1770 const char *select_query = | |
| 1771 "SELECT content, updated_at FROM editor_content WHERE access_token = ? AND doc_id = ?"; | |
| 1772 const char *params[] = { token, doc_id }; | |
| 1773 | |
| 1774 Deita_Result_Set *p_result = Deita_Query_Execute_Prepared(g_db_connection, select_query, 2, params, arena); | |
| 1775 | |
| 1776 if (p_result && Deita_Result_Set_Next(p_result)) | |
| 1777 { | |
| 1778 const char *content = Deita_Result_Set_Get_Text(p_result, 0); | |
| 1779 int64 updated_at = Deita_Result_Set_Get_Integer(p_result, 1); | |
| 1780 | |
| 1781 // Build JSON response - escape content | |
| 1782 size_t content_len = content ? strlen(content) : 0; | |
| 1783 char *response_body = Dowa_Arena_Allocate(arena, content_len + 256); | |
| 1784 snprintf(response_body, content_len + 256, | |
| 1785 "{\"doc_id\":\"%s\",\"content\":\"%s\",\"updated_at\":%lld}", | |
| 1786 doc_id, content ? content : "", (long long)updated_at); | |
| 1787 | |
| 1788 Dowa_HashMap_Push_Arena(resp, "status", "200", arena); | |
| 1789 Dowa_HashMap_Push_Arena(resp, "content-type", "application/json", arena); | |
| 1790 Dowa_HashMap_Push_Arena(resp, "body", response_body, arena); | |
| 1791 | |
| 1792 printf("[EDITOR] Loaded doc_id=%s\n", doc_id); | |
| 1793 } | |
| 1794 else | |
| 1795 { | |
| 1796 // No content found, return empty | |
| 1797 char *response_body = Dowa_Arena_Allocate(arena, 128); | |
| 1798 snprintf(response_body, 128, "{\"doc_id\":\"%s\",\"content\":\"\",\"updated_at\":0}", doc_id); | |
| 1799 | |
| 1800 Dowa_HashMap_Push_Arena(resp, "status", "200", arena); | |
| 1801 Dowa_HashMap_Push_Arena(resp, "content-type", "application/json", arena); | |
| 1802 Dowa_HashMap_Push_Arena(resp, "body", response_body, arena); | |
| 1803 } | |
| 1804 | |
| 1805 if (p_result) Deita_Result_Set_Free(p_result); | |
| 1806 return resp; | |
| 1807 } | |
| 1808 | |
| 1809 // Media Upload API - Create media record | |
| 1810 // POST /api/media/create | |
| 1811 // Headers: Authorization: Bearer <token>, Content-Type: application/json | |
| 1812 // Body: {"filename": "photo.jpg", "content_type": "image/jpeg"} | |
| 1813 // Returns: {"media_id": 123, "upload_url": "https://...", "expires": 3600} | |
| 1814 Seobeo_Request_Entry *MediaCreate(Seobeo_Request_Entry *req, Dowa_Arena *arena) | |
| 1815 { | |
| 1816 Seobeo_Request_Entry *resp = NULL; | |
| 1817 | |
| 1818 // Check auth token | |
| 1819 void *auth_kv = Dowa_HashMap_Get_Ptr(req, "Authorization"); | |
| 1820 if (!auth_kv) | |
| 1821 { | |
| 1822 Dowa_HashMap_Push_Arena(resp, "status", "401", arena); | |
| 1823 Dowa_HashMap_Push_Arena(resp, "content-type", "application/json", arena); | |
| 1824 Dowa_HashMap_Push_Arena(resp, "body", "{\"error\":\"Missing Authorization header\"}", arena); | |
| 1825 return resp; | |
| 1826 } | |
| 1827 | |
| 1828 const char *auth_header = ((Seobeo_Request_Entry*)auth_kv)->value; | |
| 1829 if (strncmp(auth_header, "Bearer ", 7) != 0) | |
| 1830 { | |
| 1831 Dowa_HashMap_Push_Arena(resp, "status", "401", arena); | |
| 1832 Dowa_HashMap_Push_Arena(resp, "content-type", "application/json", arena); | |
| 1833 Dowa_HashMap_Push_Arena(resp, "body", "{\"error\":\"Invalid Authorization format\"}", arena); | |
| 1834 return resp; | |
| 1835 } | |
| 1836 | |
| 1837 const char *token = auth_header + 7; | |
| 1838 if (strlen(g_upload_auth_token) == 0 || strcmp(token, g_upload_auth_token) != 0) | |
| 1839 { | |
| 1840 Dowa_HashMap_Push_Arena(resp, "status", "403", arena); | |
| 1841 Dowa_HashMap_Push_Arena(resp, "content-type", "application/json", arena); | |
| 1842 Dowa_HashMap_Push_Arena(resp, "body", "{\"error\":\"Invalid token\"}", arena); | |
| 1843 return resp; | |
| 1844 } | |
| 1845 | |
| 1846 if (!g_db_connection) | |
| 1847 { | |
| 1848 Dowa_HashMap_Push_Arena(resp, "status", "500", arena); | |
| 1849 Dowa_HashMap_Push_Arena(resp, "content-type", "application/json", arena); | |
| 1850 Dowa_HashMap_Push_Arena(resp, "body", "{\"error\":\"Database not available\"}", arena); | |
| 1851 return resp; | |
| 1852 } | |
| 1853 | |
| 1854 // Parse request body | |
| 1855 void *body_kv = Dowa_HashMap_Get_Ptr(req, "Body"); | |
| 1856 if (!body_kv) | |
| 1857 { | |
| 1858 Dowa_HashMap_Push_Arena(resp, "status", "400", arena); | |
| 1859 Dowa_HashMap_Push_Arena(resp, "content-type", "application/json", arena); | |
| 1860 Dowa_HashMap_Push_Arena(resp, "body", "{\"error\":\"Missing request body\"}", arena); | |
| 1861 return resp; | |
| 1862 } | |
| 1863 | |
| 1864 const char *body = ((Seobeo_Request_Entry*)body_kv)->value; | |
| 1865 | |
| 1866 // Parse filename and content_type | |
| 1867 char filename[256] = {0}; | |
| 1868 char content_type[128] = "application/octet-stream"; | |
| 1869 | |
| 1870 // Find "filename":"value" | |
| 1871 const char *fn_key = strstr(body, "\"filename\""); | |
| 1872 if (fn_key) | |
| 1873 { | |
| 1874 const char *fn_start = strchr(fn_key + 10, '"'); | |
| 1875 if (fn_start) | |
| 1876 { | |
| 1877 fn_start++; | |
| 1878 const char *fn_end = strchr(fn_start, '"'); | |
| 1879 if (fn_end && (size_t)(fn_end - fn_start) < sizeof(filename)) | |
| 1880 { | |
| 1881 memcpy(filename, fn_start, fn_end - fn_start); | |
| 1882 filename[fn_end - fn_start] = '\0'; | |
| 1883 } | |
| 1884 } | |
| 1885 } | |
| 1886 | |
| 1887 // Find "content_type":"value" | |
| 1888 const char *ct_key = strstr(body, "\"content_type\""); | |
| 1889 if (ct_key) | |
| 1890 { | |
| 1891 const char *ct_start = strchr(ct_key + 14, '"'); | |
| 1892 if (ct_start) | |
| 1893 { | |
| 1894 ct_start++; | |
| 1895 const char *ct_end = strchr(ct_start, '"'); | |
| 1896 if (ct_end && (size_t)(ct_end - ct_start) < sizeof(content_type)) | |
| 1897 { | |
| 1898 memcpy(content_type, ct_start, ct_end - ct_start); | |
| 1899 content_type[ct_end - ct_start] = '\0'; | |
| 1900 } | |
| 1901 } | |
| 1902 } | |
| 1903 | |
| 1904 if (strlen(filename) == 0) | |
| 1905 { | |
| 1906 Dowa_HashMap_Push_Arena(resp, "status", "400", arena); | |
| 1907 Dowa_HashMap_Push_Arena(resp, "content-type", "application/json", arena); | |
| 1908 Dowa_HashMap_Push_Arena(resp, "body", "{\"error\":\"Missing filename\"}", arena); | |
| 1909 return resp; | |
| 1910 } | |
| 1911 | |
| 1912 // Generate UUID for this upload | |
| 1913 char *uuid = Dowa_Arena_Allocate(arena, UUID_LEN); | |
|
250
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
1914 uint32 seed = |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
1915 (uint32)time(NULL) ^ |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
1916 (uint32)Seobeo_Thread_Current_Id() ^ |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
1917 counter++; |
| 201 | 1918 Dowa_String_UUID(seed, uuid); |
| 1919 | |
| 1920 // Generate S3 keys | |
| 1921 char s3_key_original[512]; | |
| 1922 char s3_key_processed[512]; | |
| 1923 snprintf(s3_key_original, sizeof(s3_key_original), "uploads/%s/%s", uuid, filename); | |
|
204
e5aed6c36672
[Notes] Added icons and updated styling a bit. Probalby usable now.
MrJuneJune <me@mrjunejune.com>
parents:
202
diff
changeset
|
1924 |
|
e5aed6c36672
[Notes] Added icons and updated styling a bit. Probalby usable now.
MrJuneJune <me@mrjunejune.com>
parents:
202
diff
changeset
|
1925 // Only use .webp for images |
|
e5aed6c36672
[Notes] Added icons and updated styling a bit. Probalby usable now.
MrJuneJune <me@mrjunejune.com>
parents:
202
diff
changeset
|
1926 int is_image = (strncmp(content_type, "image/", 6) == 0); |
|
e5aed6c36672
[Notes] Added icons and updated styling a bit. Probalby usable now.
MrJuneJune <me@mrjunejune.com>
parents:
202
diff
changeset
|
1927 if (is_image) |
|
e5aed6c36672
[Notes] Added icons and updated styling a bit. Probalby usable now.
MrJuneJune <me@mrjunejune.com>
parents:
202
diff
changeset
|
1928 { |
|
e5aed6c36672
[Notes] Added icons and updated styling a bit. Probalby usable now.
MrJuneJune <me@mrjunejune.com>
parents:
202
diff
changeset
|
1929 snprintf(s3_key_processed, sizeof(s3_key_processed), "uploads/%s/processed.webp", uuid); |
|
e5aed6c36672
[Notes] Added icons and updated styling a bit. Probalby usable now.
MrJuneJune <me@mrjunejune.com>
parents:
202
diff
changeset
|
1930 } |
|
e5aed6c36672
[Notes] Added icons and updated styling a bit. Probalby usable now.
MrJuneJune <me@mrjunejune.com>
parents:
202
diff
changeset
|
1931 else |
|
e5aed6c36672
[Notes] Added icons and updated styling a bit. Probalby usable now.
MrJuneJune <me@mrjunejune.com>
parents:
202
diff
changeset
|
1932 { |
|
e5aed6c36672
[Notes] Added icons and updated styling a bit. Probalby usable now.
MrJuneJune <me@mrjunejune.com>
parents:
202
diff
changeset
|
1933 s3_key_processed[0] = '\0'; // No processed version for non-images |
|
e5aed6c36672
[Notes] Added icons and updated styling a bit. Probalby usable now.
MrJuneJune <me@mrjunejune.com>
parents:
202
diff
changeset
|
1934 } |
| 201 | 1935 |
| 1936 // Insert into database | |
| 1937 const char *insert_query = | |
| 1938 "INSERT INTO media_uploads (access_token, original_filename, content_type, s3_key_original, s3_key_processed, status) " | |
|
244
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
1939 "VALUES (?, ?, ?, ?, ?, 'pending') RETURNING id"; |
| 201 | 1940 |
| 1941 const char *params[] = { token, filename, content_type, s3_key_original, s3_key_processed }; | |
|
244
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
1942 Deita_Result_Set *id_result = Deita_Query_Execute_Prepared( |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
1943 g_db_connection, |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
1944 insert_query, |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
1945 5, |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
1946 params, |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
1947 arena); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
1948 if (!id_result || !Deita_Result_Set_Next(id_result)) |
| 201 | 1949 { |
|
244
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
1950 if (id_result) Deita_Result_Set_Free(id_result); |
| 201 | 1951 Dowa_HashMap_Push_Arena(resp, "status", "500", arena); |
| 1952 Dowa_HashMap_Push_Arena(resp, "content-type", "application/json", arena); | |
| 1953 Dowa_HashMap_Push_Arena(resp, "body", "{\"error\":\"Failed to create media record\"}", arena); | |
| 1954 return resp; | |
| 1955 } | |
| 1956 | |
|
244
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
1957 int64 media_id = Deita_Result_Set_Get_Integer(id_result, 0); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
1958 Deita_Result_Set_Free(id_result); |
| 201 | 1959 |
| 1960 // Generate presigned PUT URL | |
| 1961 S3_Presigned_URL presigned = S3_Presign_Put(&g_s3_config, s3_key_original, content_type, g_s3_url_expires); | |
| 1962 | |
| 1963 if (!presigned.success) | |
| 1964 { | |
| 1965 Dowa_HashMap_Push_Arena(resp, "status", "500", arena); | |
| 1966 Dowa_HashMap_Push_Arena(resp, "content-type", "application/json", arena); | |
| 1967 char *error_body = Dowa_Arena_Allocate(arena, 256); | |
| 1968 snprintf(error_body, 256, "{\"error\":\"Failed to generate upload URL: %s\"}", | |
| 1969 presigned.error_message ? presigned.error_message : "unknown"); | |
| 1970 Dowa_HashMap_Push_Arena(resp, "body", error_body, arena); | |
| 1971 S3_Presigned_URL_Destroy(&presigned); | |
| 1972 return resp; | |
| 1973 } | |
| 1974 | |
|
204
e5aed6c36672
[Notes] Added icons and updated styling a bit. Probalby usable now.
MrJuneJune <me@mrjunejune.com>
parents:
202
diff
changeset
|
1975 // Build public URL using CloudFront or S3 |
|
e5aed6c36672
[Notes] Added icons and updated styling a bit. Probalby usable now.
MrJuneJune <me@mrjunejune.com>
parents:
202
diff
changeset
|
1976 char public_url[512]; |
|
e5aed6c36672
[Notes] Added icons and updated styling a bit. Probalby usable now.
MrJuneJune <me@mrjunejune.com>
parents:
202
diff
changeset
|
1977 if (g_s3_cloudfront_url[0]) |
|
e5aed6c36672
[Notes] Added icons and updated styling a bit. Probalby usable now.
MrJuneJune <me@mrjunejune.com>
parents:
202
diff
changeset
|
1978 { |
|
e5aed6c36672
[Notes] Added icons and updated styling a bit. Probalby usable now.
MrJuneJune <me@mrjunejune.com>
parents:
202
diff
changeset
|
1979 snprintf(public_url, sizeof(public_url), "%s/%s", g_s3_cloudfront_url, s3_key_original); |
|
e5aed6c36672
[Notes] Added icons and updated styling a bit. Probalby usable now.
MrJuneJune <me@mrjunejune.com>
parents:
202
diff
changeset
|
1980 } |
|
e5aed6c36672
[Notes] Added icons and updated styling a bit. Probalby usable now.
MrJuneJune <me@mrjunejune.com>
parents:
202
diff
changeset
|
1981 else |
|
e5aed6c36672
[Notes] Added icons and updated styling a bit. Probalby usable now.
MrJuneJune <me@mrjunejune.com>
parents:
202
diff
changeset
|
1982 { |
|
e5aed6c36672
[Notes] Added icons and updated styling a bit. Probalby usable now.
MrJuneJune <me@mrjunejune.com>
parents:
202
diff
changeset
|
1983 snprintf(public_url, sizeof(public_url), "https://%s.s3.%s.amazonaws.com/%s", |
|
e5aed6c36672
[Notes] Added icons and updated styling a bit. Probalby usable now.
MrJuneJune <me@mrjunejune.com>
parents:
202
diff
changeset
|
1984 g_s3_bucket, g_s3_region, s3_key_original); |
|
e5aed6c36672
[Notes] Added icons and updated styling a bit. Probalby usable now.
MrJuneJune <me@mrjunejune.com>
parents:
202
diff
changeset
|
1985 } |
|
e5aed6c36672
[Notes] Added icons and updated styling a bit. Probalby usable now.
MrJuneJune <me@mrjunejune.com>
parents:
202
diff
changeset
|
1986 |
| 201 | 1987 // Build response |
|
204
e5aed6c36672
[Notes] Added icons and updated styling a bit. Probalby usable now.
MrJuneJune <me@mrjunejune.com>
parents:
202
diff
changeset
|
1988 char *response_body = Dowa_Arena_Allocate(arena, 4096 + strlen(presigned.url) + strlen(public_url)); |
|
e5aed6c36672
[Notes] Added icons and updated styling a bit. Probalby usable now.
MrJuneJune <me@mrjunejune.com>
parents:
202
diff
changeset
|
1989 snprintf(response_body, 4096 + strlen(presigned.url) + strlen(public_url), |
|
e5aed6c36672
[Notes] Added icons and updated styling a bit. Probalby usable now.
MrJuneJune <me@mrjunejune.com>
parents:
202
diff
changeset
|
1990 "{\"media_id\":%lld,\"upload_url\":\"%s\",\"public_url\":\"%s\",\"expires\":%d}", |
|
e5aed6c36672
[Notes] Added icons and updated styling a bit. Probalby usable now.
MrJuneJune <me@mrjunejune.com>
parents:
202
diff
changeset
|
1991 (long long)media_id, presigned.url, public_url, g_s3_url_expires); |
| 201 | 1992 |
| 1993 S3_Presigned_URL_Destroy(&presigned); | |
| 1994 | |
| 1995 Dowa_HashMap_Push_Arena(resp, "status", "200", arena); | |
| 1996 Dowa_HashMap_Push_Arena(resp, "content-type", "application/json", arena); | |
| 1997 Dowa_HashMap_Push_Arena(resp, "body", response_body, arena); | |
| 1998 | |
| 1999 printf("[MEDIA] Created media_id=%lld, file=%s\n", (long long)media_id, filename); | |
| 2000 | |
| 2001 return resp; | |
| 2002 } | |
| 2003 | |
|
250
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
2004 // Worker-pool task for S3 media processing. |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
2005 void Media_Process_Background(void *arg) |
| 201 | 2006 { |
| 2007 Media_Processing_Context *ctx = (Media_Processing_Context *)arg; | |
| 2008 | |
|
250
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
2009 Seobeo_Log(SEOBEO_INFO, "[MEDIA] Background worker started for media_id=%lld\n", (long long)ctx->media_id); |
|
202
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2010 Seobeo_Log(SEOBEO_INFO, "[MEDIA] S3 key original: %s\n", ctx->s3_key_original); |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2011 Seobeo_Log(SEOBEO_INFO, "[MEDIA] S3 key processed: %s\n", ctx->s3_key_processed); |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2012 Seobeo_Log(SEOBEO_INFO, "[MEDIA] DB path: %s\n", ctx->db_path); |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2013 |
|
250
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
2014 // Open a worker-local DB connection. |
|
202
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2015 Deita_Connection *db_conn = Deita_Connection_Create(DEITA_DATABASE_TYPE_SQLITE3, ctx->db_path); |
| 201 | 2016 if (!db_conn || !Deita_Connection_Is_Open(db_conn)) |
| 2017 { | |
|
250
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
2018 Seobeo_Log(SEOBEO_ERROR, "[MEDIA] Worker ERROR: Failed to open database for media_id=%lld\n", (long long)ctx->media_id); |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
2019 return; |
| 201 | 2020 } |
| 2021 | |
| 2022 // Update status to 'processing' | |
| 2023 const char *update_processing = | |
| 2024 "UPDATE media_uploads SET status='processing', updated_at=strftime('%s','now') WHERE id=?"; | |
| 2025 char media_id_str[32]; | |
| 2026 snprintf(media_id_str, sizeof(media_id_str), "%lld", (long long)ctx->media_id); | |
| 2027 const char *params[] = { media_id_str }; | |
|
202
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2028 int32 update_result = Deita_Query_Execute_Update_Prepared(db_conn, update_processing, 1, params); |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2029 Seobeo_Log(SEOBEO_INFO, "[MEDIA] Updated status to 'processing' for media_id=%lld (result=%d)\n", (long long)ctx->media_id, update_result); |
| 201 | 2030 |
| 2031 // Generate presigned GET URL for download (10 min expiry) | |
|
202
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2032 Seobeo_Log(SEOBEO_INFO, "[MEDIA] Generating presigned GET URL for media_id=%lld\n", (long long)ctx->media_id); |
| 201 | 2033 S3_Presigned_URL download_url = S3_Presign_Get(&ctx->s3_config, ctx->s3_key_original, 600); |
| 2034 if (!download_url.success) | |
| 2035 { | |
|
202
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2036 const char *error_msg = download_url.error_message ? download_url.error_message : "Failed to generate download URL"; |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2037 Seobeo_Log(SEOBEO_ERROR, "[MEDIA] ERROR: Failed to generate download URL for media_id=%lld: %s\n", |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2038 (long long)ctx->media_id, error_msg); |
| 201 | 2039 const char *update_error = |
| 2040 "UPDATE media_uploads SET status='error', error_message=?, updated_at=strftime('%s','now') WHERE id=?"; | |
|
202
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2041 const char *error_params[] = { error_msg, media_id_str }; |
| 201 | 2042 Deita_Query_Execute_Update_Prepared(db_conn, update_error, 2, error_params); |
| 2043 S3_Presigned_URL_Destroy(&download_url); | |
| 2044 Deita_Connection_Close(db_conn); | |
|
250
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
2045 return; |
| 201 | 2046 } |
|
202
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2047 Seobeo_Log(SEOBEO_INFO, "[MEDIA] Generated presigned URL: %.100s...\n", download_url.url); |
| 201 | 2048 |
| 2049 // Generate temp file paths | |
| 2050 char tmp_input[256]; | |
| 2051 char tmp_output[256]; | |
| 2052 char *uuid_input = malloc(UUID_LEN); | |
| 2053 char *uuid_output = malloc(UUID_LEN); | |
|
250
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
2054 uint32 seed1 = |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
2055 (uint32)time(NULL) ^ |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
2056 (uint32)Seobeo_Thread_Current_Id() ^ |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
2057 counter++; |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
2058 uint32 seed2 = |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
2059 (uint32)time(NULL) ^ |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
2060 (uint32)Seobeo_Thread_Current_Id() ^ |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
2061 counter++; |
| 201 | 2062 Dowa_String_UUID(seed1, uuid_input); |
| 2063 Dowa_String_UUID(seed2, uuid_output); | |
| 2064 snprintf(tmp_input, sizeof(tmp_input), "/tmp/%s", uuid_input); | |
| 2065 snprintf(tmp_output, sizeof(tmp_output), "/tmp/%s.webp", uuid_output); | |
| 2066 free(uuid_input); | |
| 2067 free(uuid_output); | |
| 2068 | |
| 2069 // Download from S3 | |
|
202
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2070 Seobeo_Log(SEOBEO_INFO, "[MEDIA] Downloading from S3 to %s for media_id=%lld\n", tmp_input, (long long)ctx->media_id); |
| 201 | 2071 Seobeo_Client_Request *download_req = Seobeo_Client_Request_Create(download_url.url); |
| 2072 Seobeo_Client_Request_Set_Download_Path(download_req, tmp_input); | |
| 2073 Seobeo_Client_Response *download_resp = Seobeo_Client_Request_Execute(download_req); | |
| 2074 | |
| 2075 S3_Presigned_URL_Destroy(&download_url); | |
| 2076 | |
| 2077 if (!download_resp || download_resp->status_code != 200) | |
| 2078 { | |
|
202
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2079 int status = download_resp ? download_resp->status_code : 0; |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2080 Seobeo_Log(SEOBEO_ERROR, "[MEDIA] ERROR: Failed to download from S3 for media_id=%lld (status=%d)\n", |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2081 (long long)ctx->media_id, status); |
| 201 | 2082 const char *update_error = |
| 2083 "UPDATE media_uploads SET status='error', error_message=?, updated_at=strftime('%s','now') WHERE id=?"; | |
| 2084 const char *error_params[] = { "Failed to download from S3", media_id_str }; | |
| 2085 Deita_Query_Execute_Update_Prepared(db_conn, update_error, 2, error_params); | |
| 2086 if (download_req) Seobeo_Client_Request_Destroy(download_req); | |
| 2087 if (download_resp) Seobeo_Client_Response_Destroy(download_resp); | |
| 2088 unlink(tmp_input); | |
| 2089 Deita_Connection_Close(db_conn); | |
|
250
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
2090 return; |
| 201 | 2091 } |
| 2092 | |
|
202
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2093 Seobeo_Log(SEOBEO_INFO, "[MEDIA] Successfully downloaded file to %s\n", tmp_input); |
| 201 | 2094 Seobeo_Client_Request_Destroy(download_req); |
| 2095 Seobeo_Client_Response_Destroy(download_resp); | |
| 2096 | |
| 2097 // Convert to webp using FFmpeg | |
| 2098 char cmd[1024]; | |
| 2099 char log_file[256]; | |
| 2100 snprintf(log_file, sizeof(log_file), "/tmp/ffmpeg_%lld.log", (long long)ctx->media_id); | |
| 2101 snprintf(cmd, sizeof(cmd), "ffmpeg -y -i %s -quality 80 %s 2>%s", | |
| 2102 tmp_input, tmp_output, log_file); | |
| 2103 | |
|
202
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2104 Seobeo_Log(SEOBEO_INFO, "[MEDIA] Running FFmpeg: %s\n", cmd); |
| 201 | 2105 int ffmpeg_result = system(cmd); |
|
202
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2106 Seobeo_Log(SEOBEO_INFO, "[MEDIA] FFmpeg result: %d for media_id=%lld\n", ffmpeg_result, (long long)ctx->media_id); |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2107 |
| 201 | 2108 if (ffmpeg_result != 0) |
| 2109 { | |
|
202
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2110 Seobeo_Log(SEOBEO_ERROR, "[MEDIA] ERROR: FFmpeg conversion failed for media_id=%lld (exit code %d). Check log: %s\n", |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2111 (long long)ctx->media_id, ffmpeg_result, log_file); |
| 201 | 2112 const char *update_error = |
| 2113 "UPDATE media_uploads SET status='error', error_message=?, updated_at=strftime('%s','now') WHERE id=?"; | |
| 2114 const char *error_params[] = { "Image conversion failed", media_id_str }; | |
| 2115 Deita_Query_Execute_Update_Prepared(db_conn, update_error, 2, error_params); | |
|
202
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2116 unlink(tmp_input); |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2117 unlink(tmp_output); |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2118 Deita_Connection_Close(db_conn); |
|
250
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
2119 return; |
|
202
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2120 } |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2121 Seobeo_Log(SEOBEO_INFO, "[MEDIA] Successfully converted to webp: %s\n", tmp_output); |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2122 |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2123 // Upload processed file to S3 |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2124 Seobeo_Log(SEOBEO_INFO, "[MEDIA] Uploading processed file to S3: %s -> %s\n", tmp_output, ctx->s3_key_processed); |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2125 S3_Result upload_result = S3_Upload_File_With_Content_Type( |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2126 &ctx->s3_config, tmp_output, ctx->s3_key_processed, "image/webp"); |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2127 |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2128 if (!upload_result.success) |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2129 { |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2130 const char *error_msg = upload_result.error_message ? upload_result.error_message : "Failed to upload processed file"; |
|
206
240337164a80
[Seobeo] SSL should be used for large file as well lol.
MrJuneJune <me@mrjunejune.com>
parents:
204
diff
changeset
|
2131 Seobeo_Log(SEOBEO_ERROR, "[MEDIA] ERROR: Failed to upload processed file for media_id=%lld: %s (HTTP status: %d)\n", |
|
240337164a80
[Seobeo] SSL should be used for large file as well lol.
MrJuneJune <me@mrjunejune.com>
parents:
204
diff
changeset
|
2132 (long long)ctx->media_id, error_msg, upload_result.status_code); |
|
202
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2133 const char *update_error = |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2134 "UPDATE media_uploads SET status='error', error_message=?, updated_at=strftime('%s','now') WHERE id=?"; |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2135 const char *error_params[] = { error_msg, media_id_str }; |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2136 Deita_Query_Execute_Update_Prepared(db_conn, update_error, 2, error_params); |
| 201 | 2137 unlink(tmp_input); |
| 2138 unlink(tmp_output); | |
| 2139 Deita_Connection_Close(db_conn); | |
|
250
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
2140 return; |
| 201 | 2141 } |
| 2142 | |
|
202
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2143 Seobeo_Log(SEOBEO_INFO, "[MEDIA] Successfully uploaded processed file to S3\n"); |
| 201 | 2144 |
| 2145 // Update status to 'finished' | |
| 2146 const char *update_finished = | |
| 2147 "UPDATE media_uploads SET status='finished', updated_at=strftime('%s','now') WHERE id=?"; | |
| 2148 Deita_Query_Execute_Update_Prepared(db_conn, update_finished, 1, params); | |
| 2149 | |
|
202
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2150 Seobeo_Log(SEOBEO_INFO, "[MEDIA] Successfully processed media_id=%lld - COMPLETE\n", (long long)ctx->media_id); |
| 201 | 2151 |
| 2152 // Cleanup | |
| 2153 unlink(tmp_input); | |
| 2154 unlink(tmp_output); | |
| 2155 Deita_Connection_Close(db_conn); | |
| 2156 } | |
| 2157 | |
| 2158 // Media Upload API - Mark uploaded | |
| 2159 // POST /api/media/:id/uploaded | |
| 2160 // Headers: Authorization: Bearer <token> | |
| 2161 Seobeo_Request_Entry *MediaUploaded(Seobeo_Request_Entry *req, Dowa_Arena *arena) | |
| 2162 { | |
| 2163 Seobeo_Request_Entry *resp = NULL; | |
| 2164 | |
| 2165 // Check auth token | |
| 2166 void *auth_kv = Dowa_HashMap_Get_Ptr(req, "Authorization"); | |
| 2167 if (!auth_kv) | |
| 2168 { | |
| 2169 Dowa_HashMap_Push_Arena(resp, "status", "401", arena); | |
| 2170 Dowa_HashMap_Push_Arena(resp, "content-type", "application/json", arena); | |
| 2171 Dowa_HashMap_Push_Arena(resp, "body", "{\"error\":\"Missing Authorization header\"}", arena); | |
| 2172 return resp; | |
| 2173 } | |
| 2174 | |
| 2175 const char *auth_header = ((Seobeo_Request_Entry*)auth_kv)->value; | |
| 2176 if (strncmp(auth_header, "Bearer ", 7) != 0) | |
| 2177 { | |
| 2178 Dowa_HashMap_Push_Arena(resp, "status", "401", arena); | |
| 2179 Dowa_HashMap_Push_Arena(resp, "content-type", "application/json", arena); | |
| 2180 Dowa_HashMap_Push_Arena(resp, "body", "{\"error\":\"Invalid Authorization format\"}", arena); | |
| 2181 return resp; | |
| 2182 } | |
| 2183 | |
| 2184 const char *token = auth_header + 7; | |
| 2185 if (strlen(g_upload_auth_token) == 0 || strcmp(token, g_upload_auth_token) != 0) | |
| 2186 { | |
| 2187 Dowa_HashMap_Push_Arena(resp, "status", "403", arena); | |
| 2188 Dowa_HashMap_Push_Arena(resp, "content-type", "application/json", arena); | |
| 2189 Dowa_HashMap_Push_Arena(resp, "body", "{\"error\":\"Invalid token\"}", arena); | |
| 2190 return resp; | |
| 2191 } | |
| 2192 | |
| 2193 if (!g_db_connection) | |
| 2194 { | |
| 2195 Dowa_HashMap_Push_Arena(resp, "status", "500", arena); | |
| 2196 Dowa_HashMap_Push_Arena(resp, "content-type", "application/json", arena); | |
| 2197 Dowa_HashMap_Push_Arena(resp, "body", "{\"error\":\"Database not available\"}", arena); | |
| 2198 return resp; | |
| 2199 } | |
| 2200 | |
| 2201 // Extract media_id from URL params | |
| 2202 void *id_kv = Dowa_HashMap_Get_Ptr(req, ":id"); | |
| 2203 if (!id_kv) | |
| 2204 { | |
| 2205 Dowa_HashMap_Push_Arena(resp, "status", "400", arena); | |
| 2206 Dowa_HashMap_Push_Arena(resp, "content-type", "application/json", arena); | |
| 2207 Dowa_HashMap_Push_Arena(resp, "body", "{\"error\":\"Missing media ID\"}", arena); | |
| 2208 return resp; | |
| 2209 } | |
| 2210 | |
| 2211 const char *media_id_str = ((Seobeo_Request_Entry*)id_kv)->value; | |
| 2212 int64 media_id = atoll(media_id_str); | |
| 2213 | |
| 2214 // Verify access_token matches and get content_type | |
| 2215 const char *select_query = | |
| 2216 "SELECT content_type, s3_key_original, s3_key_processed FROM media_uploads WHERE id = ? AND access_token = ?"; | |
| 2217 const char *select_params[] = { media_id_str, token }; | |
| 2218 | |
| 2219 Deita_Result_Set *p_result = Deita_Query_Execute_Prepared(g_db_connection, select_query, 2, select_params, arena); | |
| 2220 | |
| 2221 if (!p_result || !Deita_Result_Set_Next(p_result)) | |
| 2222 { | |
| 2223 if (p_result) Deita_Result_Set_Free(p_result); | |
| 2224 Dowa_HashMap_Push_Arena(resp, "status", "404", arena); | |
| 2225 Dowa_HashMap_Push_Arena(resp, "content-type", "application/json", arena); | |
| 2226 Dowa_HashMap_Push_Arena(resp, "body", "{\"error\":\"Media not found or access denied\"}", arena); | |
| 2227 return resp; | |
| 2228 } | |
| 2229 | |
| 2230 const char *content_type = Deita_Result_Set_Get_Text(p_result, 0); | |
| 2231 const char *s3_key_original = Deita_Result_Set_Get_Text(p_result, 1); | |
| 2232 const char *s3_key_processed = Deita_Result_Set_Get_Text(p_result, 2); | |
| 2233 | |
| 2234 // Copy values before freeing result set | |
| 2235 char content_type_copy[128]; | |
| 2236 char s3_key_original_copy[512]; | |
| 2237 char s3_key_processed_copy[512]; | |
| 2238 strncpy(content_type_copy, content_type, sizeof(content_type_copy) - 1); | |
| 2239 strncpy(s3_key_original_copy, s3_key_original, sizeof(s3_key_original_copy) - 1); | |
| 2240 strncpy(s3_key_processed_copy, s3_key_processed, sizeof(s3_key_processed_copy) - 1); | |
| 2241 content_type_copy[sizeof(content_type_copy) - 1] = '\0'; | |
| 2242 s3_key_original_copy[sizeof(s3_key_original_copy) - 1] = '\0'; | |
| 2243 s3_key_processed_copy[sizeof(s3_key_processed_copy) - 1] = '\0'; | |
| 2244 | |
| 2245 Deita_Result_Set_Free(p_result); | |
| 2246 | |
| 2247 // Update status to 'uploaded' | |
| 2248 const char *update_query = | |
| 2249 "UPDATE media_uploads SET status='uploaded', updated_at=strftime('%s','now') WHERE id=?"; | |
| 2250 const char *update_params[] = { media_id_str }; | |
| 2251 int32 result = Deita_Query_Execute_Update_Prepared(g_db_connection, update_query, 1, update_params); | |
| 2252 | |
| 2253 if (result < 0) | |
| 2254 { | |
| 2255 Dowa_HashMap_Push_Arena(resp, "status", "500", arena); | |
| 2256 Dowa_HashMap_Push_Arena(resp, "content-type", "application/json", arena); | |
| 2257 Dowa_HashMap_Push_Arena(resp, "body", "{\"error\":\"Failed to update status\"}", arena); | |
| 2258 return resp; | |
| 2259 } | |
| 2260 | |
|
202
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2261 Seobeo_Log(SEOBEO_INFO, "[MEDIA] Content type for media_id=%lld: '%s'\n", (long long)media_id, content_type_copy); |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2262 |
|
250
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
2263 // Images are processed asynchronously by the bounded media pool. |
| 201 | 2264 if (strncmp(content_type_copy, "image/", 6) == 0) |
| 2265 { | |
|
250
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
2266 Seobeo_Log(SEOBEO_INFO, "[MEDIA] Queueing image processing for media_id=%lld\n", (long long)media_id); |
|
202
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2267 |
|
250
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
2268 // The pool owns this context after a successful submission. |
| 201 | 2269 Media_Processing_Context *ctx = malloc(sizeof(Media_Processing_Context)); |
|
250
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
2270 if (!ctx) |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
2271 { |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
2272 Dowa_HashMap_Push_Arena(resp, "status", "500", arena); |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
2273 Dowa_HashMap_Push_Arena(resp, "content-type", "application/json", arena); |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
2274 Dowa_HashMap_Push_Arena(resp, "body", "{\"error\":\"Unable to allocate media work\"}", arena); |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
2275 return resp; |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
2276 } |
| 201 | 2277 ctx->media_id = media_id; |
| 2278 strncpy(ctx->s3_key_original, s3_key_original_copy, sizeof(ctx->s3_key_original) - 1); | |
| 2279 strncpy(ctx->s3_key_processed, s3_key_processed_copy, sizeof(ctx->s3_key_processed) - 1); | |
| 2280 strncpy(ctx->content_type, content_type_copy, sizeof(ctx->content_type) - 1); | |
| 2281 strncpy(ctx->access_token, token, sizeof(ctx->access_token) - 1); | |
|
202
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2282 strncpy(ctx->db_path, g_db_path, sizeof(ctx->db_path) - 1); |
| 201 | 2283 ctx->s3_key_original[sizeof(ctx->s3_key_original) - 1] = '\0'; |
| 2284 ctx->s3_key_processed[sizeof(ctx->s3_key_processed) - 1] = '\0'; | |
| 2285 ctx->content_type[sizeof(ctx->content_type) - 1] = '\0'; | |
| 2286 ctx->access_token[sizeof(ctx->access_token) - 1] = '\0'; | |
|
202
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2287 ctx->db_path[sizeof(ctx->db_path) - 1] = '\0'; |
| 201 | 2288 ctx->s3_config = g_s3_config; |
| 2289 | |
|
250
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
2290 Seobeo_Worker_Result worker_result = |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
2291 g_media_worker_pool |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
2292 ? Seobeo_Worker_Pool_Submit( |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
2293 g_media_worker_pool, |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
2294 Media_Process_Background, |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
2295 ctx, |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
2296 free) |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
2297 : SEOBEO_WORKER_STOPPED; |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
2298 if (worker_result != SEOBEO_WORKER_OK) |
| 201 | 2299 { |
|
250
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
2300 Seobeo_Log( |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
2301 SEOBEO_ERROR, |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
2302 "[MEDIA] Worker submission failed with result=%d for media_id=%lld\n", |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
2303 worker_result, |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
2304 (long long)media_id); |
| 201 | 2305 free(ctx); |
|
250
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
2306 const char *update_error = |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
2307 "UPDATE media_uploads SET status='error', error_message=?, updated_at=strftime('%s','now') WHERE id=?"; |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
2308 const char *error_params[] = { |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
2309 "Media worker queue is unavailable", |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
2310 media_id_str, |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
2311 }; |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
2312 Deita_Query_Execute_Update_Prepared( |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
2313 g_db_connection, |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
2314 update_error, |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
2315 2, |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
2316 error_params); |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
2317 Dowa_HashMap_Push_Arena(resp, "status", "503", arena); |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
2318 Dowa_HashMap_Push_Arena(resp, "content-type", "application/json", arena); |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
2319 Dowa_HashMap_Push_Arena(resp, "body", "{\"error\":\"Media worker queue is unavailable\"}", arena); |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
2320 return resp; |
| 201 | 2321 } |
|
250
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
2322 Seobeo_Log( |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
2323 SEOBEO_INFO, |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
2324 "[MEDIA] Submitted media_id=%lld to the worker pool\n", |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
2325 (long long)media_id); |
| 201 | 2326 } |
|
202
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2327 else |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2328 { |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2329 Seobeo_Log(SEOBEO_INFO, "[MEDIA] Non-image file, skipping background processing for media_id=%lld\n", (long long)media_id); |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2330 } |
| 201 | 2331 |
| 2332 Dowa_HashMap_Push_Arena(resp, "status", "200", arena); | |
| 2333 Dowa_HashMap_Push_Arena(resp, "content-type", "application/json", arena); | |
| 2334 Dowa_HashMap_Push_Arena(resp, "body", "{\"success\":true,\"status\":\"uploaded\"}", arena); | |
| 2335 | |
| 2336 Seobeo_Log(SEOBEO_INFO, "[MEDIA] Marked uploaded media_id=%lld\n", (long long)media_id); | |
| 2337 | |
| 2338 return resp; | |
| 2339 } | |
| 2340 | |
| 2341 // Media Upload API - Get status | |
| 2342 // GET /api/media/:id/status | |
| 2343 // Headers: Authorization: Bearer <token> | |
| 2344 // Returns: {"id": 123, "status": "finished", "processed_url": "https://...", "error_message": null} | |
| 2345 Seobeo_Request_Entry *MediaStatus(Seobeo_Request_Entry *req, Dowa_Arena *arena) | |
| 2346 { | |
| 2347 Seobeo_Request_Entry *resp = NULL; | |
| 2348 | |
| 2349 // Check auth token | |
| 2350 void *auth_kv = Dowa_HashMap_Get_Ptr(req, "Authorization"); | |
| 2351 if (!auth_kv) | |
| 2352 { | |
| 2353 Dowa_HashMap_Push_Arena(resp, "status", "401", arena); | |
| 2354 Dowa_HashMap_Push_Arena(resp, "content-type", "application/json", arena); | |
| 2355 Dowa_HashMap_Push_Arena(resp, "body", "{\"error\":\"Missing Authorization header\"}", arena); | |
| 2356 return resp; | |
| 2357 } | |
| 2358 | |
| 2359 const char *auth_header = ((Seobeo_Request_Entry*)auth_kv)->value; | |
| 2360 if (strncmp(auth_header, "Bearer ", 7) != 0) | |
| 2361 { | |
| 2362 Dowa_HashMap_Push_Arena(resp, "status", "401", arena); | |
| 2363 Dowa_HashMap_Push_Arena(resp, "content-type", "application/json", arena); | |
| 2364 Dowa_HashMap_Push_Arena(resp, "body", "{\"error\":\"Invalid Authorization format\"}", arena); | |
| 2365 return resp; | |
| 2366 } | |
| 2367 | |
| 2368 const char *token = auth_header + 7; | |
| 2369 if (strlen(g_upload_auth_token) == 0 || strcmp(token, g_upload_auth_token) != 0) | |
| 2370 { | |
| 2371 Dowa_HashMap_Push_Arena(resp, "status", "403", arena); | |
| 2372 Dowa_HashMap_Push_Arena(resp, "content-type", "application/json", arena); | |
| 2373 Dowa_HashMap_Push_Arena(resp, "body", "{\"error\":\"Invalid token\"}", arena); | |
| 2374 return resp; | |
| 2375 } | |
| 2376 | |
| 2377 if (!g_db_connection) | |
| 2378 { | |
| 2379 Dowa_HashMap_Push_Arena(resp, "status", "500", arena); | |
| 2380 Dowa_HashMap_Push_Arena(resp, "content-type", "application/json", arena); | |
| 2381 Dowa_HashMap_Push_Arena(resp, "body", "{\"error\":\"Database not available\"}", arena); | |
| 2382 return resp; | |
| 2383 } | |
| 2384 | |
| 2385 // Extract media_id from URL params | |
| 2386 void *id_kv = Dowa_HashMap_Get_Ptr(req, ":id"); | |
| 2387 if (!id_kv) | |
| 2388 { | |
| 2389 Dowa_HashMap_Push_Arena(resp, "status", "400", arena); | |
| 2390 Dowa_HashMap_Push_Arena(resp, "content-type", "application/json", arena); | |
| 2391 Dowa_HashMap_Push_Arena(resp, "body", "{\"error\":\"Missing media ID\"}", arena); | |
| 2392 return resp; | |
| 2393 } | |
| 2394 | |
| 2395 const char *media_id_str = ((Seobeo_Request_Entry*)id_kv)->value; | |
| 2396 | |
| 2397 // Query media status | |
| 2398 const char *select_query = | |
|
202
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2399 "SELECT id, status, s3_key_original, s3_key_processed, error_message FROM media_uploads WHERE id = ? AND access_token = ?"; |
| 201 | 2400 const char *select_params[] = { media_id_str, token }; |
| 2401 | |
| 2402 Deita_Result_Set *p_result = Deita_Query_Execute_Prepared(g_db_connection, select_query, 2, select_params, arena); | |
| 2403 | |
| 2404 if (!p_result || !Deita_Result_Set_Next(p_result)) | |
| 2405 { | |
| 2406 if (p_result) Deita_Result_Set_Free(p_result); | |
| 2407 Dowa_HashMap_Push_Arena(resp, "status", "404", arena); | |
| 2408 Dowa_HashMap_Push_Arena(resp, "content-type", "application/json", arena); | |
| 2409 Dowa_HashMap_Push_Arena(resp, "body", "{\"error\":\"Media not found\"}", arena); | |
| 2410 return resp; | |
| 2411 } | |
| 2412 | |
| 2413 int64 id = Deita_Result_Set_Get_Integer(p_result, 0); | |
| 2414 const char *status = Deita_Result_Set_Get_Text(p_result, 1); | |
|
202
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2415 const char *s3_key_original = Deita_Result_Set_Get_Text(p_result, 2); |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2416 const char *s3_key_processed = Deita_Result_Set_Get_Text(p_result, 3); |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2417 const char *error_message = Deita_Result_Set_Get_Text(p_result, 4); |
| 201 | 2418 |
|
202
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2419 // Build CloudFront URL for processed file if status is 'finished' |
| 201 | 2420 char processed_url[1024] = {0}; |
| 2421 if (strcmp(status, "finished") == 0 && s3_key_processed && strlen(s3_key_processed) > 0) | |
| 2422 { | |
| 2423 if (g_s3_cloudfront_url[0]) | |
| 2424 { | |
| 2425 snprintf(processed_url, sizeof(processed_url), "%s/%s", g_s3_cloudfront_url, s3_key_processed); | |
| 2426 } | |
| 2427 else | |
| 2428 { | |
| 2429 snprintf(processed_url, sizeof(processed_url), "https://%s.s3.%s.amazonaws.com/%s", | |
| 2430 g_s3_bucket, g_s3_region, s3_key_processed); | |
| 2431 } | |
| 2432 } | |
| 2433 | |
|
202
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2434 // Build CloudFront URL for original file (for non-images or before processing completes) |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2435 char original_url[1024] = {0}; |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2436 if (s3_key_original && strlen(s3_key_original) > 0) |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2437 { |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2438 if (g_s3_cloudfront_url[0]) |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2439 { |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2440 snprintf(original_url, sizeof(original_url), "%s/%s", g_s3_cloudfront_url, s3_key_original); |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2441 } |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2442 else |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2443 { |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2444 snprintf(original_url, sizeof(original_url), "https://%s.s3.%s.amazonaws.com/%s", |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2445 g_s3_bucket, g_s3_region, s3_key_original); |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2446 } |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2447 } |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2448 |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2449 // Build JSON response with both processed_url and original_url |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2450 char *response_body = Dowa_Arena_Allocate(arena, 3072); |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2451 |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2452 // Build the base response |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2453 int offset = snprintf(response_body, 3072, |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2454 "{\"id\":%lld,\"status\":\"%s\",", |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2455 (long long)id, status); |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2456 |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2457 // Add processed_url |
| 201 | 2458 if (strlen(processed_url) > 0) |
| 2459 { | |
|
202
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2460 offset += snprintf(response_body + offset, 3072 - offset, |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2461 "\"processed_url\":\"%s\",", processed_url); |
| 201 | 2462 } |
| 2463 else | |
| 2464 { | |
|
202
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2465 offset += snprintf(response_body + offset, 3072 - offset, |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2466 "\"processed_url\":null,"); |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2467 } |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2468 |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2469 // Add original_url |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2470 if (strlen(original_url) > 0) |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2471 { |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2472 offset += snprintf(response_body + offset, 3072 - offset, |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2473 "\"original_url\":\"%s\",", original_url); |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2474 } |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2475 else |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2476 { |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2477 offset += snprintf(response_body + offset, 3072 - offset, |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2478 "\"original_url\":null,"); |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2479 } |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2480 |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2481 // Add error_message |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2482 if (error_message && strlen(error_message) > 0) |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2483 { |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2484 snprintf(response_body + offset, 3072 - offset, |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2485 "\"error_message\":\"%s\"}", error_message); |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2486 } |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2487 else |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2488 { |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2489 snprintf(response_body + offset, 3072 - offset, |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2490 "\"error_message\":null}"); |
| 201 | 2491 } |
| 2492 | |
| 2493 Deita_Result_Set_Free(p_result); | |
| 2494 | |
| 2495 Dowa_HashMap_Push_Arena(resp, "status", "200", arena); | |
| 2496 Dowa_HashMap_Push_Arena(resp, "content-type", "application/json", arena); | |
| 2497 Dowa_HashMap_Push_Arena(resp, "body", response_body, arena); | |
| 2498 | |
| 2499 return resp; | |
| 2500 } | |
| 2501 | |
|
7
114cad94008f
[Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
2502 int main(void) |
|
114cad94008f
[Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
2503 { |
|
260
1f9877b637e9
Add Copilot-powered cyberpunk JRPG chat
MrJuneJune <mrjunejune@users.noreply.github.com>
parents:
259
diff
changeset
|
2504 signal(SIGINT, handle_sigint); |
|
1f9877b637e9
Add Copilot-powered cyberpunk JRPG chat
MrJuneJune <mrjunejune@users.noreply.github.com>
parents:
259
diff
changeset
|
2505 signal(SIGTERM, handle_sigint); |
|
1f9877b637e9
Add Copilot-powered cyberpunk JRPG chat
MrJuneJune <mrjunejune@users.noreply.github.com>
parents:
259
diff
changeset
|
2506 |
|
264
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2507 // Load the ignored runtime config when present; environment overrides follow. |
|
265
056790c4fb0d
add role-aware Epi assistant prompts
MrJuneJune <me@mrjunejune.com>
parents:
264
diff
changeset
|
2508 const char *config_path = getenv("MRJUNEJUNE_CONFIG_PATH"); |
|
056790c4fb0d
add role-aware Epi assistant prompts
MrJuneJune <me@mrjunejune.com>
parents:
264
diff
changeset
|
2509 load_config( |
|
056790c4fb0d
add role-aware Epi assistant prompts
MrJuneJune <me@mrjunejune.com>
parents:
264
diff
changeset
|
2510 config_path && config_path[0] != '\0' |
|
056790c4fb0d
add role-aware Epi assistant prompts
MrJuneJune <me@mrjunejune.com>
parents:
264
diff
changeset
|
2511 ? config_path |
|
056790c4fb0d
add role-aware Epi assistant prompts
MrJuneJune <me@mrjunejune.com>
parents:
264
diff
changeset
|
2512 : "mrjunejune/.config"); |
|
200
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
2513 |
|
264
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2514 // Initialize S3 config using global credentials populated by load_config |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2515 g_s3_config.access_key_id = g_s3_access_key; |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2516 g_s3_config.secret_access_key = g_s3_secret_key; |
|
200
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
2517 g_s3_config.region = g_s3_region; |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
2518 g_s3_config.bucket = g_s3_bucket; |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
2519 g_s3_config.endpoint = NULL; |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
2520 g_s3_config.use_path_style = FALSE; |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
2521 |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
2522 printf("[S3] Configured: region=%s, bucket=%s, key=%s...\n", |
|
264
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2523 g_s3_region, g_s3_bucket, g_s3_access_key[0] ? "***" : "(missing)"); |
|
200
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
2524 |
|
202
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2525 // Show current working directory |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2526 char cwd[1024]; |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2527 if (getcwd(cwd, sizeof(cwd)) != NULL) |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2528 { |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2529 printf("[STARTUP] Current working directory: %s\n", cwd); |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2530 printf("[STARTUP] Database path (relative): %s\n", g_db_path); |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2531 } |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2532 |
| 201 | 2533 // Initialize database |
| 2534 init_database(); | |
|
264
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2535 { |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2536 /* Validate per-request token cap against daily limit now that both |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2537 * are finalised (env overrides run inside load_config). */ |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2538 if (g_guest_request_output_tokens > g_guest_daily_output_tokens) |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2539 g_guest_request_output_tokens = g_guest_daily_output_tokens; |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2540 |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2541 Conversation_API_Guest_Policy policy = { |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2542 .guest_inference_enabled = g_guest_inference_enabled, |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2543 .daily_turns = g_guest_daily_turns, |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2544 .daily_output_tokens = g_guest_daily_output_tokens, |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2545 .request_output_tokens = g_guest_request_output_tokens, |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2546 }; |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2547 if (!Conversation_API_Init(g_db_path, &policy)) |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2548 Seobeo_Log(SEOBEO_ERROR, "[CONVERSATION] Store unavailable\n"); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2549 } |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2550 |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2551 /* Validate and decode AUTH_COOKIE_SECRET: hex string → raw bytes. */ |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2552 uint8 cookie_secret_bytes[AUTH_CRYPTO_COOKIE_SECRET_MAX_BYTES]; |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2553 size_t cookie_secret_byte_len = 0; |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2554 { |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2555 size_t hex_len = strlen(g_auth_cookie_secret); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2556 boolean bad_len = ( |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2557 hex_len < (size_t)(AUTH_CRYPTO_COOKIE_SECRET_MIN_BYTES * 2) || |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2558 hex_len > (size_t)(AUTH_CRYPTO_COOKIE_SECRET_MAX_BYTES * 2) || |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2559 hex_len % 2 != 0); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2560 if (bad_len) |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2561 { |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2562 OPENSSL_cleanse(g_auth_cookie_secret, sizeof(g_auth_cookie_secret)); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2563 fprintf(stderr, |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2564 "[AUTH] AUTH_COOKIE_SECRET must be %d–%d hex chars " |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2565 "(%d–%d random bytes). " |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2566 "Generate with: openssl rand -hex 32\n", |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2567 AUTH_CRYPTO_COOKIE_SECRET_MIN_BYTES * 2, |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2568 AUTH_CRYPTO_COOKIE_SECRET_MAX_BYTES * 2, |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2569 AUTH_CRYPTO_COOKIE_SECRET_MIN_BYTES, |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2570 AUTH_CRYPTO_COOKIE_SECRET_MAX_BYTES); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2571 exit(1); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2572 } |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2573 cookie_secret_byte_len = config__hex_decode( |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2574 g_auth_cookie_secret, cookie_secret_bytes, sizeof(cookie_secret_bytes)); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2575 OPENSSL_cleanse(g_auth_cookie_secret, sizeof(g_auth_cookie_secret)); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2576 if (cookie_secret_byte_len < (size_t)AUTH_CRYPTO_COOKIE_SECRET_MIN_BYTES) |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2577 { |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2578 OPENSSL_cleanse(cookie_secret_bytes, sizeof(cookie_secret_bytes)); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2579 fprintf(stderr, |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2580 "[AUTH] AUTH_COOKIE_SECRET contains non-hex characters or " |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2581 "is too short.\n"); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2582 exit(1); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2583 } |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2584 } |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2585 |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2586 /* Bootstrap pairing: both username+hash must be set, or both absent. */ |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2587 { |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2588 boolean has_user = g_auth_bootstrap_username[0] != '\0'; |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2589 boolean has_hash = g_auth_bootstrap_password_hash[0] != '\0'; |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2590 if (has_user != has_hash) |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2591 { |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2592 OPENSSL_cleanse(cookie_secret_bytes, sizeof(cookie_secret_bytes)); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2593 fprintf(stderr, |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2594 "[AUTH] AUTH_BOOTSTRAP_USERNAME and AUTH_BOOTSTRAP_PASSWORD_HASH " |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2595 "must both be set or both absent.\n"); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2596 exit(1); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2597 } |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2598 if (has_hash && |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2599 Auth_Crypto_Password_Hash_Validate(g_auth_bootstrap_password_hash) != AUTH_CRYPTO_OK) |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2600 { |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2601 OPENSSL_cleanse(cookie_secret_bytes, sizeof(cookie_secret_bytes)); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2602 fprintf(stderr, |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2603 "[AUTH] AUTH_BOOTSTRAP_PASSWORD_HASH has unrecognized format " |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2604 "(expected zenbu-scrypt$v=1$...).\n"); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2605 exit(1); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2606 } |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2607 } |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2608 |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2609 /* TTL validation: all positive, idle <= abs, all within 1 min – 1 year. */ |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2610 #define CONFIG_TTL_MIN_SECS 60 |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2611 #define CONFIG_TTL_MAX_SECS 31536000 |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2612 { |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2613 int64 idle = g_auth_session_idle_ttl; |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2614 int64 abst = g_auth_session_abs_ttl; |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2615 int64 guest = g_auth_guest_ttl; |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2616 if (idle <= 0 || abst <= 0 || guest <= 0 || |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2617 idle < CONFIG_TTL_MIN_SECS || idle > CONFIG_TTL_MAX_SECS || |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2618 abst < CONFIG_TTL_MIN_SECS || abst > CONFIG_TTL_MAX_SECS || |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2619 guest < CONFIG_TTL_MIN_SECS || guest > CONFIG_TTL_MAX_SECS) |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2620 { |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2621 OPENSSL_cleanse(cookie_secret_bytes, sizeof(cookie_secret_bytes)); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2622 fprintf(stderr, |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2623 "[AUTH] TTL values must be %d–%d seconds.\n", |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2624 CONFIG_TTL_MIN_SECS, CONFIG_TTL_MAX_SECS); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2625 exit(1); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2626 } |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2627 if (idle > abst) |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2628 { |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2629 OPENSSL_cleanse(cookie_secret_bytes, sizeof(cookie_secret_bytes)); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2630 fprintf(stderr, |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2631 "[AUTH] AUTH_SESSION_IDLE_TTL must not exceed " |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2632 "AUTH_SESSION_ABS_TTL.\n"); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2633 exit(1); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2634 } |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2635 } |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2636 |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2637 /* Trusted proxy: validate and canonicalize via inet_pton/inet_ntop. */ |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2638 if (g_auth_trusted_proxy[0] != '\0') |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2639 { |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2640 struct in_addr addr4; |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2641 struct in6_addr addr6; |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2642 char canonical[AUTH_CRYPTO_IP_MAX_BYTES]; |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2643 canonical[0] = '\0'; |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2644 if (inet_pton(AF_INET, g_auth_trusted_proxy, &addr4) == 1) |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2645 { |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2646 if (!inet_ntop(AF_INET, &addr4, canonical, sizeof(canonical))) |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2647 { |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2648 OPENSSL_cleanse(cookie_secret_bytes, sizeof(cookie_secret_bytes)); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2649 fprintf(stderr, |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2650 "[AUTH] AUTH_TRUSTED_PROXY: IPv4 canonicalization failed.\n"); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2651 exit(1); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2652 } |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2653 } |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2654 else if (inet_pton(AF_INET6, g_auth_trusted_proxy, &addr6) == 1) |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2655 { |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2656 if (!inet_ntop(AF_INET6, &addr6, canonical, sizeof(canonical))) |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2657 { |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2658 OPENSSL_cleanse(cookie_secret_bytes, sizeof(cookie_secret_bytes)); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2659 fprintf(stderr, |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2660 "[AUTH] AUTH_TRUSTED_PROXY: IPv6 canonicalization failed.\n"); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2661 exit(1); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2662 } |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2663 } |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2664 else |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2665 { |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2666 OPENSSL_cleanse(cookie_secret_bytes, sizeof(cookie_secret_bytes)); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2667 fprintf(stderr, |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2668 "[AUTH] AUTH_TRUSTED_PROXY must be a valid IPv4 or IPv6 " |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2669 "address (e.g. 192.168.1.1 or ::1).\n"); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2670 exit(1); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2671 } |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2672 strncpy(g_auth_trusted_proxy, canonical, sizeof(g_auth_trusted_proxy) - 1); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2673 g_auth_trusted_proxy[sizeof(g_auth_trusted_proxy) - 1] = '\0'; |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2674 } |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2675 |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2676 /* |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2677 * Loopback enforcement: AUTH_DEV_INSECURE_COOKIE=true is only permitted |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2678 * when SERVER_HOST is explicitly a loopback address. Production/edge |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2679 * deployments must use Secure cookies. |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2680 */ |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2681 { |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2682 boolean is_loopback = ( |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2683 strcmp(g_server_host, "127.0.0.1") == 0 || |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2684 strcmp(g_server_host, "::1") == 0 || |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2685 strcmp(g_server_host, "localhost") == 0); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2686 if (g_auth_dev_insecure && !is_loopback) |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2687 { |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2688 OPENSSL_cleanse(cookie_secret_bytes, sizeof(cookie_secret_bytes)); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2689 fprintf(stderr, |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2690 "[AUTH] AUTH_DEV_INSECURE_COOKIE=true requires SERVER_HOST " |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2691 "to be a loopback address (127.0.0.1, ::1, or localhost). " |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2692 "Set SERVER_HOST=127.0.0.1 for local development.\n"); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2693 exit(1); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2694 } |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2695 } |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2696 |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2697 /* Initialize auth module — fail closed: no auth means no server. */ |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2698 { |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2699 if (!Auth_API_Init( |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2700 g_db_path, |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2701 cookie_secret_bytes, cookie_secret_byte_len, |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2702 g_auth_bootstrap_username[0] ? g_auth_bootstrap_username : NULL, |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2703 g_auth_bootstrap_password_hash[0] ? g_auth_bootstrap_password_hash : NULL, |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2704 g_auth_trusted_proxy[0] ? g_auth_trusted_proxy : NULL, |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2705 g_auth_session_idle_ttl, |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2706 g_auth_session_abs_ttl, |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2707 g_auth_guest_ttl, |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2708 g_auth_dev_insecure)) |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2709 { |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2710 OPENSSL_cleanse(cookie_secret_bytes, sizeof(cookie_secret_bytes)); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2711 fprintf(stderr, |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2712 "[AUTH] Auth init failed — refusing to start server.\n"); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2713 exit(1); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2714 } |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2715 OPENSSL_cleanse(cookie_secret_bytes, sizeof(cookie_secret_bytes)); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2716 } |
|
260
1f9877b637e9
Add Copilot-powered cyberpunk JRPG chat
MrJuneJune <mrjunejune@users.noreply.github.com>
parents:
259
diff
changeset
|
2717 const char *sidecar_path = getenv("MRJUNEJUNE_INFERENCE_SIDECAR_PATH"); |
|
1f9877b637e9
Add Copilot-powered cyberpunk JRPG chat
MrJuneJune <mrjunejune@users.noreply.github.com>
parents:
259
diff
changeset
|
2718 const char *copilot_cli_path = getenv("MRJUNEJUNE_COPILOT_CLI_PATH"); |
|
1f9877b637e9
Add Copilot-powered cyberpunk JRPG chat
MrJuneJune <mrjunejune@users.noreply.github.com>
parents:
259
diff
changeset
|
2719 if (sidecar_path && copilot_cli_path) |
|
1f9877b637e9
Add Copilot-powered cyberpunk JRPG chat
MrJuneJune <mrjunejune@users.noreply.github.com>
parents:
259
diff
changeset
|
2720 { |
|
1f9877b637e9
Add Copilot-powered cyberpunk JRPG chat
MrJuneJune <mrjunejune@users.noreply.github.com>
parents:
259
diff
changeset
|
2721 if (!Conversation_API_Enable_Inference(sidecar_path, copilot_cli_path)) |
|
1f9877b637e9
Add Copilot-powered cyberpunk JRPG chat
MrJuneJune <mrjunejune@users.noreply.github.com>
parents:
259
diff
changeset
|
2722 Seobeo_Log(SEOBEO_ERROR, "[INFERENCE] Sidecar unavailable\n"); |
|
1f9877b637e9
Add Copilot-powered cyberpunk JRPG chat
MrJuneJune <mrjunejune@users.noreply.github.com>
parents:
259
diff
changeset
|
2723 } |
|
1f9877b637e9
Add Copilot-powered cyberpunk JRPG chat
MrJuneJune <mrjunejune@users.noreply.github.com>
parents:
259
diff
changeset
|
2724 else |
|
1f9877b637e9
Add Copilot-powered cyberpunk JRPG chat
MrJuneJune <mrjunejune@users.noreply.github.com>
parents:
259
diff
changeset
|
2725 { |
|
1f9877b637e9
Add Copilot-powered cyberpunk JRPG chat
MrJuneJune <mrjunejune@users.noreply.github.com>
parents:
259
diff
changeset
|
2726 Seobeo_Log( |
|
1f9877b637e9
Add Copilot-powered cyberpunk JRPG chat
MrJuneJune <mrjunejune@users.noreply.github.com>
parents:
259
diff
changeset
|
2727 SEOBEO_WARNING, |
|
1f9877b637e9
Add Copilot-powered cyberpunk JRPG chat
MrJuneJune <mrjunejune@users.noreply.github.com>
parents:
259
diff
changeset
|
2728 "[INFERENCE] Runtime paths not configured; turns return 503\n"); |
|
1f9877b637e9
Add Copilot-powered cyberpunk JRPG chat
MrJuneJune <mrjunejune@users.noreply.github.com>
parents:
259
diff
changeset
|
2729 } |
| 201 | 2730 |
|
250
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
2731 g_media_worker_pool = Seobeo_Worker_Pool_Create(2, 16); |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
2732 if (!g_media_worker_pool) |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
2733 { |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
2734 Seobeo_Log( |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
2735 SEOBEO_ERROR, |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
2736 "[MEDIA] Unable to initialize the media worker pool\n"); |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
2737 } |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
2738 |
|
72
4532ce6d9eb8
[Seobeo] Added router to the server logic. Few dowa string manipulation logics.
June Park <parkjune1995@gmail.com>
parents:
36
diff
changeset
|
2739 Seobeo_Router_Init(); |
|
264
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2740 Auth_API_Register_Routes(); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2741 Admin_API_Register_Routes(); |
|
260
1f9877b637e9
Add Copilot-powered cyberpunk JRPG chat
MrJuneJune <mrjunejune@users.noreply.github.com>
parents:
259
diff
changeset
|
2742 Conversation_API_Register_Routes(); |
|
125
f236c895604e
[MrJuneJune] Added web socket for chat to this.
June Park <parkjune1995@gmail.com>
parents:
124
diff
changeset
|
2743 |
|
77
c348ac875294
[Seobeo] Server side rendering.
June Park <parkjune1995@gmail.com>
parents:
72
diff
changeset
|
2744 Seobeo_Router_Register("GET", "/", GetHomePage); |
|
79
5710108c949e
[Seobeo] Added Redirect logic.
June Park <parkjune1995@gmail.com>
parents:
78
diff
changeset
|
2745 Seobeo_Router_Register("GET", "/index.html", GetRedirectHomePage); |
| 78 | 2746 |
| 2747 Seobeo_Router_Register("GET", "/resume", GetResume); | |
|
79
5710108c949e
[Seobeo] Added Redirect logic.
June Park <parkjune1995@gmail.com>
parents:
78
diff
changeset
|
2748 Seobeo_Router_Register("GET", "/resume/index.html", GetRedirectResume); |
| 78 | 2749 |
| 2750 Seobeo_Router_Register("GET", "/tools", GetTools); | |
|
79
5710108c949e
[Seobeo] Added Redirect logic.
June Park <parkjune1995@gmail.com>
parents:
78
diff
changeset
|
2751 Seobeo_Router_Register("GET", "/tools/index.html", GetRedirectTools); |
| 78 | 2752 |
| 2753 Seobeo_Router_Register("GET", "/tools/markdown_to_html", GetMDToHTML); | |
|
79
5710108c949e
[Seobeo] Added Redirect logic.
June Park <parkjune1995@gmail.com>
parents:
78
diff
changeset
|
2754 Seobeo_Router_Register("GET", "/tools/markdown_to_html/index.html", GetRedirectMarkDownToHtml); |
| 78 | 2755 |
|
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
|
2756 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
|
2757 Seobeo_Router_Register("GET", "/tools/file_converter/index.html", GetRedirectFileConverter); |
|
242
543df0fe7168
[tools] Add full HLS player support
MrJuneJune <me@mrjunejune.com>
parents:
241
diff
changeset
|
2758 Seobeo_Router_Register("GET", "/tools/hls_player", GetHlsPlayer); |
|
543df0fe7168
[tools] Add full HLS player support
MrJuneJune <me@mrjunejune.com>
parents:
241
diff
changeset
|
2759 Seobeo_Router_Register("GET", "/tools/hls_player/index.html", GetRedirectHlsPlayer); |
|
244
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
2760 Seobeo_Router_Register("GET", "/tools/latex_editor", GetLatexEditor); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
2761 Seobeo_Router_Register("GET", "/tools/latex_editor/index.html", GetRedirectLatexEditor); |
|
92
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
2762 |
|
100
65e5a5b89a4e
[Seobeo] Migrated everything to this page.
June Park <parkjune1995@gmail.com>
parents:
96
diff
changeset
|
2763 // -- File converter --/ |
|
92
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
2764 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
|
2765 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
|
2766 Seobeo_Router_Register("GET", "/api/download/:filename", DownloadConvertedFile); |
|
263
ee04e4e69fed
Add functional JRPG frame and tools
MrJuneJune <mrjunejune@users.noreply.github.com>
parents:
260
diff
changeset
|
2767 Seobeo_Router_Register("DELETE", "/api/download/:filename", DeleteConvertedFile); |
|
244
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
2768 Seobeo_Router_Register("POST", "/api/latex/render", RenderLatexPdf); |
|
92
655ea0b661fd
[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
June Park <parkjune1995@gmail.com>
parents:
89
diff
changeset
|
2769 |
|
200
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
2770 // -- S3 Upload --/ |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
2771 Seobeo_Router_Register("POST", "/api/s3/upload-url", GetS3UploadUrl); |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
2772 |
| 201 | 2773 // -- Media Upload --/ |
| 2774 Seobeo_Router_Register("POST", "/api/media/create", MediaCreate); | |
| 2775 Seobeo_Router_Register("POST", "/api/media/:id/uploaded", MediaUploaded); | |
| 2776 Seobeo_Router_Register("GET", "/api/media/:id/status", MediaStatus); | |
| 2777 | |
| 2778 // -- Editor --/ | |
| 2779 Seobeo_Router_Register("POST", "/api/editor/save", EditorSave); | |
| 2780 Seobeo_Router_Register("GET", "/api/editor/load/:doc_id", EditorLoad); | |
| 2781 | |
|
100
65e5a5b89a4e
[Seobeo] Migrated everything to this page.
June Park <parkjune1995@gmail.com>
parents:
96
diff
changeset
|
2782 // -- Blog --/ |
|
65e5a5b89a4e
[Seobeo] Migrated everything to this page.
June Park <parkjune1995@gmail.com>
parents:
96
diff
changeset
|
2783 Seobeo_Router_Register("GET", "/blog", RenderBlogList); |
|
65e5a5b89a4e
[Seobeo] Migrated everything to this page.
June Park <parkjune1995@gmail.com>
parents:
96
diff
changeset
|
2784 Seobeo_Router_Register("GET", "/blog/:blog_id", RenderBlog); |
|
65e5a5b89a4e
[Seobeo] Migrated everything to this page.
June Park <parkjune1995@gmail.com>
parents:
96
diff
changeset
|
2785 |
|
125
f236c895604e
[MrJuneJune] Added web socket for chat to this.
June Park <parkjune1995@gmail.com>
parents:
124
diff
changeset
|
2786 // -- Talk --/ |
|
f236c895604e
[MrJuneJune] Added web socket for chat to this.
June Park <parkjune1995@gmail.com>
parents:
124
diff
changeset
|
2787 Seobeo_Router_Register("GET", "/talk", GetTalk); |
|
128
7eb79fd91c7e
[Misc] Fixed all bazel targets. I should creat a separate scripts for these lol.
June Park <parkjune1995@gmail.com>
parents:
125
diff
changeset
|
2788 Seobeo_Router_Register("GET", "/talk/index.html", GetRedirectTalk); |
|
125
f236c895604e
[MrJuneJune] Added web socket for chat to this.
June Park <parkjune1995@gmail.com>
parents:
124
diff
changeset
|
2789 |
|
260
1f9877b637e9
Add Copilot-powered cyberpunk JRPG chat
MrJuneJune <mrjunejune@users.noreply.github.com>
parents:
259
diff
changeset
|
2790 // -- JRPG agent chat --/ |
|
259
667156fcd3e3
Add dev-only cyberpunk JRPG page
MrJuneJune <me@mrjunejune.com>
parents:
250
diff
changeset
|
2791 Seobeo_Router_Register("GET", "/jrpg", GetJrpg); |
|
667156fcd3e3
Add dev-only cyberpunk JRPG page
MrJuneJune <me@mrjunejune.com>
parents:
250
diff
changeset
|
2792 Seobeo_Router_Register("GET", "/jrpg/index.html", GetRedirectJrpg); |
|
667156fcd3e3
Add dev-only cyberpunk JRPG page
MrJuneJune <me@mrjunejune.com>
parents:
250
diff
changeset
|
2793 |
|
264
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2794 // -- Login --/ |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2795 Seobeo_Router_Register("GET", "/login", GetLogin); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2796 |
| 201 | 2797 // -- Notes --/ |
| 2798 Seobeo_Router_Register("GET", "/notes", GetNotes); | |
| 2799 Seobeo_Router_Register("GET", "/notes/", GetNotes); | |
| 2800 Seobeo_Router_Register("GET", "/notes/index.html", GetNotes); | |
| 2801 Seobeo_Router_Register("GET", "/notes/login", GetNotesLogin); | |
| 2802 Seobeo_Router_Register("GET", "/notes/login/", GetNotesLogin); | |
| 2803 Seobeo_Router_Register("GET", "/notes/:note_id", GetNoteById); | |
| 2804 | |
|
125
f236c895604e
[MrJuneJune] Added web socket for chat to this.
June Park <parkjune1995@gmail.com>
parents:
124
diff
changeset
|
2805 Seobeo_WebSocket_Server_Init(); |
|
f236c895604e
[MrJuneJune] Added web socket for chat to this.
June Park <parkjune1995@gmail.com>
parents:
124
diff
changeset
|
2806 Seobeo_WebSocket_Server_Register("/chat", Chat_Handler, NULL); |
|
f236c895604e
[MrJuneJune] Added web socket for chat to this.
June Park <parkjune1995@gmail.com>
parents:
124
diff
changeset
|
2807 |
| 201 | 2808 Seobeo_Log(SEOBEO_INFO, "WTF is going on\n"); |
|
241
9c2eec61a152
[assets] Generate site images as WebP with Bazel
MrJuneJune <me@mrjunejune.com>
parents:
216
diff
changeset
|
2809 const char *server_port = getenv("MRJUNEJUNE_PORT"); |
|
9c2eec61a152
[assets] Generate site images as WebP with Bazel
MrJuneJune <me@mrjunejune.com>
parents:
216
diff
changeset
|
2810 if (!server_port || server_port[0] == '\0') |
|
9c2eec61a152
[assets] Generate site images as WebP with Bazel
MrJuneJune <me@mrjunejune.com>
parents:
216
diff
changeset
|
2811 server_port = "6969"; |
|
264
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2812 const char *server_bind = g_server_host[0] ? g_server_host : "0.0.0.0"; |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2813 Mjj_Template_Renderer_Init("mrjunejune/src"); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2814 int server_result = Seobeo_Web_Server_Start_On( |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2815 server_bind, "mrjunejune/src", server_port, SEOBEO_MODE_EDGE, 4); |
|
250
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
2816 Seobeo_Worker_Pool_Destroy(g_media_worker_pool); |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
2817 g_media_worker_pool = NULL; |
|
260
1f9877b637e9
Add Copilot-powered cyberpunk JRPG chat
MrJuneJune <mrjunejune@users.noreply.github.com>
parents:
259
diff
changeset
|
2818 Conversation_API_Destroy(); |
|
264
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2819 Auth_API_Destroy(); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2820 if (server_result != 0) |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2821 { |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2822 fprintf(stderr, "[STARTUP] Server bind/listen failed (host=%s port=%s)\n", |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2823 server_bind, server_port); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2824 return 1; |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2825 } |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2826 return 0; |
|
7
114cad94008f
[Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
2827 } |