/src/com/ilove/api/app/Photo.java

https://github.com/placido/iLoveIt-Server · Java · 147 lines · 110 code · 21 blank · 16 comment · 11 complexity · c98bb7af7c23d6840ea2fff9d07fb040 MD5 · raw file

  1. package com.ilove.api.app;
  2. import java.io.File;
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.io.PrintWriter;
  6. import java.util.Date;
  7. import java.util.Iterator;
  8. import java.util.List;
  9. import javax.servlet.ServletException;
  10. import javax.servlet.annotation.WebServlet;
  11. import javax.servlet.http.HttpServlet;
  12. import javax.servlet.http.HttpServletRequest;
  13. import javax.servlet.http.HttpServletResponse;
  14. import org.apache.commons.fileupload.FileItem;
  15. import org.apache.commons.fileupload.FileUploadException;
  16. import org.apache.commons.fileupload.ProgressListener;
  17. import org.apache.commons.fileupload.disk.DiskFileItemFactory;
  18. import org.apache.commons.fileupload.servlet.ServletFileUpload;
  19. import org.bson.types.ObjectId;
  20. import com.ilove.aws.S3MyObject;
  21. import com.ilove.aws.S3StorageManager;
  22. import com.ilove.util.ImageUtil;
  23. import com.mongodb.BasicDBObject;
  24. import com.mongodb.DB;
  25. import com.mongodb.DBCollection;
  26. import com.mongodb.DBCursor;
  27. import com.mongodb.Mongo;
  28. @WebServlet("/api/app/photo")
  29. public class Photo extends HttpServlet {
  30. private static final long serialVersionUID = 1L;
  31. public Photo() {
  32. super();
  33. }
  34. // POST: upload a new photo
  35. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  36. long maxRequestSize = 5000000;
  37. int maxMemorySize = 5000000;
  38. // TO DO: Set correct max size and tmp folder
  39. String tmpDirPath = "C:\\tmp\\ilove\\tmp";
  40. PrintWriter out = null;
  41. Mongo m = null;
  42. response.setContentType("application/json");
  43. try {
  44. out = response.getWriter();
  45. // Insert record in DB with timestamp and get the ID
  46. m = new Mongo( "localhost" , 27017 );
  47. DB db = m.getDB( "test" );
  48. Date now = new Date();
  49. BasicDBObject time = new BasicDBObject("ts", now);
  50. DBCollection coll = db.getCollection("photos");
  51. coll.insert(time);
  52. ObjectId id = (ObjectId)time.get( "_id" );
  53. String dbId = id.toString();
  54. // Parse the request
  55. DiskFileItemFactory factory = new DiskFileItemFactory(maxMemorySize, new File(tmpDirPath));
  56. ServletFileUpload upload = new ServletFileUpload(factory);
  57. upload.setSizeMax(maxRequestSize);
  58. List items = upload.parseRequest(request);
  59. // Process the uploaded items
  60. Iterator iter = items.iterator();
  61. double[] location = new double[2];
  62. String caption = null;
  63. String userId = null;
  64. while (iter.hasNext()) {
  65. FileItem item = (FileItem) iter.next();
  66. if (item.isFormField()) {
  67. // Standard form element
  68. // out.println("Standard form element "+item.getFieldName()+"="+item.getString());
  69. if (item.getFieldName().equals("lat")) {
  70. location[0] = Double.parseDouble(item.getString());
  71. }
  72. if (item.getFieldName().equals("lng")) {
  73. location[1] = Double.parseDouble(item.getString());
  74. }
  75. if (item.getFieldName().equals("caption")) {
  76. caption = item.getString();
  77. }
  78. if (item.getFieldName().equals("user")) {
  79. userId = item.getString();
  80. }
  81. } else {
  82. // Upload form element
  83. // out.println("Upload form element "+item.getName()+" ("+item.getSize()+" bytes, "+item.getContentType()+")");
  84. InputStream uploadedStream = item.getInputStream();
  85. byte [] photoData = new byte[(int)item.getSize()];
  86. uploadedStream.read(photoData, 0, (int)item.getSize());
  87. uploadedStream.close();
  88. // Store on S3
  89. S3MyObject obj = new S3MyObject(photoData, "neighbourhoods", "uploads/"+dbId+"/original.jpeg", item.getContentType());
  90. S3StorageManager mgr = new S3StorageManager();
  91. mgr.storePublicRead(obj, false);
  92. // Crop
  93. byte [] cropped = ImageUtil.cropPhoto(photoData);
  94. // Resize small and store on S3
  95. byte [] small = ImageUtil.scalePhoto(160, cropped);
  96. obj = new S3MyObject(small, "neighbourhoods", "uploads/"+dbId+"/small.jpeg", item.getContentType());
  97. mgr.storePublicRead(obj, false);
  98. // Resize large and store on S3
  99. byte [] large = ImageUtil.scalePhoto(320, cropped);
  100. obj = new S3MyObject(large, "neighbourhoods", "uploads/"+dbId+"/large.jpeg", item.getContentType());
  101. mgr.storePublicRead(obj, false);
  102. }
  103. }
  104. // update the record
  105. BasicDBObject docToUpdate = new BasicDBObject();
  106. docToUpdate.put("_id", new org.bson.types.ObjectId(dbId));
  107. BasicDBObject docUpdate = new BasicDBObject();
  108. docUpdate.put("_id", new org.bson.types.ObjectId(dbId));
  109. docUpdate.put("ts", now);
  110. docUpdate.put("caption", caption);
  111. docUpdate.put("user", userId);
  112. docUpdate.put("location", location);
  113. coll.update(docToUpdate, docUpdate);
  114. out.println(docUpdate.toString());
  115. } catch (Exception e) {
  116. // TO DO: remove from database the photos record placeholder with timestamp
  117. // TO DO: remove from S3
  118. out.println("{ \"error\": \""+e+"\" }");
  119. } finally {
  120. if (out != null) {
  121. out.close();
  122. }
  123. if (m != null) m.close();
  124. }
  125. }
  126. }