Skip to content

Instantly share code, notes, and snippets.

@kritzikratzi
Created October 10, 2011 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save kritzikratzi/1275419 to your computer and use it in GitHub Desktop.
Save kritzikratzi/1275419 to your computer and use it in GitHub Desktop.
Another way to use play-morphia blobs
package models;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import javax.imageio.ImageIO;
import play.libs.MimeTypes;
import play.modules.morphia.Blob;
import play.modules.morphia.Model;
import play.modules.morphia.MorphiaPlugin;
import utils.ImageUtils;
import com.google.code.morphia.annotations.Entity;
import com.mongodb.gridfs.GridFSDBFile;
import com.mongodb.gridfs.GridFSInputFile;
/**
* You must think of ImageBlob of an immutable object.
* Once created you cannot really modify it.
*
* @author hansi
*/
@Entity
public class ImageBlob extends Model{
// the original filename
public String filename;
// the original image data ...
public Blob data;
public ImageBlob(){
}
public ImageBlob( InputStream body, String filename ){
this.filename = filename;
data = new Blob( body, MimeTypes.getContentType( filename ) );
}
public GridFSDBFile asAvatar(){
// Is there already a file for this?
String filename = getId().toString() + "_avatar";
GridFSDBFile avatar = MorphiaPlugin.gridFs().findOne( filename );
if( avatar != null ){
return avatar;
}
else{
try {
BufferedImage originalImage = ImageIO.read( data.getGridFSFile().getInputStream() );
BufferedImage avatarImage = ImageUtils.generateAvatar( originalImage );
InputStream in = ImageUtils.toInputStream( avatarImage, "png" );
GridFSInputFile input = MorphiaPlugin.gridFs().createFile( in, filename );
System.out.println( "name = " + filename );
input.save();
return MorphiaPlugin.gridFs().findOne( filename );
}
catch (IOException e) {
e.printStackTrace();
return null;
}
}
}
public GridFSDBFile asPanorama(){
// Is there already a file for this?
String filename = getId().toString() + "_panorama";
GridFSDBFile panorama = MorphiaPlugin.gridFs().findOne( filename );
if( panorama != null ){
return panorama;
}
else{
try {
BufferedImage originalImage = ImageIO.read( data.getGridFSFile().getInputStream() );
BufferedImage avatarImage = ImageUtils.fit( originalImage, 500, 350 );
InputStream in = ImageUtils.toInputStream( avatarImage, "jpg" );
GridFSInputFile input = MorphiaPlugin.gridFs().createFile( in, filename );
System.out.println( "name = " + filename );
input.save();
return MorphiaPlugin.gridFs().findOne( filename );
}
catch (IOException e) {
e.printStackTrace();
return null;
}
}
}
}
package models;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import play.data.binding.Global;
import play.data.binding.TypeBinder;
/**
* This is a HUGE HUGE help!
* It allows you to pass in morphia references to image blobs via get/post parameters,
* e.g. you could pass
* http://host:9000/my/action?obj.title=test&obj.images=4e91eb53804cc3de71a46703&obj.images=4e91edcd804cc3de71a46711
*
* then the vcard should be properly initiated with a list of imageblobs
*
* @author hansi
*
*/
@Global
public class ImageBlobBinder implements TypeBinder<ImageBlob>{
@Override
public Object bind(String name, Annotation[] annotations, String value,
Class actualClass, Type genericType) throws Exception {
return ImageBlob.findById( value );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment