Ignore:
Timestamp:
Feb 9, 2011, 11:10:38 AM (12 years ago)
Author:
robert@…
Message:
  • Made file upload directory configurable (#282)
  • Fixed bug in uploading multiple files with the same name (#288)

This will only be fully functional after an update of the gdt plugin.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/grails-app/services/dbnp/studycapturing/FileService.groovy

    r1430 r1508  
    1515package dbnp.studycapturing
    1616
     17import java.io.File;
     18
    1719import org.codehaus.groovy.grails.commons.ApplicationHolder
     20import org.codehaus.groovy.grails.commons.ConfigurationHolder
    1821
    1922class FileService implements Serializable {
     
    2932     */
    3033    def File getUploadDir() {
    31         // Find the absolute path to the fileuploads directory. This code is found on
    32         // http://stackoverflow.com/questions/491067/how-to-find-the-physical-path-of-a-gsp-file-in-a-deployed-grails-application
    33         //
    34         // The code used in NMC-DSP didn't work, because that code only works in
    35         // controllers:
    36         //    grailsAttributes.getApplicationContext().getResource("/fileuploads").getFile();
    37         return ApplicationHolder.application.parentContext.getResource("fileuploads").getFile()
     34                // Find the file upload directory name from the configuration
     35                String dir = ConfigurationHolder.config.uploads.uploadDir
     36
     37                if( !dir )
     38                        dir = "fileuploads"
     39
     40                return absolutePath( dir );
    3841    }
     42
     43        /**
     44        * Returns the absolute path for the given pathname. If the pathname is relative, it is taken relative to the web-app directory
     45        * @param pathname
     46        * @return
     47        */
     48   private File absolutePath( String pathname ) {
     49           if( pathname == null)
     50                        return null
     51
     52           // Check if this is an absolute path
     53           File f = new File( pathname );
     54
     55           if( f.isAbsolute() ) {
     56                   return f
     57           } else {
     58                   // Find the absolute path relative to the web-app directory. This code is found on
     59                   // http://stackoverflow.com/questions/491067/how-to-find-the-physical-path-of-a-gsp-file-in-a-deployed-grails-application
     60                   return ApplicationHolder.application.parentContext.getResource(pathname).getFile()
     61           }
     62   }
    3963
    4064    /**
Note: See TracChangeset for help on using the changeset viewer.