0

I have been reading about the best place to save images uploaded by users. In my case, I think it is better to store these images in the filesystem. I want to store them in the same server and keep it easy to mantain by other administrators.

Is it possible to set the upload folder inside the application context and configure Grails in order to not delete that folder when redeploying a new war?

If not, I wonder if the next code (gotten here by @yecid) would work as I think it uploads the images inside the application context:

//for development environment
def root = System.properties['base.dir']?.toString()
if(!root){
    //for production environment in war deplements
    def tmpRoot = ApplicationHolder.application.mainContext.getResource('WEB-INF').getFile().toString()
    root = tmpRoot.substring(0, tmpRoot.indexOf(File.separator + 'temp' + File.separator))
}
if(!root){
    throw new Exception('Not found a valid path')
}
return root + File.separator
Community
  • 1
  • 1
chelder
  • 3,819
  • 6
  • 56
  • 90

1 Answers1

0

In the code, I see that your are saving uploaded files into your WEB-INF folder. Although you can do that, when you restart server/ redeploy application, the content of your web-app folder will be replaced, hence the uploaded file will be removed as well. That's how Tomcat works.

I think the better way to do that is saving your uploaded file to a folder which is NOT inside of your web container (Tomcat) folder. By that way, it won't be deleted when you:

1) Redeploy your web application

2) Restart Tomcat for whatever reason

A practice my team used to do is that we create a custom folder in the home folder of the current user. By that way we don't have to worry much about the privilege to save files.

Hoàng Long
  • 10,746
  • 20
  • 75
  • 124
  • OK. Everything is clear now. Thank you. I don't know if Tomcat will have a home folder so my idea is to save the images in the parent folder where the application is located: `String path = servletContext.getRealPath("/"); ` `String parentStr = new File(path).getParentFile().getParent(); ` – chelder Aug 01 '13 at 22:43
  • I mean, if the web application is located in `D:\somefolder\myWeb\web-app\`, `parentStr` will be `D:\somefolder\` . I want to save the images in `D:\somefolder\myWeb-images` – chelder Aug 01 '13 at 22:48
  • hi chelder, I don't mean any folder in Tomcat, I mean the home folder of your current user (most likely the user whose privilege is used to start tomcat) – Hoàng Long Aug 02 '13 at 03:53
  • 1
    I understand your method, but in some way I wouldn't recommend that. Of course you can save the image to any place you want, as long as you have the write permission to that directory. However, if you work on web server, sometimes you won't have the administration rights over the whole system. A safer option is to save your files under your current user's home folder(the personal folder in Windows), since you always have the rights to access your home folder, you always have the write permission. – Hoàng Long Aug 02 '13 at 03:58