Created
October 10, 2011 14:23
-
-
Save kritzikratzi/1275443 to your computer and use it in GitHub Desktop.
How I could imagine a more useful blobs class ...
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Usage: | |
public class MyModel extends ...{ | |
@Reference | |
public static List<Blob2> images; | |
@Reference | |
public static Blob2 avatar; | |
} | |
Supporting classes: | |
/** | |
* You must think of blobs as immutable object. | |
* Once created you may only modify metadata, but the binary data remains the same. | |
* | |
* @author hansi | |
*/ | |
@Entity | |
public class Blob2 extends Model{ | |
// the original filename (guess this could be taken from the blob object) | |
public String filename; | |
// the original image data ... | |
// (this could be a gridfsdbobject, i'm just using blob because it's already in there) | |
public Blob data; | |
public Blob2(){ | |
} | |
public Blob2( InputStream body, String filename ){ | |
this.filename = filename; | |
data = new Blob( body, MimeTypes.getContentType( filename ) ); | |
} | |
} | |
package models; | |
import java.lang.annotation.Annotation; | |
import java.lang.reflect.Type; | |
import play.data.binding.Global; | |
import play.data.binding.TypeBinder; | |
/** | |
* And again the binder. | |
* This could be a little smarter, | |
* i.e. it could auto-create blobs when file uploads happen. | |
* | |
* @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 Blob2.findById( value ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment