pharo-users@lists.pharo.org

Any question about pharo is welcome

View all threads

Zinc question (may be this is an HTML one)

SD
Stéphane Ducasse
Tue, Oct 20, 2020 7:35 AM

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. 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

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. 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
SV
Sven Van Caekenberghe
Tue, Oct 20, 2020 8:49 AM

Hi Stef,

On 20 Oct 2020, at 09:35, Stéphane Ducasse stephane.ducasse@inria.fr 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

Hi Stef, > On 20 Oct 2020, at 09:35, Stéphane Ducasse <stephane.ducasse@inria.fr> 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 >
SD
Stéphane Ducasse
Tue, Oct 20, 2020 4:15 PM

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 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 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://stephane.ducasse.free.fr/ / http://www.pharo.org 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 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

> > 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 <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 <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://stephane.ducasse.free.fr/> / http://www.pharo.org <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 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