Zinc question (may be this is an HTML one)

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Zinc question (may be this is an HTML one)

Stéphane Ducasse
Hi sven and others


While trying to improve microdown I have the following question. 
How can I have a more generic getImage: method. 

Right now I use getPng: and I would like to support getPng: and getJpeg. 

I saw that there is getImageOfType: mimeType fromUrl: urlObject


getImageOfType: mimeType fromUrl: urlObject
| client |
(client := self client)
url: urlObject;
accept: mimeType;
enforceHttpSuccess: true;
enforceAcceptContentType: true;
get.
"ImageReadWriter does automatic type detection"
^ ImageReadWriter formFromStream: client entity readStream

So it looks like what I want except that I do not know how to specify a Mime 

So I did 

 ZnEasy 
getImageOfType:  (ZnMimeType main: 'image' sub: '*')

Now I would like to know if this is the correct way to do it. 
I could imagine that we could give a set of possible mime types.
S.


--------------------------------------------
Stéphane Ducasse
03 59 35 87 52
Assistant: Aurore Dalle 
FAX 03 59 57 78 50
TEL 03 59 35 86 16
S. Ducasse - Inria
40, avenue Halley, 
Parc Scientifique de la Haute Borne, Bât.A, Park Plaza
Villeneuve d'Ascq 59650
France

Reply | Threaded
Open this post in threaded view
|

Re: Zinc question (may be this is an HTML one)

Sven Van Caekenberghe-2
Hi Stef,

> On 20 Oct 2020, at 09:35, Stéphane Ducasse <[hidden email]> wrote:
>
> Hi sven and others
>
>
> While trying to improve microdown I have the following question.
> How can I have a more generic getImage: method.
>
> Right now I use getPng: and I would like to support getPng: and getJpeg.
>
> I saw that there is getImageOfType: mimeType fromUrl: urlObject
>
>
> getImageOfType: mimeType fromUrl: urlObject
> | client |
> (client := self client)
> url: urlObject;
> accept: mimeType;
> enforceHttpSuccess: true;
> enforceAcceptContentType: true;
> get.
> "ImageReadWriter does automatic type detection"
> ^ ImageReadWriter formFromStream: client entity readStream
>
> So it looks like what I want except that I do not know how to specify a Mime
>
> So I did
>
>  ZnEasy
> getImageOfType:  (ZnMimeType main: 'image' sub: '*')
> fromUrl: 'http://pharo.org/files/pharo.png'
>
> Now I would like to know if this is the correct way to do it.
> I could imagine that we could give a set of possible mime types.

This is an HTTP question (the protocol, not HTML the document format).

And you solved your problem well, I never tried it like that myself ;-)

I would not write it differently.

Like the comment says, it will only work with those types recognised by ImageReadWriter, which are the most common ones, JPG, GIF and PNG.

Image/* is a wildcard matching any image type, and there are many, many more.

The current mechanism in Zinc with #accept: and #enforceAcceptContentType: true assumes a singular 'Accept' header. But indeed, technically, it is possible to specify more than one.

For example, this also works:

ZnClient new
  url: 'http://pharo.org/files/pharo.png';
  headerAt: 'Accept' add: ZnMimeType imageGif asString;
  headerAt: 'Accept' add: ZnMimeType imagePng asString;
  headerAt: 'Accept' add: ZnMimeType imageJpeg asString;
  contentReader: [ :entity | ImageReadWriter formFromStream: entity readStream ];
  get.

But I have to think a bit about this issue.

Another approach is to use the extension of the URL.

ZnMimeType forFilenameExtension: 'http://pharo.org/files/pharo.png' asUrl file asFileReference extension.

It all depends at what level you want to tell your user they tried getting an unsupported file type.

Sven

> S.
>
>
> --------------------------------------------
> Stéphane Ducasse
> http://stephane.ducasse.free.fr / http://www.pharo.org 
> 03 59 35 87 52
> Assistant: Aurore Dalle
> FAX 03 59 57 78 50
> TEL 03 59 35 86 16
> S. Ducasse - Inria
> 40, avenue Halley,
> Parc Scientifique de la Haute Borne, Bât.A, Park Plaza
> Villeneuve d'Ascq 59650
> France
>
Reply | Threaded
Open this post in threaded view
|

Re: Zinc question (may be this is an HTML one)

Stéphane Ducasse


This is an HTTP question (the protocol, not HTML the document format).

Yes this is what I meant but the mail was sent.

And you solved your problem well, I never tried it like that myself ;-)

I would not write it differently.

Like the comment says, it will only work with those types recognised by ImageReadWriter, which are the most common ones, JPG, GIF and PNG.

Image/* is a wildcard matching any image type, and there are many, many more. 

The current mechanism in Zinc with #accept: and #enforceAcceptContentType: true assumes a singular 'Accept' header. But indeed, technically, it is possible to specify more than one.

For example, this also works:

ZnClient new
 url: 'http://pharo.org/files/pharo.png';
 headerAt: 'Accept' add: ZnMimeType imageGif asString;
 headerAt: 'Accept' add: ZnMimeType imagePng asString;
 headerAt: 'Accept' add: ZnMimeType imageJpeg asString;
 contentReader: [ :entity | ImageReadWriter formFromStream: entity readStream ];
 get.


Ah this would be better for me because like that I can probably control which exception is raised.
Because we do not support image/svg

But I have to think a bit about this issue.

Another approach is to use the extension of the URL.

ZnMimeType forFilenameExtension: 'http://pharo.org/files/pharo.png' asUrl file asFileReference extension.

It all depends at what level you want to tell your user they tried getting an unsupported file type.

Broad :) but soon you will be my user and you will have the right to complain :)



Sven

S.


--------------------------------------------
Stéphane Ducasse
http://stephane.ducasse.free.fr / http://www.pharo.org 
03 59 35 87 52
Assistant: Aurore Dalle 
FAX 03 59 57 78 50
TEL 03 59 35 86 16
S. Ducasse - Inria
40, avenue Halley, 
Parc Scientifique de la Haute Borne, Bât.A, Park Plaza
Villeneuve d'Ascq 59650
France

--------------------------------------------
Stéphane Ducasse
03 59 35 87 52
Assistant: Aurore Dalle 
FAX 03 59 57 78 50
TEL 03 59 35 86 16
S. Ducasse - Inria
40, avenue Halley, 
Parc Scientifique de la Haute Borne, Bât.A, Park Plaza
Villeneuve d'Ascq 59650
France