From fab80c872af697d61f200fd410a7ecc64ca0a537 Mon Sep 17 00:00:00 2001
From: ulrich <undisclosed>
Date: Mon, 06 Mar 2017 22:59:47 +0000
Subject: [PATCH] dekodieren fuer Codemirror

---
 src/java/de/uhilger/filecms/api/UploadServlet.java |   70 ++++++++++++++++++++++++++---------
 1 files changed, 52 insertions(+), 18 deletions(-)

diff --git a/src/java/de/uhilger/filecms/api/UploadServlet.java b/src/java/de/uhilger/filecms/api/UploadServlet.java
index deed276..ff788ff 100644
--- a/src/java/de/uhilger/filecms/api/UploadServlet.java
+++ b/src/java/de/uhilger/filecms/api/UploadServlet.java
@@ -1,11 +1,29 @@
+/*
+    Dateiverwaltung - File management in your browser
+    Copyright (C) 2017 Ulrich Hilger, http://uhilger.de
+
+    This program is free software: you can redistribute it and/or modify
+    it under the terms of the GNU Affero General Public License as
+    published by the Free Software Foundation, either version 3 of the
+    License, or (at your option) any later version.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU Affero General Public License for more details.
+
+    You should have received a copy of the GNU Affero General Public License
+    along with this program.  If not, see <http://www.gnu.org/licenses/>.
+*/
+
 package de.uhilger.filecms.api;
 
 import static de.uhilger.filecms.api.FileMgr.HOME_DIR_NAME;
 import static de.uhilger.filecms.api.FileMgr.HOME_DIR_PATH;
 import static de.uhilger.filecms.api.FileMgr.PUB_DIR_NAME;
 import static de.uhilger.filecms.api.FileMgr.PUB_DIR_PATH;
+import de.uhilger.filecms.data.FileRef;
 import de.uhilger.filecms.web.Initialiser;
-import de.uhilger.filesystem.FileRef;
 import java.io.File;
 import java.io.IOException;
 import java.io.InputStream;
@@ -38,6 +56,8 @@
   public static final String DESC = "file-cms uploader";
 
   private static final Logger logger = Logger.getLogger(UploadServlet.class.getName());
+  
+  private String targetDirStr;
 
   /**
    * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
@@ -50,6 +70,7 @@
    */
   protected void processRequest(HttpServletRequest request, HttpServletResponse response)
           throws ServletException, IOException {
+    
     //logger.log(Level.INFO, "start");
     boolean isMultipart = ServletFileUpload.isMultipartContent(request);
 
@@ -73,6 +94,7 @@
 
         // Process the uploaded items
         Iterator<FileItem> iter = items.iterator();
+        FileItem fileItem = null;
         while (iter.hasNext()) {
           FileItem item = iter.next();
 
@@ -82,10 +104,13 @@
             //File base = repository.getParentFile().getParentFile().getParentFile().getParentFile();
             //File webapps = new File(base, "webapps");
             //logger.info(webapps.getAbsolutePath());
-            File targetDir = getTargetDir(request, PUB_DIR_NAME);
-            logger.log(Level.FINE, "rufe processUploadedFile");
-            processUploadedFile(targetDir, item, true);
+            fileItem = item;
           }
+        }
+        File targetDir = getTargetDir(request, targetDirStr);
+        if(targetDir != null && fileItem != null) {
+          logger.log(Level.FINER, "targetDir before upload: " + targetDir.getAbsolutePath());
+          processUploadedFile(targetDir, fileItem, true);
         }
       } catch (Exception ex) {
         logger.log(Level.SEVERE, null, ex);
@@ -111,17 +136,20 @@
   }
 
   private File getTargetDir(HttpServletRequest request, String relPath) {
-    logger.finer(relPath);
-    String targetPath = null;
-    if(relPath.startsWith(PUB_DIR_NAME)) {
-      targetPath = PUB_DIR_PATH + getUserName(request) + "/" + relPath.substring(PUB_DIR_NAME.length());
-    } else if(relPath.startsWith(HOME_DIR_NAME)) {
-      targetPath = HOME_DIR_PATH + getUserName(request) + "/" + relPath.substring(HOME_DIR_NAME.length());
-    } else {
-      // kann eigentlich nicht sein..
+    File targetDir = null;
+    if(relPath != null) {
+      logger.finer(relPath);
+      String targetPath = null;
+      if(relPath.startsWith(PUB_DIR_NAME)) {
+        targetPath = PUB_DIR_PATH + getUserName(request) + relPath.substring(PUB_DIR_NAME.length());
+      } else if(relPath.startsWith(HOME_DIR_NAME)) {
+        targetPath = HOME_DIR_PATH + getUserName(request) + relPath.substring(HOME_DIR_NAME.length());
+      } else {
+        // kann eigentlich nicht sein..
+      }
+      logger.finer(targetPath);
+      targetDir = new File(getBase().getAbsolutePath(), targetPath);
     }
-    logger.finer(targetPath);
-    File targetDir = new File(getBase().getAbsolutePath(), targetPath);
     return targetDir;
   }
   
@@ -137,8 +165,11 @@
   private FileRef getBase() {
     FileRef base = null;
     Object o = getServletContext().getAttribute(Initialiser.FILE_BASE);
-    if (o instanceof FileRef) {
-      base = (FileRef) o;
+    if(o instanceof String) {
+      String baseStr = (String) o;
+      logger.fine(baseStr);
+      File file = new File(baseStr);
+      base = new FileRef(file.getAbsolutePath(), file.isDirectory());
     }
     return base;
   }
@@ -154,8 +185,8 @@
       long sizeInBytes = item.getSize();
       // Process a file upload
       if (writeToFile) {
-        //logger.log(Level.INFO, "writeToFile " + fileName);
         File uploadedFile = new File(targetDir, fileName);
+        logger.log(Level.FINER, "writeToFile " + uploadedFile.getAbsolutePath());
         item.write(uploadedFile);
       } else {
         InputStream uploadedStream = item.getInputStream();
@@ -171,8 +202,11 @@
     if (item.isFormField()) {
       String name = item.getFieldName();
       String value = item.getString();
-      //logger.log(Level.INFO, "field name: " + name + ", value: " + value);
+      logger.log(Level.FINER, "field name: " + name + ", value: " + value);
       //...
+      if(name.equals("pfad")) {
+        targetDirStr = value;
+      }
     }
   }
 

--
Gitblit v1.9.3