From d9a15b8af7da2bc27d4d55ee745242f5b4e39071 Mon Sep 17 00:00:00 2001 From: default Date: Sun, 16 Oct 2022 18:03:28 +0200 Subject: [PATCH] Attachments are now starting to get real. --- data.c | 17 +++++++++++++++-- html.c | 27 ++++++++++++++++++++++++++- snac.h | 3 ++- xs.h | 4 ++-- xs_io.h | 19 +++++++++++-------- xs_version.h | 2 +- 6 files changed, 57 insertions(+), 15 deletions(-) diff --git a/data.c b/data.c index 62b7236..be58c09 100644 --- a/data.c +++ b/data.c @@ -805,14 +805,14 @@ int actor_get(snac *snac, char *actor, d_char **data) } -d_char *_static_fn(snac *snac, char *id) +d_char *_static_fn(snac *snac, const char *id) /* gets the filename for a static file */ { return xs_fmt("%s/static/%s", snac->basedir, id); } -int static_get(snac *snac, char *id, d_char **data, int *size) +int static_get(snac *snac, const char *id, d_char **data, int *size) /* returns static content */ { xs *fn = _static_fn(snac, id); @@ -830,6 +830,19 @@ int static_get(snac *snac, char *id, d_char **data, int *size) } +void static_put(snac *snac, const char *id, const char *data, int size) +/* writes status content */ +{ + xs *fn = _static_fn(snac, id); + FILE *f; + + if ((f = fopen(fn, "wb")) != NULL) { + fwrite(data, size, 1, f); + fclose(f); + } +} + + d_char *_history_fn(snac *snac, char *id) /* gets the filename for the history */ { diff --git a/html.c b/html.c index f4ecb8a..5e4f60c 100644 --- a/html.c +++ b/html.c @@ -877,12 +877,37 @@ int html_post_handler(d_char *req, char *q_path, d_char *payload, int p_size, char *content = xs_dict_get(p_vars, "content"); char *in_reply_to = xs_dict_get(p_vars, "in_reply_to"); char *attach_url = xs_dict_get(p_vars, "attach_url"); + char *attach_file = xs_dict_get(p_vars, "attach"); + xs *attach_list = xs_list_new(); + + /* is attach_url set? */ + if (!xs_is_null(attach_url) && *attach_url != '\0') + attach_list = xs_list_append(attach_list, attach_url); + + /* is attach_file set? */ + if (!xs_is_null(attach_file) && xs_type(attach_file) == XSTYPE_LIST) { + char *fn = xs_list_get(attach_file, 0); + + if (*fn != '\0') { + char *ext = strrchr(fn, '.'); + xs *ntid = tid(0); + xs *id = xs_fmt("%s%s", ntid, ext); + xs *url = xs_fmt("%s/s/%s", snac.actor, id); + int fo = xs_number_get(xs_list_get(attach_file, 1)); + int fs = xs_number_get(xs_list_get(attach_file, 2)); + + /* store */ + static_put(&snac, id, payload + fo, fs); + + attach_list = xs_list_append(attach_list, url); + } + } if (content != NULL) { xs *msg = NULL; xs *c_msg = NULL; - msg = msg_note(&snac, content, NULL, in_reply_to, attach_url); + msg = msg_note(&snac, content, NULL, in_reply_to, attach_list); c_msg = msg_create(&snac, msg); diff --git a/snac.h b/snac.h index 2e40ecb..180b8a1 100644 --- a/snac.h +++ b/snac.h @@ -80,7 +80,8 @@ int is_muted(snac *snac, char *actor); int actor_add(snac *snac, char *actor, char *msg); int actor_get(snac *snac, char *actor, d_char **data); -int static_get(snac *snac, char *id, d_char **data, int *size); +int static_get(snac *snac, const char *id, d_char **data, int *size); +void static_put(snac *snac, const char *id, const char *data, int size); double history_mtime(snac *snac, char *id); void history_add(snac *snac, char *id, char *content, int size); diff --git a/xs.h b/xs.h index 45abfae..bffc0ee 100644 --- a/xs.h +++ b/xs.h @@ -737,7 +737,7 @@ double xs_number_get(const char *v) { double f = 0.0; - if (v[0] == XSTYPE_NUMBER) + if (v != NULL && v[0] == XSTYPE_NUMBER) f = atof(&v[1]); return f; @@ -749,7 +749,7 @@ const char *xs_number_str(const char *v) { const char *p = NULL; - if (v[0] == XSTYPE_NUMBER) + if (v != NULL && v[0] == XSTYPE_NUMBER) p = &v[1]; return p; diff --git a/xs_io.h b/xs_io.h index 62af82c..ca243ad 100644 --- a/xs_io.h +++ b/xs_io.h @@ -59,26 +59,29 @@ d_char *xs_readline(FILE *f) d_char *xs_read(FILE *f, int *sz) /* reads up to size bytes from f */ { - d_char *s; - int size = *sz; - int rdsz = 0; + d_char *s = NULL; + int size = *sz; + int rdsz = 0; errno = 0; - s = xs_str_new(NULL); - while (size > 0 && !feof(f)) { - char tmp[2048]; + char tmp[4096]; int n, r; if ((n = sizeof(tmp)) > size) n = size; r = fread(tmp, 1, n, f); - s = xs_append_m(s, tmp, r); - size -= r; + /* open room */ + s = xs_realloc(s, rdsz + r); + + /* copy read data */ + memcpy(s + rdsz, tmp, r); + rdsz += r; + size -= r; } *sz = rdsz; diff --git a/xs_version.h b/xs_version.h index ad735a7..d810fd8 100644 --- a/xs_version.h +++ b/xs_version.h @@ -1 +1 @@ -/* 639f769029cee785f4e854b66c695bbf84f016b9 */ +/* 65d893d17731d4cc1bfeeff6e5395e59fc4f7875 */