abstract class Entity extends Model
{
- /**
- * Relation for the user that created this entity.
- * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
- */
- public function createdBy()
- {
- return $this->belongsTo('BookStack\User', 'created_by');
- }
-
- /**
- * Relation for the user that updated this entity.
- * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
- */
- public function updatedBy()
- {
- return $this->belongsTo('BookStack\User', 'updated_by');
- }
+ use Ownable;
/**
* Compares this entity to another given entity.
namespace BookStack;
-class Image extends Entity
+use Images;
+
+class Image
{
+ use Ownable;
protected $fillable = ['name'];
- public function getFilePath()
- {
- return storage_path() . $this->url;
- }
-
/**
- * Get the url for this item.
- * @return string
+ * Get a thumbnail for this image.
+ * @param int $width
+ * @param int $height
+ * @param bool|false $hardCrop
*/
- public function getUrl()
+ public function getThumb($width, $height, $hardCrop = false)
{
- return public_path() . $this->url;
+ Images::getThumbnail($this, $width, $height, $hardCrop);
}
}
--- /dev/null
+<?php namespace BookStack;
+
+
+trait Ownable
+{
+ /**
+ * Relation for the user that created this entity.
+ * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
+ */
+ public function createdBy()
+ {
+ return $this->belongsTo('BookStack\User', 'created_by');
+ }
+
+ /**
+ * Relation for the user that updated this entity.
+ * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
+ */
+ public function updatedBy()
+ {
+ return $this->belongsTo('BookStack\User', 'updated_by');
+ }
+}
\ No newline at end of file
namespace BookStack\Providers;
+use BookStack\Services\ImageService;
use BookStack\Services\ViewService;
use Illuminate\Support\ServiceProvider;
use BookStack\Services\ActivityService;
$this->app->make('Illuminate\Contracts\Cache\Repository')
);
});
+ $this->app->bind('images', function() {
+ return new ImageService(
+ $this->app->make('Intervention\Image\ImageManager'),
+ $this->app->make('Illuminate\Contracts\Filesystem\Factory'),
+ $this->app->make('Illuminate\Contracts\Cache\Repository')
+ );
+ });
}
}
use BookStack\Image;
-use Illuminate\Contracts\Filesystem\Filesystem as FileSystemInstance;
-use Intervention\Image\ImageManager as ImageTool;
-use Illuminate\Contracts\Filesystem\Factory as FileSystem;
-use Illuminate\Contracts\Cache\Repository as Cache;
+use BookStack\Services\ImageService;
use Setting;
use Symfony\Component\HttpFoundation\File\UploadedFile;
{
protected $image;
- protected $imageTool;
- protected $fileSystem;
- protected $cache;
-
- /**
- * @var FileSystemInstance
- */
- protected $storageInstance;
- protected $storageUrl;
-
+ protected $imageService;
/**
* ImageRepo constructor.
- * @param Image $image
- * @param ImageTool $imageTool
- * @param FileSystem $fileSystem
- * @param Cache $cache
+ * @param Image $image
+ * @param ImageService $imageService
*/
- public function __construct(Image $image, ImageTool $imageTool, FileSystem $fileSystem, Cache $cache)
+ public function __construct(Image $image,ImageService $imageService)
{
$this->image = $image;
- $this->imageTool = $imageTool;
- $this->fileSystem = $fileSystem;
- $this->cache = $cache;
+ $this->imageService = $imageService;
}
*/
public function saveNew(UploadedFile $uploadFile, $type)
{
- $storage = $this->getStorage();
- $secureUploads = Setting::get('app-secure-images');
- $imageName = str_replace(' ', '-', $uploadFile->getClientOriginalName());
-
- if ($secureUploads) $imageName = str_random(16) . '-' . $imageName;
-
- $imagePath = '/uploads/images/' . $type . '/' . Date('Y-m-M') . '/';
- while ($storage->exists($imagePath . $imageName)) {
- $imageName = str_random(3) . $imageName;
- }
- $fullPath = $imagePath . $imageName;
-
- $storage->put($fullPath, file_get_contents($uploadFile->getRealPath()));
-
- $userId = auth()->user()->id;
- $image = $this->image->forceCreate([
- 'name' => $imageName,
- 'path' => $fullPath,
- 'url' => $this->getPublicUrl($fullPath),
- 'type' => $type,
- 'created_by' => $userId,
- 'updated_by' => $userId
- ]);
-
+ $image = $this->imageService->saveNew($this->image, $uploadFile, $type);
$this->loadThumbs($image);
return $image;
}
*/
public function destroyImage(Image $image)
{
- $storage = $this->getStorage();
-
- $imageFolder = dirname($image->path);
- $imageFileName = basename($image->path);
- $allImages = collect($storage->allFiles($imageFolder));
-
- $imagesToDelete = $allImages->filter(function ($imagePath) use ($imageFileName) {
- $expectedIndex = strlen($imagePath) - strlen($imageFileName);
- return strpos($imagePath, $imageFileName) === $expectedIndex;
- });
-
- $storage->delete($imagesToDelete->all());
-
- // Cleanup of empty folders
- foreach ($storage->directories($imageFolder) as $directory) {
- if ($this->isFolderEmpty($directory)) $storage->deleteDirectory($directory);
- }
- if ($this->isFolderEmpty($imageFolder)) $storage->deleteDirectory($imageFolder);
-
- $image->delete();
+ $this->imageService->destroyImage($image);
return true;
}
- /**
- * Check whether or not a folder is empty.
- * @param $path
- * @return int
- */
- private function isFolderEmpty($path)
- {
- $files = $this->getStorage()->files($path);
- $folders = $this->getStorage()->directories($path);
- return count($files) === 0 && count($folders) === 0;
- }
/**
* Load thumbnails onto an image object.
*/
public function getThumbnail(Image $image, $width = 220, $height = 220, $keepRatio = false)
{
- $thumbDirName = '/' . ($keepRatio ? 'scaled-' : 'thumbs-') . $width . '-' . $height . '/';
- $thumbFilePath = dirname($image->path) . $thumbDirName . basename($image->path);
-
- if ($this->cache->has('images-' . $image->id . '-' . $thumbFilePath) && $this->cache->get('images-' . $thumbFilePath)) {
- return $this->getPublicUrl($thumbFilePath);
- }
-
- $storage = $this->getStorage();
-
- if ($storage->exists($thumbFilePath)) {
- return $this->getPublicUrl($thumbFilePath);
- }
-
- // Otherwise create the thumbnail
- $thumb = $this->imageTool->make($storage->get($image->path));
- if ($keepRatio) {
- $thumb->resize($width, null, function ($constraint) {
- $constraint->aspectRatio();
- $constraint->upsize();
- });
- } else {
- $thumb->fit($width, $height);
- }
-
- $thumbData = (string)$thumb->encode();
- $storage->put($thumbFilePath, $thumbData);
- $this->cache->put('images-' . $image->id . '-' . $thumbFilePath, $thumbFilePath, 60 * 72);
-
- return $this->getPublicUrl($thumbFilePath);
- }
-
- /**
- * Gets a public facing url for an image by checking relevant environment variables.
- * @param $filePath
- * @return string
- */
- private function getPublicUrl($filePath)
- {
- if ($this->storageUrl === null) {
- $storageUrl = env('STORAGE_URL');
-
- // Get the standard public s3 url if s3 is set as storage type
- if ($storageUrl == false && env('STORAGE_TYPE') === 's3') {
- $storageDetails = config('filesystems.disks.s3');
- $storageUrl = 'https://s3-' . $storageDetails['region'] . '.amazonaws.com/' . $storageDetails['bucket'];
- }
-
- $this->storageUrl = $storageUrl;
- }
-
- return ($this->storageUrl == false ? '' : rtrim($this->storageUrl, '/')) . $filePath;
- }
-
-
- /**
- * Get the storage that will be used for storing images.
- * @return FileSystemInstance
- */
- private function getStorage()
- {
- if ($this->storageInstance !== null) return $this->storageInstance;
-
- $storageType = env('STORAGE_TYPE');
- $this->storageInstance = $this->fileSystem->disk($storageType);
-
- return $this->storageInstance;
+ return $this->imageService->getThumbnail($image, $width, $height, $keepRatio);
}
--- /dev/null
+<?php namespace BookStack\Services\Facades;
+
+
+use Illuminate\Support\Facades\Facade;
+
+class Images extends Facade
+{
+ /**
+ * Get the registered name of the component.
+ *
+ * @return string
+ */
+ protected static function getFacadeAccessor() { return 'images'; }
+}
\ No newline at end of file
--- /dev/null
+<?php namespace BookStack\Services;
+
+use BookStack\Image;
+use Intervention\Image\ImageManager;
+use Illuminate\Contracts\Filesystem\Factory as FileSystem;
+use Illuminate\Contracts\Filesystem\Filesystem as FileSystemInstance;
+use Illuminate\Contracts\Cache\Repository as Cache;
+use Setting;
+use Symfony\Component\HttpFoundation\File\UploadedFile;
+
+class ImageService
+{
+
+ protected $imageTool;
+ protected $fileSystem;
+ protected $cache;
+
+ /**
+ * @var FileSystemInstance
+ */
+ protected $storageInstance;
+ protected $storageUrl;
+
+ /**
+ * ImageService constructor.
+ * @param $imageTool
+ * @param $fileSystem
+ * @param $cache
+ */
+ public function __construct(ImageManager $imageTool, FileSystem $fileSystem, Cache $cache)
+ {
+ $this->imageTool = $imageTool;
+ $this->fileSystem = $fileSystem;
+ $this->cache = $cache;
+ }
+
+ public function saveNew(Image $image, UploadedFile $uploadedFile, $type)
+ {
+ $storage = $this->getStorage();
+ $secureUploads = Setting::get('app-secure-images');
+ $imageName = str_replace(' ', '-', $uploadedFile->getClientOriginalName());
+
+ if ($secureUploads) $imageName = str_random(16) . '-' . $imageName;
+
+ $imagePath = '/uploads/images/' . $type . '/' . Date('Y-m-M') . '/';
+ while ($storage->exists($imagePath . $imageName)) {
+ $imageName = str_random(3) . $imageName;
+ }
+ $fullPath = $imagePath . $imageName;
+
+ $storage->put($fullPath, file_get_contents($uploadedFile->getRealPath()));
+
+ $userId = auth()->user()->id;
+ $image = $image->forceCreate([
+ 'name' => $imageName,
+ 'path' => $fullPath,
+ 'url' => $this->getPublicUrl($fullPath),
+ 'type' => $type,
+ 'created_by' => $userId,
+ 'updated_by' => $userId
+ ]);
+
+ return $image;
+ }
+
+ /**
+ * Get the thumbnail for an image.
+ * If $keepRatio is true only the width will be used.
+ * 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
+ * @return string
+ */
+ public function getThumbnail(Image $image, $width = 220, $height = 220, $keepRatio = false)
+ {
+ $thumbDirName = '/' . ($keepRatio ? 'scaled-' : 'thumbs-') . $width . '-' . $height . '/';
+ $thumbFilePath = dirname($image->path) . $thumbDirName . basename($image->path);
+
+ if ($this->cache->has('images-' . $image->id . '-' . $thumbFilePath) && $this->cache->get('images-' . $thumbFilePath)) {
+ return $this->getPublicUrl($thumbFilePath);
+ }
+
+ $storage = $this->getStorage();
+
+ if ($storage->exists($thumbFilePath)) {
+ return $this->getPublicUrl($thumbFilePath);
+ }
+
+ // Otherwise create the thumbnail
+ $thumb = $this->imageTool->make($storage->get($image->path));
+ if ($keepRatio) {
+ $thumb->resize($width, null, function ($constraint) {
+ $constraint->aspectRatio();
+ $constraint->upsize();
+ });
+ } else {
+ $thumb->fit($width, $height);
+ }
+
+ $thumbData = (string)$thumb->encode();
+ $storage->put($thumbFilePath, $thumbData);
+ $this->cache->put('images-' . $image->id . '-' . $thumbFilePath, $thumbFilePath, 60 * 72);
+
+ return $this->getPublicUrl($thumbFilePath);
+ }
+
+ /**
+ * Destroys an Image object along with its files and thumbnails.
+ * @param Image $image
+ * @return bool
+ */
+ public function destroyImage(Image $image)
+ {
+ $storage = $this->getStorage();
+
+ $imageFolder = dirname($image->path);
+ $imageFileName = basename($image->path);
+ $allImages = collect($storage->allFiles($imageFolder));
+
+ $imagesToDelete = $allImages->filter(function ($imagePath) use ($imageFileName) {
+ $expectedIndex = strlen($imagePath) - strlen($imageFileName);
+ return strpos($imagePath, $imageFileName) === $expectedIndex;
+ });
+
+ $storage->delete($imagesToDelete->all());
+
+ // Cleanup of empty folders
+ foreach ($storage->directories($imageFolder) as $directory) {
+ if ($this->isFolderEmpty($directory)) $storage->deleteDirectory($directory);
+ }
+ if ($this->isFolderEmpty($imageFolder)) $storage->deleteDirectory($imageFolder);
+
+ $image->delete();
+ return true;
+ }
+
+ /**
+ * Get the storage that will be used for storing images.
+ * @return FileSystemInstance
+ */
+ private function getStorage()
+ {
+ if ($this->storageInstance !== null) return $this->storageInstance;
+
+ $storageType = env('STORAGE_TYPE');
+ $this->storageInstance = $this->fileSystem->disk($storageType);
+
+ return $this->storageInstance;
+ }
+
+ /**
+ * Check whether or not a folder is empty.
+ * @param $path
+ * @return int
+ */
+ private function isFolderEmpty($path)
+ {
+ $files = $this->getStorage()->files($path);
+ $folders = $this->getStorage()->directories($path);
+ return count($files) === 0 && count($folders) === 0;
+ }
+
+ /**
+ * Gets a public facing url for an image by checking relevant environment variables.
+ * @param $filePath
+ * @return string
+ */
+ private function getPublicUrl($filePath)
+ {
+ if ($this->storageUrl === null) {
+ $storageUrl = env('STORAGE_URL');
+
+ // Get the standard public s3 url if s3 is set as storage type
+ if ($storageUrl == false && env('STORAGE_TYPE') === 's3') {
+ $storageDetails = config('filesystems.disks.s3');
+ $storageUrl = 'https://s3-' . $storageDetails['region'] . '.amazonaws.com/' . $storageDetails['bucket'];
+ }
+
+ $this->storageUrl = $storageUrl;
+ }
+
+ return ($this->storageUrl == false ? '' : rtrim($this->storageUrl, '/')) . $filePath;
+ }
+
+
+}
\ No newline at end of file
|
*/
- 'debug' => env('APP_DEBUG', false),
+ 'debug' => env('APP_DEBUG', false),
/*
|--------------------------------------------------------------------------
|
*/
- 'url' => env('APP_URL', 'http://localhost'),
+ 'url' => env('APP_URL', 'http://localhost'),
/*
|--------------------------------------------------------------------------
|
*/
- 'timezone' => 'UTC',
+ 'timezone' => 'UTC',
/*
|--------------------------------------------------------------------------
|
*/
- 'locale' => 'en',
+ 'locale' => 'en',
/*
|--------------------------------------------------------------------------
|
*/
- 'key' => env('APP_KEY', 'AbAZchsay4uBTU33RubBzLKw203yqSqr'),
+ 'key' => env('APP_KEY', 'AbAZchsay4uBTU33RubBzLKw203yqSqr'),
- 'cipher' => 'AES-256-CBC',
+ 'cipher' => 'AES-256-CBC',
/*
|--------------------------------------------------------------------------
|
*/
- 'log' => 'single',
+ 'log' => 'single',
/*
|--------------------------------------------------------------------------
|
*/
- 'providers' => [
+ 'providers' => [
/*
* Laravel Framework Service Providers...
|
*/
- 'aliases' => [
+ 'aliases' => [
'App' => Illuminate\Support\Facades\App::class,
'Artisan' => Illuminate\Support\Facades\Artisan::class,
*/
'ImageTool' => Intervention\Image\Facades\Image::class,
- 'Debugbar' => Barryvdh\Debugbar\Facade::class,
+ 'Debugbar' => Barryvdh\Debugbar\Facade::class,
/**
* Custom
*/
- 'Activity' => BookStack\Services\Facades\Activity::class,
- 'Setting' => BookStack\Services\Facades\Setting::class,
- 'Views' => BookStack\Services\Facades\Views::class,
+ 'Activity' => BookStack\Services\Facades\Activity::class,
+ 'Setting' => BookStack\Services\Facades\Setting::class,
+ 'Views' => BookStack\Services\Facades\Views::class,
+ 'Images' => \BookStack\Services\Facades\Images::class,
],