spatie / laravel-medialibrary

Associate files with Eloquent models
https://spatie.be/docs/laravel-medialibrary
MIT License
5.74k stars 1.07k forks source link

Upgrading from 4.8.4 to 4.9.1, Call to undefined method Spatie\MediaLibrary\UrlGenerator\S3UrlGenerator::getPath() #359

Closed marsderp closed 7 years ago

marsderp commented 8 years ago

After upgrading this package, the error now occurs when I upload from file to S3. None of my upload code was modified between the upgrade.

Current solution is to specify in composer "spatie/laravel-medialibrary": "^4.8.4"

Here is a snippet of what my code is doing:

$filename = 'random-generated-filename';
$uploaded = $request->file('media');
$return = $owner->addMedia($uploaded);
$extension = $uploaded->getClientOriginalExtension();
$filename = $filename . '.' . $extension;

$return = $return->preservingOriginal()
    ->usingName($filename)
    ->usingFileName($filename)
    ->toCollection('uploaded');
Symfony\Component\Debug\Exception\FatalThrowableError: Call to undefined method Spatie\MediaLibrary\UrlGenerator\S3UrlGenerator::getPath()
1 File "/var/www/myapp/vendor/spatie/laravel-medialibrary/src/Media.php" line 86 in getPath
2 File "/var/www/myapp/vendor/spatie/laravel-medialibrary/src/FileManipulator.php" line 138 in determineImageGenerator
3 File "/var/www/myapp/vendor/spatie/laravel-medialibrary/src/FileManipulator.php" line 44 in performConversions
4 File "/var/www/myapp/vendor/spatie/laravel-medialibrary/src/FileManipulator.php" line 25 in createDerivedFiles
5 File "/var/www/myapp/vendor/spatie/laravel-medialibrary/src/Filesystem.php" line 42 in add
6 File "/var/www/myapp/vendor/spatie/laravel-medialibrary/src/FileAdder/FileAdder.php" line 328 in toCollectionOnDisk
7 File "/var/www/myapp/vendor/spatie/laravel-medialibrary/src/FileAdder/FileAdder.php" line 285 in toCollection
freekmurze commented 8 years ago

Hi, my apologies for this breaking change. We try to avoid this kind of trouble.

I'll try to look at this very soon.

pinging @nicolasbeauvais for any ideas how this might have been caused.

freekmurze commented 8 years ago

@marsd in the config file laravel-medialibrary.php what did you specify in the custom_url_generator_class key?

marsderp commented 8 years ago

Currently null, was not changed previously.

    'custom_url_generator_class' => null,
    'custom_path_generator_class' => null,
khairy-pelcro commented 8 years ago

I have the same issue after some debugging i have found the problem in FileManipulator.php line 138 ,it is trying to getPath and s3UrlGenerator doesn't have getPath method

khairy-pelcro commented 8 years ago

@marsd you do not have custom URL generators but you are using s3 and it`s generator doesn't have a path

freekmurze commented 7 years ago

A fix has been made by @nicolasbeauvais in the master branch. Could you require dev-master of this package in your composer.json and let us know if your issue is resolved?

sudomabider commented 7 years ago

Experiencing the same issue here. Can confirm it is resolved in dev-master.

asartz commented 7 years ago

Had the same issue. It was fixed by pulling the dev-master branch.

freekmurze commented 7 years ago

Fix has been tagged as v4.9.2.

Reopen this issue if you're still having problems after upgrading to that version.

andaril commented 7 years ago

I'm having same problem with v4.9.2 :(

[2016-10-12 14:52:06] production.CRITICAL: Symfony\Component\Debug\Exception\FatalThrowableError: Call to undefined method Spatie\MediaLibrary\UrlGenerator\S3UrlGenerator::getPath() in /vendor/spatie/laravel-medialibrary/src/Media.php:86
Stack trace:
#0 /vendor/spatie/laravel-medialibrary/src/ImageGenerators/BaseGenerator.php(21): Spatie\MediaLibrary\Media->getPath()
#1 /vendor/spatie/laravel-medialibrary/src/FileManipulator.php(138): Spatie\MediaLibrary\ImageGenerators\BaseGenerator->canConvert(Object(Spatie\MediaLibrary\Media))
#2 /vendor/spatie/laravel-medialibrary/src/FileManipulator.php(44): Spatie\MediaLibrary\FileManipulator->determineImageGenerator(Object(Spatie\MediaLibrary\Media))
#3 /vendor/spatie/laravel-medialibrary/src/FileManipulator.php(25): Spatie\MediaLibrary\FileManipulator->performConversions(Object(Spatie\MediaLibrary\Conversion\ConversionCollection), Object(Spatie\MediaLibrary\Media))
#4 /vendor/spatie/laravel-medialibrary/src/Filesystem.php(42): Spatie\MediaLibrary\FileManipulator->createDerivedFiles(Object(Spatie\MediaLibrary\Media))
#5 /vendor/spatie/laravel-medialibrary/src/FileAdder/FileAdder.php(328): Spatie\MediaLibrary\Filesystem->add('/tmp/media-libr...', Object(Spatie\MediaLibrary\Media), '163930_new_Y4Zi...')
#6 /vendor/spatie/laravel-medialibrary/src/FileAdder/FileAdder.php(251): Spatie\MediaLibrary\FileAdder\FileAdder->toCollectionOnDisk('default', '')
#7 /app/Jobs/ConvertOldSiteItemsJob.php(59): Spatie\MediaLibrary\FileAdder\FileAdder->toMediaLibrary()
#8 [internal function]: App\Jobs\ConvertOldSiteItemsJob->handle()

My code:

        foreach ($this->images as $image) {
            if ($image->url != '') {
                $this->model->addMediaFromUrl($image->url)->toMediaLibrary();
            }
        }
freekmurze commented 7 years ago

@andaril take a look at this issue for a possible solution.

andaril commented 7 years ago

@freekmurze Yep, I found these issues later, but thanks. And I think this PR #329 is the solution. May be I must do pr with correct CS? 21 day is too long to wait response 😕

freekmurze commented 7 years ago

Closed the old PR, create a new PR to fix this

sebestenyb commented 7 years ago

I'm still having this issue on v5:

[Symfony\Component\Debug\Exception\FatalThrowableError]
Call to undefined method Spatie\MediaLibrary\UrlGenerator\S3UrlGenerator::getPath()

Exception trace:
 () at /vendor/spatie/laravel-medialibrary/src/Media.php:81
 Spatie\MediaLibrary\Media->getPath() at /vendor/spatie/laravel-medialibrary/src/ImageGenerators/BaseGenerator.php:24
 Spatie\MediaLibrary\ImageGenerators\BaseGenerator->canConvert() at /vendor/spatie/laravel-medialibrary/src/FileManipulator.php:120

Actually tried out in a blank new Laravel 5.4 project.

// composer.json
"require": {
        "php": ">=5.6.4",
        "laravel/framework": "5.4.*",
        "laravel/tinker": "~1.0",
        "league/flysystem-aws-s3-v3": "^1.0",
        "spatie/laravel-medialibrary": "^5.0"
    },
<?php
// User.php
namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Spatie\MediaLibrary\HasMedia\HasMediaTrait;
use Spatie\MediaLibrary\HasMedia\Interfaces\HasMedia;

class User extends Authenticatable implements HasMedia
{
    use Notifiable, HasMediaTrait;
// filesystem.php
        'media' => [
            'driver' => 's3',
            'key' => env('AWS_KEY'),
            'secret' => env('AWS_SECRET'),
            'region' => env('AWS_REGION'),
            'bucket' => env('AWS_BUCKET'),
        ],    
// tinker

Psy Shell v0.8.1 (PHP 7.0.8 — cli) by Justin Hileman
>>> use App\User
=> null
>>> use Faker\Factory
=> null
>>> $faker = Factory::create()
=> Faker\Generator {#678}
>>> User::first()->addMediaFromUrl($faker->imageUrl())->toMediaLibrary()
PHP error:  Call to undefined method Spatie\MediaLibrary\UrlGenerator\S3UrlGenerator::getPath() in /Users/balazssebesteny/Sites/medialibrary-test/vendor/spatie/laravel-medialibrary/src/Media.php on line 81

PS: Create new ticket #534