provideRep $ sendFile "image/png" "/path/to/file.png"

11 views
Skip to first unread message

jsch...@gmail.com

unread,
Nov 25, 2020, 11:39:48 AM11/25/20
to Yesod Web Framework

Hi,

I'm working on a resource with different representations: HTML, PNG image, JSON, Text.

I have considered to create seperate resources but in fact, the representations are really representations of one thing and they all share the same parameters and computations.

The PNG image is created on-demand as a file. So I have

getNn2GSR :: Handler TypedContent
getNn2GSR =   selectRep $ do
    provideRep $ defaultLayout $ ...
    provideRep $ sendFile "image/png" "test.png"

That doesn't work because Haskell cannot infer the type of (sendFile ...). It says "Ambigious type variable [..] prevents the constraint '(HasContentType a0)' from beeing solved."

After trial and error I found that this at least compiles:

    provideRep $ (sendFile "image/png" "test.png" :: Handler RepPlain)

But selectRep does not select this representation for a 'Accept: image/*' header.

1. Are there better ways to solve this problem?

E.g. change the route to
/my/service#Text MyServiceR GET
where #Text is "", ".html", ".png", ... and
when (format == ".png") $ sendFile ...

or better use different routes?

2. ... or do I have to define a new RepImage and define all required instances?
3. ... or can it solved even simpler?

Michael Snoyman

unread,
Nov 26, 2020, 1:28:55 AM11/26/20
to yeso...@googlegroups.com
I think you can use provideRepType for this:


--
You received this message because you are subscribed to the Google Groups "Yesod Web Framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to yesodweb+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/yesodweb/86bf079f-6502-46d1-b35e-07368b354d9cn%40googlegroups.com.

jsch...@gmail.com

unread,
Nov 26, 2020, 3:33:43 AM11/26/20
to Yesod Web Framework
Oh.. yes. Many thanks!!

I should more often take the time to read the whole Hackage pages, not just search for specific things...

    provideRepType "image/png" $ (sendFile "image/png" "test.png" :: Handler TypedContent)

I still have to cast the sendFile and found that (Handler TypedContent) semantically fits best from all predefined ToContent instances.

Thank you
- Jakob
Reply all
Reply to author
Forward
0 new messages