ascii-chat
Binary v0.16.114-28-gf5913657f
Library lib/v0.3.2-3302-gf5913657f
Video chat in your terminal
Loading...
Searching...
No Matches
downloads.c
Go to the documentation of this file.
1#include "mcp.h"
2#include <ctype.h>
3#include <stdlib.h>
4#include <time.h>
5#include <uv.h>
6
7const char *const mcp_output_formats[] = {
8 "png", "jpg", "jpeg", "bmp", "webp", "gif", "mp4", "mov",
9 "mkv", "webm", "avi", "flv", "ogv", "ogg", "3gp", NULL};
10
11const char *mcp_output_mime(const char *extension) {
12 static const char *const mime[] = {
13 "image/png", "image/jpeg", "image/jpeg", "image/bmp",
14 "image/webp", "image/gif", "video/mp4", "video/quicktime",
15 "video/x-matroska", "video/webm", "video/x-msvideo", "video/x-flv",
16 "video/ogg", "video/ogg", "video/3gpp"};
17 for (size_t i = 0; mcp_output_formats[i]; ++i)
18 if (!strcmp(extension, mcp_output_formats[i]))
19 return mime[i];
20 return NULL;
21}
22
23// Filenames are generated internally; strict parsing also prevents path
24// traversal.
25bool mcp_download_name(const char *name, int64_t *expires) {
26 if (!name || strlen(name) < 47)
27 return false;
28 const char *p = name;
29 while (isdigit((unsigned char)*p))
30 ++p;
31 if (p == name || p - name > 12 || *p++ != '-')
32 return false;
33 const char *digits_end = p - 1;
34 for (int i = 0; i < 32; ++i)
35 if (!((*p >= '0' && *p <= '9') || (*p >= 'a' && *p <= 'f')))
36 return false;
37 else
38 ++p;
39 if (*p++ != '.')
40 return false;
41 if (!mcp_output_mime(p))
42 return false;
43 char number[16];
44 size_t n = (size_t)(digits_end - name);
45 memcpy(number, name, n);
46 number[n] = '\0';
47 int64_t value = (int64_t)strtoll(number, NULL, 10);
48 if (value <= 0)
49 return false;
50 if (expires)
51 *expires = value;
52 return true;
53}
54
55void mcp_download_prune(const char *directory, int64_t now) {
56 uv_fs_t scan;
57 if (uv_fs_scandir(NULL, &scan, directory, 0, NULL) < 0) {
58 uv_fs_req_cleanup(&scan);
59 return;
60 }
61 uv_dirent_t entry;
62 while (uv_fs_scandir_next(&scan, &entry) == 0) {
63 int64_t expires;
64 if (!mcp_download_name(entry.name, &expires) || expires > now)
65 continue;
66 char path[PLATFORM_MAX_PATH_LENGTH];
67 int n = safe_snprintf(path, sizeof(path), "%s/%s", directory, entry.name);
68 if (n < 0 || (size_t)n >= sizeof(path))
69 continue;
70 uv_fs_t op;
71 uv_fs_unlink(NULL, &op, path, NULL);
72 uv_fs_req_cleanup(&op);
73 }
74 uv_fs_req_cleanup(&scan);
75}
76
78 yyjson_mut_doc *doc, int64_t now) {
79 yyjson_mut_val *result =
80 yyjson_mut_obj_get(yyjson_mut_doc_get_root(doc), "result");
81 yyjson_mut_val *data = yyjson_mut_obj_get(result, "structuredContent");
82 yyjson_mut_val *files = yyjson_mut_obj_get(data, "saved_files");
83 yyjson_mut_val *first = yyjson_mut_arr_get_first(files);
84 const char *old = yyjson_mut_get_str(first);
85 if (!old || !*old)
86 return ASCIICHAT_OK;
87 const char *name = strrchr(old, '/');
88 const char *back = strrchr(old, '\\');
89 if (back && (!name || back > name))
90 name = back;
91 name = name ? name + 1 : old;
92 int64_t ignored;
93 if (!mcp_download_name(name, &ignored))
94 return SET_ERRNO(ERROR_INVALID_STATE, "Invalid generated download name");
95 const char *token = strchr(name, '-');
96 int64_t expires = now + 1800;
97 char new_name[128], new_path[PLATFORM_MAX_PATH_LENGTH],
98 url[PLATFORM_MAX_PATH_LENGTH * 2];
99 int n = safe_snprintf(new_name, sizeof(new_name), "%lld%s",
100 (long long)expires, token);
101 if (n < 0 || (size_t)n >= sizeof(new_name))
102 return SET_ERRNO(ERROR_INVALID_STATE, "Download name is too long");
103 n = safe_snprintf(new_path, sizeof(new_path), "%s/%s", policy->downloads_dir,
104 new_name);
105 if (n < 0 || (size_t)n >= sizeof(new_path))
106 return SET_ERRNO(ERROR_INVALID_STATE, "Download path is too long");
107 const char *prefix = policy->downloads_url_prefix;
108 char fallback[128];
109 if (!prefix || !*prefix) {
110 SAFE_SNPRINTF(fallback, sizeof(fallback), "%s://127.0.0.1:%d",
111 policy->tls_cert && *policy->tls_cert ? "https" : "http",
112 policy->port);
113 prefix = fallback;
114 }
115 size_t prefix_len = strlen(prefix);
116 n = safe_snprintf(url, sizeof(url), "%s%sdownloads/%s", prefix,
117 prefix_len && prefix[prefix_len - 1] == '/' ? "" : "/",
118 new_name);
119 if (n < 0 || (size_t)n >= sizeof(url))
120 return SET_ERRNO(ERROR_INVALID_STATE, "Download URL is too long");
121 if (strcmp(old, new_path)) {
122 uv_fs_t fs;
123 int rc = uv_fs_link(NULL, &fs, old, new_path, NULL);
124 uv_fs_req_cleanup(&fs);
125 if (rc < 0)
126 return SET_ERRNO(ERROR_FILE_OPERATION, "Cannot publish download: %s",
127 uv_strerror(rc));
128 uv_fs_unlink(NULL, &fs, old, NULL);
129 uv_fs_req_cleanup(&fs);
130 }
131 time_t t = (time_t)expires;
132 struct tm utc;
133 char timestamp[32];
134 if (platform_gtime(&t, &utc) != ASCIICHAT_OK ||
135 !strftime(timestamp, sizeof(timestamp), "%Y-%m-%dT%H:%M:%SZ", &utc))
136 return SET_ERRNO(ERROR_INVALID_STATE, "Cannot format download expiration");
137 yyjson_mut_arr_clear(files);
138 yyjson_mut_arr_add_strcpy(doc, files, url);
139 yyjson_mut_obj_add_strcpy(doc, data, "download_url", url);
140 yyjson_mut_obj_add_strcpy(doc, data, "expires_at", timestamp);
141 yyjson_mut_val *content = yyjson_mut_obj_get(result, "content");
142 yyjson_mut_val *item = yyjson_mut_arr_get_first(content);
143 char description[sizeof(url) + 80];
144 SAFE_SNPRINTF(description, sizeof(description), "Download: %s\nExpires: %s",
145 url, timestamp);
146 yyjson_mut_obj_remove_str(item, "text");
147 yyjson_mut_obj_add_strcpy(doc, item, "text", description);
148 return ASCIICHAT_OK;
149}
#define SET_ERRNO(code,...)
const char *const mcp_output_formats[]
Definition downloads.c:7
void mcp_download_prune(const char *directory, int64_t now)
Definition downloads.c:55
asciichat_error_t mcp_download_finalize(const mcp_policy_t *policy, yyjson_mut_doc *doc, int64_t now)
Definition downloads.c:77
bool mcp_download_name(const char *name, int64_t *expires)
Definition downloads.c:25
const char * mcp_output_mime(const char *extension)
Definition downloads.c:11
#define SAFE_SNPRINTF(buffer, buffer_size,...)
asciichat_error_t
Error and exit codes - unified status values (0-255).
Definition error_codes.h:48
@ ERROR_INVALID_STATE
@ ASCIICHAT_OK
Definition error_codes.h:50
@ ERROR_FILE_OPERATION
const char * tls_cert
Definition mcp.h:41
char downloads_dir[PLATFORM_MAX_PATH_LENGTH]
Definition mcp.h:45
const char * downloads_url_prefix
Definition mcp.h:46
int port
Definition mcp.h:44
⏱️ High-precision timing utilities using sokol_time.h and uthash