Changeset 1182 for trunk/grails-app


Ignore:
Timestamp:
Nov 22, 2010, 5:27:23 PM (13 years ago)
Author:
robert@…
Message:

Improved file upload fields so users can delete existing files and are able to access uploaded files (using a tag in the tag library). See ticket #17

Location:
trunk/grails-app
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/grails-app/controllers/dbnp/studycapturing/FileController.groovy

    r959 r1182  
    2323     */
    2424    def get = {
    25         // Check whether the file exists
    26         def filename = params.id;
    2725        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               
    2838        try {
    2939            fileExists = fileService.fileExists( filename )
     
    3343        if( !filename || !fileExists ) {
    3444            response.status = 404;
    35             render( "" );
     45            render( "File not found" );
    3646            return;
    3747        }
     
    4252
    4353        // Return the file
     54                response.setHeader "Content-disposition", "attachment; filename=${filename}"
    4455        response.outputStream << file.newInputStream()
     56                response.outputStream.flush()
    4557    }
    4658
     
    7385        }
    7486    }
     87
     88
    7589}
  • trunk/grails-app/domain/dbnp/studycapturing/TemplateEntity.groovy

    r1175 r1182  
    450450                        return getStore(field.type)[fieldName]
    451451                }
    452 
    453452        }
    454453
     
    549548                // Magic setter for files: handle values for file fields
    550549                //
    551                 // If NULL is given, the field value is emptied and the old file is removed
     550                // If NULL is given or "*deleted*", the field value is emptied and the old file is removed
    552551                // If an empty string is given, the field value is kept as was
    553552                // If a file is given, it is moved to the right directory. Old files are deleted. If
     
    559558                        def currentFile = getFieldValue(field.name);
    560559
    561                         if (value == null) {
     560                        if (value == null || ( value.class == String && value == '*deleted*' ) ) {
    562561                                // If NULL is given, the field value is emptied and the old file is removed
    563562                                value = "";
  • trunk/grails-app/taglib/dbnp/studycapturing/WizardTagLib.groovy

    r1166 r1182  
    793793                out << '<input type="hidden" name="' + attrs.name + '" id="' + attrs.name + '" value="' + attrs.value + '">';
    794794                out << '<div id="' + attrs.name + 'Example" class="upload_info"></div>';
     795                out << '<a id="' + attrs.name + 'Delete" class="upload_del" href="#" onClick="if( confirm( \'Are you sure to delete this file?\' ) ) { deleteFile( \'' + attrs.name + '\' ); } return false;"><img src="' + resource( dir: 'images/icons', file: 'delete.png', plugin: 'famfamfam' ) + '"></a>';
    795796                out << '<script type="text/javascript">';
    796797                out << '  $(document).ready( function() { ';
     
    798799                out << '    fileUploadField( "' + attrs.name + '" );';
    799800                out << '    if( filename != "" ) {';
     801                out << '      $("#' + attrs.name + 'Delete").show();';
    800802                out << '      $("#' + attrs.name + 'Example").html("Current file: " + createFileHTML( filename ) )';
    801803                out << '    }';
     
    14321434        }
    14331435
     1436        def showTemplateField = { attrs, body ->
     1437                def field = attrs.get( 'field' );
     1438                def entity = attrs.get( 'entity' );
     1439                def fieldName = '';
     1440                def fieldType = '';
     1441               
     1442                if( entity ) {
     1443                        if( field instanceof String ) {
     1444                                fieldName = field;
     1445                                fieldType = '';
     1446                        } else if( field instanceof TemplateField ) {
     1447                                fieldName = field.name
     1448                                fieldType = field.type.toString();
     1449                        } else {
     1450                                return;
     1451                        }
     1452
     1453                        def value = entity.getFieldValue( fieldName );
     1454
     1455                        if( fieldType == 'FILE' && value != "" ) {
     1456                          out << '<a href="' + g.createLink( controller: "file", action: "get",  id: value ) + '">' + value + '</a>';
     1457                        } else {
     1458                                out << value;
     1459                        }
     1460
     1461                }
     1462        }
     1463
    14341464}
  • trunk/grails-app/views/study/show.gsp

    r1036 r1182  
    242242                    <td>
    243243                                          <g:if test="${studyInstance.fieldExists(field.name)}">
    244                                                 ${studyInstance.getFieldValue(field.name)}
     244                                                <wizard:showTemplateField field="${field}" entity="${studyInstance}" />
    245245                                          </g:if>
    246246                                          <g:else>
     
    411411                    </g:if>
    412412                    <g:each in="${subject.giveDomainFields()}" var="field">
    413                       <td>${subject.getFieldValue(field.name)}</td>
     413                      <td><wizard:showTemplateField field="${field}" entity="${subject}" /></td>
    414414                    </g:each>
    415415                 
     
    417417                      <td>
    418418                        <g:if test="${subject.fieldExists(field.name)}">
    419                           ${subject.getFieldValue(field.name)}
     419                                <wizard:showTemplateField field="${field}" entity="${subject}" />
    420420                        </g:if>
    421421                        <g:else>
     
    495495                          <g:if test="${event.getFieldValue(field.name)}">
    496496                            <g:if test="${fieldCounter > 1}">, </g:if>
    497                               ${field.name} = ${event.getFieldValue( field.name )}
     497                              ${field.name} = <wizard:showTemplateField field="${field}" entity="${event}" />
    498498                            <g:set var="fieldCounter" value="${fieldCounter + 1}" />
    499499                          </g:if>
     
    583583                              <g:if test="${event.getFieldValue(field.name)}">
    584584                                <g:if test="${fieldCounter > 1}">, </g:if>
    585                                   ${field.name} = ${event.getFieldValue( field.name )}
     585                                  ${field.name} = <wizard:showTemplateField field="${field}" entity="${event}" />
    586586                                <g:set var="fieldCounter" value="${fieldCounter + 1}" />
    587587                              </g:if>
     
    744744                          <td>${sample.parentEvent?.template?.name} at ${sample.parentEvent?.getStartTimeString()}</td>
    745745                    <g:each in="${sample.giveDomainFields()}" var="field">
    746                       <td>${sample.getFieldValue(field.name)}</td>
     746                      <td><wizard:showTemplateField field="${field}" entity="${sample}" /></td>
    747747                    </g:each>
    748748
     
    750750                      <td>
    751751                        <g:if test="${sample.fieldExists(field.name)}">
    752                           ${sample.getFieldValue(field.name)}
     752                          <wizard:showTemplateField field="${field}" entity="${sample}" />
    753753                        </g:if>
    754754                        <g:else>
Note: See TracChangeset for help on using the changeset viewer.