Gateway content-type response header

From @uptownhr on Tue Feb 07 2017 20:43:09 GMT+0000 (UTC)

Is there a way to set the header of what the gateway will return as content-type. I have an issue right now when serving svg files. Since the content-type is text, browsers do not recognize it properly and will not display the image.

Copied from original issue: https://github.com/ipfs/faq/issues/224

From @lidel on Tue Feb 07 2017 22:03:54 GMT+0000 (UTC)

I feel this requires clarification, as it may not be clear what is wrong on the first sight.

Sample SVG image at /ipfs/QmWBNMp6JxXj5aYrpef8z2xaPC7DeGftG4jTy6EyF958o4
is returned with Content-Type: text/xml and when opened directly it renders just fine under both Firefox 51 and Chromium 56. :ok_hand:

The problem is that resource returned as text/xml can’t be rendered as <img src=" or CSS background. If you open svg-mimetype-example.html you will see <img> is not rendered and alt text is displayed instead. :-1:

My guess is that go-ipfs probably uses standard mime-type sniffer from golang [1] which defaults to text/xml
Correct mime-type for image contexts in browser is image/svg+xml [2,3]

It does not seem to work correctly now, but you may want to subscribe to related issues about mime-type in go-ipfs:


1: https://golang.org/src/net/http/sniff.go
2: https://www.w3.org/TR/SVGTiny12/mimereg.html
3: https://developer.mozilla.org/en-US/docs/Web/SVG/Tutorial/Getting_Started#SVG_File_Types

From @uptownhr on Wed Feb 08 2017 00:44:15 GMT+0000 (UTC)

awesome, thank you

From @lgierth on Wed Feb 08 2017 01:02:33 GMT+0000 (UTC)

Maybe it helps to wrap the svg in a directory, in order to have it addressed with a .svg filename:

> ipfs add -w file.svg
added Qmsvg file.svg
added Qmfoo
# => /ipfs/Qmfoo/file.svg

From @lidel on Wed Feb 08 2017 07:35:55 GMT+0000 (UTC)

Indeed! If extension is .svg:

<img src="https://ipfs.io/ipfs/QmQ48wa7jc9JZJ9Lc1XCkuwa3FUYeXcMPJi3vAD78VbBM1/sample.svg" alt="if you see this text it means SVG image failed to load"/>

it IS returned as Content-Type:"image/svg+xml" and renders correctly:
/ipfs/QmRYdrzpspUpeDgn1au2L6jNYuo9SZN2TD4FABn9dY9jra/svg-mimetype-example2.html :sparkles:

From @Kubuxu on Wed Feb 08 2017 10:13:01 GMT+0000 (UTC)

The content type detection from the content is only heuristic, so it is better to use file extension for deterministic content type.