diff --git a/Makefile b/Makefile index 1a241e08..7cf045cd 100644 --- a/Makefile +++ b/Makefile @@ -68,4 +68,4 @@ mastodon.dsk: $(mastodon_disk_PROGS) dist: clean all net.dsk homectrl.dsk mastoperso.dsk mastodon.dsk upload: all - scp $(net_disk_PROGS) $(homectrl_disk_PROGS) $(mastodon_disk_PROGS) diskstation.lan:/volume1/a2repo/apple2/ + cp $(net_disk_PROGS) $(homectrl_disk_PROGS) $(mastodon_disk_PROGS) /run/user/1000/gvfs/smb-share\:server\=diskstation.lan\,share\=a2repo/apple2/ diff --git a/src/lib/surl.h b/src/lib/surl.h index 0100190b..091454ac 100644 --- a/src/lib/surl.h +++ b/src/lib/surl.h @@ -52,4 +52,8 @@ int __fastcall__ surl_send_data_params(size_t total, int raw); int __fastcall__ surl_find_line(char *buffer, size_t max_len, char *search_str); int __fastcall__ surl_get_json(char *buffer, size_t max_len, char striphtml, char *translit, char *selector); +/* Multipart helpers */ +#define surl_multipart_send_num_fields(x) simple_serial_putc(x) +#define surl_multipart_send_field_desc(name, len, type) simple_serial_printf("%s\n%d\n%s\n", name, len, type) +#define surl_multipart_send_field_data(data, len) surl_send_data(data, len) #endif diff --git a/src/mastodon/SMILEY b/src/mastodon/SMILEY deleted file mode 100644 index a35acf50..00000000 Binary files a/src/mastodon/SMILEY and /dev/null differ diff --git a/src/mastodon/api/compose.c b/src/mastodon/api/compose.c index 6bd2f7cd..986bbd67 100644 --- a/src/mastodon/api/compose.c +++ b/src/mastodon/api/compose.c @@ -7,6 +7,9 @@ #include "strsplit.h" #include "compose.h" #include "common.h" +#ifdef __APPLE2__ +#include +#endif #ifdef __CC65__ #pragma code-name (push, "LOWCODE") @@ -22,7 +25,7 @@ static char *compose_audience_str(char compose_audience) { } } -char *api_send_hgr_image(char *filename) { +char *api_send_hgr_image(char *filename, char *description, char **err) { FILE *fp; char buf[1024]; int r = 0; @@ -33,8 +36,16 @@ char *api_send_hgr_image(char *filename) { char n_lines; char *media_id; + *err = malloc(64); + *err[0] = '\0'; + +#ifdef __CC65__ + _filetype = PRODOS_T_TXT; +#endif + fp = fopen(filename, "r"); if (fp == NULL) { + snprintf(*err, 64, "Can not open file %s\r\n", filename); return NULL; } @@ -54,17 +65,24 @@ char *api_send_hgr_image(char *filename) { } /* Send num fields */ - simple_serial_putc(1); - - simple_serial_puts("file\n"); - simple_serial_printf("%d\nimage/hgr\n", to_send); + surl_multipart_send_num_fields(1); + + /* Send file */ + surl_multipart_send_field_desc("file", to_send, "image/hgr"); while ((r = fread(buf, sizeof(char), sizeof(buf), fp)) > 0) { - surl_send_data(buf, r); + surl_multipart_send_field_data(buf, r); to_send -= r; if (to_send == 0) { break; } } + while (to_send > 0) { + /* Some hgr files don't include the last bytes, that + * fall into an "HGR-memory-hole" and as such are not + * indispensable */ + surl_multipart_send_field_data(0x00, 1); + to_send--; + } fclose(fp); @@ -76,11 +94,59 @@ char *api_send_hgr_image(char *filename) { n_lines = strsplit_in_place(gen_buf, '\n', &lines); if (r >= 0 && n_lines == 1) { media_id = strdup(lines[0]); + } else { + snprintf(*err, 64, "Invalid JSON response\r\n"); } free(lines); + } else { + snprintf(*err, 64, "Invalid HTTP response %d\r\n", resp->code); } surl_response_free(resp); + if (media_id != NULL) { + /* Set description */ + int len, o, i; + char *body = malloc(1536); + snprintf(body, 1536, "description|TRANSLIT|%s\n", + translit_charset); + + /* Escape buffer */ + len = strlen(description); + o = strlen(body); + for (i = 0; i < len; i++) { + if (description[i] != '\n') { + body[o++] = description[i]; + } else { + body[o++] = '\\'; + body[o++] = 'r'; + body[o++] = '\\'; + body[o++] = 'n'; + } + } + /* End of description */ + body[o++] = '\n'; + len = o - 1; + + snprintf(endpoint_buf, ENDPOINT_BUF_SIZE, "%s/%s", "/api/v1/media", media_id); + resp = get_surl_for_endpoint(SURL_METHOD_PUT, endpoint_buf); + + surl_send_data_params(len, SURL_DATA_APPLICATION_JSON_HELP); + surl_send_data(body, len); + + free(body); + + surl_read_response_header(resp); + + if (!surl_response_ok(resp)) { + snprintf(*err, 64, "Description: invalid HTTP response %d\r\n", resp->code); + } + surl_response_free(resp); + } + + if (*err[0] == '\0') { + free(*err); + *err = NULL; + } return media_id; } @@ -140,7 +206,8 @@ char api_send_toot(char *buffer, char *in_reply_to_id, char **media_ids, char n_ translit_charset); free(in_reply_to_buf); free(medias_buf); - /* Escaped buffer */ + + /* Escape buffer */ len = strlen(buffer); o = strlen(body); for (i = 0; i < len; i++) { diff --git a/src/mastodon/api/compose.h b/src/mastodon/api/compose.h index 7333e598..0233a3e9 100644 --- a/src/mastodon/api/compose.h +++ b/src/mastodon/api/compose.h @@ -4,5 +4,5 @@ #include "common.h" char api_send_toot(char *buffer, char *in_reply_to_id, char **media_ids, char n_medias, char compose_audience); -char *api_send_hgr_image(char *filename); +char *api_send_hgr_image(char *filename, char *description, char **err); #endif diff --git a/src/mastodon/cli/compose.c b/src/mastodon/cli/compose.c index 98f7435c..bc4c9ee5 100644 --- a/src/mastodon/cli/compose.c +++ b/src/mastodon/cli/compose.c @@ -120,30 +120,47 @@ static void remove_image() { } static void add_image() { + char *err = NULL; clrscr(); gotoxy(0, 1); if (n_medias == MAX_IMAGES) { return; } - cprintf("Please insert media disk if needed.\r\n\r\n"); + cprintf("Please insert media disk if needed. If you switch disks,\r\n" + "please enter full path to file, like /VOLNAME/FILENAME\r\n" + "\r\n"); cprintf("File name: "); media_files[n_medias] = malloc(32); media_files[n_medias][0] = '\0'; dget_text(media_files[n_medias], 32, NULL); - cprintf("Description: "); + cprintf("\r\nDescription: "); media_descriptions[n_medias] = malloc(512); media_descriptions[n_medias][0] = '\0'; dget_text(media_descriptions[n_medias], 512, NULL); - media_ids[n_medias] = strdup("ID-to-do"); - // media_ids[0] = api_send_hgr_image("SMILEY"); - // if (media_ids[0]) - // n_medias++; - - n_medias++; +try_again: + free(err); + err = NULL; + media_ids[n_medias] = api_send_hgr_image(media_files[n_medias], + media_descriptions[n_medias], + &err); + if (media_ids[n_medias] == NULL) { + char t; + cprintf("\r\nAn error happened uploading the file:\r\n%s\r\n\r\nTry again? (y/n)", + err != NULL ? err:"Unknown error"); + t = cgetc(); + if (tolower(t) != 'n') { + goto try_again; + } else { + free(media_files[n_medias]); + free(media_descriptions[n_medias]); + } + } else { + n_medias++; + } } void open_images_menu(void) { @@ -235,9 +252,6 @@ static char *handle_compose_input(char *reply_to_account) { void compose_toot(char *reply_to_account) { char i; char *text; - char *media_ids[4]; - - text = NULL; text = handle_compose_input(reply_to_account);