Dec 22, 2015
0
Level 6
Image uploads with queues
I am delving into queues, and want to streamline my applications image uploader. I want to be able process the image and upload it without the user having to wait, however, the way I have done it does not seem right!
In my controller I am storing the image locally and sending the path ect over to the job.
public function updateAvatar(Request $request) {
$validator = Validator::make($request->all(), [
'file' => 'required|mimes:png,jpg,jpeg',
]);
if($validator->fails()) {
return 'fail';
}
$userId = Auth::user()->username;
$image = $request->file('file');
$newName = md5(uniqid());
$ext = $image->getClientOriginalExtension();
$name = $newName . '.' . $ext;
$image->move(storage_path() . '/app', $name);
$path = storage_path() . '/app/' . $name;
$this->dispatch(new updateAvatar($name, $path, $userId));
}
I then process the job with the data passed into the constructor.
use InteractsWithQueue, SerializesModels;
protected $name,
$path,
$userid;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct($name, $path, $userid)
{
$this->name = $name;
$this->path = $path;
$this->userid = $userid;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
$storedFile = Storage::get($this->name);
Storage::disk('s3')->put('users/' . $this->userid . '/' . $this->name, $storedFile);
}
Am I doing this correctly? Or is there a more efficient and cleaner way of doing this?
Please or to participate in this conversation.