Commit beea73de authored by Nicolas Widart's avatar Nicolas Widart

Move the delete files to the correct class

parent 538347f1
...@@ -7,6 +7,7 @@ use Laracasts\Flash\Flash; ...@@ -7,6 +7,7 @@ use Laracasts\Flash\Flash;
use Modules\Core\Http\Controllers\Admin\AdminBaseController; use Modules\Core\Http\Controllers\Admin\AdminBaseController;
use Modules\Media\Entities\File; use Modules\Media\Entities\File;
use Modules\Media\Http\Requests\UpdateMediaRequest; use Modules\Media\Http\Requests\UpdateMediaRequest;
use Modules\Media\Image\Imagy;
use Modules\Media\Repositories\FileRepository; use Modules\Media\Repositories\FileRepository;
class MediaController extends AdminBaseController class MediaController extends AdminBaseController
...@@ -19,11 +20,16 @@ class MediaController extends AdminBaseController ...@@ -19,11 +20,16 @@ class MediaController extends AdminBaseController
* @var Repository * @var Repository
*/ */
private $config; private $config;
/**
* @var Imagy
*/
private $imagy;
public function __construct(FileRepository $file, Repository $config) public function __construct(FileRepository $file, Repository $config, Imagy $imagy)
{ {
$this->file = $file; $this->file = $file;
$this->config = $config; $this->config = $config;
$this->imagy = $imagy;
} }
/** /**
...@@ -102,6 +108,7 @@ class MediaController extends AdminBaseController ...@@ -102,6 +108,7 @@ class MediaController extends AdminBaseController
*/ */
public function destroy(File $file) public function destroy(File $file)
{ {
$this->imagy->deleteAllFor($file);
$this->file->destroy($file); $this->file->destroy($file);
Flash::success('File deleted'); Flash::success('File deleted');
......
...@@ -26,7 +26,7 @@ class Imagy ...@@ -26,7 +26,7 @@ class Imagy
* All the different images types where thumbnails should be created * All the different images types where thumbnails should be created
* @var array * @var array
*/ */
private $imageExtensions = ['jpg','png','jpeg','gif']; private $imageExtensions = ['jpg', 'png', 'jpeg', 'gif'];
/** /**
* @var Repository * @var Repository
*/ */
...@@ -55,7 +55,9 @@ class Imagy ...@@ -55,7 +55,9 @@ class Imagy
*/ */
public function get($path, $thumbnail, $forceCreate = false) public function get($path, $thumbnail, $forceCreate = false)
{ {
if (!$this->isImage($path)) return; if (!$this->isImage($path)) {
return;
}
$filename = $this->config->get('media::config.files-path') . $this->newFilename($path, $thumbnail); $filename = $this->config->get('media::config.files-path') . $this->newFilename($path, $thumbnail);
...@@ -76,7 +78,9 @@ class Imagy ...@@ -76,7 +78,9 @@ class Imagy
*/ */
public function getThumbnail($originalImage, $thumbnail) public function getThumbnail($originalImage, $thumbnail)
{ {
if (!$this->isImage($originalImage)) return $originalImage; if (!$this->isImage($originalImage)) {
return $originalImage;
}
return $this->config->get('media::config.files-path') . $this->newFilename($originalImage, $thumbnail); return $this->config->get('media::config.files-path') . $this->newFilename($originalImage, $thumbnail);
} }
...@@ -87,7 +91,9 @@ class Imagy ...@@ -87,7 +91,9 @@ class Imagy
*/ */
public function createAll($path) public function createAll($path)
{ {
if (!$this->isImage($path)) return; if (!$this->isImage($path)) {
return;
}
foreach ($this->manager->all() as $thumbName => $filters) { foreach ($this->manager->all() as $thumbName => $filters) {
$image = $this->image->make(public_path() . $path); $image = $this->image->make(public_path() . $path);
...@@ -157,9 +163,32 @@ class Imagy ...@@ -157,9 +163,32 @@ class Imagy
* @param string $path * @param string $path
* @return bool * @return bool
*/ */
private function isImage($path) public function isImage($path)
{ {
return in_array(pathinfo($path, PATHINFO_EXTENSION), $this->imageExtensions); return in_array(pathinfo($path, PATHINFO_EXTENSION), $this->imageExtensions);
} }
/**
* Delete all files on disk for the given file in storage
* This means the original and the thumbnails
* @param $file
* @return bool
*/
public function deleteAllFor($file)
{
if (!$this->isImage($file->path)) {
return $this->finder->delete($file->path);
}
$paths[] = public_path() . $file->path;
$fileName = pathinfo($file->path, PATHINFO_FILENAME);
$extension = pathinfo($file->path, PATHINFO_EXTENSION);
foreach ($this->manager->all() as $thumbnail => $filters) {
$paths[] = public_path() . $this->config->get(
'media::config.files-path'
) . "{$fileName}_{$thumbnail}.{$extension}";
}
return $this->finder->delete($paths);
}
} }
...@@ -57,8 +57,6 @@ class EloquentFileRepository extends EloquentBaseRepository implements FileRepos ...@@ -57,8 +57,6 @@ class EloquentFileRepository extends EloquentBaseRepository implements FileRepos
public function destroy($file) public function destroy($file)
{ {
$this->finder->delete('public/' . $file->path);
$file->delete(); $file->delete();
} }
} }
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
use Illuminate\Contracts\Config\Repository; use Illuminate\Contracts\Config\Repository;
use Illuminate\Contracts\Queue\Queue; use Illuminate\Contracts\Queue\Queue;
use Illuminate\Support\Facades\App; use Illuminate\Support\Facades\App;
use Modules\Media\Image\Imagy;
use Modules\Media\Repositories\FileRepository; use Modules\Media\Repositories\FileRepository;
use Symfony\Component\HttpFoundation\File\UploadedFile; use Symfony\Component\HttpFoundation\File\UploadedFile;
...@@ -20,12 +21,21 @@ class FileService ...@@ -20,12 +21,21 @@ class FileService
* @var Queue * @var Queue
*/ */
private $queue; private $queue;
/**
* @var Imagy
*/
private $imagy;
public function __construct(FileRepository $file, Repository $config, Queue $queue) public function __construct(
FileRepository $file,
Repository $config,
Queue $queue,
Imagy $imagy)
{ {
$this->file = $file; $this->file = $file;
$this->config = $config; $this->config = $config;
$this->queue = $queue; $this->queue = $queue;
$this->imagy = $imagy;
} }
/** /**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment