Dateiverwaltung für die WebBox
Ulrich
2017-03-01 5bfd3435869632cf8796f116bfef12bd0d6345e7
FileSystem durch Commons io ersetzt
3 files modified
1 files added
293 ■■■■■ changed files
src/java/de/uhilger/filecms/api/FileMgr.java 92 ●●●● patch | view | raw | blame | history
src/java/de/uhilger/filecms/api/UploadServlet.java 2 ●●● patch | view | raw | blame | history
src/java/de/uhilger/filecms/data/FileRef.java 197 ●●●●● patch | view | raw | blame | history
web/WEB-INF/web.xml 2 ●●● patch | view | raw | blame | history
src/java/de/uhilger/filecms/api/FileMgr.java
@@ -18,10 +18,11 @@
package de.uhilger.filecms.api;
import de.uhilger.filecms.data.FileRef;
import de.uhilger.filecms.web.Initialiser;
import de.uhilger.filesystem.FileRef;
import de.uhilger.filesystem.FileSystem;
import de.uhilger.filesystem.LocalFileSystem;
//import de.uhilger.filesystem.FileRef;
//import de.uhilger.filesystem.FileSystem;
//import de.uhilger.filesystem.LocalFileSystem;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
@@ -29,9 +30,11 @@
import java.io.IOException;
import java.security.Principal;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.io.FileUtils;
/**
 *
@@ -64,13 +67,23 @@
      files.add(namedPublicFolder);
    } else {
      String path = getTargetDir(relPath).getAbsolutePath();
      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.fine("added " + fileRefs[i].getAbsolutePath());
      logger.fine("listing path: " + path);
      //LocalFileSystem fs = new LocalFileSystem();
      //logger.fine("listing " + getTargetDir(relPath).getAbsolutePath());
      File dir = new File(path);
      if(dir.exists()) {
        File[] fileArray = dir.listFiles();
        //FileRef[] fileRefs = fs.list(new FileRef(getTargetDir(relPath).getAbsolutePath(), true));
        for(int i = 0; i < fileArray.length; i++) {
          logger.fine(fileArray[i].toURI().toString());
          String fname = fileArray[i].toURI().toString().replace("file:/", "");
          if(fileArray[i].isDirectory()) {
            fname = fname.substring(0, fname.length() - 1);
          }
          logger.fine(fname);
          files.add(new FileRef(fname, fileArray[i].isDirectory()));
        }
      }
    }    
    return files;
@@ -129,7 +142,6 @@
  public String deleteFiles(String relPath, List fileNames) {
    String result = null;
    try {
      FileRef[] delRefs = new FileRef[fileNames.size()];
      logger.fine(fileNames.toString());
      File targetDir = getTargetDir(relPath);
      for(int i=0; i < fileNames.size(); i++) {
@@ -139,11 +151,13 @@
          logger.fine(al.get(0).toString());
          File targetFile = new File(targetDir, al.get(0).toString());
          logger.fine(targetFile.getAbsolutePath());
          delRefs[i] = new FileRef(targetFile.getAbsolutePath(), targetFile.isDirectory());
          if(targetFile.isDirectory()) {
            FileUtils.deleteDirectory(targetFile);
          } else {
            targetFile.delete();
          }
        }
      }
      FileSystem fs = new LocalFileSystem();
      fs.delete(delRefs);
      result = "deleted";
    } catch (Throwable ex) {
      logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex);
@@ -152,11 +166,54 @@
  }
  
  public String copyFiles(String fromPath, String toPath, List fileNames) {
    return copyOrMoveFiles(fromPath, toPath, fileNames, OP_COPY);
    String result = null;
    try {
      File srcDir = getTargetDir(fromPath);
      File targetDir = getTargetDir(toPath);
      Iterator i = fileNames.iterator();
      while(i.hasNext()) {
        Object o = i.next();
        if (o instanceof ArrayList) {
          ArrayList al = (ArrayList) o;
          File srcFile = new File(srcDir, al.get(0).toString());
          if(srcFile.isDirectory()) {
            logger.fine("copy dir " + srcFile.getAbsolutePath() + " to dir " + targetDir.getAbsolutePath());
            FileUtils.copyDirectoryToDirectory(srcFile, targetDir);
          } else {
            logger.fine("copy srcFile " + srcFile.getAbsolutePath() + " to dir " + targetDir.getAbsolutePath());
            FileUtils.copyFileToDirectory(srcFile, targetDir);
          }
        }
      }
    } catch (IOException ex) {
      logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex);
    }
    return result;
  }
  
  public String moveFiles(String fromPath, String toPath, List fileNames) {
    return copyOrMoveFiles(fromPath, toPath, fileNames, OP_MOVE);
    String result = null;
    try {
      File srcDir = getTargetDir(fromPath);
      File targetDir = getTargetDir(toPath);
      Iterator i = fileNames.iterator();
      while(i.hasNext()) {
        Object o = i.next();
        if (o instanceof ArrayList) {
          ArrayList al = (ArrayList) o;
          File srcFile = new File(srcDir, al.get(0).toString());
          if(srcFile.isDirectory()) {
            FileUtils.moveDirectoryToDirectory(srcFile, targetDir, false);
          } else {
            FileUtils.moveFileToDirectory(srcFile, targetDir, false);
          }
        }
      }
    } catch (IOException ex) {
      logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex);
    }
    return result;
    //return copyOrMoveFiles(fromPath, toPath, fileNames, OP_MOVE);
  }
  
  /**
@@ -168,6 +225,7 @@
   * @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 {
@@ -189,6 +247,7 @@
      switch(operation) {
        case OP_COPY:
          fs.copy(files, new FileRef(targetDir.getAbsolutePath(), true));
          FileUtils.copy
          break;
        case OP_MOVE:
          fs.move(files, new FileRef(targetDir.getAbsolutePath(), true));
@@ -200,6 +259,7 @@
    }
    return result;
  }
  */
  
  public FileRef saveTextFile(String relPath, String fileName, String contents) {
    FileRef savedFile = null;
src/java/de/uhilger/filecms/api/UploadServlet.java
@@ -22,8 +22,8 @@
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;
src/java/de/uhilger/filecms/data/FileRef.java
New file
@@ -0,0 +1,197 @@
/*
    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.data;
import java.io.Serializable;
/**
 * A reference to a file consisting of the file's absolute path and additional
 * information about whether or not the referenced file is a directory, is hidden, etc.
 * Note that FileRef only references a file, the file itself and its contents are not
 * modelled by this class.
 *
 * @author Ulrich Hilger, http://uhilger.de
 * @author Bereitgestellt unter den Bedingungen der
 *  <a href="http://www.gnu.org/licenses/agpl-3.0" target="_blank">GNU Affero
 *  General Public License</a>
 *
 * @version 2, 12.01.2008
 */
public class FileRef implements Serializable {
  public static final long serialVersionUID = 42L;
  private String absolutePath;
    private Boolean isDirectory;
    private Boolean isHidden;
    private Long lastModified;
    private Long length;
    /**
     * create a new instance of <code>FileRef</code>. Note that the created FileRef is
     * only a reference to a file, the file itself will not be created.
     *
     * @param absolutePath  the absolute path that denotes this instance of <code>FileRef</code>
     * @param isDirectory  whether or not this file is a directory
     * @param isHidden  whether or not this file is a hidden file
     * @param lastModified  the date/time this file was last modified measured in milliseconds
     *                         since the epoch (00:00:00 GMT, January 1, 1970)
     * @param length the length of this file in bytes
     */
    public FileRef(String absolutePath, boolean isDirectory, boolean isHidden,
            long lastModified, long length)
    {
        super();
        this.absolutePath = absolutePath;
        this.isDirectory = isDirectory;
        this.isHidden = isHidden;
        this.lastModified = lastModified;
        this.length = length;
    }
    /**
     * Create a new instance of class <code>FileRef</code> with a given absolute path.
     * Other characteristics of the file are created with default settings
     * (i.e. not a directory, not hidden, unknown modification date
     * and length). Note that the created FileRef is only a reference to a file, the file
     * itself will not be created.
     *
     * @param absolutePath  the absolute path that denotes this instance
     * of <code>FileRef</code>
     */
    public FileRef(String absolutePath) {
        this(absolutePath, false, false, 0, 0);
    }
    /**
     * Create a new instance of class <code>FileRef</code> with a given absolute path
     * and an indicator whether or not the new FileRef denotes a directory.
     * Other characteristics of the file are created with default settings
     * (i.e. not hidden, unknown modification date and length). Note that the created
     * FileRef is only a reference to a file, the file itself will not be created.
     *
     * @param absolutePath  the absolute path that denotes this instance
     * of <code>FileRef</code>
     * @param isDirectory  true, if the file to create should denote a directory
     */
    public FileRef(String absolutePath, boolean isDirectory) {
        this(absolutePath, isDirectory, false, 0, 0);
    }
    /**
     * get the absolute path that denotes this file
     * @return  the path
     */
    public String getAbsolutePath() {
        return absolutePath;
    }
    /**
     * Tests whether the file denoted by this abstract pathname is a
     * directory.
     *
     * @return <code>true</code> if this file is a directory,
     *          <code>false</code> otherwise
     */
    public boolean isDirectory() {
        return isDirectory;
    }
    /**
     * Tests whether the file denoted by this abstract pathname is a
     * file.
     *
     * @return <code>true</code> if this file is a file,
     *          <code>false</code> otherwise
     */
    public boolean isFile() {
        return !isDirectory;
    }
    /**
     * Tests whether the file denoted by this abstract pathname is a
     * hidden file.
     *
     * @return <code>true</code> if this file is a hidden file,
     *          <code>false</code> otherwise
     */
    public boolean isHidden() {
        return isHidden;
    }
    /**
     * Returns the time that this file was
     * last modified.
     *
     * @return  A <code>long</code> value representing the time the file was
     *          last modified, measured in milliseconds since the epoch
     *          (00:00:00 GMT, January 1, 1970)
     */
    public long getLastModified() {
        return lastModified;
    }
    /**
     * Returns the length of this file.
     * The return value is unspecified if this file denotes a directory.
     *
     * @return  The length, in bytes, of this file
     */
    public long getLength() {
        return length;
    }
    /**
     * get the name of this file without path
     * @param separatorChar  the string that is used to separate directories and file names in
     * path expressions
     * @return the name of this file without path
     */
    public String getName(String separatorChar) {
        String path = getAbsolutePath();
        String name = path.substring(path.lastIndexOf(separatorChar) + 1);
        if(name == null || name.length() < 1) {
            name = path;
        }
        return name;
    }
    /**
     * get a string representation of this instance of FileRef
     * @return the string representation of this FileRef object
     */
    public String toString() {
        return getAbsolutePath();
    }
    /**
     * Indicates whether some object is equal to this instance of class FileRef. Two
     * GenericFiles are regarded as equal when their absolute paths are equal case independently.
     *
     * @param  obj  the object to compare with this instance of FileRef
     * @return true, if obj is equal to this instance of FileRef, false if not
     */
    public boolean equals(Object obj) {
        boolean isEqual = false;
        if(obj != null && obj instanceof FileRef) {
            isEqual = ((FileRef) obj).getAbsolutePath().toLowerCase().equals(
                    this.getAbsolutePath().toLowerCase());
        }
        return isEqual;
    }
}
web/WEB-INF/web.xml
@@ -7,7 +7,7 @@
          Bleibt der Eintrag leer oder wird er ganz weggelassen, wird ein hart 
          kodierter Pfad fuer die WebBox verwendet.</description>
        <param-name>datenAblage</param-name>
        <param-value> </param-value>
        <param-value>C:\Users\hilgeru\Documents\srv\wbx\daten</param-value>
    </context-param>
    <listener>
        <description>Der Initialiser setzt globale Variable fuer die Dateiverwaltung</description>