Commit 8636c80f authored by Nicolas Widart's avatar Nicolas Widart

Using a queue for thumbnail creation

parent 81679e92
<?php namespace Modules\Media\Services; <?php namespace Modules\Media\Services;
use Modules\Media\Image\Imagy; use Illuminate\Contracts\Config\Repository;
use Illuminate\Contracts\Queue\Queue;
use Modules\Media\Image\Facade\Imagy;
use Modules\Media\Repositories\FileRepository; use Modules\Media\Repositories\FileRepository;
use Symfony\Component\HttpFoundation\File\UploadedFile; use Symfony\Component\HttpFoundation\File\UploadedFile;
...@@ -11,28 +13,49 @@ class FileService ...@@ -11,28 +13,49 @@ class FileService
*/ */
private $file; private $file;
/** /**
* @var Imagy * @var Repository
*/ */
private $imagy; private $config;
/**
* @var Queue
*/
private $queue;
public function __construct(FileRepository $file, Imagy $imagy) public function __construct(FileRepository $file, Repository $config, Queue $queue)
{ {
$this->file = $file; $this->file = $file;
$this->imagy = $imagy; $this->config = $config;
$this->queue = $queue;
} }
/**
* @param UploadedFile $file
* @return mixed
*/
public function store(UploadedFile $file) public function store(UploadedFile $file)
{ {
// Save the file info to db // Save the file info to db
$savedFile = $this->file->createFromFile($file); $savedFile = $this->file->createFromFile($file);
// Move the uploaded file to /public/assets/media/ // Move the uploaded file to /public/assets/media/
$file->move(public_path() . '/assets/media', $savedFile->filename); $file->move(public_path() . $this->config->get('media::config.files-path'), $savedFile->filename);
// Create the thumbnails $this->createThumbnails($savedFile);
$this->imagy->createAll($savedFile->path);
return $savedFile; return $savedFile;
} }
/**
* Create the necessary thumbnails for the given file
* @param $savedFile
*/
private function createThumbnails($savedFile)
{
$this->queue->push(function($job) use ($savedFile)
{
Imagy::createAll($savedFile->path);
$job->delete();
});
}
} }
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