summary refs log tree commit diff stats
path: root/server
diff options
context:
space:
mode:
authorAndinus <andinus@nand.sh>2022-02-04 11:56:54 +0530
committerAndinus <andinus@nand.sh>2022-02-04 11:56:54 +0530
commit3f8a1326dffb01512a1b19a141360180bf6b7fd8 (patch)
tree753b225d500b7dcf92b3141267e982cd790a847d /server
parent1e1e1a0728b2ec8cf42dc943606de13cb33a507c (diff)
downloadvela-3f8a1326dffb01512a1b19a141360180bf6b7fd8.tar.gz
Implement upload functionality
The authentication part on the server is not implemented properly.
Diffstat (limited to 'server')
-rw-r--r--server/service.raku58
1 files changed, 45 insertions, 13 deletions
diff --git a/server/service.raku b/server/service.raku
index 2e63e3f..638989c 100644
--- a/server/service.raku
+++ b/server/service.raku
@@ -18,6 +18,7 @@ my $application = route {
 
             my IO() $user-store = "%s/%s".sprintf: $store, $id;
             mkdir $user-store;
+            mkdir "$user-store/images";
             die "Failed to create user store" unless $user-store.d;
             spurt "$user-store/name", "$name";
             spurt "$user-store/auth", "$auth";
@@ -29,14 +30,16 @@ my $application = route {
     post -> 'verify' {
         request-body -> (:$id, :$auth) {
             my IO() $user-store = "%s/%s".sprintf: $store, $id;
-            my %res;
 
+            my %res;
             if $user-store.d {
-                %res<name> = "$user-store/name".IO.slurp;
-                %res<status> = "Authentication Failed." if "$user-store/auth".IO.slurp ne $auth;
-                %res<status> //= "Verified";
+                if "$user-store/auth".IO.slurp ne $auth {
+                    response.status = 401;
+                } else {
+                    %res<name> = "$user-store/name".IO.slurp;
+                }
             } else {
-                %res<status> = "User doesn't exist or has been deleted.";
+                response.status = 404;
             }
 
             content 'application/json', %res;
@@ -44,14 +47,43 @@ my $application = route {
     }
 
     post -> 'upload' {
-        request-body-blob
-        'image/png' => -> $png {
-            ...
-        },
-        'image/jpeg' => -> $jpeg {
-            ...
-        },
-        { bad-request 'text/plain', 'Only png or jpeg allowed'; }
+        request-body -> (:$id is copy, :$auth is copy, :$images) {
+            $id .= body-text;
+            $auth .= body-text;
+
+            my Bool $skip-loop;
+            my IO() $user-store = "%s/%s".sprintf: $store, $id;
+            if "$user-store/auth".IO.slurp ne $auth {
+                response.status = 401;
+                $skip-loop = True;
+            }
+
+            my @res;
+            IMAGE: for @($images) -> $img {
+                last IMAGE if $skip-loop;
+
+                my Bool $stored = True;
+
+                my Str @messages;
+                @messages.push("Max file size exceeded")
+                    if $img.body-blob.bytes > 2097152; # 1024 * 1024 * 2
+                @messages.push("Only png/jpeg allowed");
+                    if $img.content-type ne "image/png"|"image/jpeg";
+
+                $stored = False if @messages.elems;
+                if $stored {
+                    my Str $filename = ('a'...'z', 'A'...'Z', 0...9).roll(16).join;
+                    spurt "$user-store/images/$filename", $img.body-blob;
+                }
+
+                push @res, %(
+                    filename => $img.filename,
+                    :@messages, :$stored
+                );
+            }
+
+            content 'application/json', @res;
+        }
     }
 
     # Serving static assets.
n class="w"> a.braces cat a.braces |linux/bootstrap/bootstrap run linux/calls > a.calls cat a.calls |linux/bootstrap/bootstrap run linux/sigils > a.sigils cat a.sigils |linux/bootstrap/bootstrap run linux/tests > a.tests # no assort since baremetal SubX doesn't have segments yet cat a.tests |linux/bootstrap/bootstrap run linux/dquotes > a.dquotes cat a.dquotes |linux/bootstrap/bootstrap run linux/pack > a.pack cat a.pack |linux/bootstrap/bootstrap linux/survey_baremetal > labels cat a.pack |linux/bootstrap/bootstrap linux/labels_baremetal labels > a.survey cat a.survey |linux/bootstrap/bootstrap run linux/hex > a.bin # Create code.img containing a.bin dd if=/dev/zero of=code.img count=20160 # 20*16*63 512-byte sectors = almost 10MB dd if=a.bin of=code.img conv=notrunc if [ `stat --printf="%s" a.bin` -ge 258048 ] # 8 tracks * 63 sectors per track * 512 bytes per sector (keep this sync'd with boot.subx) then echo "a.bin won't all be loaded on boot" exit 1 fi # Latter half of disk is for debug info. dd if=labels of=code.img seek=10080 conv=notrunc if [ `stat --printf="%s" labels` -ge 131072 ] # 256 sectors * 512 bytes per sector, the most an ATA drive can read in a single command then echo "labels won't all be loaded on abort" exit 1 fi