1

In laravel 9 with spatie/laravel-medialibrary 10 I tyry to make custom path for uploaded file looking at docs : https://spatie.be/docs/laravel-medialibrary/v9/advanced-usage/using-a-custom-directory-structure But making app/Services/MediaLibrary/CustomPathGenerator.php file :

<?php

namespace App\Services\MediaLibrary;
//namespace App\MediaLibrary;

use Spatie\MediaLibrary\MediaCollections\Models\Media;
use Spatie\MediaLibrary\PathGenerator\PathGenerator;

//class CustomPathGenerator implements BasePathGenerator
class CustomPathGenerator implements PathGenerator
{
    /*
     * Get the path for the given media, relative to the root storage path.
     */
    public function getPath(Media $media): string
    {
        return md5($media->id .  config('app.key')) .'/';
    }

    /*
     * Get the path for conversions of the given media, relative to the root storage path.
     */
    public function getPathForConversions(Media $media): string
    {
        return md5($media->id .  config('app.key')) .'/conversions/';

    }

    /*
     * Get the path for responsive images of the given media, relative to the root storage path.
     */
    public function getPathForResponsiveImages(Media $media): string {
        return md5($media->id .  config('app.key')) .'/responsive-images/';

    }
}

I got error :

[2022-02-16 17:52:44] local.ERROR: Interface "App\Services\MediaLibrary\BasePathGenerator" not found {"userId":1,"exception":"[object] (Error(code: 0): Interface \"App\\Services\\MediaLibrary\\BasePathGenerator\" not found at /mnt/_work_sdb8/wwwroot/lar/hostels4j/app/Services/MediaLibrary/CustomPathGenerator.php:8)

Looks like header of my file is invalid.

Which is valid way to fix it ?

Thanks in advance!

Petro Gromovo
  • 1,755
  • 5
  • 33
  • 91
  • This is ambiguous. Did the error exist before you changed from `BasePathGenerator` to `PathGenerator`? (code comment) – Wahyu Kristianto Feb 16 '22 at 16:26
  • I found BasePathGenerator referenced searching in net, but looks like it is invalid - I did not find any reference to it in my project. I want to know valid syntax of this file – Petro Gromovo Feb 18 '22 at 11:15

1 Answers1

2

You have defined the BasePathGenerator incorrectly please use the following instead

<?php

namespace App\Services\MediaLibrary;

use Spatie\MediaLibrary\MediaCollections\Models\Media;
use Spatie\MediaLibrary\Support\PathGenerator\PathGenerator as BasePathGenerator;

class CustomPathGenerator implements BasePathGenerator {
        /*
     * Get the path for the given media, relative to the root storage path.
     */
    public function getPath(Media $media): string {
        return '';
    }

    /*
     * Get the path for conversions of the given media, relative to the root storage path.
     */
    public function getPathForConversions(Media $media): string {
        return '';
    }

    /*
     * Get the path for responsive images of the given media, relative to the root storage path.
     */
    public function getPathForResponsiveImages(Media $media): string {
        return '';
    }
}

This should solve your issue.