changeset 30:584bef22b25e

misc: similar to paster, implement raw POST data http://hg.malikania.fr/paster/rev/a93f4e3313e9
author David Demelier <markand@malikania.fr>
date Thu, 26 May 2022 11:37:50 +0200
parents d843b03defc0
children 53229f0a5c59
files imgup.sh page-new.c
diffstat 2 files changed, 23 insertions(+), 18 deletions(-) [+]
line wrap: on
line diff
--- a/imgup.sh	Thu Mar 31 09:42:12 2022 +0200
+++ b/imgup.sh	Thu May 26 11:37:50 2022 +0200
@@ -55,13 +55,16 @@
 
 	if [ $verbose -eq 0 ]; then
 		with_verbose="-s"
+	else
+		with_verbose="-i"
 	fi
 
-	curl -i \
+	curl -L -X POST \
 		-F author="$author" \
 		-F duration="$duration" \
 		-F title="$title" \
 		-F filename=@"$1" \
+		-F raw="on" \
 		$with_private \
 		$with_verbose \
 		"$2"/new
@@ -103,15 +106,4 @@
 	usage
 fi
 
-# If verbose, dump all headers.
-if [ $verbose -eq 1 ]; then
-	send "$1" "$2"
-else
-	url=$(send "$1" "$2" | grep -E "^Location: " | awk '{ print $2 }')
-
-	if [ -z "$url" ]; then
-		die "abort: error occured, retry with -v"
-	fi
-
-	printf "%s%s\n" "$2" "$url"
-fi
+send "$1" "$2"
--- a/page-new.c	Thu Mar 31 09:42:12 2022 +0200
+++ b/page-new.c	Thu May 26 11:37:50 2022 +0200
@@ -95,6 +95,7 @@
 		.visible        = true,
 		.duration       = IMAGE_DURATION_DAY
 	};
+	int raw = 0;
 
 	for (size_t i = 0; i < r->fieldsz; ++i) {
 		const char *key = r->fields[i].key;
@@ -114,6 +115,8 @@
 			image.datasz = r->fields[i].valsz;
 		} else if (strcmp(key, "private") == 0)
 			image.visible = strcmp(val, "on") != 0;
+		else if (strcmp(key, "raw") == 0)
+			raw = strcmp(val, "on") == 0;
 	}
 
 	/* TODO: image_isvalid should check for all stuff. */
@@ -122,11 +125,21 @@
 	else if (!database_insert(&image))
 		page(r, NULL, KHTTP_500, "pages/500.html", "500");
 	else {
-		/* Redirect to image details. */
-		khttp_head(r, kresps[KRESP_STATUS], "%s", khttps[KHTTP_302]);
-		khttp_head(r, kresps[KRESP_LOCATION], "/image/%s", image.id);
-		khttp_body(r);
-		khttp_free(r);
+		if (raw) {
+			/* For CLI users (e.g. imgup) just print the location. */
+			khttp_head(r, kresps[KRESP_STATUS], "%s", khttps[KHTTP_201]);
+			khttp_body(r);
+			khttp_printf(r, "%s://%s/image/%s\n",
+			    r->scheme == KSCHEME_HTTP ? "http" : "https",
+			    r->host, image.id);
+			khttp_free(r);
+		} else {
+			/* Otherwise, redirect to image details. */
+			khttp_head(r, kresps[KRESP_STATUS], "%s", khttps[KHTTP_302]);
+			khttp_head(r, kresps[KRESP_LOCATION], "/image/%s", image.id);
+			khttp_body(r);
+			khttp_free(r);
+		}
 	}
 
 	image_finish(&image);