0

I'm having a problem with Spatie Media Library. I created my class to use a different filesystem (specifically a Google bucket). Everything works smooth and I can integrate the filesystem correctly, save and view through the custom url. I created my class and gave what "Spatie" describes in its documentation as a namespace namespace Spatie\MediaLibrary\UrlGenerator; . However, when I run the "artisan config: cache" command I get the error mentioned above.

Here my Custom Class Code extending BaseUrlGenerator:

namespace Spatie\MediaLibrary\UrlGenerator;

use DateTimeInterface;
use Illuminate\Contracts\Config\Repository as Config;
use Illuminate\Filesystem\FilesystemManager;

class GcsUrlGenerator extends BaseUrlGenerator
{
/** @var \Illuminate\Filesystem\FilesystemManager */
protected $filesystemManager;

public function __construct(Config $config, FilesystemManager $filesystemManager)
{
    $this->filesystemManager = $filesystemManager;

    parent::__construct($config);
}

/**
 * Get the url for a media item.
 *
 * @return string
 */
public function getUrl(): string
{
    $url = $this->getPathRelativeToRoot();

    if ($root = config('filesystems.disks.'.$this->media->disk.'.root')) {
        $url = $root.'/'.$url;
    }

    $url = $this->rawUrlEncodeFilename($url);

    $url = $this->versionUrl($url);

    return config('medialibrary.gcs.domain').'/'.$url;
}

/**
 * Get the temporary url for a media item.
 *
 * @param \DateTimeInterface $expiration
 * @param array $options
 *
 * @return string
 */
public function getTemporaryUrl(DateTimeInterface $expiration, array $options = []): string
{
    return $this
        ->filesystemManager
        ->disk($this->media->disk)
        ->temporaryUrl($this->getPath(), $expiration, $options);
}

/**
 * Get the url for the profile of a media item.
 *
 * @return string
 */
public function getPath(): string
{
    return $this->getPathRelativeToRoot();
}

/**
 * Get the url to the directory containing responsive images.
 *
 * @return string
 */
public function getResponsiveImagesDirectoryUrl(): string
{
    $url = $this->pathGenerator->getPathForResponsiveImages($this->media);
    if ($root = config('filesystems.disks.'.$this->media->disk.'.root')) {
        $url = $root.'/'.$url;
    }

    return config('medialibrary.gcs.domain').'/'.$url;
}
}

Here what I included in the published vendor of medialibrary

    'custom_url_generator_class' => \Spatie\MediaLibrary\UrlGenerator\GcsUrlGenerator::class,

What I'm missing here?

Thanks for helping me

apokryfos
  • 38,771
  • 9
  • 70
  • 114

1 Answers1

1

According to the documentation you should implement the Spatie\MediaLibrary\UrlGenerator interface, not the namespace. Alternatively you can extend Spatie\MediaLibrary\UrlGenerator\BaseUrlGenerator which implements that interface itself. So the namespace of your custom class should still adhere to default naming, meaning it should have namespacing according to the folder structure and classname so it gets autoloaded properly.

PtrTon
  • 3,705
  • 2
  • 14
  • 24
  • Ok it works and I implemented the UrlGenerator correctly but I think I messed up the things because now if I delete that class and try to use the new one it always recall the old class. Also if i clear routes, caches, configs, etc. Laravel always try to use the old class. – Giuseppe Albrizio Dec 23 '19 at 21:18
  • Try manually deleting the contents of `storage/framework` and `bootstrap` to be sure no caching issue is at hand. Also run `composer dump-autoload` just for sanity sake. – PtrTon Dec 23 '19 at 21:22