source: trunk/grails-app/services/dbnp/studycapturing/FileService.groovy @ 1430

Last change on this file since 1430 was 1430, checked in by work@…, 10 years ago
  • set keyword expansion
  • Property svn:keywords set to Rev Author Date
File size: 4.9 KB
Line 
1/**
2 * FileService Service
3 *
4 * Contains methods for file uploads
5 *
6 * @author      Robert Horlings
7 * @since       20100521
8 * @package     dbnp.studycapturing
9 *
10 * Revision information:
11 * $Rev: 1430 $
12 * $Author: work@osx.eu $
13 * $Date: 2011-01-21 20:05:36 +0000 (vr, 21 jan 2011) $
14 */
15package dbnp.studycapturing
16
17import org.codehaus.groovy.grails.commons.ApplicationHolder
18
19class FileService implements Serializable {
20    // ApplicationContext applicationContext
21
22    // Must be false, since the webflow can't use a transactional service. See
23    // http://www.grails.org/WebFlow for more information
24    static transactional = false
25
26    /**
27     * Returns the directory for uploading files. Makes it easy to change the
28     * path to the directory, if needed
29     */
30    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()
38    }
39
40    /**
41     * Returns as File object to a given file
42     */
43    def File get( String filename ) {
44        return new File( getUploadDir(), filename );
45    }
46
47    /**
48     * Check whether the given file exists in the upload directory
49     */
50    def boolean fileExists( String filename ) {
51        return new File( getUploadDir(), filename ).exists();
52    }
53
54    /**
55     * Deletes a file in the upload dir, if it exists
56     */
57    def boolean delete( String filename ) {
58        def f = new File( getUploadDir(), filename );
59        if( f.exists() ) {
60            f.delete();
61        }
62    }
63
64    /**
65     * Moves the given file to the upload directory.
66     *
67     * @return Filename given to the file on our system or "" if the moving fails
68     */
69    def String moveFileToUploadDir( File file, String originalFilename ) {
70        try {
71            if( file.exists() ) {
72                def newFilename = getUniqueFilename( originalFilename );
73                file.renameTo( new File( getUploadDir(), newFilename ))
74                return newFilename
75            } else {
76                return "";
77            }
78        } catch(Exception exception) {
79            throw exception; // return ""
80        }
81    }
82
83    /**
84     * Moves the given uploaded file to the upload directory
85     *
86     * MultipartFile is the class used for uploaded files
87     *
88     * @return Filename given to the file on our system or "" if the moving fails
89     */
90    def String moveFileToUploadDir( org.springframework.web.multipart.MultipartFile file, String originalFilename ) {
91        try {
92            def newFilename = getUniqueFilename( originalFilename );
93            file.transferTo( new File( getUploadDir(), newFilename ))
94            return newFilename
95        } catch(Exception exception) {
96            throw exception; // return ""
97        }
98    }
99
100    /**
101     * Moves the given file to the upload directory.
102     *
103     * @return Filename given to the file on our system or "" if the moving fails
104     */
105    def String moveFileToUploadDir( File file ) {
106        moveFileToUploadDir( file, file.getName() );
107    }
108
109    /**
110     * Moves the given uploaded file to the upload directory
111     *
112     * MultipartFile is the class used for uploaded files
113     *
114     * @return Filename given to the file on our system or "" if the moving fails
115     */
116    def String moveFileToUploadDir( org.springframework.web.multipart.MultipartFile file ) {
117        moveFileToUploadDir( file, file.getOriginalFilename() );
118    }
119
120    /**
121     * Returns a filename that looks like the originalFilename and does not yet
122     * exist in the upload directory.
123     *
124     * @return String filename that does not yet exist in the upload directory
125     */
126    def String getUniqueFilename( String originalFilename ) {
127        if( fileExists( originalFilename ) ) {
128            def basename;
129            def extension;
130           
131            // Split the filename into basename and extension
132            if( originalFilename.lastIndexOf('.') >= 0 ) {
133                basename = originalFilename[ 0 .. originalFilename.lastIndexOf('.') - 1 ];
134                extension = originalFilename[ originalFilename.lastIndexOf('.')..originalFilename.size() - 1];
135            } else {
136                basename = originalFilename;
137                extension = '';
138            }
139
140            // Find a filename that does not yet exist
141            def postfix = 0;
142            def newFilename = basename + postfix + extension;
143            while( fileExists( newFilename ) ) {
144                postfix++;
145                newFilename = basename + postfix + extension;
146            }
147
148            return newFilename;
149        } else {
150            return originalFilename;
151        }
152    }
153
154}
Note: See TracBrowser for help on using the repository browser.