From 9e296453bf0b7ee35e2ed3c992198584bf2f2c0a Mon Sep 17 00:00:00 2001
From: Ulrich <undisclosed>
Date: Tue, 28 Feb 2017 16:31:16 +0000
Subject: [PATCH] copy, paste

---
 src/java/de/uhilger/filecms/api/FileMgr.java |   63 +++++++++++++++++++++++++++++--
 1 files changed, 59 insertions(+), 4 deletions(-)

diff --git a/src/java/de/uhilger/filecms/api/FileMgr.java b/src/java/de/uhilger/filecms/api/FileMgr.java
index d045623..52c4e8e 100644
--- a/src/java/de/uhilger/filecms/api/FileMgr.java
+++ b/src/java/de/uhilger/filecms/api/FileMgr.java
@@ -45,6 +45,9 @@
   public static final String PUB_DIR_NAME = "Oeffentlich";
   public static final String HOME_DIR_NAME = "Persoenlicher Ordner";
   
+  public static final int OP_COPY = 1;
+  public static final int OP_MOVE = 2;
+  
   public String hallo() {
     return "Hallo Welt!";
   }
@@ -61,12 +64,13 @@
       files.add(namedPublicFolder);
     } else {
       String path = getTargetDir(relPath).getAbsolutePath();
-      logger.fine(path);
+      logger.fine("path: " + path);
       LocalFileSystem fs = new LocalFileSystem();
+      logger.fine("listing " + getTargetDir(relPath).getAbsolutePath());
       FileRef[] fileRefs = fs.list(new FileRef(getTargetDir(relPath).getAbsolutePath(), true));
       for(int i = 0; i < fileRefs.length; i++) {
         files.add(fileRefs[i]);
-        logger.finer("added " + fileRefs[i].getAbsolutePath());
+        logger.fine("added " + fileRefs[i].getAbsolutePath());
       }
     }    
     return files;
@@ -147,6 +151,56 @@
     return result;
   }
   
+  public String copyFiles(String fromPath, String toPath, List fileNames) {
+    return copyOrMoveFiles(fromPath, toPath, fileNames, OP_COPY);
+  }
+  
+  public String moveFiles(String fromPath, String toPath, List fileNames) {
+    return copyOrMoveFiles(fromPath, toPath, fileNames, OP_MOVE);
+  }
+  
+  /**
+   * Dateien kopieren
+   * 
+   * @param fromPath der relative Pfad, aus dem die Dateien stammen, die kopiert werden sollen
+   * @param toPath der relative Pfad, an den die Dateien kopiert werden sollen
+   * @param fileNames  Liste mit Namen der Dateien, die kopiert werden sollen
+   * @param operation OP_COPY oder OP_MOVE
+   * @return null bei Fehler oder Quittungstext, wenn erfolgreich
+   */
+  private String copyOrMoveFiles(String fromPath, String toPath, List fileNames, int operation) {
+    String result = null;
+    try {
+      FileRef[] files = new FileRef[fileNames.size()];
+      logger.fine(fileNames.toString());
+      File srcDir = getTargetDir(fromPath);
+      for(int i=0; i < fileNames.size(); i++) {
+        Object o = fileNames.get(i);
+        if(o instanceof ArrayList) {
+          ArrayList al = (ArrayList) o;
+          logger.fine(al.get(0).toString());
+          File srcFile = new File(srcDir, al.get(0).toString());
+          logger.fine(srcFile.getAbsolutePath());
+          files[i] = new FileRef(srcFile.getAbsolutePath(), srcFile.isDirectory());
+        }
+      }
+      File targetDir = getTargetDir(toPath);
+      FileSystem fs = new LocalFileSystem();
+      switch(operation) {
+        case OP_COPY:
+          fs.copy(files, new FileRef(targetDir.getAbsolutePath(), true));
+          break;
+        case OP_MOVE:
+          fs.move(files, new FileRef(targetDir.getAbsolutePath(), true));
+          break;
+      }
+      result = "kopiert";
+    } catch (Throwable ex) {
+      logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex);
+    }
+    return result;
+  }
+  
   public FileRef saveTextFile(String relPath, String fileName, String contents) {
     FileRef savedFile = null;
     try {
@@ -190,9 +244,9 @@
     logger.fine(relPath);
     String targetPath = null;
     if(relPath.startsWith(PUB_DIR_NAME)) {
-      targetPath = PUB_DIR_PATH + getUserName() + "/" + relPath.substring(PUB_DIR_NAME.length());
+      targetPath = PUB_DIR_PATH + getUserName() + relPath.substring(PUB_DIR_NAME.length());
     } else if(relPath.startsWith(HOME_DIR_NAME)) {
-      targetPath = HOME_DIR_PATH + getUserName() + "/" + relPath.substring(HOME_DIR_NAME.length());
+      targetPath = HOME_DIR_PATH + getUserName() + relPath.substring(HOME_DIR_NAME.length());
     } else {
       // kann eigentlich nicht sein..
     }
@@ -200,6 +254,7 @@
     File targetDir = new File(getBase().getAbsolutePath(), targetPath);
     return targetDir;
   }
+  
   private FileRef getBase() {
     FileRef base = null;
     Object o = getServletContext().getAttribute(Initialiser.FILE_BASE);

--
Gitblit v1.9.3