Search This Blog

Showing posts with label Grails. Show all posts
Showing posts with label Grails. Show all posts

Thursday, November 18, 2010

Grails: Storing Uploaded Files with Unique Names

This is how I was able to store uploaded files in a project directory rather than a blob in a database, as well as provide unique file names for each file by providing hash names.

Within my save action of the class with a property for the uploaded file

//handle uploaded file
def uploadedFile = request.getFile('file')
if(!uploadedFile.empty){
println "Class: ${uploadedFile.class}"
println "Name: ${uploadedFile.name}"
println "OriginalFileName: ${uploadedFile.originalFilename}"
println "Size: ${uploadedFile.size}"
println "ContentType: ${uploadedFile.contentType}"

//Creates hashed fileName using 128bit identifier
def splitName = uploadedFile.originalFilename.split("\\.") <--Splits the name of the file and the file type def hashedName = java.util.UUID.randomUUID().toString() def newFileName = hashedName + splitName[1].toString() //Sets the directory of the file
def dir = servletContext.getRealPath("/")
def userDir = new File(dir, "/uploaded/files")
userDir.mkdirs()
uploadedFile.transferTo( new File( userDir, newFileName))

Wednesday, November 17, 2010

Installing Groovy/Grails on MAC OSX Snow Leopard

Installing Groovy/Grails on MAC OSX Snow Leopard

Do this through terminal:
Install Groovy:
$ sudo mv groovy-1.x.x /usr/share
$ cd /usr/share
$ sudo chmod 0755 groovy-1.x.x/bin/*
$ sudo ln -s groovy-1.x.x groovy


Install Grails:
$ sudo mv grails-1.x.x /usr/share
$ cd /usr/share
$ sudo chmod 0755 grails-1.x.x/bin/*
$ sudo ln -s grails-1.x.x grails


Edit or create your ~/.profile file:

To create a .profile use the command pico .profile
export GROOVY_HOME=/usr/share/groovy
export GRAILS_HOME=/usr/share/grails
export PATH=${PATH}:$GROOVY_HOME/bin
export PATH=${PATH}:$GRAILS_HOME/bin

Reload your profile:
$ . ~/.profile
or
$ source ~/.profile.