From 0c9c1e4c6b7aa38034c65a98a0499ae4fcd323b3 Mon Sep 17 00:00:00 2001 From: Dan Brown Date: Sun, 20 May 2018 16:40:30 +0100 Subject: [PATCH] Reverted work on revisions Improved linkage of drawings and image manager. Updated image updates to create new versions. --- app/Http/Controllers/ImageController.php | 39 +--------- app/Image.php | 19 ----- app/ImageRevision.php | 26 ------- app/Repos/ImageRepo.php | 13 +--- app/Services/ImageService.php | 77 ------------------- ...13_090521_create_image_revisions_table.php | 38 --------- .../assets/js/components/wysiwyg-editor.js | 42 ++++++---- resources/assets/js/vues/image-manager.js | 19 +---- .../views/components/image-manager.blade.php | 35 +-------- routes/web.php | 2 - 10 files changed, 33 insertions(+), 277 deletions(-) delete mode 100644 app/ImageRevision.php delete mode 100644 database/migrations/2018_05_13_090521_create_image_revisions_table.php diff --git a/app/Http/Controllers/ImageController.php b/app/Http/Controllers/ImageController.php index bf7f3bb82..eb92ae9a8 100644 --- a/app/Http/Controllers/ImageController.php +++ b/app/Http/Controllers/ImageController.php @@ -164,32 +164,6 @@ 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 updateDrawing(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->updateDrawing($image, $imageBase64Data); - } catch (ImageUploadException $e) { - return response($e->getMessage(), 500); - } - - return response()->json($image); - } - /** * Get the content of an image based64 encoded. * @param $id @@ -257,22 +231,11 @@ class ImageController extends Controller return response()->json($pageSearch); } - /** - * Get the revisions for an image. - * @param $id - * @return \Illuminate\Http\JsonResponse - */ - public function getRevisions($id) - { - $image = $this->imageRepo->getById($id); - $revisions = $image->revisions()->orderBy('id', 'desc')->get(); - return response()->json($revisions); - } - /** * Deletes an image and all thumbnail/image files * @param int $id * @return \Illuminate\Http\JsonResponse + * @throws \Exception */ public function destroy($id) { diff --git a/app/Image.php b/app/Image.php index ac94d9bf0..412beea90 100644 --- a/app/Image.php +++ b/app/Image.php @@ -20,23 +20,4 @@ class Image extends Ownable return Images::getThumbnail($this, $width, $height, $keepRatio); } - /** - * Get the revisions for this image. - * @return \Illuminate\Database\Eloquent\Relations\HasMany - */ - public function revisions() - { - return $this->hasMany(ImageRevision::class); - } - - /** - * Get the count of revisions made to this image. - * Based off numbers on revisions rather than raw count of attached revisions - * as they may be cleared up or revisions deleted at some point. - * @return int - */ - public function revisionCount() - { - return intval($this->revisions()->max('revision')); - } } diff --git a/app/ImageRevision.php b/app/ImageRevision.php deleted file mode 100644 index fde232867..000000000 --- a/app/ImageRevision.php +++ /dev/null @@ -1,26 +0,0 @@ -belongsTo(User::class, 'created_by'); - } - - /** - * Get the image that this is a revision of. - * @return \Illuminate\Database\Eloquent\Relations\BelongsTo - */ - public function image() - { - return $this->belongsTo(Image::class); - } -} diff --git a/app/Repos/ImageRepo.php b/app/Repos/ImageRepo.php index 384ea5454..4ccd719ad 100644 --- a/app/Repos/ImageRepo.php +++ b/app/Repos/ImageRepo.php @@ -153,17 +153,6 @@ class ImageRepo return $image; } - /** - * Replace the image content of a drawing. - * @param Image $image - * @param string $base64Uri - * @return Image - * @throws \BookStack\Exceptions\ImageUploadException - */ - public function updateDrawing(Image $image, string $base64Uri) - { - return $this->imageService->updateImageFromBase64Uri($image, $base64Uri); - } /** * Update the details of an image via an array of properties. @@ -251,7 +240,7 @@ class ImageRepo */ public function isValidType($type) { - $validTypes = ['drawing', 'gallery', 'cover', 'system', 'user']; + $validTypes = ['gallery', 'cover', 'system', 'user']; return in_array($type, $validTypes); } } diff --git a/app/Services/ImageService.php b/app/Services/ImageService.php index e83c1860b..736b30705 100644 --- a/app/Services/ImageService.php +++ b/app/Services/ImageService.php @@ -2,7 +2,6 @@ use BookStack\Exceptions\ImageUploadException; use BookStack\Image; -use BookStack\ImageRevision; use BookStack\User; use Exception; use Intervention\Image\Exception\NotSupportedException; @@ -82,22 +81,6 @@ class ImageService extends UploadService return $this->saveNew($name, $data, $type, $uploadedTo); } - /** - * @param Image $image - * @param string $base64Uri - * @return Image - * @throws ImageUploadException - */ - public function updateImageFromBase64Uri(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]); - return $this->update($image, $data); - } - /** * Gets an image from url and saves it to the database. * @param $url @@ -168,59 +151,6 @@ class ImageService extends UploadService return $image; } - /** - * Update the content of an existing image. - * Uploaded the new image content and creates a revision for the old image content. - * @param Image $image - * @param $imageData - * @return Image - * @throws ImageUploadException - */ - private function update(Image $image, $imageData) - { - // Save image revision if not previously exists to ensure we always have - // a reference to the image files being uploaded. - if ($image->revisions()->count() === 0) { - $this->saveImageRevision($image); - } - - $pathInfo = pathinfo($image->path); - $revisionCount = $image->revisionCount() + 1; - $newFileName = preg_replace('/^(.+?)(-v\d+)?$/', '$1-v' . $revisionCount, $pathInfo['filename']); - - $image->path = str_replace_last($pathInfo['filename'], $newFileName, $image->path); - $image->url = $this->getPublicUrl($image->path); - $image->updated_by = user()->id; - - $storage = $this->getStorage(); - - try { - $storage->put($image->path, $imageData); - $storage->setVisibility($image->path, 'public'); - $image->save(); - $this->saveImageRevision($image); - } catch (Exception $e) { - throw new ImageUploadException(trans('errors.path_not_writable', ['filePath' => $image->path])); - } - return $image; - } - - /** - * Save a new revision for an image. - * @param Image $image - * @return ImageRevision - */ - protected function saveImageRevision(Image $image) - { - $revision = new ImageRevision(); - $revision->image_id = $image->id; - $revision->path = $image->path; - $revision->url = $image->url; - $revision->created_by = user()->id; - $revision->revision = $image->revisionCount() + 1; - $revision->save(); - return $revision; - } /** * Checks if the image is a gif. Returns true if it is, else false. @@ -309,13 +239,6 @@ class ImageService extends UploadService */ public function destroy(Image $image) { - // Destroy image revisions - foreach ($image->revisions as $revision) { - $this->destroyImagesFromPath($revision->path); - $revision->delete(); - } - - // Destroy main image $this->destroyImagesFromPath($image->path); $image->delete(); } diff --git a/database/migrations/2018_05_13_090521_create_image_revisions_table.php b/database/migrations/2018_05_13_090521_create_image_revisions_table.php deleted file mode 100644 index d3032258f..000000000 --- a/database/migrations/2018_05_13_090521_create_image_revisions_table.php +++ /dev/null @@ -1,38 +0,0 @@ -increments('id'); - $table->integer('image_id'); - $table->integer('revision'); - $table->string('path'); - $table->string('url'); - $table->integer('created_by'); - - $table->index('image_id'); - $table->timestamps(); - }); - } - - /** - * Reverse the migrations. - * - * @return void - */ - public function down() - { - Schema::dropIfExists('image_revisions'); - } -} diff --git a/resources/assets/js/components/wysiwyg-editor.js b/resources/assets/js/components/wysiwyg-editor.js index 03d73d2b5..764b0a929 100644 --- a/resources/assets/js/components/wysiwyg-editor.js +++ b/resources/assets/js/components/wysiwyg-editor.js @@ -229,16 +229,18 @@ function drawIoPlugin() { } function showDrawingManager(mceEditor, selectedNode = null) { - // TODO - Handle how image manager links in. + pageEditor = mceEditor; + currentNode = selectedNode; // Show image manager window.ImageManager.show(function (image) { - - - // // Replace the actively selected content with the linked image - // let html = ``; - // html += `${image.name}`; - // html += ''; - // win.tinyMCE.activeEditor.execCommand('mceInsertContent', false, html); + if (selectedNode) { + let imgElem = selectedNode.querySelector('img'); + pageEditor.dom.setAttrib(imgElem, 'src', image.url); + pageEditor.dom.setAttrib(selectedNode, 'drawio-diagram', image.id); + } else { + let imgHTML = `
`; + pageEditor.insertContent(imgHTML); + } }, 'drawio'); } @@ -260,9 +262,9 @@ function drawIoPlugin() { if (currentNode) { DrawIO.close(); 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()}`); + window.$http.post(window.baseUrl(`/images/drawing/upload`), data).then(resp => { + pageEditor.dom.setAttrib(imgElem, 'src', resp.data.url); + pageEditor.dom.setAttrib(currentNode, 'drawio-diagram', resp.data.id); }).catch(err => { window.$events.emit('error', trans('errors.image_upload_error')); console.log(err); @@ -300,17 +302,23 @@ function drawIoPlugin() { editor.addCommand('drawio', () => { let selectedNode = editor.selection.getNode(); - if (isDrawing(selectedNode)) { - showDrawingManager(editor, selectedNode); - } else { - showDrawingEditor(editor); - } + showDrawingEditor(editor, isDrawing(selectedNode) ? selectedNode : null); }); editor.addButton('drawio', { + type: 'splitbutton', tooltip: 'Drawing', image: window.baseUrl('/icon/drawing.svg?color=000000'), - cmd: 'drawio' + cmd: 'drawio', + menu: [ + { + text: 'Drawing Manager', + onclick() { + let selectedNode = editor.selection.getNode(); + showDrawingManager(editor, isDrawing(selectedNode) ? selectedNode : null); + } + } + ] }); editor.on('dblclick', event => { diff --git a/resources/assets/js/vues/image-manager.js b/resources/assets/js/vues/image-manager.js index 2a11a6ff2..f355107c0 100644 --- a/resources/assets/js/vues/image-manager.js +++ b/resources/assets/js/vues/image-manager.js @@ -24,9 +24,6 @@ const data = { searching: false, searchTerm: '', - revisions: [], - selectedRevision: null, - imageUpdateSuccess: false, imageDeleteSuccess: false, deleteConfirm: false, @@ -50,9 +47,11 @@ const methods = { }, hide() { + if (this.$refs.dropzone) { + this.$refs.dropzone.onClose(); + } this.showing = false; this.selectedImage = false; - this.$refs.dropzone.onClose(); this.$el.children[0].components.overlay.hide(); }, @@ -113,8 +112,6 @@ const methods = { let currentTime = Date.now(); let timeDiff = currentTime - previousClickTime; let isDblClick = timeDiff < dblClickTime && image.id === previousClickImage; - this.revisions = []; - this.selectedRevision = null if (isDblClick) { this.callbackAndHide(image); @@ -122,11 +119,6 @@ const methods = { this.selectedImage = image; this.deleteConfirm = false; this.dependantPages = false; - if (this.imageType === 'drawio') { - this.$http.get(window.baseUrl(`/images/revisions/${image.id}`)).then(resp => { - this.revisions = resp.data; - }) - } } previousClickTime = currentTime; @@ -182,11 +174,6 @@ const methods = { this.images.unshift(event.data); this.$events.emit('success', trans('components.image_upload_success')); }, - - selectRevision(revision) { - let rev = (this.selectedRevision === revision) ? null : revision; - this.selectedRevision = rev; - } }; const computed = { diff --git a/resources/views/components/image-manager.blade.php b/resources/views/components/image-manager.blade.php index 92a69c05d..eca35b8aa 100644 --- a/resources/views/components/image-manager.blade.php +++ b/resources/views/components/image-manager.blade.php @@ -41,20 +41,13 @@
- +