Attachments are now starting to get real.

This commit is contained in:
default 2022-10-16 18:03:28 +02:00
parent 0d79e465e6
commit d9a15b8af7
6 changed files with 57 additions and 15 deletions

17
data.c
View File

@ -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 */ /* gets the filename for a static file */
{ {
return xs_fmt("%s/static/%s", snac->basedir, id); 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 */ /* returns static content */
{ {
xs *fn = _static_fn(snac, id); 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) d_char *_history_fn(snac *snac, char *id)
/* gets the filename for the history */ /* gets the filename for the history */
{ {

27
html.c
View File

@ -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 *content = xs_dict_get(p_vars, "content");
char *in_reply_to = xs_dict_get(p_vars, "in_reply_to"); char *in_reply_to = xs_dict_get(p_vars, "in_reply_to");
char *attach_url = xs_dict_get(p_vars, "attach_url"); 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) { if (content != NULL) {
xs *msg = NULL; xs *msg = NULL;
xs *c_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); c_msg = msg_create(&snac, msg);

3
snac.h
View File

@ -80,7 +80,8 @@ int is_muted(snac *snac, char *actor);
int actor_add(snac *snac, char *actor, char *msg); int actor_add(snac *snac, char *actor, char *msg);
int actor_get(snac *snac, char *actor, d_char **data); 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); double history_mtime(snac *snac, char *id);
void history_add(snac *snac, char *id, char *content, int size); void history_add(snac *snac, char *id, char *content, int size);

4
xs.h
View File

@ -737,7 +737,7 @@ double xs_number_get(const char *v)
{ {
double f = 0.0; double f = 0.0;
if (v[0] == XSTYPE_NUMBER) if (v != NULL && v[0] == XSTYPE_NUMBER)
f = atof(&v[1]); f = atof(&v[1]);
return f; return f;
@ -749,7 +749,7 @@ const char *xs_number_str(const char *v)
{ {
const char *p = NULL; const char *p = NULL;
if (v[0] == XSTYPE_NUMBER) if (v != NULL && v[0] == XSTYPE_NUMBER)
p = &v[1]; p = &v[1];
return p; return p;

15
xs_io.h
View File

@ -59,26 +59,29 @@ d_char *xs_readline(FILE *f)
d_char *xs_read(FILE *f, int *sz) d_char *xs_read(FILE *f, int *sz)
/* reads up to size bytes from f */ /* reads up to size bytes from f */
{ {
d_char *s; d_char *s = NULL;
int size = *sz; int size = *sz;
int rdsz = 0; int rdsz = 0;
errno = 0; errno = 0;
s = xs_str_new(NULL);
while (size > 0 && !feof(f)) { while (size > 0 && !feof(f)) {
char tmp[2048]; char tmp[4096];
int n, r; int n, r;
if ((n = sizeof(tmp)) > size) if ((n = sizeof(tmp)) > size)
n = size; n = size;
r = fread(tmp, 1, n, f); 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; rdsz += r;
size -= r;
} }
*sz = rdsz; *sz = rdsz;

View File

@ -1 +1 @@
/* 639f769029cee785f4e854b66c695bbf84f016b9 */ /* 65d893d17731d4cc1bfeeff6e5395e59fc4f7875 */