statamic / cms

The core Laravel CMS Composer package
https://statamic.com
Other
3.71k stars 508 forks source link

Using glide with S3 asset storage returns wrong url for SVG #8637

Open godismyjudge95 opened 1 year ago

godismyjudge95 commented 1 year ago

Bug description

I was attempting to use S3 to store images and using glide to resize the images. This does not appear to work with SVG's however.

How to reproduce

  1. Create a new site and set up an S3 asset container
  2. Store an SVG in the asset container
  3. Assign the stored SVG to a field
  4. In the template use the SVG like you normally would:
    {{ glide:field_name }}
  5. The returned url is the result I would expect if the image was locally stored instead of returning the url to the S3 bucket. (ie. the AWS_URL + the path to the SVG)

Example of what is outputted:

/bucketname/logos/main.svg

Example of what should be outputted:

https://us-southeast-1.linodeobjects.com/bucketname/logos/main.svg

Logs

No response

Environment

Environment
Application Name: Dev
Laravel Version: 9.39.0
PHP Version: 8.0.29
Composer Version: 2.4.4
Environment: staging
Debug Mode: ENABLED
URL: dev.test
Maintenance Mode: OFF

Cache
Config: NOT CACHED
Events: NOT CACHED
Routes: NOT CACHED
Views: CACHED

Drivers
Broadcasting: log
Cache: memcached
Database: sqlite
Logs: stack / single
Mail: smtp
Queue: database
Session: file

Statamic
Addons: 14
Antlers: runtime
Stache Watcher: Disabled
Static Caching: Disabled
Version: 3.3.64 PRO

Statamic Addons
aerni/livewire-forms: 6.2.0
aerni/social-links: 2.3.1
aryehraber/statamic-impersonator: 2.4.1
aryehraber/statamic-logbook: 2.1.0
jonassiewertsen/statamic-livewire: 2.9.0
kolaente/statamic-snippet: 1.1.2
mitydigital/statamic-google-fonts: 1.0.1
plugrbase/statamic-maintenance-mode: 1.0.6
rias/statamic-data-import: 1.2.2
thoughtco/statamic-restrict-fields: 1.0.2
webographen/statamic-admin-log: 1.0.6
webographen/statamic-widget-cache-controller: 1.0.1
webographen/statamic-widget-continue-editing: 1.0.1
withcandour/aardvark-seo: 2.0.30

Installation

Fresh statamic/statamic site via CLI

Antlers Parser

runtime (new)

Additional details

No response

jasonvarga commented 10 months ago

You can work around this for now by checking for svgs and avoid using glide.

{{ if field_name:is_svg }}{{ url }}{{ else }}{{ glide:field_name }}{{ /if }}

ryanmitchell commented 9 months ago

Do you have anything set in your AWS_URL .env variable? If I have a value in that then I get an absolute S3 path instead a relative one.

godismyjudge95 commented 9 months ago

Do you have anything set in your AWS_URL .env variable? If I have a value in that then I get an absolute S3 path instead a relative one.

Yeah we do have that set... It's this code in particular that's the problem: https://github.com/statamic/cms/blob/4.x/src/Tags/Glide.php#L206

The problem is that the code defaults to not making the asset url not absolute which then takes the AWS asset url and removes the domain and scheme. But this only needs to happen when the url is to an asset on the current site url (ie. it shouldn't happen if the asset url is through another domain)

I attempted a PR to fix it, but ran into an issue - https://github.com/statamic/cms/pull/8901 I might take another swing at it soon.