source: trunk/grails-app/controllers/dbnp/studycapturing/FileController.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: 2.2 KB
Line 
1/**
2 * FileController
3 *
4 * Handles file uploads and downloads
5 *
6 * @author      Robert Horlings
7 * @since       20100601
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
17class FileController {
18
19    def fileService;
20
21    /**
22     * Returns the file that is asked for or a 404 error if the file doesn't exist
23     */
24    def get = {
25        def fileExists;
26
27                // Filename is not url decoded for some reason
28                def coder = new org.apache.commons.codec.net.URLCodec()
29                def filename = coder.decode(params.id)
30
31                // Security check to prevent accessing files in other directories
32                if( filename.contains( '..' ) ) {
33                        response.status = 500;
34                        render "Invalid filename given";
35                        return;
36                }
37               
38        try {
39            fileExists = fileService.fileExists( filename )
40        } catch( FileNotFoundException e ) {
41            fileExists = false;
42        }
43        if( !filename || !fileExists ) {
44            response.status = 404;
45            render( "File not found" );
46            return;
47        }
48        def file = fileService.get( filename );
49
50        //response.setContentType("application/octet-stream")
51        //response.setContentType( "image/jpeg" );
52
53        // Return the file
54                response.setHeader "Content-disposition", "attachment; filename=${filename}"
55        response.outputStream << file.newInputStream()
56                response.outputStream.flush()
57    }
58
59    /**
60     * Uploads a file and returns the filename under which the file is saved
61     */
62    def upload = {
63        def file = request.getFile( params.get( 'field' ) );
64
65        // If no file is uploaded, raise an error
66        if( file == null ) {
67            response.status = 500;
68            render( "" );
69        }
70
71        // If an old file exists, delete it
72        if( params.get( 'oldFile' ) ) {
73            fileService.delete( params.get( 'oldFile' ) );
74        }
75       
76        // Move the file to a upload dir
77        def newfilename = fileService.moveFileToUploadDir( file, file.getOriginalFilename() );
78
79        // Return data to the user
80        if( newfilename ) {
81            render( newfilename );
82        } else {
83            response.status = 500;
84            render( "" );
85        }
86    }
87
88
89}
Note: See TracBrowser for help on using the repository browser.