diff --git a/landerctl/include/landerctl.h b/landerctl/include/landerctl.h index b6e0d8c..55acf66 100644 --- a/landerctl/include/landerctl.h +++ b/landerctl/include/landerctl.h @@ -75,6 +75,8 @@ landerctl_err landerctl_post_paste(landerctl_ctx *ctx); landerctl_err landerctl_post_file(landerctl_ctx *ctx); int landerctl_cmd_short(landerctl_args *args); +int landerctl_cmd_paste(landerctl_args *args); +int landerctl_cmd_file(landerctl_args *args); int landerctl_parse_args(landerctl_args *out, int argc, char **argv); #endif diff --git a/landerctl/src/commands.c b/landerctl/src/commands.c index 4ab4c8b..3b91fa4 100644 --- a/landerctl/src/commands.c +++ b/landerctl/src/commands.c @@ -1,6 +1,9 @@ +#include #include +#include #include +#include #include "landerctl.h" @@ -147,3 +150,112 @@ int landerctl_cmd_short(landerctl_args *args) { return res; } + +int landerctl_cmd_paste(landerctl_args *args) { + // TODO argument count check + int res; + landerctl_curl curl; + + if ((res = landerctl_curl_init(&curl))) { + return res; + } + + const char *key = args->args.len == 2 ? args->args.arr[1] : NULL; + if ((res = landerctl_curl_set_common(&curl, args, key))) { + return res; + } + + const char *data_path = args->args.arr[0]; + FILE *f = fopen(data_path, "rb"); + + if (f == NULL) { + fprintf(stderr, "Failed to open data file %s\n", args->args.arr[0]); + + return 1; + } + + struct stat sb; + stat(data_path, &sb); + + curl_easy_setopt(curl.curl, CURLOPT_POST, 1L); + curl_easy_setopt(curl.curl, CURLOPT_READDATA, f); + curl_easy_setopt(curl.curl, CURLOPT_POSTFIELDSIZE, sb.st_size); + + res = landerctl_curl_perform(&curl); + + if (res == 0) { + landerctl_curl_inspect(&curl, args); + } + + landerctl_curl_cleanup(&curl); + + return res; +} + +int landerctl_cmd_file(landerctl_args *args) { + // TODO argument count check + int res; + landerctl_curl curl; + + if ((res = landerctl_curl_init(&curl))) { + return res; + } + + const char *key = args->args.len == 2 ? args->args.arr[1] : NULL; + if ((res = landerctl_curl_set_common(&curl, args, key))) { + return res; + } + + const char *data_path = args->args.arr[0]; + FILE *f = fopen(data_path, "rb"); + + if (f == NULL) { + fprintf(stderr, "Failed to open data file %s\n", args->args.arr[0]); + + return 1; + } + + struct stat sb; + stat(data_path, &sb); + + curl_easy_setopt(curl.curl, CURLOPT_POST, 1L); + curl_easy_setopt(curl.curl, CURLOPT_READDATA, f); + curl_easy_setopt(curl.curl, CURLOPT_POSTFIELDSIZE_LARGE, sb.st_size); + curl_easy_setopt(curl.curl, CURLOPT_NOPROGRESS, 0L); + + magic_t cookie = magic_open(MAGIC_MIME_TYPE); + + if (magic_load(cookie, NULL) == 0) { + const char *mime_type = magic_file(cookie, data_path); + + if (mime_type != NULL) { + char content_type_header[strlen(mime_type) + 24]; + sprintf(content_type_header, "X-Lander-Content-Type: %s", mime_type); + + curl.headers = curl_slist_append(curl.headers, content_type_header); + } else { + printf("Couldn't determine mime type; skipping Content-Type header\n"); + } + } else { + printf("Couldn't load magic file; skipping Content-Type header\n"); + } + + char s[strlen(data_path) + 1]; + strcpy(s, data_path); + const char *base_name = basename(s); + + char filename_header[strlen(base_name) + 20]; + sprintf(filename_header, "X-Lander-Filename: %s", base_name); + + curl.headers = curl_slist_append(curl.headers, filename_header); + + res = landerctl_curl_perform(&curl); + + if (res == 0) { + landerctl_curl_inspect(&curl, args); + } + + landerctl_curl_cleanup(&curl); + + return res; +} diff --git a/landerctl/src/main.c b/landerctl/src/main.c index 0566648..b1dab2f 100644 --- a/landerctl/src/main.c +++ b/landerctl/src/main.c @@ -21,6 +21,12 @@ int main(int argc, char **argv) { case landerctl_mode_short: res = landerctl_cmd_short(&args); break; + case landerctl_mode_paste: + res = landerctl_cmd_paste(&args); + break; + case landerctl_mode_file: + res = landerctl_cmd_file(&args); + break; /* case landerctl_mode_paste: */ /* res = landerctl_post_paste(&ctx); */ /* break; */