]> BookStack Code Mirror - bookstack/blobdiff - app/Http/Controllers/ImageController.php
Actually fixed the BaseURL this time 🤦
[bookstack] / app / Http / Controllers / ImageController.php
index 1b064de01c9e6ce543977b8f2426ef5ebf0df477..e675bff0c5b52bbaef6e13db509f287ddfb31a63 100644 (file)
@@ -1,6 +1,7 @@
 <?php namespace BookStack\Http\Controllers;
 
 use BookStack\Exceptions\ImageUploadException;
+use BookStack\Exceptions\NotFoundException;
 use BookStack\Repos\EntityRepo;
 use BookStack\Repos\ImageRepo;
 use Illuminate\Filesystem\Filesystem as File;
@@ -28,6 +29,21 @@ class ImageController extends Controller
         parent::__construct();
     }
 
+    /**
+     * Provide an image file from storage.
+     * @param string $path
+     * @return mixed
+     */
+    public function showImage(string $path)
+    {
+        $path = storage_path('uploads/images/' . $path);
+        if (!file_exists($path)) {
+            abort(404);
+        }
+
+        return response()->file($path);
+    }
+
     /**
      * Get all images for a specific type, Paginated
      * @param string $type
@@ -104,7 +120,10 @@ class ImageController extends Controller
         $this->validate($request, [
             'file' => 'is_image'
         ]);
-        // TODO - Restrict & validate types
+
+        if (!$this->imageRepo->isValidType($type)) {
+            return $this->jsonError(trans('errors.image_upload_type_error'));
+        }
 
         $imageUpload = $request->file('file');
 
@@ -142,6 +161,32 @@ class ImageController extends Controller
         return response()->json($image);
     }
 
+    /**
+     * Replace the data content of a drawing.
+     * @param string $id
+     * @param Request $request
+     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\JsonResponse|\Symfony\Component\HttpFoundation\Response
+     */
+    public function replaceDrawing(string $id, Request $request)
+    {
+        $this->validate($request, [
+            'image' => 'required|string'
+        ]);
+        $this->checkPermission('image-create-all');
+
+        $imageBase64Data = $request->get('image');
+        $image = $this->imageRepo->getById($id);
+        $this->checkOwnablePermission('image-update', $image);
+
+        try {
+            $image = $this->imageRepo->replaceDrawingContent($image, $imageBase64Data);
+        } catch (ImageUploadException $e) {
+            return response($e->getMessage(), 500);
+        }
+
+        return response()->json($image);
+    }
+
     /**
      * Get the content of an image based64 encoded.
      * @param $id