Post process uploaded images in order to fix orientation
[gpl/argeo-suite.git] / org.argeo.app.servlet.odk / src / org / argeo / app / servlet / odk / OdkSubmissionServlet.java
index 5efbd1b689fb4b518a65f0e0c5d429a8a9a5124a..a5864d83336206d6e81c7229c05aea3febb3ef97 100644 (file)
@@ -1,6 +1,8 @@
 package org.argeo.app.servlet.odk;
 
 import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
 import java.time.Instant;
 import java.time.ZoneId;
 import java.time.ZoneOffset;
@@ -21,11 +23,12 @@ import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 import javax.servlet.http.Part;
 
+import org.argeo.api.cms.CmsLog;
 import org.argeo.api.cms.CmsSession;
 import org.argeo.app.core.SuiteUtils;
+import org.argeo.app.image.ImageProcessor;
 import org.argeo.app.odk.OrxType;
 import org.argeo.app.xforms.FormSubmissionListener;
-import org.argeo.api.cms.CmsLog;
 import org.argeo.cms.auth.RemoteAuthRequest;
 import org.argeo.cms.auth.RemoteAuthUtils;
 import org.argeo.cms.jcr.CmsJcrUtils;
@@ -58,12 +61,8 @@ public class OdkSubmissionServlet extends HttpServlet {
                Session session = RemoteAuthUtils.doAs(() -> Jcr.login(repository, null), request);
 
                try {
-//                     Node submissions = JcrUtils.mkdirs(session,
-//                                     "/" + EntityType.form.get() + "/" + EntityNames.SUBMISSIONS_BASE);
                        CmsSession cmsSession = RemoteAuthUtils.getCmsSession(request);
 
-//                     ClassLoader currentContextCl = Thread.currentThread().getContextClassLoader();
-//                     Thread.currentThread().setContextClassLoader(CmsJcrUtils.class.getClassLoader());
                        Session adminSession = null;
                        try {
                                // TODO centralise at a deeper level
@@ -71,7 +70,6 @@ public class OdkSubmissionServlet extends HttpServlet {
                                SuiteUtils.getOrCreateCmsSessionNode(adminSession, cmsSession);
                        } finally {
                                Jcr.logout(adminSession);
-//                             Thread.currentThread().setContextClassLoader(currentContextCl);
                        }
 
                        Node cmsSessionNode = SuiteUtils.getCmsSessionNode(session, cmsSession);
@@ -87,7 +85,22 @@ public class OdkSubmissionServlet extends HttpServlet {
                                                        ImportUUIDBehavior.IMPORT_UUID_COLLISION_REPLACE_EXISTING);
 
                                } else {
-                                       Node fileNode = JcrUtils.copyStreamAsFile(submission, part.getName(), part.getInputStream());
+                                       Node fileNode;
+                                       if (part.getName().endsWith(".jpg")) {
+                                               // Fix metadata
+                                               Path temp = Files.createTempFile("image", ".jpg");
+                                               try {
+                                                       ImageProcessor imageProcessor = new ImageProcessor(() -> part.getInputStream(),
+                                                                       () -> Files.newOutputStream(temp));
+                                                       imageProcessor.process();
+                                                       fileNode = JcrUtils.copyStreamAsFile(submission, part.getName(),
+                                                                       Files.newInputStream(temp));
+                                               } finally {
+                                                       Files.deleteIfExists(temp);
+                                               }
+                                       } else {
+                                               fileNode = JcrUtils.copyStreamAsFile(submission, part.getName(), part.getInputStream());
+                                       }
                                        String contentType = part.getContentType();
                                        if (contentType != null) {
                                                fileNode.addMixin(NodeType.MIX_MIMETYPE);