summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--client/src/views/CreateSmiles.vue3
-rw-r--r--client/src/views/ManageSmiles.vue43
-rw-r--r--server/service.raku58
3 files changed, 88 insertions, 16 deletions
diff --git a/client/src/views/CreateSmiles.vue b/client/src/views/CreateSmiles.vue
index 7c285f0..88da1f4 100644
--- a/client/src/views/CreateSmiles.vue
+++ b/client/src/views/CreateSmiles.vue
@@ -112,7 +112,8 @@ export default {
                 body: JSON.stringify(data)
             }).then(response => {
                 if (!response.ok)
-                    throw new Error('HTTP error, status = ' + response.status);
+                    throw new Error('HTTP error, status = ' + response.status
+                                    + ' | ' + response.statusText);
                 return response.json();
             }).then(res => {
                 this.id = res.id;
diff --git a/client/src/views/ManageSmiles.vue b/client/src/views/ManageSmiles.vue
index 9a535b7..370b908 100644
--- a/client/src/views/ManageSmiles.vue
+++ b/client/src/views/ManageSmiles.vue
@@ -25,7 +25,10 @@
         </div>
       </div>
       <div class="col-12 md:col-6">
-        <FileUpload name="demo[]" url="/upload" @upload="onUpload" :multiple="true" accept="image/*" :maxFileSize="1000000">
+        <FileUpload @error="onError" @before-send="beforeSend" @upload="onUpload"
+                    name="images" url="http://localhost:9090/upload"
+                    :multiple="true" accept="image/*"
+                    :maxFileSize="2097152"> <!-- 1024 * 1024 * 2 -->
           <template #empty>
             <p>Drag and drop files to here to upload.</p>
           </template>
@@ -61,6 +64,39 @@ export default {
             link.push(this.id);
             return link.join('/');
         },
+        beforeSend(xhr) {
+            xhr.formData.append('id', this.id);
+            xhr.formData.append('auth', this.auth);
+        },
+        onError(error) {
+            this.$toast.add(
+                {severity: 'error',
+                 summary: 'Upload failed',
+                 detail: `${error.xhr.status} ${error.xhr.statusText} | ${error.xhr.response}`});
+        },
+        onUpload(res) {
+            const response = JSON.parse(res.xhr.response);
+            for(let i = 0; i < response.length; i++) {
+                const x = response[i];
+                if (x.stored === false) {
+                    this.$toast.add(
+                        {
+                            severity: 'error',
+                            summary: 'Upload failed',
+                            detail: `${x.filename}: ${x.messages}`
+                        }
+                    );
+                } else if (x.messages.length !== 0) {
+                    this.$toast.add(
+                        {
+                            severity: 'warn',
+                            summary: 'Upload Issues',
+                            detail: `${x.filename}: ${x.messages}`
+                        }
+                    );
+                }
+            }
+        },
         verify() {
             const data = { id: this.id, auth: this.auth };
             const toast = this.$toast;
@@ -73,12 +109,15 @@ export default {
                 referrerPolicy: 'no-referrer',
                 body: JSON.stringify(data)
             }).then(response => {
+                if (response.status === 401)
+                    throw new Error(response.status + ' # ' + 'Authentication Failed');
+                if (response.status === 404)
+                    throw new Error(response.status + ' # ' + 'Smiles deleted or invalid link');
                 if (!response.ok)
                     throw new Error('HTTP error, status = ' + response.status);
                 return response.json();
             }).then(res => {
                 this.name = res.name;
-                console.log(res);
                 this.verified = true;
             }).catch(error => {
                 console.log(error)
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.