1
0
mirror of https://github.com/BookStackApp/BookStack.git synced 2025-07-28 17:02:04 +03:00

Reverted work on revisions

Improved linkage of drawings and image manager.
Updated image updates to create new versions.
This commit is contained in:
Dan Brown
2018-05-20 16:40:30 +01:00
parent 6cdb943916
commit 0c9c1e4c6b
10 changed files with 33 additions and 277 deletions

View File

@ -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)
{

View File

@ -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'));
}
}

View File

@ -1,26 +0,0 @@
<?php
namespace BookStack;
use Illuminate\Database\Eloquent\Model;
class ImageRevision extends Model
{
/**
* Relation for the user that created this entity.
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function createdBy()
{
return $this->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);
}
}

View File

@ -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);
}
}

View File

@ -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();
}