diff options
| author | 2022-10-16 18:03:28 +0200 | |
|---|---|---|
| committer | 2022-10-16 18:03:28 +0200 | |
| commit | d9a15b8af7da2bc27d4d55ee745242f5b4e39071 (patch) | |
| tree | f890fe2b2f0a327f08da31404fd267c675fac690 | |
| parent | Use multipart/form-data for posts (on the way to supporting uploads). (diff) | |
| download | penes-snac2-d9a15b8af7da2bc27d4d55ee745242f5b4e39071.tar.gz penes-snac2-d9a15b8af7da2bc27d4d55ee745242f5b4e39071.tar.xz penes-snac2-d9a15b8af7da2bc27d4d55ee745242f5b4e39071.zip | |
Attachments are now starting to get real.
Diffstat (limited to '')
| -rw-r--r-- | data.c | 17 | ||||
| -rw-r--r-- | html.c | 27 | ||||
| -rw-r--r-- | snac.h | 3 | ||||
| -rw-r--r-- | xs.h | 4 | ||||
| -rw-r--r-- | xs_io.h | 19 | ||||
| -rw-r--r-- | xs_version.h | 2 |
6 files changed, 57 insertions, 15 deletions
| @@ -805,14 +805,14 @@ int actor_get(snac *snac, char *actor, d_char **data) | |||
| 805 | } | 805 | } |
| 806 | 806 | ||
| 807 | 807 | ||
| 808 | d_char *_static_fn(snac *snac, char *id) | 808 | d_char *_static_fn(snac *snac, const char *id) |
| 809 | /* gets the filename for a static file */ | 809 | /* gets the filename for a static file */ |
| 810 | { | 810 | { |
| 811 | return xs_fmt("%s/static/%s", snac->basedir, id); | 811 | return xs_fmt("%s/static/%s", snac->basedir, id); |
| 812 | } | 812 | } |
| 813 | 813 | ||
| 814 | 814 | ||
| 815 | int static_get(snac *snac, char *id, d_char **data, int *size) | 815 | int static_get(snac *snac, const char *id, d_char **data, int *size) |
| 816 | /* returns static content */ | 816 | /* returns static content */ |
| 817 | { | 817 | { |
| 818 | xs *fn = _static_fn(snac, id); | 818 | xs *fn = _static_fn(snac, id); |
| @@ -830,6 +830,19 @@ int static_get(snac *snac, char *id, d_char **data, int *size) | |||
| 830 | } | 830 | } |
| 831 | 831 | ||
| 832 | 832 | ||
| 833 | void static_put(snac *snac, const char *id, const char *data, int size) | ||
| 834 | /* writes status content */ | ||
| 835 | { | ||
| 836 | xs *fn = _static_fn(snac, id); | ||
| 837 | FILE *f; | ||
| 838 | |||
| 839 | if ((f = fopen(fn, "wb")) != NULL) { | ||
| 840 | fwrite(data, size, 1, f); | ||
| 841 | fclose(f); | ||
| 842 | } | ||
| 843 | } | ||
| 844 | |||
| 845 | |||
| 833 | d_char *_history_fn(snac *snac, char *id) | 846 | d_char *_history_fn(snac *snac, char *id) |
| 834 | /* gets the filename for the history */ | 847 | /* gets the filename for the history */ |
| 835 | { | 848 | { |
| @@ -877,12 +877,37 @@ int html_post_handler(d_char *req, char *q_path, d_char *payload, int p_size, | |||
| 877 | char *content = xs_dict_get(p_vars, "content"); | 877 | char *content = xs_dict_get(p_vars, "content"); |
| 878 | char *in_reply_to = xs_dict_get(p_vars, "in_reply_to"); | 878 | char *in_reply_to = xs_dict_get(p_vars, "in_reply_to"); |
| 879 | char *attach_url = xs_dict_get(p_vars, "attach_url"); | 879 | char *attach_url = xs_dict_get(p_vars, "attach_url"); |
| 880 | char *attach_file = xs_dict_get(p_vars, "attach"); | ||
| 881 | xs *attach_list = xs_list_new(); | ||
| 882 | |||
| 883 | /* is attach_url set? */ | ||
| 884 | if (!xs_is_null(attach_url) && *attach_url != '\0') | ||
| 885 | attach_list = xs_list_append(attach_list, attach_url); | ||
| 886 | |||
| 887 | /* is attach_file set? */ | ||
| 888 | if (!xs_is_null(attach_file) && xs_type(attach_file) == XSTYPE_LIST) { | ||
| 889 | char *fn = xs_list_get(attach_file, 0); | ||
| 890 | |||
| 891 | if (*fn != '\0') { | ||
| 892 | char *ext = strrchr(fn, '.'); | ||
| 893 | xs *ntid = tid(0); | ||
| 894 | xs *id = xs_fmt("%s%s", ntid, ext); | ||
| 895 | xs *url = xs_fmt("%s/s/%s", snac.actor, id); | ||
| 896 | int fo = xs_number_get(xs_list_get(attach_file, 1)); | ||
| 897 | int fs = xs_number_get(xs_list_get(attach_file, 2)); | ||
| 898 | |||
| 899 | /* store */ | ||
| 900 | static_put(&snac, id, payload + fo, fs); | ||
| 901 | |||
| 902 | attach_list = xs_list_append(attach_list, url); | ||
| 903 | } | ||
| 904 | } | ||
| 880 | 905 | ||
| 881 | if (content != NULL) { | 906 | if (content != NULL) { |
| 882 | xs *msg = NULL; | 907 | xs *msg = NULL; |
| 883 | xs *c_msg = NULL; | 908 | xs *c_msg = NULL; |
| 884 | 909 | ||
| 885 | msg = msg_note(&snac, content, NULL, in_reply_to, attach_url); | 910 | msg = msg_note(&snac, content, NULL, in_reply_to, attach_list); |
| 886 | 911 | ||
| 887 | c_msg = msg_create(&snac, msg); | 912 | c_msg = msg_create(&snac, msg); |
| 888 | 913 | ||
| @@ -80,7 +80,8 @@ int is_muted(snac *snac, char *actor); | |||
| 80 | int actor_add(snac *snac, char *actor, char *msg); | 80 | int actor_add(snac *snac, char *actor, char *msg); |
| 81 | int actor_get(snac *snac, char *actor, d_char **data); | 81 | int actor_get(snac *snac, char *actor, d_char **data); |
| 82 | 82 | ||
| 83 | int static_get(snac *snac, char *id, d_char **data, int *size); | 83 | int static_get(snac *snac, const char *id, d_char **data, int *size); |
| 84 | void static_put(snac *snac, const char *id, const char *data, int size); | ||
| 84 | 85 | ||
| 85 | double history_mtime(snac *snac, char *id); | 86 | double history_mtime(snac *snac, char *id); |
| 86 | void history_add(snac *snac, char *id, char *content, int size); | 87 | void history_add(snac *snac, char *id, char *content, int size); |
| @@ -737,7 +737,7 @@ double xs_number_get(const char *v) | |||
| 737 | { | 737 | { |
| 738 | double f = 0.0; | 738 | double f = 0.0; |
| 739 | 739 | ||
| 740 | if (v[0] == XSTYPE_NUMBER) | 740 | if (v != NULL && v[0] == XSTYPE_NUMBER) |
| 741 | f = atof(&v[1]); | 741 | f = atof(&v[1]); |
| 742 | 742 | ||
| 743 | return f; | 743 | return f; |
| @@ -749,7 +749,7 @@ const char *xs_number_str(const char *v) | |||
| 749 | { | 749 | { |
| 750 | const char *p = NULL; | 750 | const char *p = NULL; |
| 751 | 751 | ||
| 752 | if (v[0] == XSTYPE_NUMBER) | 752 | if (v != NULL && v[0] == XSTYPE_NUMBER) |
| 753 | p = &v[1]; | 753 | p = &v[1]; |
| 754 | 754 | ||
| 755 | return p; | 755 | return p; |
| @@ -59,26 +59,29 @@ d_char *xs_readline(FILE *f) | |||
| 59 | d_char *xs_read(FILE *f, int *sz) | 59 | d_char *xs_read(FILE *f, int *sz) |
| 60 | /* reads up to size bytes from f */ | 60 | /* reads up to size bytes from f */ |
| 61 | { | 61 | { |
| 62 | d_char *s; | 62 | d_char *s = NULL; |
| 63 | int size = *sz; | 63 | int size = *sz; |
| 64 | int rdsz = 0; | 64 | int rdsz = 0; |
| 65 | 65 | ||
| 66 | errno = 0; | 66 | errno = 0; |
| 67 | 67 | ||
| 68 | s = xs_str_new(NULL); | ||
| 69 | |||
| 70 | while (size > 0 && !feof(f)) { | 68 | while (size > 0 && !feof(f)) { |
| 71 | char tmp[2048]; | 69 | char tmp[4096]; |
| 72 | int n, r; | 70 | int n, r; |
| 73 | 71 | ||
| 74 | if ((n = sizeof(tmp)) > size) | 72 | if ((n = sizeof(tmp)) > size) |
| 75 | n = size; | 73 | n = size; |
| 76 | 74 | ||
| 77 | r = fread(tmp, 1, n, f); | 75 | r = fread(tmp, 1, n, f); |
| 78 | s = xs_append_m(s, tmp, r); | ||
| 79 | 76 | ||
| 80 | size -= r; | 77 | /* open room */ |
| 78 | s = xs_realloc(s, rdsz + r); | ||
| 79 | |||
| 80 | /* copy read data */ | ||
| 81 | memcpy(s + rdsz, tmp, r); | ||
| 82 | |||
| 81 | rdsz += r; | 83 | rdsz += r; |
| 84 | size -= r; | ||
| 82 | } | 85 | } |
| 83 | 86 | ||
| 84 | *sz = rdsz; | 87 | *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 */ | ||