From a1ecdcacbaf3194ae44a588cdd2f853f5edef647 Mon Sep 17 00:00:00 2001
From: Dan Brown <ssddanbrown@googlemail.com>
Date: Sun, 20 May 2018 11:06:10 +0100
Subject: [PATCH] Fixed attachment error handling, Allowed all link types

Related to #812
---
 app/Attachment.php                             |  3 +++
 app/Http/Controllers/AttachmentController.php  |  5 +++--
 resources/assets/js/vues/attachment-manager.js | 10 ++++++----
 3 files changed, 12 insertions(+), 6 deletions(-)

diff --git a/app/Attachment.php b/app/Attachment.php
index 55344cd7d..6749130d9 100644
--- a/app/Attachment.php
+++ b/app/Attachment.php
@@ -31,6 +31,9 @@ class Attachment extends Ownable
      */
     public function getUrl()
     {
+        if ($this->external && strpos($this->path, 'http') !== 0) {
+            return $this->path;
+        }
         return baseUrl('/attachments/' . $this->id);
     }
 }
diff --git a/app/Http/Controllers/AttachmentController.php b/app/Http/Controllers/AttachmentController.php
index ea41278ae..54e14bfb6 100644
--- a/app/Http/Controllers/AttachmentController.php
+++ b/app/Http/Controllers/AttachmentController.php
@@ -103,7 +103,7 @@ class AttachmentController extends Controller
         $this->validate($request, [
             'uploaded_to' => 'required|integer|exists:pages,id',
             'name' => 'required|string|min:1|max:255',
-            'link' =>  'url|min:1|max:255'
+            'link' =>  'string|min:1|max:255'
         ]);
 
         $pageId = $request->get('uploaded_to');
@@ -131,7 +131,7 @@ class AttachmentController extends Controller
         $this->validate($request, [
             'uploaded_to' => 'required|integer|exists:pages,id',
             'name' => 'required|string|min:1|max:255',
-            'link' =>  'required|url|min:1|max:255'
+            'link' =>  'required|string|min:1|max:255'
         ]);
 
         $pageId = $request->get('uploaded_to');
@@ -184,6 +184,7 @@ class AttachmentController extends Controller
      * @param $attachmentId
      * @return \Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|\Symfony\Component\HttpFoundation\Response
      * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
+     * @throws NotFoundException
      */
     public function get($attachmentId)
     {
diff --git a/resources/assets/js/vues/attachment-manager.js b/resources/assets/js/vues/attachment-manager.js
index 635622b93..16f96c70b 100644
--- a/resources/assets/js/vues/attachment-manager.js
+++ b/resources/assets/js/vues/attachment-manager.js
@@ -31,6 +31,9 @@ let methods = {
     },
 
     getFileUrl(file) {
+        if (file.external && file.path.indexOf('http') !== 0) {
+            return file.path;
+        }
         return window.baseUrl(`/attachments/${file.id}`);
     },
 
@@ -79,10 +82,8 @@ let methods = {
     },
 
     checkValidationErrors(groupName, err) {
-        console.error(err);
-        if (typeof err.response.data === "undefined" && typeof err.response.data.validation === "undefined") return;
-        this.errors[groupName] = err.response.data.validation;
-        console.log(this.errors[groupName]);
+        if (typeof err.response.data === "undefined" && typeof err.response.data === "undefined") return;
+        this.errors[groupName] = err.response.data;
     },
 
     getUploadUrl(file) {
@@ -97,6 +98,7 @@ let methods = {
 
     attachNewLink(file) {
         file.uploaded_to = this.pageId;
+        this.errors.link = {};
         this.$http.post(window.baseUrl('/attachments/link'), file).then(resp => {
             this.files.push(resp.data);
             this.file = this.newFile();