Skip to content

Commit

Permalink
Finish image selection TUI and plumbing
Browse files Browse the repository at this point in the history
  • Loading branch information
colinleroy committed Jul 24, 2023
1 parent 2f52095 commit 7081093
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 20 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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/
4 changes: 4 additions & 0 deletions src/lib/surl.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Binary file removed src/mastodon/SMILEY
Binary file not shown.
81 changes: 74 additions & 7 deletions src/mastodon/api/compose.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
#include "strsplit.h"
#include "compose.h"
#include "common.h"
#ifdef __APPLE2__
#include <apple2enh.h>
#endif

#ifdef __CC65__
#pragma code-name (push, "LOWCODE")
Expand All @@ -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;
Expand All @@ -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;
}

Expand All @@ -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);

Expand All @@ -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;
}

Expand Down Expand Up @@ -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++) {
Expand Down
2 changes: 1 addition & 1 deletion src/mastodon/api/compose.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
36 changes: 25 additions & 11 deletions src/mastodon/cli/compose.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);

Expand Down

0 comments on commit 7081093

Please sign in to comment.