Pregunta

Im trying to implement some pics upload to my webapp. The problem im having is that im trying to use Imagescalr to create thumbs of the original photos, and Im using AWS Plugin to upload them to my bucket. So, the code i've is the following: (deleted validations and things that dont influence the question/posible answer)

def uploadPic() {
def f = request.getFile('file')
.   
.
.       
def s3file = f.inputStream.s3upload(filename) { //this is for the normal photo
            path "POI/ID/"
    }

def imageIn = ImageIO.read(???); //Dont know if I can put the f file here as parameter... do I have to store it somewhere first, call the s3 file, or I can resize on - the - fly?
BufferedImage scaledImage = Scalr.resize(imageIn, 150);



//Here I should upload the thumb. How can I call something like what is done for the normal photo? 

So the problems/questions are explained along the code, hope someone knows how to do this. Thanks in advance.

¿Fue útil?

Solución

in Grails, request.getFile() doesn't return a java.io.File object. You could use the input stream to write out a file but I'd probably do something like this, although I'd use services and break things up a little more. But this should get you started in the right direction. Consider this more of a pseudo code workflow suggestion.

def uploadPic() {

   def f = request.getFile('file')
   def tempFile = new File('/some/local/dir/myImage.png') 
   f.transferTo(tempFile)

   // upload the original image to S3
   whateverApi.s3Upload(tempFile)

   def bufferedImage = ImageIO.read(tempFile)
   def scaledBufferedImage = Scalr.resize(bufferedImage, 150)

   // write the scaledImg to disk
   def scaledImage = new File('/some/local/dir/myImage-150.png');
   ImageIO.write(scaledBufferedImage, "png", scaledImage);

   //upload scaled image to S3
   whateverApi.s3Upload(scaledImage)

   // clean up
   tempFile.delete()
   scaledImage.delete()

}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top