[web2py] Intercept file download

2012-01-11 Thread Angelo Compagnucci
Hi guys,

For a web application, I have to implement a sort of responsive design for
images. There are tons of solution online, but they requires to use some
sort of javascript and custom html to work (insert special tags or classes
in code). I don't like the approch because it make the code unmaintenable.

The idea I have in mind is something like this:

1) On page load, a jquery function calls a controller and communicates
infos about the device.
2) Controller stores those infos in session
3) I intercept images download and
3a) If there are session infos about device width and is_mobile, I resize
the image with PIL to the exact width server side and give the new url to
the response.
3b) If there aren't infos (browser doesn't support cookies) and is_mobile,
I resize the image to an arbitrary small size (ex: 480 px) and give the new
url to the response.
3c) If !is_mobile, I return the normal url

How can I intercept file download also the static ones?

Thank you!

-- 
Profile: http://it.linkedin.com/in/compagnucciangelo


Re: [web2py] Intercept file download

2012-01-11 Thread Bruno Rocha
https://github.com/globocom/thumbor

On Wed, Jan 11, 2012 at 2:16 PM, Angelo Compagnucci 
angelo.compagnu...@gmail.com wrote:

 Hi guys,

 For a web application, I have to implement a sort of responsive design for
 images. There are tons of solution online, but they requires to use some
 sort of javascript and custom html to work (insert special tags or classes
 in code). I don't like the approch because it make the code unmaintenable.

 The idea I have in mind is something like this:

 1) On page load, a jquery function calls a controller and communicates
 infos about the device.
 2) Controller stores those infos in session
 3) I intercept images download and
 3a) If there are session infos about device width and is_mobile, I resize
 the image with PIL to the exact width server side and give the new url to
 the response.
 3b) If there aren't infos (browser doesn't support cookies) and is_mobile,
 I resize the image to an arbitrary small size (ex: 480 px) and give the new
 url to the response.
 3c) If !is_mobile, I return the normal url

 How can I intercept file download also the static ones?

 Thank you!

 --
 Profile: http://it.linkedin.com/in/compagnucciangelo




-- 

Bruno Rocha
[http://rochacbruno.com.br]


Re: [web2py] Intercept file download

2012-01-11 Thread Bruno Rocha
On Wed, Jan 11, 2012 at 2:23 PM, Bruno Rocha rochacbr...@gmail.com wrote:

 https://github.com/globocom/thumbor


and

https://github.com/globocom/mugshot


Re: [web2py] Intercept file download

2012-01-11 Thread Anthony
There's also this 
service: http://www.sencha.com/learn/how-to-use-src-sencha-io/

On Wednesday, January 11, 2012 11:24:23 AM UTC-5, rochacbruno wrote:



 On Wed, Jan 11, 2012 at 2:23 PM, Bruno Rocha rocha...@gmail.com wrote:

 https://github.com/globocom/thumbor


 and

 https://github.com/globocom/mugshot 
  


Re: [web2py] Intercept file download

2012-01-11 Thread Angelo Compagnucci
Thanks Bruno,

These not solve my problem! To use thumbor, I have to prepare links using a
special syntax, that is what I want to avoid. If I wanted to change links I
would have done using responsive design tecniques and my own webserver!

@Antony:
I know sencha and it adopts an approch like the one I proposed, they have a
proxy that intercepts requests to images and resize them basing on the
knowledge of the device they have by the useragent. The only caveat is that
you have to change all of your urls!

I think that we have a framework, we have user_agent_parser and we can
include responsive images in web2py!
If we can intercept GET to images, we can build something totally
integrated with web2py! And this is what I'm doing!
I'm thinking of a responsive design module for web2py that can be
configured (local processor power with PIL, online service like thumbor)
and when plugged in, it makes automatically responsive all of your images.

So, my question is, there is a way to intercept GET to static image files?

2012/1/11 Bruno Rocha rochacbr...@gmail.com



 On Wed, Jan 11, 2012 at 2:23 PM, Bruno Rocha rochacbr...@gmail.comwrote:

 https://github.com/globocom/thumbor


 and

 https://github.com/globocom/mugshot




-- 
Profile: http://it.linkedin.com/in/compagnucciangelo


Re: [web2py] Intercept file download

2012-01-11 Thread Anthony
On Wednesday, January 11, 2012 11:49:18 AM UTC-5, AngeloC wrote:

 Thanks Bruno,

 These not solve my problem! To use thumbor, I have to prepare links using 
 a special syntax, that is what I want to avoid. If I wanted to change links 
 I would have done using responsive design tecniques and my own webserver!

 @Antony:
 I know sencha and it adopts an approch like the one I proposed, they have 
 a proxy that intercepts requests to images and resize them basing on the 
 knowledge of the device they have by the useragent. The only caveat is that 
 you have to change all of your urls!

 I think that we have a framework, we have user_agent_parser and we can 
 include responsive images in web2py!
 If we can intercept GET to images, we can build something totally 
 integrated with web2py! And this is what I'm doing!
 I'm thinking of a responsive design module for web2py that can be 
 configured (local processor power with PIL, online service like thumbor) 
 and when plugged in, it makes automatically responsive all of your images.

 So, my question is, there is a way to intercept GET to static image files?


Assuming web2py is serving static files to begin with, I believe it does so 
very early in the process, before the session is read, etc. If you can add 
a flag to the URL (e.g., in the query string) only in cases where you need 
to offer the alternative (non-static) image, maybe you could handle that 
via the pattern-based rewrite system in routes.py (though that would 
prevent using the parameter-based system for other purposes).

Anthony


Re: [web2py] Intercept file download

2012-01-11 Thread Angelo Compagnucci
Thanks Anthony,

Ok, this way I have to play anyway with urls. I'll try another approach.
There is a way to rewrite the page after rendering, just before the page
will be sent to the browser?
This way we can analyze img urls, resize images accordingly, and rewrite
the new ones.

What do you tink?


 Assuming web2py is serving static files to begin with, I believe it does
 so very early in the process, before the session is read, etc. If you can
 add a flag to the URL (e.g., in the query string) only in cases where you
 need to offer the alternative (non-static) image, maybe you could handle
 that via the pattern-based rewrite system in routes.py (though that would
 prevent using the parameter-based system for other purposes).

 Anthony




-- 
Profile: http://it.linkedin.com/in/compagnucciangelo


Re: [web2py] Intercept file download

2012-01-11 Thread Anthony
On Wednesday, January 11, 2012 12:16:50 PM UTC-5, AngeloC wrote:

 Thanks Anthony,

 Ok, this way I have to play anyway with urls. I'll try another approach. 
 There is a way to rewrite the page after rendering, just before the page 
 will be sent to the browser?


html = response.render()
return replace_image_urls(html)

or maybe WSGI middleware. But that approach seems messy (you have to parse 
all the html). And what about images linked from static files (e.g., CSS 
background images)?

Anthony


Re: [web2py] Intercept file download

2012-01-11 Thread Angelo Compagnucci
Yes, you are right, the approach is messy.

So the best way could be something like a module. The image URL is passed
as an argument, the module crunch the image and returns the correct URL.

So in code you have to write something like:

IMG(_src=responsive(URL('static','image.img')),_alt='test')


This way the programmer is aware of responsiveness and he could use the
feature where he wants!

The responsive module could be configure to use local cpu or a remote
service.

What do you think?

2012/1/11 Anthony abasta...@gmail.com

 On Wednesday, January 11, 2012 12:16:50 PM UTC-5, AngeloC wrote:

 Thanks Anthony,

 Ok, this way I have to play anyway with urls. I'll try another approach.
 There is a way to rewrite the page after rendering, just before the page
 will be sent to the browser?


 html = response.render()
 return replace_image_urls(html)

 or maybe WSGI middleware. But that approach seems messy (you have to parse
 all the html). And what about images linked from static files (e.g., CSS
 background images)?

 Anthony




-- 
Profile: http://it.linkedin.com/in/compagnucciangelo