laravel / nova-issues

554 stars 34 forks source link

Can't access VaporImage #3180

Closed 1stevengrant closed 3 years ago

1stevengrant commented 3 years ago

Description:

I have a VaporImage field defined like so

VaporImage::make('Featured Image')
  ->help('Upload a featured image for the post that will display on landing pages ')
  ->storeAs(function ($request) {
    $img = $request->input('vaporFile')['featured_image'];
    return $img['key'].'.'.$img['extension'];
}),

The upload works, Nova displays the preview, and all is well. However, the featured_image column now has tmp/a-random-uuid.jpg as it's value. I can look into the s3 bucket and see the image.

On the front end, if I do {{ asset($post->featured_image) }} I get a URL but the image is 404.

There are 2 things here I guess

  1. the image not actually appearing at all on the front end
  2. I don't want to have tmp/my-file-name.jpg in the database. I dare say I could live with it if I could solve number 1

I can't call if this is a Nova thing or a Vapor thing or if it's even a bug, undocumented or documented and I've totally missed it.

crynobone commented 3 years ago

I personally never seen an example using VaporImage with storeAs(), but will check it out tommorow.

You might also want to ask if anyone has tried something similar on #nova on Laravel's Discord or other support channel listed at https://nova.laravel.com/docs/3.0/support.html#support-questions

1stevengrant commented 3 years ago

Thank you. Yeah, I've trawled through all the usual places; Discord, SO, Vapor's Github issues as well as Nova's and there was a reference in #2970 to the storeAs method but nothing for the issue I'm running into.

On Tue, 19 Jan 2021 at 13:22, Mior Muhammad Zaki notifications@github.com wrote:

I personally never seen an example using VaporImage with storeAs(), but will check it out tommorow.

You might also want to ask if anyone has tried something similar on #nova on Laravel's Discord https://discordapp.com/invite/mPZNm7A or other support channel listed at https://nova.laravel.com/docs/3.0/support.html#support-questions

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/laravel/nova-issues/issues/3180#issuecomment-762834958, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAA3OWKKBZ6SFNE3W2LTVKLS2WBSXANCNFSM4WIVL3RQ .

-- Steven Grant

crynobone commented 3 years ago

Based on the code review, since you explicitly call storeAs() you need to trim tmp/ from the key.

E.g:


VaporImage::make('Featured Image')
  ->help('Upload a featured image for the post that will display on landing pages ')
  ->storeAs(function ($request) {
    $img = $request->input('vaporFile')['featured_image'];
    return str_replace('tmp/', '', $img['key']).'.'.$img['extension'];
}),
1stevengrant commented 3 years ago

Right, that won't solve the issue of displaying the image

crynobone commented 3 years ago

You mean using {{ asset($post->featured_image) }}, outside of Laravel Nova?

Wouldn't you need to access the the URL via Storage? https://laravel.com/docs/8.x/filesystem#file-urls

1stevengrant commented 3 years ago

yeah, it doesn't resolve. I wouldn't have thought so because the {{ asset() }} helper is supposed to route to the s3 bucket?

On Wed, 20 Jan 2021 at 09:42, Mior Muhammad Zaki notifications@github.com wrote:

You mean using {{ asset($post->featured_image) }}?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/laravel/nova-issues/issues/3180#issuecomment-763475035, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAA3OWLSV47CXUIPJZLHJV3S22QRPANCNFSM4WIVL3RQ .

-- Steven Grant

crynobone commented 3 years ago

Use Storage::url(), asset() are only meant for file stored on the application instance. Vapor store everything on s3.

1stevengrant commented 3 years ago

thanks for that added my own helper function

function s3Url($file): string
{
    $disk = Storage::disk('s3');
    $disk->setVisibility($file, 'public');

    return $disk->url($file);
}