Added drawing update ability
This commit is contained in:
		
							parent
							
								
									920964a561
								
							
						
					
					
						commit
						1bfd77e7a1
					
				| 
						 | 
					@ -142,6 +142,32 @@ class ImageController extends Controller
 | 
				
			||||||
        return response()->json($image);
 | 
					        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.
 | 
					     * Get the content of an image based64 encoded.
 | 
				
			||||||
     * @param $id
 | 
					     * @param $id
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -156,6 +156,18 @@ class ImageRepo
 | 
				
			||||||
        return $image;
 | 
					        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.
 | 
					     * Update the details of an image via an array of properties.
 | 
				
			||||||
     * @param Image $image
 | 
					     * @param Image $image
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -65,6 +65,31 @@ class ImageService extends UploadService
 | 
				
			||||||
        return $this->saveNew($name, $data, $type, $uploadedTo);
 | 
					        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.
 | 
					     * Gets an image from url and saves it to the database.
 | 
				
			||||||
     * @param             $url
 | 
					     * @param             $url
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -265,7 +265,20 @@ function drawIoPlugin() {
 | 
				
			||||||
            uploaded_to: Number(document.getElementById('page-editor').getAttribute('page-id'))
 | 
					            uploaded_to: Number(document.getElementById('page-editor').getAttribute('page-id'))
 | 
				
			||||||
        };
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        // TODO - Handle updating an existing image
 | 
					        // Handle updating an existing image
 | 
				
			||||||
 | 
					        if (currentNode) {
 | 
				
			||||||
 | 
					            console.log(currentNode);
 | 
				
			||||||
 | 
					            drawEventClose();
 | 
				
			||||||
 | 
					            let imgElem = currentNode.querySelector('img');
 | 
				
			||||||
 | 
					            let drawingId = currentNode.getAttribute('drawio-diagram');
 | 
				
			||||||
 | 
					            window.$http.put(window.baseUrl(`/images/drawing/upload/${drawingId}`), data).then(resp => {
 | 
				
			||||||
 | 
					                pageEditor.dom.setAttrib(imgElem, 'src', `${resp.data.url}?updated=${Date.now()}`);
 | 
				
			||||||
 | 
					            }).catch(err => {
 | 
				
			||||||
 | 
					                window.$events.emit('error', trans('errors.image_upload_error'));
 | 
				
			||||||
 | 
					                console.log(err);
 | 
				
			||||||
 | 
					            });
 | 
				
			||||||
 | 
					            return;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        setTimeout(() => {
 | 
					        setTimeout(() => {
 | 
				
			||||||
            pageEditor.insertContent(`<div drawio-diagram contenteditable="false"><img src="${loadingImage}" id="${id}"></div>`);
 | 
					            pageEditor.insertContent(`<div drawio-diagram contenteditable="false"><img src="${loadingImage}" id="${id}"></div>`);
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -89,6 +89,7 @@ Route::group(['middleware' => 'auth'], function () {
 | 
				
			||||||
        Route::get('/base64/{id}', 'ImageController@getBase64Image');
 | 
					        Route::get('/base64/{id}', 'ImageController@getBase64Image');
 | 
				
			||||||
        Route::put('/update/{imageId}', 'ImageController@update');
 | 
					        Route::put('/update/{imageId}', 'ImageController@update');
 | 
				
			||||||
        Route::post('/drawing/upload', 'ImageController@uploadDrawing');
 | 
					        Route::post('/drawing/upload', 'ImageController@uploadDrawing');
 | 
				
			||||||
 | 
					        Route::put('/drawing/upload/{id}', 'ImageController@replaceDrawing');
 | 
				
			||||||
        Route::post('/{type}/upload', 'ImageController@uploadByType');
 | 
					        Route::post('/{type}/upload', 'ImageController@uploadByType');
 | 
				
			||||||
        Route::get('/{type}/all', 'ImageController@getAllByType');
 | 
					        Route::get('/{type}/all', 'ImageController@getAllByType');
 | 
				
			||||||
        Route::get('/{type}/all/{page}', 'ImageController@getAllByType');
 | 
					        Route::get('/{type}/all/{page}', 'ImageController@getAllByType');
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue