From c11b26e1f75b643997787b76f5181731058a28f9 Mon Sep 17 00:00:00 2001
From: kaa <kaa@disroot.org>
Date: Sun, 19 Nov 2023 00:56:10 -0800
Subject: Basis for directory listing and file download.

---
 canls.c   | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 canup.c   | 19 ++++++++-----------
 config.h  |  7 ++++++-
 dump      | 10 ++++++++++
 dump.sh   |  4 ++++
 makefile  |  5 +++--
 testing.c | 28 ++++++++++++++++++++++++++++
 7 files changed, 114 insertions(+), 14 deletions(-)
 create mode 100644 canls.c
 create mode 100644 dump
 create mode 100755 dump.sh
 create mode 100644 testing.c

diff --git a/canls.c b/canls.c
new file mode 100644
index 0000000..f47656e
--- /dev/null
+++ b/canls.c
@@ -0,0 +1,55 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+enum {
+	bufsize = 16,
+};
+
+typedef char byte;
+
+struct listing {
+	long id, folder_id, size;
+	char *uuid, *filename, *upload_status, *content_type, *url;
+	char *created_at, *updated_at, *unlock_at, *lock_at, thumbnail_url;
+	char *modified_at, *mime_class, *media_entry_id, *category;
+	byte locked, hidden, hidden_for_user, locked_for_user;
+};
+
+typedef struct listing Listing;
+
+int
+main()
+{
+	FILE* infile = fopen("dump", "r");
+	if (infile == NULL) {
+		fprintf(stderr, "No dump provided. Run dump.sh.\n");
+	}
+	
+	char buf[bufsize];
+	char *contents = NULL;
+	int read, size = 0;
+	while ((read = fread(buf, 1, bufsize, infile)) > 0) {
+		size += read;
+		contents = realloc(contents, size);
+		memmove(&contents[size-read], buf, read);
+	}
+
+	int i, j = 0;
+	char ch;
+	Listing *l = calloc(1, sizeof(Listing));
+	int l_size = sizeof(Listing);
+	for (i = 0; i < size; i++) {
+		ch = contents[i];
+		switch (ch) {
+		case '\n':
+			j++;
+			l_size += sizeof(Listing);
+			l = realloc(l, l_size);
+			break;
+		default:
+		}
+	}
+	printf("%d\n", l_size);
+	return 0;
+}
diff --git a/canup.c b/canup.c
index f7bfa2d..ffb5ba1 100644
--- a/canup.c
+++ b/canup.c
@@ -1,4 +1,4 @@
-#define _BSD_SOURCE
+#define _BSD_SOURCE /* Needed for popen(3) on Linux. */
 #include <stdio.h>
 #include <string.h>
 #include <stdlib.h>
@@ -6,10 +6,7 @@
 #include <unistd.h>
 #include "config.h"
 
-#define MAXPATH 1024
-#define MAXCMD 4096
-
-char *upload_key;
+char *url = "https://sdccd.instructure.com/api/v1/users/self/files";
 
 char *
 json_match(char *JSON, char *key)
@@ -51,9 +48,9 @@ int
 upfile(char *name, char *rdir)
 {
 	printf("%s/%s\n", rdir, name);
-	char *cmd = calloc(MAXCMD, sizeof(char));
+	char *cmd = calloc(cmdsize, sizeof(char));
 	char *dir = strip_fn(name);
-	snprintf(cmd, MAXCMD, "curl -s '%s' -F 'name=%s'"
+	snprintf(cmd, cmdsize, "curl -s '%s' -F 'name=%s'"
 		" -F 'parent_folder_path=%s/%s'"
 		" -H 'Authorization: Bearer %s'", url, name,
 		rdir, dir, upload_key);
@@ -83,7 +80,7 @@ upfile(char *name, char *rdir)
 		return -1;
 	}
 
-	snprintf(cmd, MAXCMD, "curl -s '%s' -F 'key=%s'"
+	snprintf(cmd, cmdsize, "curl -s '%s' -F 'key=%s'"
 		" -F 'content_type=%s' -F 'file=@%s'",
 		upload_url, filename, content_type, name);
 	/* Throw away output; assume that, if auth is successful,
@@ -103,7 +100,7 @@ updir(DIR *dirp, char *rdir, char *dir)
 {
 	DIR *nestdirp;
 	struct dirent *dp;
-	char *name = calloc(MAXPATH, sizeof(char));
+	char *name = calloc(pathsize, sizeof(char));
 	while ((dp = readdir(dirp)) != NULL) {
 		if (strcmp(".", dp->d_name) == 0
 			|| strcmp("..", dp->d_name) == 0)
@@ -141,9 +138,9 @@ main(int argc, char **argv)
 		return -1;
 	}
 	char *rdir;
-	rdir = calloc(MAXPATH, sizeof(char));
+	rdir = calloc(pathsize, sizeof(char));
 	char *local;
-	local = calloc(MAXPATH, sizeof(char));
+	local = calloc(pathsize, sizeof(char));
 	local[0] = '.';
 	int i;
 	for (i = 1; i < argc; ++i) {
diff --git a/config.h b/config.h
index 9af76d6..055895c 100644
--- a/config.h
+++ b/config.h
@@ -1 +1,6 @@
-char *url = "https://sdccd.instructure.com/api/v1/users/self/files";
+enum {
+	cmdsize = 4096,
+	pathsize = 1024,
+};
+
+char *upload_key;
diff --git a/dump b/dump
new file mode 100644
index 0000000..116b04f
--- /dev/null
+++ b/dump
@@ -0,0 +1,10 @@
+"id":145759442,"uuid":"FBbPqRtIqzAsed4Nl1Huht1mWZCxauZfBQTYjbjd","folder_id":17503637,"display_name":"1-1.mp4","filename":"1.mp4","upload_status":"success","content-type":"video/mp4","url":"https://sdccd.instructure.com/files/145759442/download?download_frd=1\u0026verifier=FBbPqRtIqzAsed4Nl1Huht1mWZCxauZfBQTYjbjd","size":457740,"created_at":"2023-10-10T18:22:45Z","updated_at":"2023-10-10T18:22:48Z","unlock_at":null,"locked":false,"hidden":true,"lock_at":null,"hidden_for_user":true,"thumbnail_url":null,"modified_at":"2023-10-10T18:22:45Z","mime_class":"video","media_entry_id":"m-5VgVtDFKPLCjwPrGKvdEPJV7USYAS8p8","category":"uncategorized","locked_for_user":false
+"id":145759448,"uuid":"OFek0dF01u2BaEEBzV6VWsh7gipwrwd59ozmsMAR","folder_id":17503637,"display_name":"1-2.mp4","filename":"1.mp4","upload_status":"success","content-type":"video/mp4","url":"https://sdccd.instructure.com/files/145759448/download?download_frd=1\u0026verifier=OFek0dF01u2BaEEBzV6VWsh7gipwrwd59ozmsMAR","size":698610,"created_at":"2023-10-10T18:23:48Z","updated_at":"2023-10-10T18:23:51Z","unlock_at":null,"locked":false,"hidden":true,"lock_at":null,"hidden_for_user":true,"thumbnail_url":null,"modified_at":"2023-10-10T18:23:48Z","mime_class":"video","media_entry_id":"m-pv8VkiJkWJDxxkJfhoGs4dSYYLom4j6","category":"uncategorized","locked_for_user":false
+"id":145808071,"uuid":"FqiRoD9sO2WOJqzsDrQXNlW3YyDWv5J87ZoKwBwk","folder_id":17503637,"display_name":"1-3.mp4","filename":"1.mp4","upload_status":"success","content-type":"video/mp4","url":"https://sdccd.instructure.com/files/145808071/download?download_frd=1\u0026verifier=FqiRoD9sO2WOJqzsDrQXNlW3YyDWv5J87ZoKwBwk","size":7859303,"created_at":"2023-10-15T00:11:04Z","updated_at":"2023-10-15T00:16:10Z","unlock_at":null,"locked":false,"hidden":true,"lock_at":null,"hidden_for_user":true,"thumbnail_url":null,"modified_at":"2023-10-15T00:11:04Z","mime_class":"video","media_entry_id":"m-2iJ8QpBNPNk9Dz9jxeE6iFgXtMCR3Pxi","category":"uncategorized","locked_for_user":false
+"id":145759343,"uuid":"bw5CbntslXR4hpfaE25sP8UwTSbUiwnleqCs7yL3","folder_id":17503637,"display_name":"1.mp4","filename":"1.mp4","upload_status":"success","content-type":"video/mp4","url":"https://sdccd.instructure.com/files/145759343/download?download_frd=1\u0026verifier=bw5CbntslXR4hpfaE25sP8UwTSbUiwnleqCs7yL3","size":394585,"created_at":"2023-10-10T18:13:47Z","updated_at":"2023-10-10T18:13:53Z","unlock_at":null,"locked":false,"hidden":true,"lock_at":null,"hidden_for_user":true,"thumbnail_url":null,"modified_at":"2023-10-10T18:13:47Z","mime_class":"video","media_entry_id":"m-4AaUe8Wsar2aeUR2Gif6cuqTphvyoNqz","category":"uncategorized","locked_for_user":false
+"id":145759359,"uuid":"AClgbYCaPnUaXx7XoIi4Uqv95Mj6tboBTvbKVQhi","folder_id":17503637,"display_name":"2.mp4","filename":"2.mp4","upload_status":"success","content-type":"video/mp4","url":"https://sdccd.instructure.com/files/145759359/download?download_frd=1\u0026verifier=AClgbYCaPnUaXx7XoIi4Uqv95Mj6tboBTvbKVQhi","size":311927,"created_at":"2023-10-10T18:16:38Z","updated_at":"2023-10-10T18:21:39Z","unlock_at":null,"locked":false,"hidden":true,"lock_at":null,"hidden_for_user":true,"thumbnail_url":null,"modified_at":"2023-10-10T18:16:38Z","mime_class":"video","media_entry_id":"m-p3NrT3AXosPwAcX7f1PawKfEGYjsQRw","category":"uncategorized","locked_for_user":false
+"id":145759375,"uuid":"LfdmKamZskIckgWc9tF2Wky2R7AxO5K58v4nPdkI","folder_id":17503637,"display_name":"3.mp4","filename":"3.mp4","upload_status":"success","content-type":"video/mp4","url":"https://sdccd.instructure.com/files/145759375/download?download_frd=1\u0026verifier=LfdmKamZskIckgWc9tF2Wky2R7AxO5K58v4nPdkI","size":null,"created_at":"2023-10-10T18:18:16Z","updated_at":"2023-10-10T18:18:16Z","unlock_at":null,"locked":false,"hidden":true,"lock_at":null,"hidden_for_user":true,"thumbnail_url":null,"modified_at":"2023-10-10T18:18:16Z","mime_class":"video","media_entry_id":"m-2BwVBf9erBu5Q6tXn1knyxZ7FfeJDEQM","category":"uncategorized","locked_for_user":false
+"id":145759410,"uuid":"90CiCh9Dp1MHqA8xxpFaethz99Nf8LS4MysPtOZa","folder_id":17503637,"display_name":"4.mp4","filename":"4.mp4","upload_status":"success","content-type":"video/mp4","url":"https://sdccd.instructure.com/files/145759410/download?download_frd=1\u0026verifier=90CiCh9Dp1MHqA8xxpFaethz99Nf8LS4MysPtOZa","size":513597,"created_at":"2023-10-10T18:20:22Z","updated_at":"2023-10-10T18:20:25Z","unlock_at":null,"locked":false,"hidden":true,"lock_at":null,"hidden_for_user":true,"thumbnail_url":null,"modified_at":"2023-10-10T18:20:22Z","mime_class":"video","media_entry_id":"m-4a4DtDGce89fexDZPcZwWfvLtF3CLLbi","category":"uncategorized","locked_for_user":false
+"id":145759517,"uuid":"g0XdLJIZyfOYtj11QzKDu5dQWHmUUK2QxY8qinQu","folder_id":17503637,"display_name":"7_1-1.mp4","filename":"7_1.mp4","upload_status":"success","content-type":"video/mp4","url":"https://sdccd.instructure.com/files/145759517/download?download_frd=1\u0026verifier=g0XdLJIZyfOYtj11QzKDu5dQWHmUUK2QxY8qinQu","size":512562,"created_at":"2023-10-10T18:31:08Z","updated_at":"2023-10-10T18:31:09Z","unlock_at":null,"locked":false,"hidden":true,"lock_at":null,"hidden_for_user":true,"thumbnail_url":null,"modified_at":"2023-10-10T18:31:08Z","mime_class":"video","media_entry_id":"m-xgCoyMC1GBbEsp7VQyVygzHT34dAovn","category":"uncategorized","locked_for_user":false
+"id":145759511,"uuid":"nXeUtVHYtGUarYFm1N8Wo1jXf6oryNiw36qAbVts","folder_id":17503637,"display_name":"7_1.mp4","filename":"7_1.mp4","upload_status":"success","content-type":"video/mp4","url":"https://sdccd.instructure.com/files/145759511/download?download_frd=1\u0026verifier=nXeUtVHYtGUarYFm1N8Wo1jXf6oryNiw36qAbVts","size":512562,"created_at":"2023-10-10T18:30:37Z","updated_at":"2023-10-10T18:30:38Z","unlock_at":null,"locked":false,"hidden":true,"lock_at":null,"hidden_for_user":true,"thumbnail_url":null,"modified_at":"2023-10-10T18:30:37Z","mime_class":"video","media_entry_id":"m-9VTHrTDPdeTL5CbshFAM7vFXwVTBa8R","category":"uncategorized","locked_for_user":false
+"id":145759527,"uuid":"YTRw1Fii0QqILHlmfhsnSJVYB9nDak9kc1EDMjcB","folder_id":17503637,"display_name":"7_2.mp4","filename":"7_2.mp4","upload_status":"success","content-type":"video/mp4","url":"https://sdccd.instructure.com/files/145759527/download?download_frd=1\u0026verifier=YTRw1Fii0QqILHlmfhsnSJVYB9nDak9kc1EDMjcB","size":311927,"created_at":"2023-10-10T18:32:22Z","updated_at":"2023-10-10T18:32:24Z","unlock_at":null,"locked":false,"hidden":true,"lock_at":null,"hidden_for_user":true,"thumbnail_url":null,"modified_at":"2023-10-10T18:32:22Z","mime_class":"video","media_entry_id":"m-2sf1gc4JAtY95rs67DvBJAbuAjw7JCCR","category":"uncategorized","locked_for_user":false
diff --git a/dump.sh b/dump.sh
new file mode 100755
index 0000000..21ae5da
--- /dev/null
+++ b/dump.sh
@@ -0,0 +1,4 @@
+#!/bin/sh
+
+curl -s -X GET "https://sdccd.instructure.com/api/v1/users/self/files" -H "Authorization: Bearer $CANKEY" | tr '}' '\n' | sed -e 's/{//g' -e's/\[//g' -e 's/\]//g' -e 's/^,//g' > dump
+
diff --git a/makefile b/makefile
index c17d9af..15425c0 100644
--- a/makefile
+++ b/makefile
@@ -3,9 +3,10 @@ CFLAGS = -std=c89 -g -Wall
 #CC = tcc
 #CFLAGS = -Wall -g
 LDFLAGS = -static
-all: canup
+OUT = canup testing canls
+all: $(OUT)
 
 .c:
 	$(CC) $(LDFLAGS) $(CFLAGS) -o $@ $<
 clean:
-	rm canup
+	rm $(OUT)
diff --git a/testing.c b/testing.c
new file mode 100644
index 0000000..f50a0a7
--- /dev/null
+++ b/testing.c
@@ -0,0 +1,28 @@
+#include <stdio.h>
+#include <stdlib.h>
+
+enum {
+	cmdsize = 4096,
+};
+
+char *upload_key;
+char *base = "https://sdccd.instructure.com/api/v1/";
+
+int
+main(int argc, char **argv)
+{
+	if (argc < 2) {
+		fputs("Provide an API argument.\n", stderr);
+		return 1;
+	}
+	upload_key = getenv("CANKEY");
+	if (upload_key == NULL) {
+		return -1;
+	}
+	char *cmd = calloc(cmdsize, sizeof(char));
+	snprintf(cmd, cmdsize, "curl -X GET '%s%s' -H 'Authorization: Bearer %s'",
+		base, argv[1], upload_key);
+	puts(cmd);
+	system(cmd);
+	return 0;
+}
-- 
cgit v1.2.3