Permalink
Browse files

Code improvements, and DateBlob for attachment subdirectories

  • Loading branch information...
1 parent 854519c commit 16153c141f4c691047fac20c1bd79ebdb21935ce Peter Hilton committed Apr 28, 2011
Showing with 212 additions and 8 deletions.
  1. +1 −0 .gitignore
  2. +18 −4 app/controllers/Application.java
  3. +156 −0 app/models/DateBlob.java
  4. +10 −2 app/models/User.java
  5. +23 −2 app/views/Application/index.html
  6. +2 −0 conf/application.conf
  7. +2 −0 conf/routes
View
@@ -2,3 +2,4 @@
attachments
tmp
*.iml
+photos
@@ -1,5 +1,6 @@
package controllers;
+import models.DateBlob;
import models.User;
import play.db.jpa.Blob;
import play.libs.MimeTypes;
@@ -22,14 +23,15 @@ public static void addUser(User user) {
public static void userPhoto(long id) {
final User user = User.findById(id);
+ notFoundIfNull(user);
response.setContentTypeIfNotSet(user.photo.type());
renderBinary(user.photo.get());
}
public static void addUserWithFileName(File photo) throws FileNotFoundException {
- final User user = new User();
+ final User user = new User();
user.photoFileName = photo.getName();
- user.photo = new Blob();
+ user.photo = new DateBlob();
user.photo.set(new FileInputStream(photo), MimeTypes.getContentType(photo.getName()));
user.save();
index();
@@ -38,8 +40,20 @@ public static void addUserWithFileName(File photo) throws FileNotFoundException
public static void downloadUserPhoto(long id) {
final User user = User.findById(id);
response.setContentTypeIfNotSet(user.photo.type());
- response.setHeader("Content-Disposition", "attachment; filename=\"" + user.photoFileName + "\"");
- renderBinary(user.photo.get());
+ renderBinary(user.photo.get(), user.photoFileName);
+ }
+
+ public static void updateUser(User user) {
+ user.save();
+ index();
}
+ public static void deleteUser(long id) {
+ final User user = User.findById(id);
+// user.photo.getFile().delete();
+ user.delete();
+ index();
+ }
+
+
}
View
@@ -0,0 +1,156 @@
+package models;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.InputStream;
+import java.io.Serializable;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Types;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+
+import org.hibernate.HibernateException;
+import org.hibernate.type.StringType;
+import org.hibernate.usertype.UserType;
+
+import play.Logger;
+import play.Play;
+import play.db.Model.BinaryField;
+import play.exceptions.UnexpectedException;
+import play.libs.Codec;
+import play.libs.IO;
+
+public class DateBlob implements BinaryField, UserType {
+
+ private String path;
+ private String type;
+ private File file;
+
+ public DateBlob() {}
+
+ private DateBlob(String path, String type) {
+ this.path = path;
+ this.type = type;
+ }
+
+ public InputStream get() {
+ if(exists()) {
+ try {
+ return new FileInputStream(getFile());
+ } catch(Exception e) {
+ throw new UnexpectedException(e);
+ }
+ }
+ return null;
+ }
+
+ // Make a copy of play.db.jpa.Blob, rename the 'UUID' field to 'path', and replace the set method with this.
+ public void set(InputStream is, String type) {
+ final String datePath = new SimpleDateFormat("/yyyy/MM/dd/").format(new Date());
+ final File dateDirectory = new File(getStore(), datePath);
+ dateDirectory.mkdirs();
+ this.path = datePath + Codec.UUID();
+ this.type = type;
+ IO.write(is, getFile());
+ }
+
+ public long length() {
+ return getFile().length();
+ }
+
+ public String type() {
+ return type;
+ }
+
+ public boolean exists() {
+ return path != null && getFile().exists();
+ }
+
+ public File getFile() {
+ if(file == null) {
+ file = new File(getStore(), path);
+ }
+ Logger.info("PATH %s", file.getAbsolutePath());
+ return file;
+ }
+
+ //
+
+ public int[] sqlTypes() {
+ return new int[] {Types.VARCHAR};
+ }
+
+ public Class returnedClass() {
+ return DateBlob.class;
+ }
+
+ public boolean equals(Object o, Object o1) throws HibernateException {
+ return o == null ? false : o.equals(o1);
+ }
+
+ public int hashCode(Object o) throws HibernateException {
+ return o.hashCode();
+ }
+
+ public Object nullSafeGet(ResultSet rs, String[] names, Object o) throws HibernateException, SQLException {
+ String val = (String) StringType.INSTANCE.nullSafeGet(rs, names[0]);
+ if(val == null || val.length() == 0 || !val.contains("|")) {
+ return new DateBlob();
+ }
+ return new DateBlob(val.split("[|]")[0], val.split("[|]")[1]);
+ }
+
+ public void nullSafeSet(PreparedStatement ps, Object o, int i) throws HibernateException, SQLException {
+ if(o != null) {
+ ps.setString(i, ((DateBlob)o).path + "|" + ((DateBlob)o).type);
+ } else {
+ ps.setNull(i, Types.VARCHAR);
+ }
+ }
+
+ public Object deepCopy(Object o) throws HibernateException {
+ if(o == null) {
+ return null;
+ }
+ return new DateBlob(this.path, this.type);
+ }
+
+ public boolean isMutable() {
+ return true;
+ }
+
+ public Serializable disassemble(Object o) throws HibernateException {
+ throw new UnsupportedOperationException("Not supported yet.");
+ }
+
+ public Object assemble(Serializable srlzbl, Object o) throws HibernateException {
+ throw new UnsupportedOperationException("Not supported yet.");
+ }
+
+ public Object replace(Object o, Object o1, Object o2) throws HibernateException {
+ throw new UnsupportedOperationException("Not supported yet.");
+ }
+
+ //
+
+ public static String getUUID(String dbValue) {
+ return dbValue.split("[|]")[0];
+ }
+
+ public static File getStore() {
+ String name = Play.configuration.getProperty("attachments.path", "attachments");
+ File store = null;
+ if(new File(name).isAbsolute()) {
+ store = new File(name);
+ } else {
+ store = Play.getFile(name);
+ }
+ if(!store.exists()) {
+ store.mkdirs();
+ }
+ return store;
+ }
+
+}
View
@@ -4,10 +4,18 @@
import play.db.jpa.Model;
import javax.persistence.Entity;
+import java.io.File;
@Entity
public class User extends Model {
- public String photoFileName;
- public Blob photo;
+ public String photoFileName;
+// public Blob photo;
+ public DateBlob photo;
+
+ @Override
+ public void _delete() {
+ super._delete();
+ photo.getFile().delete();
+ }
}
@@ -1,22 +1,43 @@
#{extends 'main.html' /}
#{set title:'Home' /}
+
+<h1>List</h1>
<p>
#{list items:models.User.findAll(), as:'user'}
<a href="@{downloadUserPhoto(user.id)}"><img title="${user.photoFileName}" src="@{Application.userPhoto(user.id)}"></a>
#{/list}
</p>
+<h1>Add user</h1>
#{form @Application.addUser(), enctype:'multipart/form-data'}
<p>
- <input id="photo" type="file" name="user.photo">
+ <input type="file" name="user.photo">
<input type="submit" name="submit" value="Upload">
</p>
#{/form}
+<h1>Add user with file name</h1>
#{form @addUserWithFileName(), enctype:'multipart/form-data'}
<p>
- <input id="photo" type="file" name="photo">
+ <input type="file" name="photo">
<input type="submit" name="submit" value="Upload">
</p>
#{/form}
+
+<h1>Update user</h1>
+#{form @updateUser(), enctype:'multipart/form-data'}
+ <p>
+ #{select 'user.id', items:models.User.findAll(), valueProperty:'id', labelProperty:'id' /}
+ <input type="file" name="user.photo">
+ <input type="submit" name="submit" value="Update User">
+ </p>
+#{/form}
+
+<h1>Delete user</h1>
+#{form @deleteUser()}
+ <p>
+ #{select 'id', items:models.User.findAll(), valueProperty:'id', labelProperty:'id' /}
+ <input type="submit" name="submit" value="Delete">
+ </p>
+#{/form}
View
@@ -2,6 +2,8 @@ application.name=blob
application.mode=dev
application.secret=ngiQiLsL54WYp18lXqg6QRqWEAy0sY2zuJRF1XXupHzSMAn42sTmbx2IXqs4lKFp
+attachments.path=photos
+
date.format=yyyy-MM-dd
db=mem
View
@@ -1,7 +1,9 @@
GET / Application.index
POST / Application.addUser
+POST /user Application.updateUser
POST /name Application.addUserWithFileName
GET /photo/{id} Application.userPhoto
+DELETE /user Application.deleteUser
GET /photo/{id}/download Application.downloadUserPhoto
GET /public/ staticDir:public

0 comments on commit 16153c1

Please sign in to comment.