0
0
Fork 0
mirror of https://github.com/BookStackApp/BookStack.git synced 2025-05-10 02:50:39 +00:00

Removed generic "UploadService" which was doing very little

This commit is contained in:
Dan Brown 2020-12-06 12:58:40 +00:00
parent 162d893143
commit 7d38c96a23
No known key found for this signature in database
GPG key ID: 46D9F943C24A2EF9
3 changed files with 55 additions and 100 deletions

View file

@ -2,17 +2,29 @@
use BookStack\Exceptions\FileUploadException; use BookStack\Exceptions\FileUploadException;
use Exception; use Exception;
use Illuminate\Contracts\Filesystem\Factory as FileSystem;
use Illuminate\Contracts\Filesystem\Filesystem as FileSystemInstance;
use Illuminate\Support\Str; use Illuminate\Support\Str;
use Symfony\Component\HttpFoundation\File\UploadedFile; use Symfony\Component\HttpFoundation\File\UploadedFile;
class AttachmentService extends UploadService class AttachmentService
{ {
protected $fileSystem;
/**
* AttachmentService constructor.
*/
public function __construct(FileSystem $fileSystem)
{
$this->fileSystem = $fileSystem;
}
/** /**
* Get the storage that will be used for storing files. * Get the storage that will be used for storing files.
* @return \Illuminate\Contracts\Filesystem\Filesystem
*/ */
protected function getStorage() protected function getStorage(): FileSystemInstance
{ {
$storageType = config('filesystems.attachments'); $storageType = config('filesystems.attachments');

View file

@ -4,16 +4,18 @@ use BookStack\Auth\User;
use BookStack\Exceptions\HttpFetchException; use BookStack\Exceptions\HttpFetchException;
use BookStack\Exceptions\ImageUploadException; use BookStack\Exceptions\ImageUploadException;
use DB; use DB;
use ErrorException;
use Exception; use Exception;
use Illuminate\Contracts\Cache\Repository as Cache; use Illuminate\Contracts\Cache\Repository as Cache;
use Illuminate\Contracts\Filesystem\Factory as FileSystem; use Illuminate\Contracts\Filesystem\Factory as FileSystem;
use Illuminate\Contracts\Filesystem\Filesystem as FileSystemInstance;
use Illuminate\Contracts\Filesystem\FileNotFoundException;
use Illuminate\Support\Str; use Illuminate\Support\Str;
use Intervention\Image\Exception\NotSupportedException; use Intervention\Image\Exception\NotSupportedException;
use Intervention\Image\ImageManager; use Intervention\Image\ImageManager;
use phpDocumentor\Reflection\Types\Integer;
use Symfony\Component\HttpFoundation\File\UploadedFile; use Symfony\Component\HttpFoundation\File\UploadedFile;
class ImageService extends UploadService class ImageService
{ {
protected $imageTool; protected $imageTool;
@ -21,30 +23,24 @@ class ImageService extends UploadService
protected $storageUrl; protected $storageUrl;
protected $image; protected $image;
protected $http; protected $http;
protected $fileSystem;
/** /**
* ImageService constructor. * ImageService constructor.
* @param Image $image
* @param ImageManager $imageTool
* @param FileSystem $fileSystem
* @param Cache $cache
* @param HttpFetcher $http
*/ */
public function __construct(Image $image, ImageManager $imageTool, FileSystem $fileSystem, Cache $cache, HttpFetcher $http) public function __construct(Image $image, ImageManager $imageTool, FileSystem $fileSystem, Cache $cache, HttpFetcher $http)
{ {
$this->image = $image; $this->image = $image;
$this->imageTool = $imageTool; $this->imageTool = $imageTool;
$this->fileSystem = $fileSystem;
$this->cache = $cache; $this->cache = $cache;
$this->http = $http; $this->http = $http;
parent::__construct($fileSystem);
} }
/** /**
* Get the storage that will be used for storing images. * Get the storage that will be used for storing images.
* @param string $type
* @return \Illuminate\Contracts\Filesystem\Filesystem
*/ */
protected function getStorage($type = '') protected function getStorage(string $type = ''): FileSystemInstance
{ {
$storageType = config('filesystems.images'); $storageType = config('filesystems.images');
@ -58,12 +54,6 @@ class ImageService extends UploadService
/** /**
* Saves a new image from an upload. * Saves a new image from an upload.
* @param UploadedFile $uploadedFile
* @param string $type
* @param int $uploadedTo
* @param int|null $resizeWidth
* @param int|null $resizeHeight
* @param bool $keepRatio
* @return mixed * @return mixed
* @throws ImageUploadException * @throws ImageUploadException
*/ */
@ -107,10 +97,10 @@ class ImageService extends UploadService
/** /**
* 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
* @param string $type * @param string $type
* @param bool|string $imageName * @param bool|string $imageName
* @return mixed * @return mixed
* @throws \Exception * @throws Exception
*/ */
private function saveNewFromUrl($url, $type, $imageName = false) private function saveNewFromUrl($url, $type, $imageName = false)
{ {
@ -118,7 +108,7 @@ class ImageService extends UploadService
try { try {
$imageData = $this->http->fetch($url); $imageData = $this->http->fetch($url);
} catch (HttpFetchException $exception) { } catch (HttpFetchException $exception) {
throw new \Exception(trans('errors.cannot_get_image_from_url', ['url' => $url])); throw new Exception(trans('errors.cannot_get_image_from_url', ['url' => $url]));
} }
return $this->saveNew($imageName, $imageData, $type); return $this->saveNew($imageName, $imageData, $type);
} }
@ -152,10 +142,10 @@ class ImageService extends UploadService
} }
$imageDetails = [ $imageDetails = [
'name' => $imageName, 'name' => $imageName,
'path' => $fullPath, 'path' => $fullPath,
'url' => $this->getPublicUrl($fullPath), 'url' => $this->getPublicUrl($fullPath),
'type' => $type, 'type' => $type,
'uploaded_to' => $uploadedTo 'uploaded_to' => $uploadedTo
]; ];
@ -185,15 +175,13 @@ class ImageService extends UploadService
$name = Str::random(10); $name = Str::random(10);
} }
return $name . '.' . $extension; return $name . '.' . $extension;
} }
/** /**
* Checks if the image is a gif. Returns true if it is, else false. * Checks if the image is a gif. Returns true if it is, else false.
* @param Image $image
* @return boolean
*/ */
protected function isGif(Image $image) protected function isGif(Image $image): bool
{ {
return strtolower(pathinfo($image->path, PATHINFO_EXTENSION)) === 'gif'; return strtolower(pathinfo($image->path, PATHINFO_EXTENSION)) === 'gif';
} }
@ -253,7 +241,7 @@ class ImageService extends UploadService
try { try {
$thumb = $this->imageTool->make($imageData); $thumb = $this->imageTool->make($imageData);
} catch (Exception $e) { } catch (Exception $e) {
if ($e instanceof \ErrorException || $e instanceof NotSupportedException) { 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; throw $e;
@ -281,11 +269,9 @@ class ImageService extends UploadService
/** /**
* Get the raw data content from an image. * Get the raw data content from an image.
* @param Image $image * @throws FileNotFoundException
* @return string
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
*/ */
public function getImageData(Image $image) public function getImageData(Image $image): string
{ {
$imagePath = $image->path; $imagePath = $image->path;
$storage = $this->getStorage(); $storage = $this->getStorage();
@ -294,7 +280,6 @@ class ImageService extends UploadService
/** /**
* Destroy an image along with its revisions, thumbnails and remaining folders. * Destroy an image along with its revisions, thumbnails and remaining folders.
* @param Image $image
* @throws Exception * @throws Exception
*/ */
public function destroy(Image $image) public function destroy(Image $image)
@ -324,7 +309,7 @@ class ImageService extends UploadService
// Cleanup of empty folders // Cleanup of empty folders
$foldersInvolved = array_merge([$imageFolder], $storage->directories($imageFolder)); $foldersInvolved = array_merge([$imageFolder], $storage->directories($imageFolder));
foreach ($foldersInvolved as $directory) { foreach ($foldersInvolved as $directory) {
if ($this->isFolderEmpty($directory)) { if ($this->isFolderEmpty($storage, $directory)) {
$storage->deleteDirectory($directory); $storage->deleteDirectory($directory);
} }
} }
@ -332,14 +317,21 @@ class ImageService extends UploadService
return true; return true;
} }
/**
* Check whether or not a folder is empty.
*/
protected function isFolderEmpty(FileSystemInstance $storage, string $path): bool
{
$files = $storage->files($path);
$folders = $storage->directories($path);
return (count($files) === 0 && count($folders) === 0);
}
/** /**
* Save an avatar image from an external service. * Save an avatar image from an external service.
* @param \BookStack\Auth\User $user
* @param int $size
* @return Image
* @throws Exception * @throws Exception
*/ */
public function saveUserAvatar(User $user, $size = 500) public function saveUserAvatar(User $user, int $size = 500): Image
{ {
$avatarUrl = $this->getAvatarUrl(); $avatarUrl = $this->getAvatarUrl();
$email = strtolower(trim($user->email)); $email = strtolower(trim($user->email));
@ -363,9 +355,8 @@ class ImageService extends UploadService
/** /**
* Check if fetching external avatars is enabled. * Check if fetching external avatars is enabled.
* @return bool
*/ */
public function avatarFetchEnabled() public function avatarFetchEnabled(): bool
{ {
$fetchUrl = $this->getAvatarUrl(); $fetchUrl = $this->getAvatarUrl();
return is_string($fetchUrl) && strpos($fetchUrl, 'http') === 0; return is_string($fetchUrl) && strpos($fetchUrl, 'http') === 0;
@ -407,11 +398,11 @@ class ImageService extends UploadService
foreach ($images as $image) { foreach ($images as $image) {
$searchQuery = '%' . basename($image->path) . '%'; $searchQuery = '%' . basename($image->path) . '%';
$inPage = DB::table('pages') $inPage = DB::table('pages')
->where('html', 'like', $searchQuery)->count() > 0; ->where('html', 'like', $searchQuery)->count() > 0;
$inRevision = false; $inRevision = false;
if ($checkRevisions) { if ($checkRevisions) {
$inRevision = DB::table('page_revisions') $inRevision = DB::table('page_revisions')
->where('html', 'like', $searchQuery)->count() > 0; ->where('html', 'like', $searchQuery)->count() > 0;
} }
if (!$inPage && !$inRevision) { if (!$inPage && !$inRevision) {
@ -428,11 +419,9 @@ class ImageService extends UploadService
/** /**
* Convert a image URI to a Base64 encoded string. * Convert a image URI to a Base64 encoded string.
* Attempts to find locally via set storage method first. * Attempts to find locally via set storage method first.
* @param string $uri * @throws FileNotFoundException
* @return null|string
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
*/ */
public function imageUriToBase64(string $uri) public function imageUriToBase64(string $uri): ?string
{ {
$isLocal = strpos(trim($uri), 'http') !== 0; $isLocal = strpos(trim($uri), 'http') !== 0;
@ -454,7 +443,7 @@ class ImageService extends UploadService
} else { } else {
try { try {
$imageData = $this->http->fetch($uri); $imageData = $this->http->fetch($uri);
} catch (\Exception $e) { } catch (Exception $e) {
} }
} }
@ -472,10 +461,9 @@ class ImageService extends UploadService
/** /**
* Gets a public facing url for an image by checking relevant environment variables. * Gets a public facing url for an image by checking relevant environment variables.
* @param string $filePath * If s3-style store is in use it will default to guessing a public bucket URL.
* @return string
*/ */
private function getPublicUrl($filePath) private function getPublicUrl(string $filePath): string
{ {
if ($this->storageUrl === null) { if ($this->storageUrl === null) {
$storageUrl = config('filesystems.url'); $storageUrl = config('filesystems.url');

View file

@ -1,45 +0,0 @@
<?php namespace BookStack\Uploads;
use Illuminate\Contracts\Filesystem\Factory as FileSystem;
use Illuminate\Contracts\Filesystem\Filesystem as FileSystemInstance;
abstract class UploadService
{
/**
* @var FileSystem
*/
protected $fileSystem;
/**
* FileService constructor.
* @param $fileSystem
*/
public function __construct(FileSystem $fileSystem)
{
$this->fileSystem = $fileSystem;
}
/**
* Get the storage that will be used for storing images.
* @return FileSystemInstance
*/
protected function getStorage()
{
$storageType = config('filesystems.default');
return $this->fileSystem->disk($storageType);
}
/**
* Check whether or not a folder is empty.
* @param $path
* @return bool
*/
protected function isFolderEmpty($path)
{
$files = $this->getStorage()->files($path);
$folders = $this->getStorage()->directories($path);
return (count($files) === 0 && count($folders) === 0);
}
}