Dateiverwaltung für die WebBox
ulrich
2018-03-03 eb2a2d78152c7ca689e3aac116c107229a896a66
src/java/de/uhilger/filecms/api/FileMgr.java
@@ -1,34 +1,485 @@
/*
    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 de.uhilger.filesystem.FileRef;
import de.uhilger.transit.web.WebKontext;
import javax.servlet.ServletContext;
import de.uhilger.filecms.data.Bild;
import de.uhilger.filecms.data.FileRef;
import de.uhilger.filecms.pub.AbstractComparator;
import de.uhilger.filecms.pub.FileNameComparator;
import java.awt.Container;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.Toolkit;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.security.Principal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import org.apache.commons.io.FileUtils;
/**
 *
 * @author ulrich
 * Methoden zur Verwaltung von Dateien
 */
public class FileMgr implements WebKontext {
public class FileMgr extends Api {
  private static final Logger logger = Logger.getLogger(FileMgr.class.getName());
  
  public static final String FILE_BASE = "fileBase";
  
  private ServletContext ctx;
  public static final int OP_COPY = 1;
  public static final int OP_MOVE = 2;
  
  public FileRef getBase() {
    FileRef ref = null;
    String fileBase = getServletContext().getInitParameter(FILE_BASE);
    return ref;
  public String hallo() {
    return "Hallo Welt!";
  }
  /**
   * Inhalte der WebBox listen. Hier wird nur der relative Pfad
   * ausgehend von www oder home ausgegeben sowie zudem ausgehend
   * von $daten und $basis, sofern der Benutzer die Rolle wbxAdmin hat.
   *
   * Andere Inhalte werden nicht ausgegeben.
   *
   * @param relPath
   * @return
   */
  public List<FileRef> list(String relPath) {
    return listInt(relPath, "name", AbstractComparator.ORDER_ASC);
  }
  public List<FileRef> listOrdered(String relPath, String orderBy, String order) {
    return listInt(relPath, orderBy, order);
  }
  /**
   * Inhalte der WebBox listen. Hier wird nur der relative Pfad
   * ausgehend von www oder home ausgegeben sowie zudem ausgehend
   * von $daten und $basis, sofern der Benutzer die Rolle wbxAdmin hat.
   *
   * Andere Inhalte werden nicht ausgegeben.
   *
   * @param relPath
   * @param orderBy 'name'
   * @param order AbstractComparator.ORDER_ASC oder AbstractComparator.ORDER_DESC
   * @return
   */
  private List<FileRef> listInt(String relPath, String orderBy, String order) {
    Bild bild = new Bild();
    List<FileRef> files = new ArrayList();
    if (!relPath.startsWith(".")) {
      if (relPath.length() == 0) {
        FileRef namedPublicFolder = new FileRef(PUB_DIR_NAME, true);
        logger.finer(namedPublicFolder.getAbsolutePath());
        FileRef namedHomeFolder = new FileRef(HOME_DIR_NAME, true);
        logger.finer(namedHomeFolder.getAbsolutePath());
        FileRef namedDavFolder = new FileRef(DAV_DIR_NAME, true);
        logger.finer(namedDavFolder.getAbsolutePath());
        files = new ArrayList();
        files.add(namedHomeFolder);
        files.add(namedPublicFolder);
        files.add(namedDavFolder);
        if (getRequest().isUserInRole(WBX_ADMIN_ROLE)) {
          FileRef namedBaseFolder = new FileRef(WBX_BASE, true);
          FileRef namedDataFolder = new FileRef(WBX_DATA, true);
          files.add(namedBaseFolder);
          files.add(namedDataFolder);
        }
      } else {
        String path = getTargetDir(relPath).getAbsolutePath();
        logger.fine("listing path: " + path);
        File dir = new File(path);
        if (dir.exists()) {
          File[] fileArray = dir.listFiles();
          if (orderBy != null && orderBy.equalsIgnoreCase("name")) {
            Arrays.sort(fileArray, new FileNameComparator(order));
          } else {
            Arrays.sort(fileArray, new FileNameComparator(AbstractComparator.ORDER_ASC));
          }
          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);
            FileRef ref = new FileRef(fname, fileArray[i].isDirectory());
            ref.setMimetype(bild.getMimeType(fileArray[i]));
            files.add(ref);
          }
        }
      }
    }
    return files;
  }
  public FileRef newFolder(String relPath, String folderName) {
    if (!relPath.startsWith(".")) {
      logger.finer(relPath);
      String targetPath = null;
      if(relPath.startsWith(PUB_DIR_NAME)) {
        targetPath = PUB_DIR_PATH + getUserName() + "/" + relPath.substring(PUB_DIR_NAME.length()) + "/" + folderName;
      } else if(relPath.startsWith(HOME_DIR_NAME)) {
        targetPath = HOME_DIR_PATH + getUserName() + "/" + relPath.substring(HOME_DIR_NAME.length()) + "/" + folderName;
      } else {
        // kann eigentlich nicht sein..
      }
      logger.finer(targetPath);
      File targetDir = new File(getBase().getAbsolutePath(), targetPath);
      targetDir.mkdirs();
      return new FileRef(targetDir.getAbsolutePath(), true);
    } else {
      return null;
    }
  }
  public String getCode(String relPath, String fileName) {
    String code = null;
    if (!relPath.startsWith(".")) {
      Object p = getRequest().getUserPrincipal();
      if (p instanceof Principal) {
        Reader reader = null;
        try {
          File targetFile = new File(getTargetDir(relPath), fileName);
  @Override
  public ServletContext getServletContext() {
    return ctx;
  }
          //reader = new InputStreamReader(new FileInputStream(targetFile), "UTF8");
          reader = new FileReader(targetFile);
          StringBuffer buf = new StringBuffer();
          char[] readBuffer = new char[1024];
          int charsRead = reader.read(readBuffer);
          while (charsRead > -1) {
            buf.append(readBuffer, 0, charsRead);
            charsRead = reader.read(readBuffer);
          }
          code = buf.toString();
        } catch (FileNotFoundException ex) {
          Logger.getLogger(FileMgr.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
          Logger.getLogger(FileMgr.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
          try {
            reader.close();
          } catch (IOException ex) {
            Logger.getLogger(FileMgr.class.getName()).log(Level.SEVERE, null, ex);
          }
        }
  @Override
  public void setServletContext(ServletContext servletContext) {
    this.ctx = servletContext;
      }
    }
    return code;
  }
  public String renameFile(String relPath, String fname, String newName) {
    if (!relPath.startsWith(".")) {
      File targetDir = getTargetDir(relPath);
      File file = new File(targetDir, fname);
      file.renameTo(new File(targetDir, newName));
      return fname + " umbenannt zu " + newName;
    } else {
      return "Pfad nicht erlaubt.";
    }
  }
  public String deleteFiles(String relPath, List fileNames) {
    String result = null;
    try {
      logger.fine(fileNames.toString());
      if (!relPath.startsWith(".")) {
        File targetDir = getTargetDir(relPath);
        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 targetFile = new File(targetDir, al.get(0).toString());
            logger.fine(targetFile.getAbsolutePath());
            if(targetFile.isDirectory()) {
              FileUtils.deleteDirectory(targetFile);
            } else {
              targetFile.delete();
            }
          }
        }
        result = "deleted";
      }
    } catch (Throwable ex) {
      logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex);
    }
    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);
  }
  private String copyOrMoveFiles(String fromPath, String toPath, List fileNames, int operation) {
    String result = null;
    try {
      if (!fromPath.startsWith(".")) {
        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()) {
              if(operation == OP_MOVE) {
                FileUtils.moveDirectoryToDirectory(srcFile, targetDir, false);
              } else {
                FileUtils.copyDirectoryToDirectory(srcFile, targetDir);
              }
            } else {
              if(operation == OP_MOVE) {
                FileUtils.moveFileToDirectory(srcFile, targetDir, false);
              } else {
                FileUtils.copyFileToDirectory(srcFile, targetDir);
              }
            }
          }
        }
      }
    } catch (IOException ex) {
      logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex);
    }
    return result;
  }
  public FileRef saveTextFileAs(String relPath, String fileName, String contents) {
    FileRef savedFile = null;
    logger.fine(relPath + " " + fileName);
    if (!relPath.startsWith(".")) {
      //FileRef datenRef = getBase();
      Object p = getRequest().getUserPrincipal();
      if(p instanceof Principal) {
        File targetFile = new File(getTargetDir(relPath), fileName);
        if(targetFile.exists()) {
          targetFile = getNewFileName(targetFile);
        } else {
          targetFile.getParentFile().mkdirs();
        }
        saveToFile(targetFile, contents);
      }
    }
    return savedFile;
  }
  private File getNewFileName(File file) {
    File dir = file.getParentFile();
    String targetName = file.getName();
    logger.fine("targetName: " + targetName);
    String ext = "";
    int dotpos = targetName.indexOf(".");
    if(dotpos > -1) {
      ext = targetName.substring(dotpos);
      targetName = targetName.substring(0, dotpos);
    }
    logger.fine("targetName: " + targetName + ", ext: " + ext);
    int i = 1;
    while(file.exists()) {
      StringBuffer buf = new StringBuffer();
      buf.append(targetName);
      buf.append("-");
      buf.append(i);
      if(ext.length() > 0) {
        buf.append(ext);
      }
      file = new File(dir, buf.toString());
      i++;
    }
    logger.fine("new file: " + file.getName());
    return file;
  }
  private FileRef saveToFile(File targetFile, String contents) {
    FileRef savedFile = null;
    try {
      targetFile.createNewFile();
      FileWriter w = new FileWriter(targetFile);
      //w.write(StringEscapeUtils.unescapeHtml(contents));
      w.write(contents);
      w.flush();
      w.close();
      savedFile = new FileRef(
              targetFile.getAbsolutePath(),
              targetFile.isDirectory(),
              targetFile.isHidden(),
              targetFile.lastModified(),
              targetFile.length());
    } catch (IOException ex) {
      logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex);
    }
    return savedFile;
  }
  public FileRef saveTextFile(String relPath, String fileName, String contents) {
    FileRef savedFile = null;
    logger.fine(relPath + " " + fileName);
    if (!relPath.startsWith(".")) {
      //FileRef datenRef = getBase();
      Object p = getRequest().getUserPrincipal();
      if(p instanceof Principal) {
        File targetFile = new File(getTargetDir(relPath), fileName);
        if(targetFile.exists()) {
          /*
            muss delete() sein?
            pruefen: ueberschreibt der FileWriter den alteen Inhalt oder
            entsteht eine unerwuenschte Mischung aus altem und neuem
            Inhalt?
          */
          targetFile.delete();
        } else {
          targetFile.getParentFile().mkdirs();
        }
        saveToFile(targetFile, contents);
      }
    }
    return savedFile;
  }
  public String bildVerkleinern(String relPath, String bildName) {
    if (!relPath.startsWith(".")) {
      File dir = getTargetDir(relPath);
      File original = new File(dir, bildName);
      Bild bild = new Bild();
      //for (int i = 0; i < Bild.GR.length; i++) {
      //int gr = bild.getVariantenGroesse(i);
      String ext = "";
      String nurname = bildName;
      int dotpos = bildName.indexOf(".");
      if (dotpos > -1) {
        ext = bildName.substring(dotpos);
        nurname = bildName.substring(0, dotpos);
      }
      Image image = Toolkit.getDefaultToolkit().getImage(original.getAbsolutePath());
      MediaTracker mediaTracker = new MediaTracker(new Container());
      mediaTracker.addImage(image, 0);
      try {
        mediaTracker.waitForID(0);
        if (!mediaTracker.isErrorAny()) {
          for (int i = 0; i < Bild.GR.length; i++) {
            StringBuffer buf = new StringBuffer();
            buf.append(nurname);
            buf.append(bild.getVariantenName(i));
            buf.append(ext);
            File newImgFile = new File(dir, buf.toString());
            if (!newImgFile.exists()) {
              logger.fine(original.getAbsolutePath() + " " + newImgFile.getAbsolutePath());
              bild.writeImageFile(image, bild.getVariantenGroesse(i), bild.getMimeType(original), newImgFile.getAbsolutePath());
              //bild.writeImageFile(image, photo.getVariantenGroesse(i), photo.getMimetype(), photo.getAbsolutePath(basisPfad), photo.getVariantenName(basisPfad, i));
            }
          }
        }
      } catch (IOException | InterruptedException ex) {
        logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex);
      }
      return "ok";
    } else {
      return "Pfad micht erlaubt.";
    }
  }
  public String extractZipfile(String relPath, String filename) {
    String result = null;
    if (!relPath.startsWith(".")) {
      try {
        File targetDir = getTargetDir(relPath);
        File archive = new File(targetDir, filename);
        if(extract(archive)) {
          result = "ok";
        } else {
          result = "error while extracting";
        }
      } catch(Exception ex) {
        result = ex.getLocalizedMessage();
        logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex);
      }
    }
    return result;
  }
  /**
    * extract a given ZIP archive to the folder respective archive resides in
    * @param archive  the archive to extract
    * @throws Exception
    */
   private boolean extract(File archive) throws Exception {
      ZipFile zipfile = new ZipFile(archive);
      Enumeration en = zipfile.entries();
      while(en.hasMoreElements()) {
         ZipEntry zipentry = (ZipEntry) en.nextElement();
         unzip(zipfile, zipentry, archive.getParent());
      }
      zipfile.close();
      return true;
   }
   /**
    * unzip a given entry of a given zip file to a given location
    * @param zipfile  the zip file to read an entry from
    * @param zipentry  the zip entry to read
    * @param destPath  the path to the destination location for the extracted content
    * @throws IOException
    */
   private void unzip(ZipFile zipfile, ZipEntry zipentry, String destPath) throws IOException {
      byte buf[] = new byte[1024];
      InputStream is = zipfile.getInputStream(zipentry);
      String outFileName = destPath + File.separator + zipentry.getName();
      File file = new File(outFileName);
      if(!zipentry.isDirectory()) {
         file.getParentFile().mkdirs();
         if(!file.exists())
            file.createNewFile();
         FileOutputStream fos = new FileOutputStream(file);
         int i = is.read(buf, 0, 1024);
         while(i > -1) {
            fos.write(buf, 0, i);
            i = is.read(buf, 0, 1024);
         }
         fos.close();
         is.close();
      } else {
         file.mkdirs();
      }
   }
  
}
  /* ---- Hilfsfunktionen ---- */
}