diff --git a/app/Http/Controllers/ImageController.php b/app/Http/Controllers/ImageController.php index d78350754..e675bff0c 100644 --- a/app/Http/Controllers/ImageController.php +++ b/app/Http/Controllers/ImageController.php @@ -112,6 +112,7 @@ class ImageController extends Controller * @param string $type * @param Request $request * @return \Illuminate\Http\JsonResponse + * @throws \Exception */ public function uploadByType($type, Request $request) { @@ -120,10 +121,14 @@ class ImageController extends Controller 'file' => 'is_image' ]); + if (!$this->imageRepo->isValidType($type)) { + return $this->jsonError(trans('errors.image_upload_type_error')); + } + $imageUpload = $request->file('file'); try { - $uploadedTo = $request->filled('uploaded_to') ? $request->get('uploaded_to') : 0; + $uploadedTo = $request->get('uploaded_to', 0); $image = $this->imageRepo->saveNew($imageUpload, $type, $uploadedTo); } catch (ImageUploadException $e) { return response($e->getMessage(), 500); @@ -132,6 +137,73 @@ class ImageController extends Controller return response()->json($image); } + /** + * Upload a drawing to the system. + * @param Request $request + * @return \Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\JsonResponse|\Symfony\Component\HttpFoundation\Response + */ + public function uploadDrawing(Request $request) + { + $this->validate($request, [ + 'image' => 'required|string', + 'uploaded_to' => 'required|integer' + ]); + $this->checkPermission('image-create-all'); + $imageBase64Data = $request->get('image'); + + try { + $uploadedTo = $request->get('uploaded_to', 0); + $image = $this->imageRepo->saveDrawing($imageBase64Data, $uploadedTo); + } catch (ImageUploadException $e) { + return response($e->getMessage(), 500); + } + + 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 + * @return \Illuminate\Http\JsonResponse|mixed + */ + public function getBase64Image($id) + { + $image = $this->imageRepo->getById($id); + $imageData = $this->imageRepo->getImageData($image); + if ($imageData === null) { + return $this->jsonError("Image data could not be found"); + } + return response()->json([ + 'content' => base64_encode($imageData) + ]); + } + /** * Generate a sized thumbnail for an image. * @param $id @@ -139,6 +211,8 @@ class ImageController extends Controller * @param $height * @param $crop * @return \Illuminate\Http\JsonResponse + * @throws ImageUploadException + * @throws \Exception */ public function getThumbnail($id, $width, $height, $crop) { @@ -153,6 +227,8 @@ class ImageController extends Controller * @param integer $imageId * @param Request $request * @return \Illuminate\Http\JsonResponse + * @throws ImageUploadException + * @throws \Exception */ public function update($imageId, Request $request) { diff --git a/app/Repos/ImageRepo.php b/app/Repos/ImageRepo.php index 5f04a74b1..0c15a4310 100644 --- a/app/Repos/ImageRepo.php +++ b/app/Repos/ImageRepo.php @@ -1,12 +1,9 @@ getShortName(40) . '-' . strval(time()) . '.png'; + $image = $this->imageService->saveNewFromBase64Uri($base64Uri, $name, 'drawio', $uploadedTo); + return $image; + } + + /** + * Replace the image content of a drawing. + * @param Image $image + * @param string $base64Uri + * @return Image + * @throws \BookStack\Exceptions\ImageUploadException + */ + public function replaceDrawingContent(Image $image, string $base64Uri) + { + return $this->imageService->replaceImageDataFromBase64Uri($image, $base64Uri); + } + /** * Update the details of an image via an array of properties. * @param Image $image * @param array $updateDetails * @return Image + * @throws \BookStack\Exceptions\ImageUploadException + * @throws \Exception */ public function updateImageDetails(Image $image, $updateDetails) { @@ -170,6 +197,8 @@ class ImageRepo /** * Load thumbnails onto an image object. * @param Image $image + * @throws \BookStack\Exceptions\ImageUploadException + * @throws \Exception */ private function loadThumbs(Image $image) { @@ -188,6 +217,8 @@ class ImageRepo * @param int $height * @param bool $keepRatio * @return string + * @throws \BookStack\Exceptions\ImageUploadException + * @throws \Exception */ public function getThumbnail(Image $image, $width = 220, $height = 220, $keepRatio = false) { @@ -199,5 +230,29 @@ class ImageRepo } } + /** + * Get the raw image data from an Image. + * @param Image $image + * @return null|string + */ + public function getImageData(Image $image) + { + try { + return $this->imageService->getImageData($image); + } catch (\Exception $exception) { + return null; + } + } + + /** + * Check if the provided image type is valid. + * @param $type + * @return bool + */ + public function isValidType($type) + { + $validTypes = ['drawing', 'gallery', 'cover', 'system', 'user']; + return in_array($type, $validTypes); + } } \ No newline at end of file diff --git a/app/Services/ImageService.php b/app/Services/ImageService.php index 43375ee09..5eea285e5 100644 --- a/app/Services/ImageService.php +++ b/app/Services/ImageService.php @@ -46,6 +46,50 @@ class ImageService extends UploadService return $this->saveNew($imageName, $imageData, $type, $uploadedTo); } + /** + * Save a new image from a uri-encoded base64 string of data. + * @param string $base64Uri + * @param string $name + * @param string $type + * @param int $uploadedTo + * @return Image + * @throws ImageUploadException + */ + public function saveNewFromBase64Uri(string $base64Uri, string $name, string $type, $uploadedTo = 0) + { + $splitData = explode(';base64,', $base64Uri); + if (count($splitData) < 2) { + throw new ImageUploadException("Invalid base64 image data provided"); + } + $data = base64_decode($splitData[1]); + return $this->saveNew($name, $data, $type, $uploadedTo); + } + + /** + * Replace the data for an image via a Base64 encoded string. + * @param Image $image + * @param string $base64Uri + * @return Image + * @throws ImageUploadException + */ + public function replaceImageDataFromBase64Uri(Image $image, string $base64Uri) + { + $splitData = explode(';base64,', $base64Uri); + if (count($splitData) < 2) { + throw new ImageUploadException("Invalid base64 image data provided"); + } + $data = base64_decode($splitData[1]); + $storage = $this->getStorage(); + + try { + $storage->put($image->path, $data); + } catch (Exception $e) { + throw new ImageUploadException(trans('errors.path_not_writable', ['filePath' => $image->path])); + } + + return $image; + } + /** * Gets an image from url and saves it to the database. * @param $url @@ -175,6 +219,19 @@ class ImageService extends UploadService return $this->getPublicUrl($thumbFilePath); } + /** + * Get the raw data content from an image. + * @param Image $image + * @return string + * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException + */ + public function getImageData(Image $image) + { + $imagePath = $this->getPath($image); + $storage = $this->getStorage(); + return $storage->get($imagePath); + } + /** * Destroys an Image object along with its files and thumbnails. * @param Image $image diff --git a/config/services.php b/config/services.php index ba9be69de..8695ea91c 100644 --- a/config/services.php +++ b/config/services.php @@ -13,7 +13,12 @@ return [ | to have a conventional place to find your various credentials. | */ + + // Single option to disable non-auth external services such as Gravatar and Draw.io 'disable_services' => env('DISABLE_EXTERNAL_SERVICES', false), + 'drawio' => env('DRAWIO', !env('DISABLE_EXTERNAL_SERVICES', false)), + + 'callback_url' => env('APP_URL', false), 'mailgun' => [ diff --git a/public/system_images/drawing.svg b/public/system_images/drawing.svg new file mode 100644 index 000000000..9a9231a18 --- /dev/null +++ b/public/system_images/drawing.svg @@ -0,0 +1,107 @@ + + + + diff --git a/resources/assets/js/components/markdown-editor.js b/resources/assets/js/components/markdown-editor.js index 7b051dd12..3393829cc 100644 --- a/resources/assets/js/components/markdown-editor.js +++ b/resources/assets/js/components/markdown-editor.js @@ -1,6 +1,8 @@ const MarkdownIt = require("markdown-it"); const mdTasksLists = require('markdown-it-task-lists'); -const code = require('../code'); +const code = require('../libs/code'); + +const DrawIO = require('../libs/drawio'); class MarkdownEditor { @@ -20,13 +22,26 @@ class MarkdownEditor { init() { + let lastClick = 0; + // Prevent markdown display link click redirect this.display.addEventListener('click', event => { - let link = event.target.closest('a'); - if (link === null) return; + let isDblClick = Date.now() - lastClick < 300; - event.preventDefault(); - window.open(link.getAttribute('href')); + let link = event.target.closest('a'); + if (link !== null) { + event.preventDefault(); + window.open(link.getAttribute('href')); + return; + } + + let drawing = event.target.closest('[drawio-diagram]'); + if (drawing !== null && isDblClick) { + this.actionEditDrawing(drawing); + return; + } + + lastClick = Date.now(); }); // Button actions @@ -37,6 +52,7 @@ class MarkdownEditor { let action = button.getAttribute('data-action'); if (action === 'insertImage') this.actionInsertImage(); if (action === 'insertLink') this.actionShowLinkSelector(); + if (action === 'insertDrawing') this.actionStartDrawing(); }); window.$events.listen('editor-markdown-update', value => { @@ -290,6 +306,70 @@ class MarkdownEditor { }); } + // Show draw.io if enabled and handle save. + actionStartDrawing() { + if (document.querySelector('[drawio-enabled]').getAttribute('drawio-enabled') !== 'true') return; + let cursorPos = this.cm.getCursor('from'); + + DrawIO.show(() => { + return Promise.resolve(''); + }, (pngData) => { + // let id = "image-" + Math.random().toString(16).slice(2); + // let loadingImage = window.baseUrl('/loading.gif'); + let data = { + image: pngData, + uploaded_to: Number(document.getElementById('page-editor').getAttribute('page-id')) + }; + + window.$http.post(window.baseUrl('/images/drawing/upload'), data).then(resp => { + let newText = `