Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using this package with spatie media library #78

Closed
phuclh opened this issue May 11, 2020 · 2 comments
Closed

Using this package with spatie media library #78

phuclh opened this issue May 11, 2020 · 2 comments
Assignees

Comments

@phuclh
Copy link

phuclh commented May 11, 2020

I am using this package with Spatie media library (https://docs.spatie.be/laravel-medialibrary/v8/basic-usage/retrieving-media/)

When I upload an image, it will be added to storage/{tenant_id}/app/public. Normally, when I want to return the image URL, I just use: $mediaItems[0]->getFullUrl() or $mediaItems[0]->getUrl(), but it return an global of storage path:

http://domain.test/storage/16/conversions/image-medium.jpg.

Right now, I have to do some string handling to make that above URL turn to:
http://tenant.domain.test/tenancy/assets/16/conversions/image-medium.jpg

Are there any ways to make the getFullUrl or getUrl return the right URL without handle string stuff? Thank you!

@kakajansh
Copy link
Contributor

There is Generating custom urls config for medialibrary package.

Create custom UrlGenerator class extending Spatie\MediaLibrary\Support\UrlGenerator\BaseUrlGenerator,

/*
* When urls to files get generated, this class will be called. Use the default
* if your files are stored locally above the site root or on s3.
*/
'url_generator' => Spatie\MediaLibrary\Support\UrlGenerator\TenantAwareUrlGenerator::class,

I think using asset helper of tenancy, something like below should work

class TenantAwareUrlGenerator extends BaseUrlGenerator
{
    public function getUrl(): string
    {
        $url = asset($this->getPathRelativeToRoot());

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

        return $url;
    }
   ...
}

@stancl stancl transferred this issue from archtechx/tenancy Aug 31, 2020
@izshreyansh
Copy link

@kakajansh 's answer did the trick.

But it helps notice that you'll likely have to implement these function: getTemporaryUrl, getPath from default URL
Generator class found in Spatie\MediaLibrary\UrlGenerator.
[Because that is mandated by abstract class Spatie\MediaLibrary\UrlGenerator\BaseUrlGenerator]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants