Quantcast

link to images outside servlet container

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

link to images outside servlet container

Bugzilla from corey_s@qwest.net

From a gsp view, how do I display an image file that is located on a path
outside the grails environment?



---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: link to images outside servlet container

BrockHeinz
From a controller action:

- Load your image to a javax.activation.FileDataSource
- Set the content type on the response
- Set the content length on the response
- Write the bytes to the servlet output stream
- Flush the stream

hth,
Brock


On Mon, Jul 7, 2008 at 4:14 PM, Corey <[hidden email]> wrote:

>
> From a gsp view, how do I display an image file that is located on a path
> outside the grails environment?
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>    http://xircles.codehaus.org/manage_email
>
>
>

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: link to images outside servlet container

Bugzilla from corey_s@qwest.net
On Monday 07 July 2008 02:49:13 pm Brock Heinz wrote:
> From a controller action:
>
> - Load your image to a javax.activation.FileDataSource
> - Set the content type on the response
> - Set the content length on the response
> - Write the bytes to the servlet output stream
> - Flush the stream
>

Yikes.

All of that simply because the image is not somewhere under web-app?

Any examples that I can use to go by? The above description is helpful,
but since I haven't had to do that yet in any of my grails apps, much
of it is beyond my experience.

I have an Image domain class, which belongsTo an Item domain class. When
creating an Item, the user uploads an image for it. The Image class
resizes the image into three sizes (thumb, small, large), and stores
the images to an arbitrary path on the server, the path/filename of
the images is persisted in the Image class as fields (image.thumb,
image.small, image.large), but does not store the images as blobs in
the db.

When showing and listing items (ItemController.list ItemController.show),
I want to display the images along with the rest of the Item details.


Thanks!



---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: link to images outside servlet container

BrockHeinz
On Mon, Jul 7, 2008 at 4:50 PM, Corey <[hidden email]> wrote:

> On Monday 07 July 2008 02:49:13 pm Brock Heinz wrote:
>> From a controller action:
>>
>> - Load your image to a javax.activation.FileDataSource
>> - Set the content type on the response
>> - Set the content length on the response
>> - Write the bytes to the servlet output stream
>> - Flush the stream
>>
>
> Yikes.
>
> All of that simply because the image is not somewhere under web-app?
>
> Any examples that I can use to go by? The above description is helpful,
> but since I haven't had to do that yet in any of my grails apps, much
> of it is beyond my experience.

It sounds harder than it really is.  Here's a short snippet that
should get you on your way:

def youraction = {
  def ds = new FileDataSource(ImageDomain.get(id).imagePath)
  def is = ds.inputStream
  def os = response.outputStream
  response.contentLength = is.available()
  response.contentType = ds.contentType
  //set cache / pragma headers to cache image in browser?
  os << is
  os.flush()
}

Also, here's a recent thread that discusses a few different approaches
to what you've described:
http://www.nabble.com/File-resource-location-td18201634.html


hth,
Brock



>
> I have an Image domain class, which belongsTo an Item domain class. When
> creating an Item, the user uploads an image for it. The Image class
> resizes the image into three sizes (thumb, small, large), and stores
> the images to an arbitrary path on the server, the path/filename of
> the images is persisted in the Image class as fields (image.thumb,
> image.small, image.large), but does not store the images as blobs in
> the db.
>
> When showing and listing items (ItemController.list ItemController.show),
> I want to display the images along with the rest of the Item details.
>
>
> Thanks!
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>    http://xircles.codehaus.org/manage_email
>
>
>

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: link to images outside servlet container

Bugzilla from corey_s@qwest.net
On Monday 07 July 2008 03:26:43 pm Brock Heinz wrote:
> On Mon, Jul 7, 2008 at 4:50 PM, Corey <[hidden email]> wrote:
<snip>

> It sounds harder than it really is.  Here's a short snippet that
> should get you on your way:
>
> def youraction = {
>   def ds = new FileDataSource(ImageDomain.get(id).imagePath)
>   def is = ds.inputStream
>   def os = response.outputStream
>   response.contentLength = is.available()
>   response.contentType = ds.contentType
>   //set cache / pragma headers to cache image in browser?
>   os << is
>   os.flush()
> }
>

Thanks -- and your right, not so difficult; I also got another example
from http://grails.org/Simple+Avatar+Uploader , towards the bottom
with the UserController.avatar_image and views/user/show.gsp
examples.

So, for the archives, I ended up with:

class ItemController {

   import javax.activation.FileDataSource

   // ...snipped irrelevant actions

   def image = {

      def image = Image.get( params.id )

      if ( ! image )
         return response.sendError( 404 )

      FileDataSource ds =
         new FileDataSource( Shoppe.imagesPath + image."${params.type}" )

      InputStream is = ds.inputStream

      response.setContentLength( is.available() )
      response.setContentType( ds.contentType )

      OutputStream out = response.outputStream

      out << is

      out.close()
   }

}


And then in a gsp view:

   <g:link action="manage" id="${item.name}">
<img src="${createLink(controller:'item', action:'image', id:item.image.id,
params:[type:'thumb'])}" />
   </g:link>



Many thanks for the pointer!


Cheers


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: link to images outside servlet container

Bugzilla from corey_s@qwest.net
On Monday 07 July 2008 04:02:20 pm Corey wrote:
<snip>
>
> So, for the archives, I ended up with:
>
> class ItemController {
>
>    import javax.activation.FileDataSource
>

whoops... obviously - that import goes outside the class definition.


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: link to images outside servlet container

Brad Whitaker
In reply to this post by Bugzilla from corey_s@qwest.net
One option is to use the Static Resources plugin: http://www.grails.org/Static+Resources+Plugin. I've been using it for a scenario very similar to yours. It will require you to set up a different "image serving" application in your production environment, e.g. Apache httpd. The plugin provides the image serving app during development.

Corey-24 wrote
On Monday 07 July 2008 02:49:13 pm Brock Heinz wrote:
> From a controller action:
>
> - Load your image to a javax.activation.FileDataSource
> - Set the content type on the response
> - Set the content length on the response
> - Write the bytes to the servlet output stream
> - Flush the stream
>

Yikes.

All of that simply because the image is not somewhere under web-app?

Any examples that I can use to go by? The above description is helpful,
but since I haven't had to do that yet in any of my grails apps, much
of it is beyond my experience.

I have an Image domain class, which belongsTo an Item domain class. When
creating an Item, the user uploads an image for it. The Image class
resizes the image into three sizes (thumb, small, large), and stores
the images to an arbitrary path on the server, the path/filename of
the images is persisted in the Image class as fields (image.thumb,
image.small, image.large), but does not store the images as blobs in
the db.

When showing and listing items (ItemController.list ItemController.show),
I want to display the images along with the rest of the Item details.


Thanks!
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: link to images outside servlet container

Bugzilla from corey_s@qwest.net
On Monday 07 July 2008 04:10:32 pm Brad Whitaker wrote:
> One option is to use the Static Resources plugin:
> http://www.grails.org/Static+Resources+Plugin. I've been using it for a
> scenario very similar to yours. It will require you to set up a different
> "image serving" application in your production environment, e.g. Apache
> httpd. The plugin provides the image serving app during development.
>

Cool - thanks, I'll keep this in mind and try it out when I get a chance;
it won't fit my current app, but it looks like it would be useful in future
projects.


Cheers


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Loading...