2

My idea is to save the images which the user uploads outside the context path as follow:

D:\somefolder\myWeb\web-app\
D:\somefolder\imagesOutsideContextPath\

The code for that is the next (working locally):

String path = servletContext.getRealPath("/"); 
String parentFolder = new File(path).getParentFile().getParent();
String imagesFolder = parentFolder + "\\imagesOutsideContextPath";

Another idea (if this one doesn't work on server) would be to save the images in the current user's home folder as @HoàngLong suggested me.

But I'm not able to load the images from the view. I think this article from official documentation is not valid for that purpose. The next code desn't load anything:

<img src="D:\\somefolder\\imagesOutsideContextPath\\bestImageEver.jpg" alt="if I don't see this message, I'll be happier">

How could I use the real path instead the an url path to load these images?

Community
  • 1
  • 1
chelder
  • 3,819
  • 6
  • 56
  • 90

4 Answers4

2

There's a new plugin that makes this easy, check out http://grails.org/plugin/img-indirect

Burt Beckwith
  • 75,342
  • 5
  • 143
  • 156
  • I think it is usually the best solution. The plugin is very nice. I just choose the @SérgioMichels answer because it fits better with my already written code. – chelder Aug 18 '13 at 01:33
2

Create an action

def profileImage() {
    String profilePicturePath = "${grailsApplication.config.profilePictureDirectoryPath}/${params.id}"
    File file = new File(profilePicturePath)
    response.contentType = URLConnection.guessContentTypeFromName(file.getName())
    response.outputStream << file.bytes
    response.outputStream.flush()
}

and then call this action with image name in params like:

<g:img uri="${grailsApplication.config.grails.serverURL}/controller/profileImage/${user?.profilePicture?.fileName}"/>

I have declared the image directory file in my config.groovy file like:

profilePictureDirectoryPath = '/opt/CvSurgeon/profileImages'
MKB
  • 7,587
  • 9
  • 45
  • 71
1

You can set the src to an action. With that your user will not know where your images are stored (security) and you can easily change your logic to display them.

In the action, just get your image and print the bytes. Example here.

Community
  • 1
  • 1
  • Thank you! This solution fits better with my already written code. But all the proposed solutions are perfectly correct and could be better depending on the particular case. – chelder Aug 18 '13 at 01:25
1

Firstly, thank you for your reference.

It's insecure to load images using real path. The web browser should know nothing about how the pictures are saved on server, therefore not aware of the folder structure.

What I mean is that the system should use a specific URL for all your pictures, such as http://your_app/photo/user/{id}. Then to that URL, you can construct an action which gets id as a parameter, look up the photo in your file system(of course you must store the picture folder in configuration), and render the photo back.

Hoàng Long
  • 10,746
  • 20
  • 75
  • 124