source: trunk/grails-app/controllers/dbnp/studycapturing/FileController.groovy @ 1509

Last change on this file since 1509 was 1509, checked in by work@…, 9 years ago
  • gdt to 0.0.10
  • bgdt to 0.0.4
  • ajaxflow to 0.2.0
  • removed fileservice as that is in gdt
  • fixed 'species' field in bootstrapped templates to be a OntologyTerm? instead of a String
  • 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: 1509 $
12 * $Author: work@osx.eu $
13 * $Date: 2011-02-09 15:33:23 +0000 (wo, 09 feb 2011) $
14 */
15package dbnp.studycapturing
16
17import org.dbnp.gdt.FileService
18
19class FileController {
20    def fileService;
21
22    /**
23     * Returns the file that is asked for or a 404 error if the file doesn't exist
24     */
25    def get = {
26        def fileExists;
27
28                // Filename is not url decoded for some reason
29                def coder = new org.apache.commons.codec.net.URLCodec()
30                def filename = coder.decode(params.id)
31
32                // Security check to prevent accessing files in other directories
33                if( filename.contains( '..' ) ) {
34                        response.status = 500;
35                        render "Invalid filename given";
36                        return;
37                }
38               
39        try {
40            fileExists = fileService.fileExists( filename )
41        } catch( FileNotFoundException e ) {
42            fileExists = false;
43        }
44        if( !filename || !fileExists ) {
45            response.status = 404;
46            render( "File not found" );
47            return;
48        }
49        def file = fileService.get( filename );
50
51        //response.setContentType("application/octet-stream")
52        //response.setContentType( "image/jpeg" );
53
54        // Return the file
55                response.setHeader "Content-disposition", "attachment; filename=${filename}"
56        response.outputStream << file.newInputStream()
57                response.outputStream.flush()
58    }
59
60    /**
61     * Uploads a file and returns the filename under which the file is saved
62     */
63    def upload = {
64        def file = request.getFile( params.get( 'field' ) );
65
66        // If no file is uploaded, raise an error
67        if( file == null ) {
68            response.status = 500;
69            render( "" );
70        }
71
72        // If an old file exists, delete it
73        if( params.get( 'oldFile' ) ) {
74            fileService.delete( params.get( 'oldFile' ) );
75        }
76       
77        // Move the file to a upload dir
78        def newfilename = fileService.moveFileToUploadDir( file, file.getOriginalFilename() );
79
80        // Return data to the user
81        if( newfilename ) {
82            render( newfilename );
83        } else {
84            response.status = 500;
85            render( "" );
86        }
87    }
88}
Note: See TracBrowser for help on using the repository browser.