samedi 9 mars 2019

Unresolvable dependency when handling image uploads with laravel jobs

Each uploaded image creates four more images for different screens and then are transferred to a space on Digital Ocean. So it's very time consuming. Therefor I want to use Laravels Job feature. This is what it looks like by now:

ImageController:

public function uploadMultiple($vehicle_id, Request $request)
{
    $this->setVehicle($vehicle_id);
    $this->validate($request, [
        'images'    => 'required',
        'images.*'  => 'image'
    ]);
    foreach ($request->images as $image){
        $this->uploadSingleImage($image);
    }
    \Alert::success('Bilder wurden hochgeladen')->flash();

    return redirect()->back();
}

protected function uploadSingleImage(UploadedFile $image)
{
    VehicleImageUploaderJob::dispatch($this->getVehicle());
}

VehicleImageUploaderJob:

public function handle(UploadedFile $image)
{
    $disk = FilesystemHelper::getDisk('producerData');
    $originalName = basename($image->getClientOriginalName(), '.'.$image->getClientOriginalExtension());
    $extension = $image->getClientOriginalExtension();

    $dbImage = new Image();

    // Store image in original size
    $path = $image->storeAs(
        'producers/' . $this->getVehicle()->producer->id . '/vehicles/' . $this->getVehicle()->id, // Folder or Directory
        $image->getClientOriginalName(), // File Name
        $disk // Disk
    );
    Storage::disk($disk)->setVisibility($path, 'public');

    $dbImage->original = $path;
    $dbImage->disk = $disk;

    $file = ImageManipulator::make($image);

    foreach(config('images.vehicles.formats') as $format => $values){
        $file->backup();
        $fileName = $originalName . '-' . str_random(6) . '-' . $values['width'] . 'x' . $values['height'] . '.' . $extension;
        if($format === 'thumbnail'){
            $file->fit($values['width'],$values['height'])
                ->save();
        } else {
            $file->fit($values['width'], $values['height'], function ($constraint) {
                $constraint->aspectRatio();
                $constraint->upsize();
            })->save();
        }
        $path = $image->storeAs(
            'producers/' . $this->getVehicle()->producer->id . '/vehicles/' . $this->getVehicle()->id, // Folder or Directory
            $fileName, // File Name
            $disk // Disk
        );

        Storage::disk($disk)->setVisibility($path, 'public');
        $dbImage->{$format} = $path;
        $file->reset();
    }

    $dbImage->save();
    $this->getVehicle()->images()->attach($dbImage->id);
    $this->getVehicle()->save();
}

Unfortunatelly I get an error each time I want to upload an image (currently the queue driver is "sync"): Unresolvable dependency resolving [Parameter #0 [ string $path ]] in class Symfony\Component\HttpFoundation\File\UploadedFile

Maybe its because I should pass in the image and not the vehicle. The vehicle is expected in the constructor and the image in the handle method. But when I pass in the image, it throws an error saying that it expects an instance of Vehicle.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire