I needed a secured solution that was made in combination of http://grails.asia/grails-example-application-simple-document-management-system and http://grails.asia/grails-render-images-on-the-fly-in-gsp . For that purpouse i used a Domain where i store the path of images, a gsp to show the images and a Controller to serve the images
Domain:
class Document {
String filename
String fullPath
Date uploadDate = new Date()
static constraints = {
filename(blank:false,nullable:false)
fullPath(blank:false,nullable:false)
}
}
Grails Server Page:
Document List
Document List
${documentInstance.filename}
Controller:
import org.springframework.security.access.annotation.Secured
class DoclistController {
@Secured(['ROLE_ADMIN'])
def list(){
params.max = 10
[documentInstanceList: Document.list(params), documentInstanceTotal: Document.count()]
//render view: 'list'
}
@Secured(['ROLE_ADMIN'])
def images(long id){
Document documentInstance = Document.get(id)
if ( documentInstance == null) {
flash.message = "Document not found."
redirect (action:'list')
} else {
def file = new File(documentInstance.fullPath)
def fileInputStream = new FileInputStream(file)
def outputStream = response.getOutputStream()
byte[] buffer = new byte[4096];
int len;
while ((len = fileInputStream.read(buffer)) > 0) {
outputStream.write(buffer, 0, len);
}
outputStream.flush()
outputStream.close()
fileInputStream.close()
}
}
}
I show the images of database as follows
Hope this helps