Mercurial
annotate mrjunejune/main.c @ 265:056790c4fb0d
add role-aware Epi assistant prompts
Add verified June knowledge, guest/member/admin Copilot profiles, profile-isolated session recovery, animated Epi greetings, and a single authoritative runtime config workflow for inference.
Co-authored-by: Copilot <[email protected]>
| author | MrJuneJune <me@mrjunejune.com> |
|---|---|
| date | Fri, 07 Aug 2026 10:50:30 -0700 |
| parents | 04fee26ecce0 |
| children | e02e2036ef84 |
| 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 |
|
259
667156fcd3e3
Add dev-only cyberpunk JRPG page
MrJuneJune <me@mrjunejune.com>
parents:
250
diff
changeset
|
1217 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
|
1218 { |
|
667156fcd3e3
Add dev-only cyberpunk JRPG page
MrJuneJune <me@mrjunejune.com>
parents:
250
diff
changeset
|
1219 Seobeo_Request_Entry *resp = NULL; |
|
264
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
1220 char *final_body = Dowa_Arena_Allocate(arena, HTML_PAGE_CAP); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
1221 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
|
1222 return html_render_error(arena, "Internal Server Error"); |
|
259
667156fcd3e3
Add dev-only cyberpunk JRPG page
MrJuneJune <me@mrjunejune.com>
parents:
250
diff
changeset
|
1223 Dowa_HashMap_Push_Arena(resp, "body", final_body, arena); |
|
264
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
1224 Dowa_HashMap_Push_Arena(resp, "referrer-policy", "no-referrer", arena); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
1225 return resp; |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
1226 } |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
1227 |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
1228 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
|
1229 { |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
1230 (void)req; |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
1231 Seobeo_Request_Entry *resp = NULL; |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
1232 char *final_body = Dowa_Arena_Allocate(arena, HTML_PAGE_CAP); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
1233 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
|
1234 { |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
1235 Seobeo_Request_Entry *err = NULL; |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
1236 Dowa_HashMap_Push_Arena(err, "status", "500", arena); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
1237 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
|
1238 Dowa_HashMap_Push_Arena(err, "cache-control", "no-store", arena); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
1239 Dowa_HashMap_Push_Arena(err, "body", "Internal Server Error", arena); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
1240 return err; |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
1241 } |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
1242 Dowa_HashMap_Push_Arena(resp, "body", final_body, arena); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
1243 Dowa_HashMap_Push_Arena( |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
1244 resp, "content-type", "text/html; charset=utf-8", arena); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
1245 Dowa_HashMap_Push_Arena(resp, "cache-control", "no-store", arena); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
1246 Dowa_HashMap_Push_Arena(resp, "pragma", "no-cache", arena); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
1247 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
|
1248 Dowa_HashMap_Push_Arena(resp, "referrer-policy", "no-referrer", arena); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
1249 Dowa_HashMap_Push_Arena( |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
1250 resp, "content-security-policy", "frame-ancestors 'none'", arena); |
|
259
667156fcd3e3
Add dev-only cyberpunk JRPG page
MrJuneJune <me@mrjunejune.com>
parents:
250
diff
changeset
|
1251 return resp; |
|
667156fcd3e3
Add dev-only cyberpunk JRPG page
MrJuneJune <me@mrjunejune.com>
parents:
250
diff
changeset
|
1252 } |
|
667156fcd3e3
Add dev-only cyberpunk JRPG page
MrJuneJune <me@mrjunejune.com>
parents:
250
diff
changeset
|
1253 |
| 201 | 1254 Seobeo_Request_Entry *GetNotesLogin(Seobeo_Request_Entry *req, Dowa_Arena *arena) |
| 1255 { | |
| 1256 Seobeo_Request_Entry *resp = NULL; | |
|
264
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
1257 char *final_body = Dowa_Arena_Allocate(arena, HTML_PAGE_CAP); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
1258 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
|
1259 return html_render_error(arena, "Internal Server Error"); |
| 201 | 1260 Dowa_HashMap_Push_Arena(resp, "body", final_body, arena); |
| 1261 return resp; | |
| 1262 } | |
| 1263 | |
| 1264 Seobeo_Request_Entry *GetNotes(Seobeo_Request_Entry *req, Dowa_Arena *arena) | |
| 1265 { | |
| 1266 Seobeo_Request_Entry *resp = NULL; | |
|
264
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
1267 char *final_body = Dowa_Arena_Allocate(arena, HTML_PAGE_CAP); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
1268 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
|
1269 return html_render_error(arena, "Internal Server Error"); |
| 201 | 1270 Dowa_HashMap_Push_Arena(resp, "body", final_body, arena); |
| 1271 return resp; | |
| 1272 } | |
| 1273 | |
| 1274 Seobeo_Request_Entry *GetNoteById(Seobeo_Request_Entry *req, Dowa_Arena *arena) | |
| 1275 { | |
| 1276 Seobeo_Request_Entry *resp = NULL; | |
|
264
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
1277 char *final_body = Dowa_Arena_Allocate(arena, HTML_PAGE_CAP); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
1278 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
|
1279 return html_render_error(arena, "Internal Server Error"); |
| 201 | 1280 Dowa_HashMap_Push_Arena(resp, "body", final_body, arena); |
| 1281 return resp; | |
| 1282 } | |
| 1283 | |
| 88 | 1284 CREATE_REDIRECT_HANDLER(HomePage, "/") |
| 1285 CREATE_REDIRECT_HANDLER(Resume, "/resume") | |
| 1286 CREATE_REDIRECT_HANDLER(Tools, "/tools") | |
| 1287 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
|
1288 CREATE_REDIRECT_HANDLER(FileConverter, "/tools/file_converter") |
|
242
543df0fe7168
[tools] Add full HLS player support
MrJuneJune <me@mrjunejune.com>
parents:
241
diff
changeset
|
1289 CREATE_REDIRECT_HANDLER(HlsPlayer, "/tools/hls_player") |
|
244
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
1290 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
|
1291 CREATE_REDIRECT_HANDLER(Talk, "/talk") |
|
259
667156fcd3e3
Add dev-only cyberpunk JRPG page
MrJuneJune <me@mrjunejune.com>
parents:
250
diff
changeset
|
1292 CREATE_REDIRECT_HANDLER(Jrpg, "/jrpg") |
| 201 | 1293 CREATE_REDIRECT_HANDLER(Editor, "/editor") |
|
79
5710108c949e
[Seobeo] Added Redirect logic.
June Park <parkjune1995@gmail.com>
parents:
78
diff
changeset
|
1294 |
|
200
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1295 // S3 Upload URL API |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1296 // POST /api/s3/upload-url |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1297 // 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
|
1298 // 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
|
1299 // Returns: {"upload_url": "https://...", "key": "uploads/..."} |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1300 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
|
1301 { |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1302 Seobeo_Request_Entry *resp = NULL; |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1303 |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1304 // Check auth token |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1305 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
|
1306 if (!auth_kv) |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1307 { |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1308 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
|
1309 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
|
1310 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
|
1311 return resp; |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1312 } |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1313 |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1314 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
|
1315 |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1316 // Expect "Bearer <token>" |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1317 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
|
1318 { |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1319 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
|
1320 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
|
1321 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
|
1322 return resp; |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1323 } |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1324 |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1325 const char *token = auth_header + 7; |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1326 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
|
1327 { |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1328 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
|
1329 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
|
1330 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
|
1331 return resp; |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1332 } |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1333 |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1334 // 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
|
1335 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
|
1336 if (!body_kv) |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1337 { |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1338 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
|
1339 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
|
1340 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
|
1341 return resp; |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1342 } |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1343 |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1344 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
|
1345 |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1346 // 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
|
1347 char filename[256] = {0}; |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1348 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
|
1349 |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1350 // Find "filename":"value" |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1351 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
|
1352 if (fn_key) |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1353 { |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1354 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
|
1355 if (fn_start) |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1356 { |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1357 fn_start++; |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1358 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
|
1359 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
|
1360 { |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1361 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
|
1362 filename[fn_end - fn_start] = '\0'; |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1363 } |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1364 } |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1365 } |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1366 |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1367 // Find "content_type":"value" |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1368 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
|
1369 if (ct_key) |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1370 { |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1371 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
|
1372 if (ct_start) |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1373 { |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1374 ct_start++; |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1375 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
|
1376 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
|
1377 { |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1378 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
|
1379 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
|
1380 } |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1381 } |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1382 } |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1383 |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1384 if (strlen(filename) == 0) |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1385 { |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1386 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
|
1387 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
|
1388 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
|
1389 return resp; |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1390 } |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1391 |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1392 // Generate unique S3 key with timestamp |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1393 char s3_key[512]; |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1394 char *uuid = Dowa_Arena_Allocate(arena, UUID_LEN); |
|
250
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
1395 uint32 seed = |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
1396 (uint32)time(NULL) ^ |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
1397 (uint32)Seobeo_Thread_Current_Id() ^ |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
1398 counter++; |
|
200
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1399 Dowa_String_UUID(seed, uuid); |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1400 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
|
1401 |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1402 // Generate presigned URL |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1403 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
|
1404 |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1405 if (!presigned.success) |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1406 { |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1407 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
|
1408 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
|
1409 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
|
1410 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
|
1411 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
|
1412 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
|
1413 S3_Presigned_URL_Destroy(&presigned); |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1414 return resp; |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1415 } |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1416 |
| 201 | 1417 // Build public URL using CloudFront |
| 1418 char public_url[512]; | |
| 1419 if (g_s3_cloudfront_url[0]) | |
| 1420 { | |
| 1421 snprintf(public_url, sizeof(public_url), "%s/%s", g_s3_cloudfront_url, s3_key); | |
| 1422 } | |
| 1423 else | |
| 1424 { | |
| 1425 snprintf(public_url, sizeof(public_url), "https://%s.s3.%s.amazonaws.com/%s", | |
| 1426 g_s3_bucket, g_s3_region, s3_key); | |
| 1427 } | |
| 1428 | |
|
200
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1429 // Build response |
| 201 | 1430 char *response_body = Dowa_Arena_Allocate(arena, 4096 + strlen(presigned.url)); |
| 1431 snprintf(response_body, 4096 + strlen(presigned.url), | |
| 1432 "{\"upload_url\":\"%s\",\"public_url\":\"%s\",\"key\":\"%s\",\"expires\":%d}", | |
| 1433 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
|
1434 |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1435 S3_Presigned_URL_Destroy(&presigned); |
|
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 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
|
1438 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
|
1439 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
|
1440 |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1441 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
|
1442 |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1443 return resp; |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1444 } |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
1445 |
| 201 | 1446 // Editor Content Save API |
| 1447 // POST /api/editor/save | |
| 1448 // Headers: Authorization: Bearer <token> | |
| 1449 // Body: {"doc_id": "my-doc", "content": "<html content>"} | |
| 1450 Seobeo_Request_Entry *EditorSave(Seobeo_Request_Entry *req, Dowa_Arena *arena) | |
| 1451 { | |
| 1452 Seobeo_Request_Entry *resp = NULL; | |
| 1453 | |
| 1454 // Check auth token | |
| 1455 void *auth_kv = Dowa_HashMap_Get_Ptr(req, "Authorization"); | |
| 1456 if (!auth_kv) | |
| 1457 { | |
| 1458 Dowa_HashMap_Push_Arena(resp, "status", "401", arena); | |
| 1459 Dowa_HashMap_Push_Arena(resp, "content-type", "application/json", arena); | |
| 1460 Dowa_HashMap_Push_Arena(resp, "body", "{\"error\":\"Missing Authorization header\"}", arena); | |
| 1461 return resp; | |
| 1462 } | |
| 1463 | |
| 1464 const char *auth_header = ((Seobeo_Request_Entry*)auth_kv)->value; | |
| 1465 if (strncmp(auth_header, "Bearer ", 7) != 0) | |
| 1466 { | |
| 1467 Dowa_HashMap_Push_Arena(resp, "status", "401", arena); | |
| 1468 Dowa_HashMap_Push_Arena(resp, "content-type", "application/json", arena); | |
| 1469 Dowa_HashMap_Push_Arena(resp, "body", "{\"error\":\"Invalid Authorization format\"}", arena); | |
| 1470 return resp; | |
| 1471 } | |
| 1472 | |
| 1473 const char *token = auth_header + 7; | |
| 1474 if (strlen(g_upload_auth_token) == 0 || strcmp(token, g_upload_auth_token) != 0) | |
| 1475 { | |
| 1476 Dowa_HashMap_Push_Arena(resp, "status", "403", arena); | |
| 1477 Dowa_HashMap_Push_Arena(resp, "content-type", "application/json", arena); | |
| 1478 Dowa_HashMap_Push_Arena(resp, "body", "{\"error\":\"Invalid token\"}", arena); | |
| 1479 return resp; | |
| 1480 } | |
| 1481 | |
| 1482 if (!g_db_connection) | |
| 1483 { | |
| 1484 Dowa_HashMap_Push_Arena(resp, "status", "500", arena); | |
| 1485 Dowa_HashMap_Push_Arena(resp, "content-type", "application/json", arena); | |
| 1486 Dowa_HashMap_Push_Arena(resp, "body", "{\"error\":\"Database not available\"}", arena); | |
| 1487 return resp; | |
| 1488 } | |
| 1489 | |
| 1490 // Parse request body | |
| 1491 void *body_kv = Dowa_HashMap_Get_Ptr(req, "Body"); | |
| 1492 if (!body_kv) | |
| 1493 { | |
| 1494 Dowa_HashMap_Push_Arena(resp, "status", "400", arena); | |
| 1495 Dowa_HashMap_Push_Arena(resp, "content-type", "application/json", arena); | |
| 1496 Dowa_HashMap_Push_Arena(resp, "body", "{\"error\":\"Missing request body\"}", arena); | |
| 1497 return resp; | |
| 1498 } | |
| 1499 | |
| 1500 const char *body = ((Seobeo_Request_Entry*)body_kv)->value; | |
| 1501 | |
| 1502 // Parse doc_id and content from JSON | |
| 1503 char doc_id[256] = "default"; | |
| 1504 char *content = NULL; | |
| 1505 size_t content_len = 0; | |
| 1506 | |
| 1507 // Find "doc_id":"value" | |
| 1508 const char *doc_key = strstr(body, "\"doc_id\""); | |
| 1509 if (doc_key) | |
| 1510 { | |
| 1511 const char *doc_start = strchr(doc_key + 8, '"'); | |
| 1512 if (doc_start) | |
| 1513 { | |
| 1514 doc_start++; | |
| 1515 const char *doc_end = strchr(doc_start, '"'); | |
| 1516 if (doc_end && (size_t)(doc_end - doc_start) < sizeof(doc_id)) | |
| 1517 { | |
| 1518 memcpy(doc_id, doc_start, doc_end - doc_start); | |
| 1519 doc_id[doc_end - doc_start] = '\0'; | |
| 1520 } | |
| 1521 } | |
| 1522 } | |
| 1523 | |
| 1524 // Find "content":"value" - content can be large and contain escaped characters | |
| 1525 const char *content_key = strstr(body, "\"content\""); | |
| 1526 if (content_key) | |
| 1527 { | |
| 1528 const char *content_start = strchr(content_key + 9, '"'); | |
| 1529 if (content_start) | |
| 1530 { | |
| 1531 content_start++; | |
| 1532 // Find closing quote (accounting for escaped quotes) | |
| 1533 const char *p = content_start; | |
| 1534 while (*p) | |
| 1535 { | |
| 1536 if (*p == '\\' && *(p+1)) | |
| 1537 { | |
| 1538 p += 2; | |
| 1539 continue; | |
| 1540 } | |
| 1541 if (*p == '"') break; | |
| 1542 p++; | |
| 1543 } | |
| 1544 content_len = p - content_start; | |
| 1545 content = Dowa_Arena_Allocate(arena, content_len + 1); | |
| 1546 memcpy(content, content_start, content_len); | |
| 1547 content[content_len] = '\0'; | |
| 1548 } | |
| 1549 } | |
| 1550 | |
| 1551 if (!content) | |
| 1552 { | |
| 1553 Dowa_HashMap_Push_Arena(resp, "status", "400", arena); | |
| 1554 Dowa_HashMap_Push_Arena(resp, "content-type", "application/json", arena); | |
| 1555 Dowa_HashMap_Push_Arena(resp, "body", "{\"error\":\"Missing content\"}", arena); | |
| 1556 return resp; | |
| 1557 } | |
| 1558 | |
| 1559 // Upsert content | |
| 1560 const char *upsert_query = | |
| 1561 "INSERT INTO editor_content (access_token, doc_id, content, updated_at) " | |
| 1562 "VALUES (?, ?, ?, strftime('%s', 'now')) " | |
| 1563 "ON CONFLICT(access_token, doc_id) DO UPDATE SET " | |
| 1564 "content = excluded.content, updated_at = strftime('%s', 'now')"; | |
| 1565 | |
| 1566 const char *params[] = { token, doc_id, content }; | |
| 1567 int32 result = Deita_Query_Execute_Update_Prepared(g_db_connection, upsert_query, 3, params); | |
| 1568 | |
| 1569 if (result < 0) | |
| 1570 { | |
| 1571 Dowa_HashMap_Push_Arena(resp, "status", "500", arena); | |
| 1572 Dowa_HashMap_Push_Arena(resp, "content-type", "application/json", arena); | |
| 1573 Dowa_HashMap_Push_Arena(resp, "body", "{\"error\":\"Failed to save\"}", arena); | |
| 1574 return resp; | |
| 1575 } | |
| 1576 | |
| 1577 printf("[EDITOR] Saved doc_id=%s, content_len=%zu\n", doc_id, content_len); | |
| 1578 | |
| 1579 Dowa_HashMap_Push_Arena(resp, "status", "200", arena); | |
| 1580 Dowa_HashMap_Push_Arena(resp, "content-type", "application/json", arena); | |
| 1581 Dowa_HashMap_Push_Arena(resp, "body", "{\"success\":true}", arena); | |
| 1582 return resp; | |
| 1583 } | |
| 1584 | |
| 1585 // Editor Content Load API | |
| 1586 // GET /api/editor/load/:doc_id | |
| 1587 // Headers: Authorization: Bearer <token> | |
| 1588 Seobeo_Request_Entry *EditorLoad(Seobeo_Request_Entry *req, Dowa_Arena *arena) | |
| 1589 { | |
| 1590 Seobeo_Request_Entry *resp = NULL; | |
| 1591 | |
| 1592 // Check auth token | |
| 1593 void *auth_kv = Dowa_HashMap_Get_Ptr(req, "Authorization"); | |
| 1594 if (!auth_kv) | |
| 1595 { | |
| 1596 Dowa_HashMap_Push_Arena(resp, "status", "401", arena); | |
| 1597 Dowa_HashMap_Push_Arena(resp, "content-type", "application/json", arena); | |
| 1598 Dowa_HashMap_Push_Arena(resp, "body", "{\"error\":\"Missing Authorization header\"}", arena); | |
| 1599 return resp; | |
| 1600 } | |
| 1601 | |
| 1602 const char *auth_header = ((Seobeo_Request_Entry*)auth_kv)->value; | |
| 1603 if (strncmp(auth_header, "Bearer ", 7) != 0) | |
| 1604 { | |
| 1605 Dowa_HashMap_Push_Arena(resp, "status", "401", arena); | |
| 1606 Dowa_HashMap_Push_Arena(resp, "content-type", "application/json", arena); | |
| 1607 Dowa_HashMap_Push_Arena(resp, "body", "{\"error\":\"Invalid Authorization format\"}", arena); | |
| 1608 return resp; | |
| 1609 } | |
| 1610 | |
| 1611 const char *token = auth_header + 7; | |
| 1612 if (strlen(g_upload_auth_token) == 0 || strcmp(token, g_upload_auth_token) != 0) | |
| 1613 { | |
| 1614 Dowa_HashMap_Push_Arena(resp, "status", "403", arena); | |
| 1615 Dowa_HashMap_Push_Arena(resp, "content-type", "application/json", arena); | |
| 1616 Dowa_HashMap_Push_Arena(resp, "body", "{\"error\":\"Invalid token\"}", arena); | |
| 1617 return resp; | |
| 1618 } | |
| 1619 | |
| 1620 if (!g_db_connection) | |
| 1621 { | |
| 1622 Dowa_HashMap_Push_Arena(resp, "status", "500", arena); | |
| 1623 Dowa_HashMap_Push_Arena(resp, "content-type", "application/json", arena); | |
| 1624 Dowa_HashMap_Push_Arena(resp, "body", "{\"error\":\"Database not available\"}", arena); | |
| 1625 return resp; | |
| 1626 } | |
| 1627 | |
| 1628 // Get doc_id from URL parameter | |
| 1629 void *doc_id_kv = Dowa_HashMap_Get_Ptr(req, ":doc_id"); | |
| 1630 const char *doc_id = "default"; | |
| 1631 if (doc_id_kv) | |
| 1632 { | |
| 1633 doc_id = ((Seobeo_Request_Entry*)doc_id_kv)->value; | |
| 1634 } | |
| 1635 | |
| 1636 // Query content | |
| 1637 const char *select_query = | |
| 1638 "SELECT content, updated_at FROM editor_content WHERE access_token = ? AND doc_id = ?"; | |
| 1639 const char *params[] = { token, doc_id }; | |
| 1640 | |
| 1641 Deita_Result_Set *p_result = Deita_Query_Execute_Prepared(g_db_connection, select_query, 2, params, arena); | |
| 1642 | |
| 1643 if (p_result && Deita_Result_Set_Next(p_result)) | |
| 1644 { | |
| 1645 const char *content = Deita_Result_Set_Get_Text(p_result, 0); | |
| 1646 int64 updated_at = Deita_Result_Set_Get_Integer(p_result, 1); | |
| 1647 | |
| 1648 // Build JSON response - escape content | |
| 1649 size_t content_len = content ? strlen(content) : 0; | |
| 1650 char *response_body = Dowa_Arena_Allocate(arena, content_len + 256); | |
| 1651 snprintf(response_body, content_len + 256, | |
| 1652 "{\"doc_id\":\"%s\",\"content\":\"%s\",\"updated_at\":%lld}", | |
| 1653 doc_id, content ? content : "", (long long)updated_at); | |
| 1654 | |
| 1655 Dowa_HashMap_Push_Arena(resp, "status", "200", arena); | |
| 1656 Dowa_HashMap_Push_Arena(resp, "content-type", "application/json", arena); | |
| 1657 Dowa_HashMap_Push_Arena(resp, "body", response_body, arena); | |
| 1658 | |
| 1659 printf("[EDITOR] Loaded doc_id=%s\n", doc_id); | |
| 1660 } | |
| 1661 else | |
| 1662 { | |
| 1663 // No content found, return empty | |
| 1664 char *response_body = Dowa_Arena_Allocate(arena, 128); | |
| 1665 snprintf(response_body, 128, "{\"doc_id\":\"%s\",\"content\":\"\",\"updated_at\":0}", doc_id); | |
| 1666 | |
| 1667 Dowa_HashMap_Push_Arena(resp, "status", "200", arena); | |
| 1668 Dowa_HashMap_Push_Arena(resp, "content-type", "application/json", arena); | |
| 1669 Dowa_HashMap_Push_Arena(resp, "body", response_body, arena); | |
| 1670 } | |
| 1671 | |
| 1672 if (p_result) Deita_Result_Set_Free(p_result); | |
| 1673 return resp; | |
| 1674 } | |
| 1675 | |
| 1676 // Media Upload API - Create media record | |
| 1677 // POST /api/media/create | |
| 1678 // Headers: Authorization: Bearer <token>, Content-Type: application/json | |
| 1679 // Body: {"filename": "photo.jpg", "content_type": "image/jpeg"} | |
| 1680 // Returns: {"media_id": 123, "upload_url": "https://...", "expires": 3600} | |
| 1681 Seobeo_Request_Entry *MediaCreate(Seobeo_Request_Entry *req, Dowa_Arena *arena) | |
| 1682 { | |
| 1683 Seobeo_Request_Entry *resp = NULL; | |
| 1684 | |
| 1685 // Check auth token | |
| 1686 void *auth_kv = Dowa_HashMap_Get_Ptr(req, "Authorization"); | |
| 1687 if (!auth_kv) | |
| 1688 { | |
| 1689 Dowa_HashMap_Push_Arena(resp, "status", "401", arena); | |
| 1690 Dowa_HashMap_Push_Arena(resp, "content-type", "application/json", arena); | |
| 1691 Dowa_HashMap_Push_Arena(resp, "body", "{\"error\":\"Missing Authorization header\"}", arena); | |
| 1692 return resp; | |
| 1693 } | |
| 1694 | |
| 1695 const char *auth_header = ((Seobeo_Request_Entry*)auth_kv)->value; | |
| 1696 if (strncmp(auth_header, "Bearer ", 7) != 0) | |
| 1697 { | |
| 1698 Dowa_HashMap_Push_Arena(resp, "status", "401", arena); | |
| 1699 Dowa_HashMap_Push_Arena(resp, "content-type", "application/json", arena); | |
| 1700 Dowa_HashMap_Push_Arena(resp, "body", "{\"error\":\"Invalid Authorization format\"}", arena); | |
| 1701 return resp; | |
| 1702 } | |
| 1703 | |
| 1704 const char *token = auth_header + 7; | |
| 1705 if (strlen(g_upload_auth_token) == 0 || strcmp(token, g_upload_auth_token) != 0) | |
| 1706 { | |
| 1707 Dowa_HashMap_Push_Arena(resp, "status", "403", arena); | |
| 1708 Dowa_HashMap_Push_Arena(resp, "content-type", "application/json", arena); | |
| 1709 Dowa_HashMap_Push_Arena(resp, "body", "{\"error\":\"Invalid token\"}", arena); | |
| 1710 return resp; | |
| 1711 } | |
| 1712 | |
| 1713 if (!g_db_connection) | |
| 1714 { | |
| 1715 Dowa_HashMap_Push_Arena(resp, "status", "500", arena); | |
| 1716 Dowa_HashMap_Push_Arena(resp, "content-type", "application/json", arena); | |
| 1717 Dowa_HashMap_Push_Arena(resp, "body", "{\"error\":\"Database not available\"}", arena); | |
| 1718 return resp; | |
| 1719 } | |
| 1720 | |
| 1721 // Parse request body | |
| 1722 void *body_kv = Dowa_HashMap_Get_Ptr(req, "Body"); | |
| 1723 if (!body_kv) | |
| 1724 { | |
| 1725 Dowa_HashMap_Push_Arena(resp, "status", "400", arena); | |
| 1726 Dowa_HashMap_Push_Arena(resp, "content-type", "application/json", arena); | |
| 1727 Dowa_HashMap_Push_Arena(resp, "body", "{\"error\":\"Missing request body\"}", arena); | |
| 1728 return resp; | |
| 1729 } | |
| 1730 | |
| 1731 const char *body = ((Seobeo_Request_Entry*)body_kv)->value; | |
| 1732 | |
| 1733 // Parse filename and content_type | |
| 1734 char filename[256] = {0}; | |
| 1735 char content_type[128] = "application/octet-stream"; | |
| 1736 | |
| 1737 // Find "filename":"value" | |
| 1738 const char *fn_key = strstr(body, "\"filename\""); | |
| 1739 if (fn_key) | |
| 1740 { | |
| 1741 const char *fn_start = strchr(fn_key + 10, '"'); | |
| 1742 if (fn_start) | |
| 1743 { | |
| 1744 fn_start++; | |
| 1745 const char *fn_end = strchr(fn_start, '"'); | |
| 1746 if (fn_end && (size_t)(fn_end - fn_start) < sizeof(filename)) | |
| 1747 { | |
| 1748 memcpy(filename, fn_start, fn_end - fn_start); | |
| 1749 filename[fn_end - fn_start] = '\0'; | |
| 1750 } | |
| 1751 } | |
| 1752 } | |
| 1753 | |
| 1754 // Find "content_type":"value" | |
| 1755 const char *ct_key = strstr(body, "\"content_type\""); | |
| 1756 if (ct_key) | |
| 1757 { | |
| 1758 const char *ct_start = strchr(ct_key + 14, '"'); | |
| 1759 if (ct_start) | |
| 1760 { | |
| 1761 ct_start++; | |
| 1762 const char *ct_end = strchr(ct_start, '"'); | |
| 1763 if (ct_end && (size_t)(ct_end - ct_start) < sizeof(content_type)) | |
| 1764 { | |
| 1765 memcpy(content_type, ct_start, ct_end - ct_start); | |
| 1766 content_type[ct_end - ct_start] = '\0'; | |
| 1767 } | |
| 1768 } | |
| 1769 } | |
| 1770 | |
| 1771 if (strlen(filename) == 0) | |
| 1772 { | |
| 1773 Dowa_HashMap_Push_Arena(resp, "status", "400", arena); | |
| 1774 Dowa_HashMap_Push_Arena(resp, "content-type", "application/json", arena); | |
| 1775 Dowa_HashMap_Push_Arena(resp, "body", "{\"error\":\"Missing filename\"}", arena); | |
| 1776 return resp; | |
| 1777 } | |
| 1778 | |
| 1779 // Generate UUID for this upload | |
| 1780 char *uuid = Dowa_Arena_Allocate(arena, UUID_LEN); | |
|
250
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
1781 uint32 seed = |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
1782 (uint32)time(NULL) ^ |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
1783 (uint32)Seobeo_Thread_Current_Id() ^ |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
1784 counter++; |
| 201 | 1785 Dowa_String_UUID(seed, uuid); |
| 1786 | |
| 1787 // Generate S3 keys | |
| 1788 char s3_key_original[512]; | |
| 1789 char s3_key_processed[512]; | |
| 1790 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
|
1791 |
|
e5aed6c36672
[Notes] Added icons and updated styling a bit. Probalby usable now.
MrJuneJune <me@mrjunejune.com>
parents:
202
diff
changeset
|
1792 // 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
|
1793 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
|
1794 if (is_image) |
|
e5aed6c36672
[Notes] Added icons and updated styling a bit. Probalby usable now.
MrJuneJune <me@mrjunejune.com>
parents:
202
diff
changeset
|
1795 { |
|
e5aed6c36672
[Notes] Added icons and updated styling a bit. Probalby usable now.
MrJuneJune <me@mrjunejune.com>
parents:
202
diff
changeset
|
1796 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
|
1797 } |
|
e5aed6c36672
[Notes] Added icons and updated styling a bit. Probalby usable now.
MrJuneJune <me@mrjunejune.com>
parents:
202
diff
changeset
|
1798 else |
|
e5aed6c36672
[Notes] Added icons and updated styling a bit. Probalby usable now.
MrJuneJune <me@mrjunejune.com>
parents:
202
diff
changeset
|
1799 { |
|
e5aed6c36672
[Notes] Added icons and updated styling a bit. Probalby usable now.
MrJuneJune <me@mrjunejune.com>
parents:
202
diff
changeset
|
1800 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
|
1801 } |
| 201 | 1802 |
| 1803 // Insert into database | |
| 1804 const char *insert_query = | |
| 1805 "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
|
1806 "VALUES (?, ?, ?, ?, ?, 'pending') RETURNING id"; |
| 201 | 1807 |
| 1808 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
|
1809 Deita_Result_Set *id_result = Deita_Query_Execute_Prepared( |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
1810 g_db_connection, |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
1811 insert_query, |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
1812 5, |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
1813 params, |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
1814 arena); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
1815 if (!id_result || !Deita_Result_Set_Next(id_result)) |
| 201 | 1816 { |
|
244
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
1817 if (id_result) Deita_Result_Set_Free(id_result); |
| 201 | 1818 Dowa_HashMap_Push_Arena(resp, "status", "500", arena); |
| 1819 Dowa_HashMap_Push_Arena(resp, "content-type", "application/json", arena); | |
| 1820 Dowa_HashMap_Push_Arena(resp, "body", "{\"error\":\"Failed to create media record\"}", arena); | |
| 1821 return resp; | |
| 1822 } | |
| 1823 | |
|
244
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
1824 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
|
1825 Deita_Result_Set_Free(id_result); |
| 201 | 1826 |
| 1827 // Generate presigned PUT URL | |
| 1828 S3_Presigned_URL presigned = S3_Presign_Put(&g_s3_config, s3_key_original, content_type, g_s3_url_expires); | |
| 1829 | |
| 1830 if (!presigned.success) | |
| 1831 { | |
| 1832 Dowa_HashMap_Push_Arena(resp, "status", "500", arena); | |
| 1833 Dowa_HashMap_Push_Arena(resp, "content-type", "application/json", arena); | |
| 1834 char *error_body = Dowa_Arena_Allocate(arena, 256); | |
| 1835 snprintf(error_body, 256, "{\"error\":\"Failed to generate upload URL: %s\"}", | |
| 1836 presigned.error_message ? presigned.error_message : "unknown"); | |
| 1837 Dowa_HashMap_Push_Arena(resp, "body", error_body, arena); | |
| 1838 S3_Presigned_URL_Destroy(&presigned); | |
| 1839 return resp; | |
| 1840 } | |
| 1841 | |
|
204
e5aed6c36672
[Notes] Added icons and updated styling a bit. Probalby usable now.
MrJuneJune <me@mrjunejune.com>
parents:
202
diff
changeset
|
1842 // 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
|
1843 char public_url[512]; |
|
e5aed6c36672
[Notes] Added icons and updated styling a bit. Probalby usable now.
MrJuneJune <me@mrjunejune.com>
parents:
202
diff
changeset
|
1844 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
|
1845 { |
|
e5aed6c36672
[Notes] Added icons and updated styling a bit. Probalby usable now.
MrJuneJune <me@mrjunejune.com>
parents:
202
diff
changeset
|
1846 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
|
1847 } |
|
e5aed6c36672
[Notes] Added icons and updated styling a bit. Probalby usable now.
MrJuneJune <me@mrjunejune.com>
parents:
202
diff
changeset
|
1848 else |
|
e5aed6c36672
[Notes] Added icons and updated styling a bit. Probalby usable now.
MrJuneJune <me@mrjunejune.com>
parents:
202
diff
changeset
|
1849 { |
|
e5aed6c36672
[Notes] Added icons and updated styling a bit. Probalby usable now.
MrJuneJune <me@mrjunejune.com>
parents:
202
diff
changeset
|
1850 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
|
1851 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
|
1852 } |
|
e5aed6c36672
[Notes] Added icons and updated styling a bit. Probalby usable now.
MrJuneJune <me@mrjunejune.com>
parents:
202
diff
changeset
|
1853 |
| 201 | 1854 // Build response |
|
204
e5aed6c36672
[Notes] Added icons and updated styling a bit. Probalby usable now.
MrJuneJune <me@mrjunejune.com>
parents:
202
diff
changeset
|
1855 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
|
1856 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
|
1857 "{\"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
|
1858 (long long)media_id, presigned.url, public_url, g_s3_url_expires); |
| 201 | 1859 |
| 1860 S3_Presigned_URL_Destroy(&presigned); | |
| 1861 | |
| 1862 Dowa_HashMap_Push_Arena(resp, "status", "200", arena); | |
| 1863 Dowa_HashMap_Push_Arena(resp, "content-type", "application/json", arena); | |
| 1864 Dowa_HashMap_Push_Arena(resp, "body", response_body, arena); | |
| 1865 | |
| 1866 printf("[MEDIA] Created media_id=%lld, file=%s\n", (long long)media_id, filename); | |
| 1867 | |
| 1868 return resp; | |
| 1869 } | |
| 1870 | |
|
250
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
1871 // Worker-pool task for S3 media processing. |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
1872 void Media_Process_Background(void *arg) |
| 201 | 1873 { |
| 1874 Media_Processing_Context *ctx = (Media_Processing_Context *)arg; | |
| 1875 | |
|
250
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
1876 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
|
1877 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
|
1878 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
|
1879 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
|
1880 |
|
250
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
1881 // 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
|
1882 Deita_Connection *db_conn = Deita_Connection_Create(DEITA_DATABASE_TYPE_SQLITE3, ctx->db_path); |
| 201 | 1883 if (!db_conn || !Deita_Connection_Is_Open(db_conn)) |
| 1884 { | |
|
250
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
1885 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
|
1886 return; |
| 201 | 1887 } |
| 1888 | |
| 1889 // Update status to 'processing' | |
| 1890 const char *update_processing = | |
| 1891 "UPDATE media_uploads SET status='processing', updated_at=strftime('%s','now') WHERE id=?"; | |
| 1892 char media_id_str[32]; | |
| 1893 snprintf(media_id_str, sizeof(media_id_str), "%lld", (long long)ctx->media_id); | |
| 1894 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
|
1895 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
|
1896 Seobeo_Log(SEOBEO_INFO, "[MEDIA] Updated status to 'processing' for media_id=%lld (result=%d)\n", (long long)ctx->media_id, update_result); |
| 201 | 1897 |
| 1898 // 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
|
1899 Seobeo_Log(SEOBEO_INFO, "[MEDIA] Generating presigned GET URL for media_id=%lld\n", (long long)ctx->media_id); |
| 201 | 1900 S3_Presigned_URL download_url = S3_Presign_Get(&ctx->s3_config, ctx->s3_key_original, 600); |
| 1901 if (!download_url.success) | |
| 1902 { | |
|
202
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
1903 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
|
1904 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
|
1905 (long long)ctx->media_id, error_msg); |
| 201 | 1906 const char *update_error = |
| 1907 "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
|
1908 const char *error_params[] = { error_msg, media_id_str }; |
| 201 | 1909 Deita_Query_Execute_Update_Prepared(db_conn, update_error, 2, error_params); |
| 1910 S3_Presigned_URL_Destroy(&download_url); | |
| 1911 Deita_Connection_Close(db_conn); | |
|
250
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
1912 return; |
| 201 | 1913 } |
|
202
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
1914 Seobeo_Log(SEOBEO_INFO, "[MEDIA] Generated presigned URL: %.100s...\n", download_url.url); |
| 201 | 1915 |
| 1916 // Generate temp file paths | |
| 1917 char tmp_input[256]; | |
| 1918 char tmp_output[256]; | |
| 1919 char *uuid_input = malloc(UUID_LEN); | |
| 1920 char *uuid_output = malloc(UUID_LEN); | |
|
250
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
1921 uint32 seed1 = |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
1922 (uint32)time(NULL) ^ |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
1923 (uint32)Seobeo_Thread_Current_Id() ^ |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
1924 counter++; |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
1925 uint32 seed2 = |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
1926 (uint32)time(NULL) ^ |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
1927 (uint32)Seobeo_Thread_Current_Id() ^ |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
1928 counter++; |
| 201 | 1929 Dowa_String_UUID(seed1, uuid_input); |
| 1930 Dowa_String_UUID(seed2, uuid_output); | |
| 1931 snprintf(tmp_input, sizeof(tmp_input), "/tmp/%s", uuid_input); | |
| 1932 snprintf(tmp_output, sizeof(tmp_output), "/tmp/%s.webp", uuid_output); | |
| 1933 free(uuid_input); | |
| 1934 free(uuid_output); | |
| 1935 | |
| 1936 // Download from S3 | |
|
202
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
1937 Seobeo_Log(SEOBEO_INFO, "[MEDIA] Downloading from S3 to %s for media_id=%lld\n", tmp_input, (long long)ctx->media_id); |
| 201 | 1938 Seobeo_Client_Request *download_req = Seobeo_Client_Request_Create(download_url.url); |
| 1939 Seobeo_Client_Request_Set_Download_Path(download_req, tmp_input); | |
| 1940 Seobeo_Client_Response *download_resp = Seobeo_Client_Request_Execute(download_req); | |
| 1941 | |
| 1942 S3_Presigned_URL_Destroy(&download_url); | |
| 1943 | |
| 1944 if (!download_resp || download_resp->status_code != 200) | |
| 1945 { | |
|
202
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
1946 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
|
1947 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
|
1948 (long long)ctx->media_id, status); |
| 201 | 1949 const char *update_error = |
| 1950 "UPDATE media_uploads SET status='error', error_message=?, updated_at=strftime('%s','now') WHERE id=?"; | |
| 1951 const char *error_params[] = { "Failed to download from S3", media_id_str }; | |
| 1952 Deita_Query_Execute_Update_Prepared(db_conn, update_error, 2, error_params); | |
| 1953 if (download_req) Seobeo_Client_Request_Destroy(download_req); | |
| 1954 if (download_resp) Seobeo_Client_Response_Destroy(download_resp); | |
| 1955 unlink(tmp_input); | |
| 1956 Deita_Connection_Close(db_conn); | |
|
250
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
1957 return; |
| 201 | 1958 } |
| 1959 | |
|
202
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
1960 Seobeo_Log(SEOBEO_INFO, "[MEDIA] Successfully downloaded file to %s\n", tmp_input); |
| 201 | 1961 Seobeo_Client_Request_Destroy(download_req); |
| 1962 Seobeo_Client_Response_Destroy(download_resp); | |
| 1963 | |
| 1964 // Convert to webp using FFmpeg | |
| 1965 char cmd[1024]; | |
| 1966 char log_file[256]; | |
| 1967 snprintf(log_file, sizeof(log_file), "/tmp/ffmpeg_%lld.log", (long long)ctx->media_id); | |
| 1968 snprintf(cmd, sizeof(cmd), "ffmpeg -y -i %s -quality 80 %s 2>%s", | |
| 1969 tmp_input, tmp_output, log_file); | |
| 1970 | |
|
202
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
1971 Seobeo_Log(SEOBEO_INFO, "[MEDIA] Running FFmpeg: %s\n", cmd); |
| 201 | 1972 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
|
1973 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
|
1974 |
| 201 | 1975 if (ffmpeg_result != 0) |
| 1976 { | |
|
202
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
1977 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
|
1978 (long long)ctx->media_id, ffmpeg_result, log_file); |
| 201 | 1979 const char *update_error = |
| 1980 "UPDATE media_uploads SET status='error', error_message=?, updated_at=strftime('%s','now') WHERE id=?"; | |
| 1981 const char *error_params[] = { "Image conversion failed", media_id_str }; | |
| 1982 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
|
1983 unlink(tmp_input); |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
1984 unlink(tmp_output); |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
1985 Deita_Connection_Close(db_conn); |
|
250
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
1986 return; |
|
202
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
1987 } |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
1988 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
|
1989 |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
1990 // 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
|
1991 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
|
1992 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
|
1993 &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
|
1994 |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
1995 if (!upload_result.success) |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
1996 { |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
1997 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
|
1998 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
|
1999 (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
|
2000 const char *update_error = |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2001 "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
|
2002 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
|
2003 Deita_Query_Execute_Update_Prepared(db_conn, update_error, 2, error_params); |
| 201 | 2004 unlink(tmp_input); |
| 2005 unlink(tmp_output); | |
| 2006 Deita_Connection_Close(db_conn); | |
|
250
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
2007 return; |
| 201 | 2008 } |
| 2009 | |
|
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] Successfully uploaded processed file to S3\n"); |
| 201 | 2011 |
| 2012 // Update status to 'finished' | |
| 2013 const char *update_finished = | |
| 2014 "UPDATE media_uploads SET status='finished', updated_at=strftime('%s','now') WHERE id=?"; | |
| 2015 Deita_Query_Execute_Update_Prepared(db_conn, update_finished, 1, params); | |
| 2016 | |
|
202
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2017 Seobeo_Log(SEOBEO_INFO, "[MEDIA] Successfully processed media_id=%lld - COMPLETE\n", (long long)ctx->media_id); |
| 201 | 2018 |
| 2019 // Cleanup | |
| 2020 unlink(tmp_input); | |
| 2021 unlink(tmp_output); | |
| 2022 Deita_Connection_Close(db_conn); | |
| 2023 } | |
| 2024 | |
| 2025 // Media Upload API - Mark uploaded | |
| 2026 // POST /api/media/:id/uploaded | |
| 2027 // Headers: Authorization: Bearer <token> | |
| 2028 Seobeo_Request_Entry *MediaUploaded(Seobeo_Request_Entry *req, Dowa_Arena *arena) | |
| 2029 { | |
| 2030 Seobeo_Request_Entry *resp = NULL; | |
| 2031 | |
| 2032 // Check auth token | |
| 2033 void *auth_kv = Dowa_HashMap_Get_Ptr(req, "Authorization"); | |
| 2034 if (!auth_kv) | |
| 2035 { | |
| 2036 Dowa_HashMap_Push_Arena(resp, "status", "401", arena); | |
| 2037 Dowa_HashMap_Push_Arena(resp, "content-type", "application/json", arena); | |
| 2038 Dowa_HashMap_Push_Arena(resp, "body", "{\"error\":\"Missing Authorization header\"}", arena); | |
| 2039 return resp; | |
| 2040 } | |
| 2041 | |
| 2042 const char *auth_header = ((Seobeo_Request_Entry*)auth_kv)->value; | |
| 2043 if (strncmp(auth_header, "Bearer ", 7) != 0) | |
| 2044 { | |
| 2045 Dowa_HashMap_Push_Arena(resp, "status", "401", arena); | |
| 2046 Dowa_HashMap_Push_Arena(resp, "content-type", "application/json", arena); | |
| 2047 Dowa_HashMap_Push_Arena(resp, "body", "{\"error\":\"Invalid Authorization format\"}", arena); | |
| 2048 return resp; | |
| 2049 } | |
| 2050 | |
| 2051 const char *token = auth_header + 7; | |
| 2052 if (strlen(g_upload_auth_token) == 0 || strcmp(token, g_upload_auth_token) != 0) | |
| 2053 { | |
| 2054 Dowa_HashMap_Push_Arena(resp, "status", "403", arena); | |
| 2055 Dowa_HashMap_Push_Arena(resp, "content-type", "application/json", arena); | |
| 2056 Dowa_HashMap_Push_Arena(resp, "body", "{\"error\":\"Invalid token\"}", arena); | |
| 2057 return resp; | |
| 2058 } | |
| 2059 | |
| 2060 if (!g_db_connection) | |
| 2061 { | |
| 2062 Dowa_HashMap_Push_Arena(resp, "status", "500", arena); | |
| 2063 Dowa_HashMap_Push_Arena(resp, "content-type", "application/json", arena); | |
| 2064 Dowa_HashMap_Push_Arena(resp, "body", "{\"error\":\"Database not available\"}", arena); | |
| 2065 return resp; | |
| 2066 } | |
| 2067 | |
| 2068 // Extract media_id from URL params | |
| 2069 void *id_kv = Dowa_HashMap_Get_Ptr(req, ":id"); | |
| 2070 if (!id_kv) | |
| 2071 { | |
| 2072 Dowa_HashMap_Push_Arena(resp, "status", "400", arena); | |
| 2073 Dowa_HashMap_Push_Arena(resp, "content-type", "application/json", arena); | |
| 2074 Dowa_HashMap_Push_Arena(resp, "body", "{\"error\":\"Missing media ID\"}", arena); | |
| 2075 return resp; | |
| 2076 } | |
| 2077 | |
| 2078 const char *media_id_str = ((Seobeo_Request_Entry*)id_kv)->value; | |
| 2079 int64 media_id = atoll(media_id_str); | |
| 2080 | |
| 2081 // Verify access_token matches and get content_type | |
| 2082 const char *select_query = | |
| 2083 "SELECT content_type, s3_key_original, s3_key_processed FROM media_uploads WHERE id = ? AND access_token = ?"; | |
| 2084 const char *select_params[] = { media_id_str, token }; | |
| 2085 | |
| 2086 Deita_Result_Set *p_result = Deita_Query_Execute_Prepared(g_db_connection, select_query, 2, select_params, arena); | |
| 2087 | |
| 2088 if (!p_result || !Deita_Result_Set_Next(p_result)) | |
| 2089 { | |
| 2090 if (p_result) Deita_Result_Set_Free(p_result); | |
| 2091 Dowa_HashMap_Push_Arena(resp, "status", "404", arena); | |
| 2092 Dowa_HashMap_Push_Arena(resp, "content-type", "application/json", arena); | |
| 2093 Dowa_HashMap_Push_Arena(resp, "body", "{\"error\":\"Media not found or access denied\"}", arena); | |
| 2094 return resp; | |
| 2095 } | |
| 2096 | |
| 2097 const char *content_type = Deita_Result_Set_Get_Text(p_result, 0); | |
| 2098 const char *s3_key_original = Deita_Result_Set_Get_Text(p_result, 1); | |
| 2099 const char *s3_key_processed = Deita_Result_Set_Get_Text(p_result, 2); | |
| 2100 | |
| 2101 // Copy values before freeing result set | |
| 2102 char content_type_copy[128]; | |
| 2103 char s3_key_original_copy[512]; | |
| 2104 char s3_key_processed_copy[512]; | |
| 2105 strncpy(content_type_copy, content_type, sizeof(content_type_copy) - 1); | |
| 2106 strncpy(s3_key_original_copy, s3_key_original, sizeof(s3_key_original_copy) - 1); | |
| 2107 strncpy(s3_key_processed_copy, s3_key_processed, sizeof(s3_key_processed_copy) - 1); | |
| 2108 content_type_copy[sizeof(content_type_copy) - 1] = '\0'; | |
| 2109 s3_key_original_copy[sizeof(s3_key_original_copy) - 1] = '\0'; | |
| 2110 s3_key_processed_copy[sizeof(s3_key_processed_copy) - 1] = '\0'; | |
| 2111 | |
| 2112 Deita_Result_Set_Free(p_result); | |
| 2113 | |
| 2114 // Update status to 'uploaded' | |
| 2115 const char *update_query = | |
| 2116 "UPDATE media_uploads SET status='uploaded', updated_at=strftime('%s','now') WHERE id=?"; | |
| 2117 const char *update_params[] = { media_id_str }; | |
| 2118 int32 result = Deita_Query_Execute_Update_Prepared(g_db_connection, update_query, 1, update_params); | |
| 2119 | |
| 2120 if (result < 0) | |
| 2121 { | |
| 2122 Dowa_HashMap_Push_Arena(resp, "status", "500", arena); | |
| 2123 Dowa_HashMap_Push_Arena(resp, "content-type", "application/json", arena); | |
| 2124 Dowa_HashMap_Push_Arena(resp, "body", "{\"error\":\"Failed to update status\"}", arena); | |
| 2125 return resp; | |
| 2126 } | |
| 2127 | |
|
202
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2128 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
|
2129 |
|
250
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
2130 // Images are processed asynchronously by the bounded media pool. |
| 201 | 2131 if (strncmp(content_type_copy, "image/", 6) == 0) |
| 2132 { | |
|
250
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
2133 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
|
2134 |
|
250
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
2135 // The pool owns this context after a successful submission. |
| 201 | 2136 Media_Processing_Context *ctx = malloc(sizeof(Media_Processing_Context)); |
|
250
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
2137 if (!ctx) |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
2138 { |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
2139 Dowa_HashMap_Push_Arena(resp, "status", "500", arena); |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
2140 Dowa_HashMap_Push_Arena(resp, "content-type", "application/json", arena); |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
2141 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
|
2142 return resp; |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
2143 } |
| 201 | 2144 ctx->media_id = media_id; |
| 2145 strncpy(ctx->s3_key_original, s3_key_original_copy, sizeof(ctx->s3_key_original) - 1); | |
| 2146 strncpy(ctx->s3_key_processed, s3_key_processed_copy, sizeof(ctx->s3_key_processed) - 1); | |
| 2147 strncpy(ctx->content_type, content_type_copy, sizeof(ctx->content_type) - 1); | |
| 2148 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
|
2149 strncpy(ctx->db_path, g_db_path, sizeof(ctx->db_path) - 1); |
| 201 | 2150 ctx->s3_key_original[sizeof(ctx->s3_key_original) - 1] = '\0'; |
| 2151 ctx->s3_key_processed[sizeof(ctx->s3_key_processed) - 1] = '\0'; | |
| 2152 ctx->content_type[sizeof(ctx->content_type) - 1] = '\0'; | |
| 2153 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
|
2154 ctx->db_path[sizeof(ctx->db_path) - 1] = '\0'; |
| 201 | 2155 ctx->s3_config = g_s3_config; |
| 2156 | |
|
250
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
2157 Seobeo_Worker_Result worker_result = |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
2158 g_media_worker_pool |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
2159 ? Seobeo_Worker_Pool_Submit( |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
2160 g_media_worker_pool, |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
2161 Media_Process_Background, |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
2162 ctx, |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
2163 free) |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
2164 : SEOBEO_WORKER_STOPPED; |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
2165 if (worker_result != SEOBEO_WORKER_OK) |
| 201 | 2166 { |
|
250
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
2167 Seobeo_Log( |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
2168 SEOBEO_ERROR, |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
2169 "[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
|
2170 worker_result, |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
2171 (long long)media_id); |
| 201 | 2172 free(ctx); |
|
250
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
2173 const char *update_error = |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
2174 "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
|
2175 const char *error_params[] = { |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
2176 "Media worker queue is unavailable", |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
2177 media_id_str, |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
2178 }; |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
2179 Deita_Query_Execute_Update_Prepared( |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
2180 g_db_connection, |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
2181 update_error, |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
2182 2, |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
2183 error_params); |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
2184 Dowa_HashMap_Push_Arena(resp, "status", "503", arena); |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
2185 Dowa_HashMap_Push_Arena(resp, "content-type", "application/json", arena); |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
2186 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
|
2187 return resp; |
| 201 | 2188 } |
|
250
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
2189 Seobeo_Log( |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
2190 SEOBEO_INFO, |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
2191 "[MEDIA] Submitted media_id=%lld to the worker pool\n", |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
2192 (long long)media_id); |
| 201 | 2193 } |
|
202
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2194 else |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2195 { |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2196 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
|
2197 } |
| 201 | 2198 |
| 2199 Dowa_HashMap_Push_Arena(resp, "status", "200", arena); | |
| 2200 Dowa_HashMap_Push_Arena(resp, "content-type", "application/json", arena); | |
| 2201 Dowa_HashMap_Push_Arena(resp, "body", "{\"success\":true,\"status\":\"uploaded\"}", arena); | |
| 2202 | |
| 2203 Seobeo_Log(SEOBEO_INFO, "[MEDIA] Marked uploaded media_id=%lld\n", (long long)media_id); | |
| 2204 | |
| 2205 return resp; | |
| 2206 } | |
| 2207 | |
| 2208 // Media Upload API - Get status | |
| 2209 // GET /api/media/:id/status | |
| 2210 // Headers: Authorization: Bearer <token> | |
| 2211 // Returns: {"id": 123, "status": "finished", "processed_url": "https://...", "error_message": null} | |
| 2212 Seobeo_Request_Entry *MediaStatus(Seobeo_Request_Entry *req, Dowa_Arena *arena) | |
| 2213 { | |
| 2214 Seobeo_Request_Entry *resp = NULL; | |
| 2215 | |
| 2216 // Check auth token | |
| 2217 void *auth_kv = Dowa_HashMap_Get_Ptr(req, "Authorization"); | |
| 2218 if (!auth_kv) | |
| 2219 { | |
| 2220 Dowa_HashMap_Push_Arena(resp, "status", "401", arena); | |
| 2221 Dowa_HashMap_Push_Arena(resp, "content-type", "application/json", arena); | |
| 2222 Dowa_HashMap_Push_Arena(resp, "body", "{\"error\":\"Missing Authorization header\"}", arena); | |
| 2223 return resp; | |
| 2224 } | |
| 2225 | |
| 2226 const char *auth_header = ((Seobeo_Request_Entry*)auth_kv)->value; | |
| 2227 if (strncmp(auth_header, "Bearer ", 7) != 0) | |
| 2228 { | |
| 2229 Dowa_HashMap_Push_Arena(resp, "status", "401", arena); | |
| 2230 Dowa_HashMap_Push_Arena(resp, "content-type", "application/json", arena); | |
| 2231 Dowa_HashMap_Push_Arena(resp, "body", "{\"error\":\"Invalid Authorization format\"}", arena); | |
| 2232 return resp; | |
| 2233 } | |
| 2234 | |
| 2235 const char *token = auth_header + 7; | |
| 2236 if (strlen(g_upload_auth_token) == 0 || strcmp(token, g_upload_auth_token) != 0) | |
| 2237 { | |
| 2238 Dowa_HashMap_Push_Arena(resp, "status", "403", arena); | |
| 2239 Dowa_HashMap_Push_Arena(resp, "content-type", "application/json", arena); | |
| 2240 Dowa_HashMap_Push_Arena(resp, "body", "{\"error\":\"Invalid token\"}", arena); | |
| 2241 return resp; | |
| 2242 } | |
| 2243 | |
| 2244 if (!g_db_connection) | |
| 2245 { | |
| 2246 Dowa_HashMap_Push_Arena(resp, "status", "500", arena); | |
| 2247 Dowa_HashMap_Push_Arena(resp, "content-type", "application/json", arena); | |
| 2248 Dowa_HashMap_Push_Arena(resp, "body", "{\"error\":\"Database not available\"}", arena); | |
| 2249 return resp; | |
| 2250 } | |
| 2251 | |
| 2252 // Extract media_id from URL params | |
| 2253 void *id_kv = Dowa_HashMap_Get_Ptr(req, ":id"); | |
| 2254 if (!id_kv) | |
| 2255 { | |
| 2256 Dowa_HashMap_Push_Arena(resp, "status", "400", arena); | |
| 2257 Dowa_HashMap_Push_Arena(resp, "content-type", "application/json", arena); | |
| 2258 Dowa_HashMap_Push_Arena(resp, "body", "{\"error\":\"Missing media ID\"}", arena); | |
| 2259 return resp; | |
| 2260 } | |
| 2261 | |
| 2262 const char *media_id_str = ((Seobeo_Request_Entry*)id_kv)->value; | |
| 2263 | |
| 2264 // Query media status | |
| 2265 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
|
2266 "SELECT id, status, s3_key_original, s3_key_processed, error_message FROM media_uploads WHERE id = ? AND access_token = ?"; |
| 201 | 2267 const char *select_params[] = { media_id_str, token }; |
| 2268 | |
| 2269 Deita_Result_Set *p_result = Deita_Query_Execute_Prepared(g_db_connection, select_query, 2, select_params, arena); | |
| 2270 | |
| 2271 if (!p_result || !Deita_Result_Set_Next(p_result)) | |
| 2272 { | |
| 2273 if (p_result) Deita_Result_Set_Free(p_result); | |
| 2274 Dowa_HashMap_Push_Arena(resp, "status", "404", arena); | |
| 2275 Dowa_HashMap_Push_Arena(resp, "content-type", "application/json", arena); | |
| 2276 Dowa_HashMap_Push_Arena(resp, "body", "{\"error\":\"Media not found\"}", arena); | |
| 2277 return resp; | |
| 2278 } | |
| 2279 | |
| 2280 int64 id = Deita_Result_Set_Get_Integer(p_result, 0); | |
| 2281 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
|
2282 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
|
2283 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
|
2284 const char *error_message = Deita_Result_Set_Get_Text(p_result, 4); |
| 201 | 2285 |
|
202
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2286 // Build CloudFront URL for processed file if status is 'finished' |
| 201 | 2287 char processed_url[1024] = {0}; |
| 2288 if (strcmp(status, "finished") == 0 && s3_key_processed && strlen(s3_key_processed) > 0) | |
| 2289 { | |
| 2290 if (g_s3_cloudfront_url[0]) | |
| 2291 { | |
| 2292 snprintf(processed_url, sizeof(processed_url), "%s/%s", g_s3_cloudfront_url, s3_key_processed); | |
| 2293 } | |
| 2294 else | |
| 2295 { | |
| 2296 snprintf(processed_url, sizeof(processed_url), "https://%s.s3.%s.amazonaws.com/%s", | |
| 2297 g_s3_bucket, g_s3_region, s3_key_processed); | |
| 2298 } | |
| 2299 } | |
| 2300 | |
|
202
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2301 // 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
|
2302 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
|
2303 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
|
2304 { |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2305 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
|
2306 { |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2307 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
|
2308 } |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2309 else |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2310 { |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2311 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
|
2312 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
|
2313 } |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2314 } |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2315 |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2316 // 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
|
2317 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
|
2318 |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2319 // Build the base response |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2320 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
|
2321 "{\"id\":%lld,\"status\":\"%s\",", |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2322 (long long)id, status); |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2323 |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2324 // Add processed_url |
| 201 | 2325 if (strlen(processed_url) > 0) |
| 2326 { | |
|
202
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2327 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
|
2328 "\"processed_url\":\"%s\",", processed_url); |
| 201 | 2329 } |
| 2330 else | |
| 2331 { | |
|
202
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2332 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
|
2333 "\"processed_url\":null,"); |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2334 } |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2335 |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2336 // Add original_url |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2337 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
|
2338 { |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2339 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
|
2340 "\"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
|
2341 } |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2342 else |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2343 { |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2344 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
|
2345 "\"original_url\":null,"); |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2346 } |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2347 |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2348 // Add error_message |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2349 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
|
2350 { |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2351 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
|
2352 "\"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
|
2353 } |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2354 else |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2355 { |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2356 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
|
2357 "\"error_message\":null}"); |
| 201 | 2358 } |
| 2359 | |
| 2360 Deita_Result_Set_Free(p_result); | |
| 2361 | |
| 2362 Dowa_HashMap_Push_Arena(resp, "status", "200", arena); | |
| 2363 Dowa_HashMap_Push_Arena(resp, "content-type", "application/json", arena); | |
| 2364 Dowa_HashMap_Push_Arena(resp, "body", response_body, arena); | |
| 2365 | |
| 2366 return resp; | |
| 2367 } | |
| 2368 | |
|
7
114cad94008f
[Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
2369 int main(void) |
|
114cad94008f
[Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
2370 { |
|
260
1f9877b637e9
Add Copilot-powered cyberpunk JRPG chat
MrJuneJune <mrjunejune@users.noreply.github.com>
parents:
259
diff
changeset
|
2371 signal(SIGINT, handle_sigint); |
|
1f9877b637e9
Add Copilot-powered cyberpunk JRPG chat
MrJuneJune <mrjunejune@users.noreply.github.com>
parents:
259
diff
changeset
|
2372 signal(SIGTERM, handle_sigint); |
|
1f9877b637e9
Add Copilot-powered cyberpunk JRPG chat
MrJuneJune <mrjunejune@users.noreply.github.com>
parents:
259
diff
changeset
|
2373 |
|
264
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2374 // 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
|
2375 const char *config_path = getenv("MRJUNEJUNE_CONFIG_PATH"); |
|
056790c4fb0d
add role-aware Epi assistant prompts
MrJuneJune <me@mrjunejune.com>
parents:
264
diff
changeset
|
2376 load_config( |
|
056790c4fb0d
add role-aware Epi assistant prompts
MrJuneJune <me@mrjunejune.com>
parents:
264
diff
changeset
|
2377 config_path && config_path[0] != '\0' |
|
056790c4fb0d
add role-aware Epi assistant prompts
MrJuneJune <me@mrjunejune.com>
parents:
264
diff
changeset
|
2378 ? config_path |
|
056790c4fb0d
add role-aware Epi assistant prompts
MrJuneJune <me@mrjunejune.com>
parents:
264
diff
changeset
|
2379 : "mrjunejune/.config"); |
|
200
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
2380 |
|
264
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2381 // Initialize S3 config using global credentials populated by load_config |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2382 g_s3_config.access_key_id = g_s3_access_key; |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2383 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
|
2384 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
|
2385 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
|
2386 g_s3_config.endpoint = NULL; |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
2387 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
|
2388 |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
2389 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
|
2390 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
|
2391 |
|
202
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2392 // Show current working directory |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2393 char cwd[1024]; |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2394 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
|
2395 { |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2396 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
|
2397 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
|
2398 } |
|
b9b184b3303c
[Notes] Images get processed and it is properly fetched. Thank you.
MrJuneJune <me@mrjunejune.com>
parents:
201
diff
changeset
|
2399 |
| 201 | 2400 // Initialize database |
| 2401 init_database(); | |
|
264
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2402 { |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2403 /* 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
|
2404 * are finalised (env overrides run inside load_config). */ |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2405 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
|
2406 g_guest_request_output_tokens = g_guest_daily_output_tokens; |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2407 |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2408 Conversation_API_Guest_Policy policy = { |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2409 .guest_inference_enabled = g_guest_inference_enabled, |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2410 .daily_turns = g_guest_daily_turns, |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2411 .daily_output_tokens = g_guest_daily_output_tokens, |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2412 .request_output_tokens = g_guest_request_output_tokens, |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2413 }; |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2414 if (!Conversation_API_Init(g_db_path, &policy)) |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2415 Seobeo_Log(SEOBEO_ERROR, "[CONVERSATION] Store unavailable\n"); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2416 } |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2417 |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2418 /* Validate and decode AUTH_COOKIE_SECRET: hex string → raw bytes. */ |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2419 uint8 cookie_secret_bytes[AUTH_CRYPTO_COOKIE_SECRET_MAX_BYTES]; |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2420 size_t cookie_secret_byte_len = 0; |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2421 { |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2422 size_t hex_len = strlen(g_auth_cookie_secret); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2423 boolean bad_len = ( |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2424 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
|
2425 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
|
2426 hex_len % 2 != 0); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2427 if (bad_len) |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2428 { |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2429 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
|
2430 fprintf(stderr, |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2431 "[AUTH] AUTH_COOKIE_SECRET must be %d–%d hex chars " |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2432 "(%d–%d random bytes). " |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2433 "Generate with: openssl rand -hex 32\n", |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2434 AUTH_CRYPTO_COOKIE_SECRET_MIN_BYTES * 2, |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2435 AUTH_CRYPTO_COOKIE_SECRET_MAX_BYTES * 2, |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2436 AUTH_CRYPTO_COOKIE_SECRET_MIN_BYTES, |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2437 AUTH_CRYPTO_COOKIE_SECRET_MAX_BYTES); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2438 exit(1); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2439 } |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2440 cookie_secret_byte_len = config__hex_decode( |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2441 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
|
2442 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
|
2443 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
|
2444 { |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2445 OPENSSL_cleanse(cookie_secret_bytes, sizeof(cookie_secret_bytes)); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2446 fprintf(stderr, |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2447 "[AUTH] AUTH_COOKIE_SECRET contains non-hex characters or " |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2448 "is too short.\n"); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2449 exit(1); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2450 } |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2451 } |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2452 |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2453 /* 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
|
2454 { |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2455 boolean has_user = g_auth_bootstrap_username[0] != '\0'; |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2456 boolean has_hash = g_auth_bootstrap_password_hash[0] != '\0'; |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2457 if (has_user != has_hash) |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2458 { |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2459 OPENSSL_cleanse(cookie_secret_bytes, sizeof(cookie_secret_bytes)); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2460 fprintf(stderr, |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2461 "[AUTH] AUTH_BOOTSTRAP_USERNAME and AUTH_BOOTSTRAP_PASSWORD_HASH " |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2462 "must both be set or both absent.\n"); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2463 exit(1); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2464 } |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2465 if (has_hash && |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2466 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
|
2467 { |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2468 OPENSSL_cleanse(cookie_secret_bytes, sizeof(cookie_secret_bytes)); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2469 fprintf(stderr, |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2470 "[AUTH] AUTH_BOOTSTRAP_PASSWORD_HASH has unrecognized format " |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2471 "(expected zenbu-scrypt$v=1$...).\n"); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2472 exit(1); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2473 } |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2474 } |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2475 |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2476 /* 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
|
2477 #define CONFIG_TTL_MIN_SECS 60 |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2478 #define CONFIG_TTL_MAX_SECS 31536000 |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2479 { |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2480 int64 idle = g_auth_session_idle_ttl; |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2481 int64 abst = g_auth_session_abs_ttl; |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2482 int64 guest = g_auth_guest_ttl; |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2483 if (idle <= 0 || abst <= 0 || guest <= 0 || |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2484 idle < CONFIG_TTL_MIN_SECS || idle > CONFIG_TTL_MAX_SECS || |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2485 abst < CONFIG_TTL_MIN_SECS || abst > CONFIG_TTL_MAX_SECS || |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2486 guest < CONFIG_TTL_MIN_SECS || guest > CONFIG_TTL_MAX_SECS) |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2487 { |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2488 OPENSSL_cleanse(cookie_secret_bytes, sizeof(cookie_secret_bytes)); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2489 fprintf(stderr, |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2490 "[AUTH] TTL values must be %d–%d seconds.\n", |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2491 CONFIG_TTL_MIN_SECS, CONFIG_TTL_MAX_SECS); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2492 exit(1); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2493 } |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2494 if (idle > abst) |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2495 { |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2496 OPENSSL_cleanse(cookie_secret_bytes, sizeof(cookie_secret_bytes)); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2497 fprintf(stderr, |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2498 "[AUTH] AUTH_SESSION_IDLE_TTL must not exceed " |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2499 "AUTH_SESSION_ABS_TTL.\n"); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2500 exit(1); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2501 } |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2502 } |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2503 |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2504 /* Trusted proxy: validate and canonicalize via inet_pton/inet_ntop. */ |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2505 if (g_auth_trusted_proxy[0] != '\0') |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2506 { |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2507 struct in_addr addr4; |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2508 struct in6_addr addr6; |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2509 char canonical[AUTH_CRYPTO_IP_MAX_BYTES]; |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2510 canonical[0] = '\0'; |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2511 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
|
2512 { |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2513 if (!inet_ntop(AF_INET, &addr4, canonical, sizeof(canonical))) |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2514 { |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2515 OPENSSL_cleanse(cookie_secret_bytes, sizeof(cookie_secret_bytes)); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2516 fprintf(stderr, |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2517 "[AUTH] AUTH_TRUSTED_PROXY: IPv4 canonicalization failed.\n"); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2518 exit(1); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2519 } |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2520 } |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2521 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
|
2522 { |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2523 if (!inet_ntop(AF_INET6, &addr6, canonical, sizeof(canonical))) |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2524 { |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2525 OPENSSL_cleanse(cookie_secret_bytes, sizeof(cookie_secret_bytes)); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2526 fprintf(stderr, |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2527 "[AUTH] AUTH_TRUSTED_PROXY: IPv6 canonicalization failed.\n"); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2528 exit(1); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2529 } |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2530 } |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2531 else |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2532 { |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2533 OPENSSL_cleanse(cookie_secret_bytes, sizeof(cookie_secret_bytes)); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2534 fprintf(stderr, |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2535 "[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
|
2536 "address (e.g. 192.168.1.1 or ::1).\n"); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2537 exit(1); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2538 } |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2539 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
|
2540 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
|
2541 } |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2542 |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2543 /* |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2544 * Loopback enforcement: AUTH_DEV_INSECURE_COOKIE=true is only permitted |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2545 * when SERVER_HOST is explicitly a loopback address. Production/edge |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2546 * deployments must use Secure cookies. |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2547 */ |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2548 { |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2549 boolean is_loopback = ( |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2550 strcmp(g_server_host, "127.0.0.1") == 0 || |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2551 strcmp(g_server_host, "::1") == 0 || |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2552 strcmp(g_server_host, "localhost") == 0); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2553 if (g_auth_dev_insecure && !is_loopback) |
|
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 OPENSSL_cleanse(cookie_secret_bytes, sizeof(cookie_secret_bytes)); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2556 fprintf(stderr, |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2557 "[AUTH] AUTH_DEV_INSECURE_COOKIE=true requires SERVER_HOST " |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2558 "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
|
2559 "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
|
2560 exit(1); |
|
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 } |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2563 |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2564 /* Initialize auth module — fail closed: no auth means no server. */ |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2565 { |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2566 if (!Auth_API_Init( |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2567 g_db_path, |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2568 cookie_secret_bytes, cookie_secret_byte_len, |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2569 g_auth_bootstrap_username[0] ? g_auth_bootstrap_username : NULL, |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2570 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
|
2571 g_auth_trusted_proxy[0] ? g_auth_trusted_proxy : NULL, |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2572 g_auth_session_idle_ttl, |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2573 g_auth_session_abs_ttl, |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2574 g_auth_guest_ttl, |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2575 g_auth_dev_insecure)) |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2576 { |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2577 OPENSSL_cleanse(cookie_secret_bytes, sizeof(cookie_secret_bytes)); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2578 fprintf(stderr, |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2579 "[AUTH] Auth init failed — refusing to start server.\n"); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2580 exit(1); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2581 } |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2582 OPENSSL_cleanse(cookie_secret_bytes, sizeof(cookie_secret_bytes)); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2583 } |
|
260
1f9877b637e9
Add Copilot-powered cyberpunk JRPG chat
MrJuneJune <mrjunejune@users.noreply.github.com>
parents:
259
diff
changeset
|
2584 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
|
2585 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
|
2586 if (sidecar_path && copilot_cli_path) |
|
1f9877b637e9
Add Copilot-powered cyberpunk JRPG chat
MrJuneJune <mrjunejune@users.noreply.github.com>
parents:
259
diff
changeset
|
2587 { |
|
1f9877b637e9
Add Copilot-powered cyberpunk JRPG chat
MrJuneJune <mrjunejune@users.noreply.github.com>
parents:
259
diff
changeset
|
2588 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
|
2589 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
|
2590 } |
|
1f9877b637e9
Add Copilot-powered cyberpunk JRPG chat
MrJuneJune <mrjunejune@users.noreply.github.com>
parents:
259
diff
changeset
|
2591 else |
|
1f9877b637e9
Add Copilot-powered cyberpunk JRPG chat
MrJuneJune <mrjunejune@users.noreply.github.com>
parents:
259
diff
changeset
|
2592 { |
|
1f9877b637e9
Add Copilot-powered cyberpunk JRPG chat
MrJuneJune <mrjunejune@users.noreply.github.com>
parents:
259
diff
changeset
|
2593 Seobeo_Log( |
|
1f9877b637e9
Add Copilot-powered cyberpunk JRPG chat
MrJuneJune <mrjunejune@users.noreply.github.com>
parents:
259
diff
changeset
|
2594 SEOBEO_WARNING, |
|
1f9877b637e9
Add Copilot-powered cyberpunk JRPG chat
MrJuneJune <mrjunejune@users.noreply.github.com>
parents:
259
diff
changeset
|
2595 "[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
|
2596 } |
| 201 | 2597 |
|
250
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
2598 g_media_worker_pool = Seobeo_Worker_Pool_Create(2, 16); |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
2599 if (!g_media_worker_pool) |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
2600 { |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
2601 Seobeo_Log( |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
2602 SEOBEO_ERROR, |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
2603 "[MEDIA] Unable to initialize the media worker pool\n"); |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
2604 } |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
2605 |
|
72
4532ce6d9eb8
[Seobeo] Added router to the server logic. Few dowa string manipulation logics.
June Park <parkjune1995@gmail.com>
parents:
36
diff
changeset
|
2606 Seobeo_Router_Init(); |
|
264
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2607 Auth_API_Register_Routes(); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2608 Admin_API_Register_Routes(); |
|
260
1f9877b637e9
Add Copilot-powered cyberpunk JRPG chat
MrJuneJune <mrjunejune@users.noreply.github.com>
parents:
259
diff
changeset
|
2609 Conversation_API_Register_Routes(); |
|
125
f236c895604e
[MrJuneJune] Added web socket for chat to this.
June Park <parkjune1995@gmail.com>
parents:
124
diff
changeset
|
2610 |
|
77
c348ac875294
[Seobeo] Server side rendering.
June Park <parkjune1995@gmail.com>
parents:
72
diff
changeset
|
2611 Seobeo_Router_Register("GET", "/", GetHomePage); |
|
79
5710108c949e
[Seobeo] Added Redirect logic.
June Park <parkjune1995@gmail.com>
parents:
78
diff
changeset
|
2612 Seobeo_Router_Register("GET", "/index.html", GetRedirectHomePage); |
| 78 | 2613 |
| 2614 Seobeo_Router_Register("GET", "/resume", GetResume); | |
|
79
5710108c949e
[Seobeo] Added Redirect logic.
June Park <parkjune1995@gmail.com>
parents:
78
diff
changeset
|
2615 Seobeo_Router_Register("GET", "/resume/index.html", GetRedirectResume); |
| 78 | 2616 |
| 2617 Seobeo_Router_Register("GET", "/tools", GetTools); | |
|
79
5710108c949e
[Seobeo] Added Redirect logic.
June Park <parkjune1995@gmail.com>
parents:
78
diff
changeset
|
2618 Seobeo_Router_Register("GET", "/tools/index.html", GetRedirectTools); |
| 78 | 2619 |
| 2620 Seobeo_Router_Register("GET", "/tools/markdown_to_html", GetMDToHTML); | |
|
79
5710108c949e
[Seobeo] Added Redirect logic.
June Park <parkjune1995@gmail.com>
parents:
78
diff
changeset
|
2621 Seobeo_Router_Register("GET", "/tools/markdown_to_html/index.html", GetRedirectMarkDownToHtml); |
| 78 | 2622 |
|
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
|
2623 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
|
2624 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
|
2625 Seobeo_Router_Register("GET", "/tools/hls_player", GetHlsPlayer); |
|
543df0fe7168
[tools] Add full HLS player support
MrJuneJune <me@mrjunejune.com>
parents:
241
diff
changeset
|
2626 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
|
2627 Seobeo_Router_Register("GET", "/tools/latex_editor", GetLatexEditor); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
2628 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
|
2629 |
|
100
65e5a5b89a4e
[Seobeo] Migrated everything to this page.
June Park <parkjune1995@gmail.com>
parents:
96
diff
changeset
|
2630 // -- 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
|
2631 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
|
2632 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
|
2633 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
|
2634 Seobeo_Router_Register("DELETE", "/api/download/:filename", DeleteConvertedFile); |
|
244
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
242
diff
changeset
|
2635 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
|
2636 |
|
200
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
2637 // -- S3 Upload --/ |
|
90dfcef375fb
Added my own s3 bucket uploader url to mrjunejune.
MrJuneJune <me@mrjunejune.com>
parents:
169
diff
changeset
|
2638 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
|
2639 |
| 201 | 2640 // -- Media Upload --/ |
| 2641 Seobeo_Router_Register("POST", "/api/media/create", MediaCreate); | |
| 2642 Seobeo_Router_Register("POST", "/api/media/:id/uploaded", MediaUploaded); | |
| 2643 Seobeo_Router_Register("GET", "/api/media/:id/status", MediaStatus); | |
| 2644 | |
| 2645 // -- Editor --/ | |
| 2646 Seobeo_Router_Register("POST", "/api/editor/save", EditorSave); | |
| 2647 Seobeo_Router_Register("GET", "/api/editor/load/:doc_id", EditorLoad); | |
| 2648 | |
|
100
65e5a5b89a4e
[Seobeo] Migrated everything to this page.
June Park <parkjune1995@gmail.com>
parents:
96
diff
changeset
|
2649 // -- Blog --/ |
|
65e5a5b89a4e
[Seobeo] Migrated everything to this page.
June Park <parkjune1995@gmail.com>
parents:
96
diff
changeset
|
2650 Seobeo_Router_Register("GET", "/blog", RenderBlogList); |
|
65e5a5b89a4e
[Seobeo] Migrated everything to this page.
June Park <parkjune1995@gmail.com>
parents:
96
diff
changeset
|
2651 Seobeo_Router_Register("GET", "/blog/:blog_id", RenderBlog); |
|
65e5a5b89a4e
[Seobeo] Migrated everything to this page.
June Park <parkjune1995@gmail.com>
parents:
96
diff
changeset
|
2652 |
|
125
f236c895604e
[MrJuneJune] Added web socket for chat to this.
June Park <parkjune1995@gmail.com>
parents:
124
diff
changeset
|
2653 // -- Talk --/ |
|
f236c895604e
[MrJuneJune] Added web socket for chat to this.
June Park <parkjune1995@gmail.com>
parents:
124
diff
changeset
|
2654 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
|
2655 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
|
2656 |
|
260
1f9877b637e9
Add Copilot-powered cyberpunk JRPG chat
MrJuneJune <mrjunejune@users.noreply.github.com>
parents:
259
diff
changeset
|
2657 // -- JRPG agent chat --/ |
|
259
667156fcd3e3
Add dev-only cyberpunk JRPG page
MrJuneJune <me@mrjunejune.com>
parents:
250
diff
changeset
|
2658 Seobeo_Router_Register("GET", "/jrpg", GetJrpg); |
|
667156fcd3e3
Add dev-only cyberpunk JRPG page
MrJuneJune <me@mrjunejune.com>
parents:
250
diff
changeset
|
2659 Seobeo_Router_Register("GET", "/jrpg/index.html", GetRedirectJrpg); |
|
667156fcd3e3
Add dev-only cyberpunk JRPG page
MrJuneJune <me@mrjunejune.com>
parents:
250
diff
changeset
|
2660 |
|
264
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2661 // -- Login --/ |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2662 Seobeo_Router_Register("GET", "/login", GetLogin); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2663 |
| 201 | 2664 // -- Notes --/ |
| 2665 Seobeo_Router_Register("GET", "/notes", GetNotes); | |
| 2666 Seobeo_Router_Register("GET", "/notes/", GetNotes); | |
| 2667 Seobeo_Router_Register("GET", "/notes/index.html", GetNotes); | |
| 2668 Seobeo_Router_Register("GET", "/notes/login", GetNotesLogin); | |
| 2669 Seobeo_Router_Register("GET", "/notes/login/", GetNotesLogin); | |
| 2670 Seobeo_Router_Register("GET", "/notes/:note_id", GetNoteById); | |
| 2671 | |
|
125
f236c895604e
[MrJuneJune] Added web socket for chat to this.
June Park <parkjune1995@gmail.com>
parents:
124
diff
changeset
|
2672 Seobeo_WebSocket_Server_Init(); |
|
f236c895604e
[MrJuneJune] Added web socket for chat to this.
June Park <parkjune1995@gmail.com>
parents:
124
diff
changeset
|
2673 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
|
2674 |
| 201 | 2675 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
|
2676 const char *server_port = getenv("MRJUNEJUNE_PORT"); |
|
9c2eec61a152
[assets] Generate site images as WebP with Bazel
MrJuneJune <me@mrjunejune.com>
parents:
216
diff
changeset
|
2677 if (!server_port || server_port[0] == '\0') |
|
9c2eec61a152
[assets] Generate site images as WebP with Bazel
MrJuneJune <me@mrjunejune.com>
parents:
216
diff
changeset
|
2678 server_port = "6969"; |
|
264
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2679 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
|
2680 Mjj_Template_Renderer_Init("mrjunejune/src"); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2681 int server_result = Seobeo_Web_Server_Start_On( |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2682 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
|
2683 Seobeo_Worker_Pool_Destroy(g_media_worker_pool); |
|
745fd127b2a1
[seobeo] Add bounded worker interface
MrJuneJune <me@mrjunejune.com>
parents:
248
diff
changeset
|
2684 g_media_worker_pool = NULL; |
|
260
1f9877b637e9
Add Copilot-powered cyberpunk JRPG chat
MrJuneJune <mrjunejune@users.noreply.github.com>
parents:
259
diff
changeset
|
2685 Conversation_API_Destroy(); |
|
264
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2686 Auth_API_Destroy(); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2687 if (server_result != 0) |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2688 { |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2689 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
|
2690 server_bind, server_port); |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2691 return 1; |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2692 } |
|
04fee26ecce0
add authenticated JRPG conversation platform
MrJuneJune <me@mrjunejune.com>
parents:
263
diff
changeset
|
2693 return 0; |
|
7
114cad94008f
[Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
2694 } |