mirror of
https://github.com/BookStackApp/BookStack.git
synced 2024-10-01 05:36:00 +00:00
Ran a pass through image and attachment routes
Added some stronger types, formatting changes and simplifications along the way.
This commit is contained in:
parent
c7fea8fe08
commit
4360da03d4
@ -66,13 +66,13 @@ class CommentRepo
|
|||||||
/**
|
/**
|
||||||
* Delete a comment from the system.
|
* Delete a comment from the system.
|
||||||
*/
|
*/
|
||||||
public function delete(Comment $comment)
|
public function delete(Comment $comment): void
|
||||||
{
|
{
|
||||||
$comment->delete();
|
$comment->delete();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Convert the given comment markdown text to HTML.
|
* Convert the given comment Markdown to HTML.
|
||||||
*/
|
*/
|
||||||
public function commentToHtml(string $commentText): string
|
public function commentToHtml(string $commentText): string
|
||||||
{
|
{
|
||||||
|
@ -68,6 +68,7 @@ class AttachmentController extends Controller
|
|||||||
'file' => 'required|file',
|
'file' => 'required|file',
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
/** @var Attachment $attachment */
|
||||||
$attachment = Attachment::query()->findOrFail($attachmentId);
|
$attachment = Attachment::query()->findOrFail($attachmentId);
|
||||||
$this->checkOwnablePermission('view', $attachment->page);
|
$this->checkOwnablePermission('view', $attachment->page);
|
||||||
$this->checkOwnablePermission('page-update', $attachment->page);
|
$this->checkOwnablePermission('page-update', $attachment->page);
|
||||||
@ -86,11 +87,10 @@ class AttachmentController extends Controller
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the update form for an attachment.
|
* Get the update form for an attachment.
|
||||||
*
|
|
||||||
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
|
|
||||||
*/
|
*/
|
||||||
public function getUpdateForm(string $attachmentId)
|
public function getUpdateForm(string $attachmentId)
|
||||||
{
|
{
|
||||||
|
/** @var Attachment $attachment */
|
||||||
$attachment = Attachment::query()->findOrFail($attachmentId);
|
$attachment = Attachment::query()->findOrFail($attachmentId);
|
||||||
|
|
||||||
$this->checkOwnablePermission('page-update', $attachment->page);
|
$this->checkOwnablePermission('page-update', $attachment->page);
|
||||||
@ -173,6 +173,7 @@ class AttachmentController extends Controller
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the attachments for a specific page.
|
* Get the attachments for a specific page.
|
||||||
|
* @throws NotFoundException
|
||||||
*/
|
*/
|
||||||
public function listForPage(int $pageId)
|
public function listForPage(int $pageId)
|
||||||
{
|
{
|
||||||
|
@ -67,13 +67,12 @@ class DrawioImageController extends Controller
|
|||||||
public function getAsBase64($id)
|
public function getAsBase64($id)
|
||||||
{
|
{
|
||||||
$image = $this->imageRepo->getById($id);
|
$image = $this->imageRepo->getById($id);
|
||||||
$page = $image->getPage();
|
if (is_null($image) || $image->type !== 'drawio' || !userCan('page-view', $image->getPage())) {
|
||||||
if ($image === null || $image->type !== 'drawio' || !userCan('page-view', $page)) {
|
|
||||||
return $this->jsonError('Image data could not be found');
|
return $this->jsonError('Image data could not be found');
|
||||||
}
|
}
|
||||||
|
|
||||||
$imageData = $this->imageRepo->getImageData($image);
|
$imageData = $this->imageRepo->getImageData($image);
|
||||||
if ($imageData === null) {
|
if (is_null($imageData)) {
|
||||||
return $this->jsonError('Image data could not be found');
|
return $this->jsonError('Image data could not be found');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -17,10 +17,7 @@ class ImageRepo
|
|||||||
/**
|
/**
|
||||||
* ImageRepo constructor.
|
* ImageRepo constructor.
|
||||||
*/
|
*/
|
||||||
public function __construct(
|
public function __construct(ImageService $imageService, PermissionService $permissionService) {
|
||||||
ImageService $imageService,
|
|
||||||
PermissionService $permissionService
|
|
||||||
) {
|
|
||||||
$this->imageService = $imageService;
|
$this->imageService = $imageService;
|
||||||
$this->restrictionService = $permissionService;
|
$this->restrictionService = $permissionService;
|
||||||
}
|
}
|
||||||
@ -43,7 +40,7 @@ class ImageRepo
|
|||||||
$hasMore = count($images) > $pageSize;
|
$hasMore = count($images) > $pageSize;
|
||||||
|
|
||||||
$returnImages = $images->take($pageSize);
|
$returnImages = $images->take($pageSize);
|
||||||
$returnImages->each(function ($image) {
|
$returnImages->each(function (Image $image) {
|
||||||
$this->loadThumbs($image);
|
$this->loadThumbs($image);
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -96,6 +93,7 @@ class ImageRepo
|
|||||||
int $uploadedTo = null,
|
int $uploadedTo = null,
|
||||||
string $search = null
|
string $search = null
|
||||||
): array {
|
): array {
|
||||||
|
/** @var Page $contextPage */
|
||||||
$contextPage = Page::visible()->findOrFail($uploadedTo);
|
$contextPage = Page::visible()->findOrFail($uploadedTo);
|
||||||
$parentFilter = null;
|
$parentFilter = null;
|
||||||
|
|
||||||
@ -131,7 +129,7 @@ class ImageRepo
|
|||||||
*
|
*
|
||||||
* @throws ImageUploadException
|
* @throws ImageUploadException
|
||||||
*/
|
*/
|
||||||
public function saveNewFromData(string $imageName, string $imageData, string $type, int $uploadedTo = 0)
|
public function saveNewFromData(string $imageName, string $imageData, string $type, int $uploadedTo = 0): Image
|
||||||
{
|
{
|
||||||
$image = $this->imageService->saveNew($imageName, $imageData, $type, $uploadedTo);
|
$image = $this->imageService->saveNew($imageName, $imageData, $type, $uploadedTo);
|
||||||
$this->loadThumbs($image);
|
$this->loadThumbs($image);
|
||||||
@ -140,13 +138,13 @@ class ImageRepo
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Save a drawing the the database.
|
* Save a drawing in the database.
|
||||||
*
|
*
|
||||||
* @throws ImageUploadException
|
* @throws ImageUploadException
|
||||||
*/
|
*/
|
||||||
public function saveDrawing(string $base64Uri, int $uploadedTo): Image
|
public function saveDrawing(string $base64Uri, int $uploadedTo): Image
|
||||||
{
|
{
|
||||||
$name = 'Drawing-' . strval(user()->id) . '-' . strval(time()) . '.png';
|
$name = 'Drawing-' . user()->id . '-' . time() . '.png';
|
||||||
|
|
||||||
return $this->imageService->saveNewFromBase64Uri($base64Uri, $name, 'drawio', $uploadedTo);
|
return $this->imageService->saveNewFromBase64Uri($base64Uri, $name, 'drawio', $uploadedTo);
|
||||||
}
|
}
|
||||||
@ -154,7 +152,6 @@ class ImageRepo
|
|||||||
/**
|
/**
|
||||||
* Update the details of an image via an array of properties.
|
* Update the details of an image via an array of properties.
|
||||||
*
|
*
|
||||||
* @throws ImageUploadException
|
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function updateImageDetails(Image $image, $updateDetails): Image
|
public function updateImageDetails(Image $image, $updateDetails): Image
|
||||||
@ -171,13 +168,11 @@ class ImageRepo
|
|||||||
*
|
*
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function destroyImage(Image $image = null): bool
|
public function destroyImage(Image $image = null): void
|
||||||
{
|
{
|
||||||
if ($image) {
|
if ($image) {
|
||||||
$this->imageService->destroy($image);
|
$this->imageService->destroy($image);
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -185,7 +180,7 @@ class ImageRepo
|
|||||||
*
|
*
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function destroyByType(string $imageType)
|
public function destroyByType(string $imageType): void
|
||||||
{
|
{
|
||||||
$images = Image::query()->where('type', '=', $imageType)->get();
|
$images = Image::query()->where('type', '=', $imageType)->get();
|
||||||
foreach ($images as $image) {
|
foreach ($images as $image) {
|
||||||
@ -195,10 +190,8 @@ class ImageRepo
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Load thumbnails onto an image object.
|
* Load thumbnails onto an image object.
|
||||||
*
|
|
||||||
* @throws Exception
|
|
||||||
*/
|
*/
|
||||||
public function loadThumbs(Image $image)
|
public function loadThumbs(Image $image): void
|
||||||
{
|
{
|
||||||
$image->setAttribute('thumbs', [
|
$image->setAttribute('thumbs', [
|
||||||
'gallery' => $this->getThumbnail($image, 150, 150, false),
|
'gallery' => $this->getThumbnail($image, 150, 150, false),
|
||||||
@ -210,10 +203,8 @@ class ImageRepo
|
|||||||
* Get the thumbnail for an image.
|
* Get the thumbnail for an image.
|
||||||
* If $keepRatio is true only the width will be used.
|
* If $keepRatio is true only the width will be used.
|
||||||
* Checks the cache then storage to avoid creating / accessing the filesystem on every check.
|
* Checks the cache then storage to avoid creating / accessing the filesystem on every check.
|
||||||
*
|
|
||||||
* @throws Exception
|
|
||||||
*/
|
*/
|
||||||
protected function getThumbnail(Image $image, ?int $width = 220, ?int $height = 220, bool $keepRatio = false): ?string
|
protected function getThumbnail(Image $image, ?int $width, ?int $height, bool $keepRatio): ?string
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
return $this->imageService->getThumbnail($image, $width, $height, $keepRatio);
|
return $this->imageService->getThumbnail($image, $width, $height, $keepRatio);
|
||||||
|
@ -15,6 +15,8 @@ use Illuminate\Support\Str;
|
|||||||
use Intervention\Image\Exception\NotSupportedException;
|
use Intervention\Image\Exception\NotSupportedException;
|
||||||
use Intervention\Image\ImageManager;
|
use Intervention\Image\ImageManager;
|
||||||
use League\Flysystem\Util;
|
use League\Flysystem\Util;
|
||||||
|
use Log;
|
||||||
|
use Psr\SimpleCache\InvalidArgumentException;
|
||||||
use Symfony\Component\HttpFoundation\File\UploadedFile;
|
use Symfony\Component\HttpFoundation\File\UploadedFile;
|
||||||
use Symfony\Component\HttpFoundation\StreamedResponse;
|
use Symfony\Component\HttpFoundation\StreamedResponse;
|
||||||
|
|
||||||
@ -156,7 +158,7 @@ class ImageService
|
|||||||
try {
|
try {
|
||||||
$this->saveImageDataInPublicSpace($storage, $this->adjustPathForStorageDisk($fullPath, $type), $imageData);
|
$this->saveImageDataInPublicSpace($storage, $this->adjustPathForStorageDisk($fullPath, $type), $imageData);
|
||||||
} catch (Exception $e) {
|
} catch (Exception $e) {
|
||||||
\Log::error('Error when attempting image upload:' . $e->getMessage());
|
Log::error('Error when attempting image upload:' . $e->getMessage());
|
||||||
|
|
||||||
throw new ImageUploadException(trans('errors.path_not_writable', ['filePath' => $fullPath]));
|
throw new ImageUploadException(trans('errors.path_not_writable', ['filePath' => $fullPath]));
|
||||||
}
|
}
|
||||||
@ -230,18 +232,10 @@ class ImageService
|
|||||||
* Get the thumbnail for an image.
|
* Get the thumbnail for an image.
|
||||||
* If $keepRatio is true only the width will be used.
|
* If $keepRatio is true only the width will be used.
|
||||||
* Checks the cache then storage to avoid creating / accessing the filesystem on every check.
|
* Checks the cache then storage to avoid creating / accessing the filesystem on every check.
|
||||||
*
|
|
||||||
* @param Image $image
|
|
||||||
* @param int $width
|
|
||||||
* @param int $height
|
|
||||||
* @param bool $keepRatio
|
|
||||||
*
|
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
* @throws ImageUploadException
|
* @throws InvalidArgumentException
|
||||||
*
|
|
||||||
* @return string
|
|
||||||
*/
|
*/
|
||||||
public function getThumbnail(Image $image, $width = 220, $height = 220, $keepRatio = false)
|
public function getThumbnail(Image $image, ?int $width, ?int $height, bool $keepRatio = false): string
|
||||||
{
|
{
|
||||||
if ($keepRatio && $this->isGif($image)) {
|
if ($keepRatio && $this->isGif($image)) {
|
||||||
return $this->getPublicUrl($image->path);
|
return $this->getPublicUrl($image->path);
|
||||||
@ -269,27 +263,16 @@ class ImageService
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Resize image data.
|
* Resize the image of given data to the specified size, and return the new image data.
|
||||||
*
|
|
||||||
* @param string $imageData
|
|
||||||
* @param int $width
|
|
||||||
* @param int $height
|
|
||||||
* @param bool $keepRatio
|
|
||||||
*
|
*
|
||||||
* @throws ImageUploadException
|
* @throws ImageUploadException
|
||||||
*
|
|
||||||
* @return string
|
|
||||||
*/
|
*/
|
||||||
protected function resizeImage(string $imageData, $width = 220, $height = null, bool $keepRatio = true)
|
protected function resizeImage(string $imageData, ?int $width, ?int $height, bool $keepRatio): string
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
$thumb = $this->imageTool->make($imageData);
|
$thumb = $this->imageTool->make($imageData);
|
||||||
} catch (Exception $e) {
|
} catch (ErrorException | NotSupportedException $e) {
|
||||||
if ($e instanceof ErrorException || $e instanceof NotSupportedException) {
|
throw new ImageUploadException(trans('errors.cannot_create_thumbs'));
|
||||||
throw new ImageUploadException(trans('errors.cannot_create_thumbs'));
|
|
||||||
}
|
|
||||||
|
|
||||||
throw $e;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($keepRatio) {
|
if ($keepRatio) {
|
||||||
@ -523,7 +506,7 @@ class ImageService
|
|||||||
*/
|
*/
|
||||||
private function getPublicUrl(string $filePath): string
|
private function getPublicUrl(string $filePath): string
|
||||||
{
|
{
|
||||||
if ($this->storageUrl === null) {
|
if (is_null($this->storageUrl)) {
|
||||||
$storageUrl = config('filesystems.url');
|
$storageUrl = config('filesystems.url');
|
||||||
|
|
||||||
// Get the standard public s3 url if s3 is set as storage type
|
// Get the standard public s3 url if s3 is set as storage type
|
||||||
@ -537,6 +520,7 @@ class ImageService
|
|||||||
$storageUrl = 'https://s3-' . $storageDetails['region'] . '.amazonaws.com/' . $storageDetails['bucket'];
|
$storageUrl = 'https://s3-' . $storageDetails['region'] . '.amazonaws.com/' . $storageDetails['bucket'];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->storageUrl = $storageUrl;
|
$this->storageUrl = $storageUrl;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user