7

I wrote a small Web-Application using Grails. The resulting war-file is deployed on a Tomcat application server.

The app has an upload functionality for attachments. I chose to save the uploaded files to disk (rather than storing blobs in the DB).

At first i chose a filepath inside the webapp-dir to store the files, but then i realized that this dir will be overwritten with every deployment of a new war-file.

Is there a best practice for "where to store uploaded files in your local filesystem"?

Thanks for your help.

DiegoFrings
  • 3,043
  • 3
  • 26
  • 30

4 Answers4

2

AlphaOne, we do something similar to what you are asking for, but we use an Apache webserver in front of Tomcat. We pick up the full path from the config file and write to it. Say "/tmp/branding". Then we also pick up the url path for this location from the config file (say "myapp/branding") and use this within our GSPs. Then, in Apache, we map app requests for that url ("myapp/branding") to go directly to the file location, bypassing Tomcat.

Sunny
  • 1,129
  • 4
  • 13
  • 25
  • 1
    Word of warning, many operating systems will purge the tmp directory occasionally. It is often done at boot time if a certain number of days have passed. It can appear unpredictable and may be tricky to debug. – Casey Watson Aug 30 '11 at 15:10
2

Create a separate project for upload and pass the user name for creating a user specific folder. Provide a link to this project from the main project when user wants to upload the files.

cweston
  • 11,297
  • 19
  • 82
  • 107
kris
  • 21
  • 2
1

I would recommend using an environment variable to store the location, or better yet using an environment variable to set the location of your config properties file and setting a property there. That way the location becomes configurable by the operations team, who should be managing filesystem/configuration concerns.

codelark
  • 12,254
  • 1
  • 45
  • 49
  • Sounds interresting, but: the operations team can configure the dir only to paths the application server can handle. if the files will be stored in lets say "/tmp/" the app server will not be able to send this image to the browser. so where is good location that meets both requirements: app server can send files to browser AND dir will not be overwritten on deployment ? – DiegoFrings Feb 09 '11 at 15:02
  • They should also control the configuration of the appserver/webserver. If you also need to serve the content back, have another configuration parameter be the url location to the directory being served. – codelark Feb 09 '11 at 17:19
0

I guess you are looking for a best practice solution. Well, there isn't really one, but there are some places you should avoid putting them in. I' ll assume a UNIX environment.

For example:

  • The $CATALINA_HOME. Just leave the application folder for what it is, don' t pollute it with uploaded files.
  • /tmp . You never know who will clean this up.

Though I 'm not sure it is a locked specification, you could look at: linux directory structure .

The way I read it you have 3 options:

  1. If Tomcat runs using a dedicated user -> The users home directory. For example ~/Fileuploads/appname
  2. Since the var could be seen as a growing directory of application data. For Example: /var/Fileuploads/appname
  3. Leave it to the sys admin. Check out this: Externalizing Grails Configuration

Good luck!

Community
  • 1
  • 1
Peter De Winter
  • 1,183
  • 2
  • 15
  • 27