Dateiverwaltung für die WebBox
ulrich
2017-02-20 8931b77df172c4a2a106d471a566479ac9c5f6b0
src/java/de/uhilger/filecms/api/FileMgr.java
@@ -1,24 +1,41 @@
/*
    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.RequestKontext;
import de.uhilger.transit.web.WebKontext;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.security.Principal;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
/**
 *
 * @author ulrich
 */
public class FileMgr implements WebKontext, RequestKontext {
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;
  private HttpServletRequest req;
  
  public String hallo() {
    return "Hallo Welt!";
@@ -89,34 +106,40 @@
  
  public FileRef saveTextFile(String relPath, String fileName, String contents) {
    FileRef savedFile = null;
    try {
      FileRef datenRef = getBase();
      File daten = new File(datenRef.getAbsolutePath());
      Object p = getRequest().getUserPrincipal();
      if(p instanceof Principal) {
        File userDir = new File(daten, "www/" + ((Principal) p).getName());
        File saveDir = new File(userDir, relPath);
        File targetFile = new File(saveDir, fileName);
        if(!targetFile.exists()) {
          targetFile.getParentFile().mkdirs();
          targetFile.createNewFile();
          FileWriter w = new FileWriter(targetFile);
          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, null, ex);
    }
    return savedFile;
  }
  
  private File getWebappsDir() {
    File cfile = new File(this.getClass().getResource(this.getClass().getSimpleName() + ".class").getFile());
    File cfile = new File(this.getClass().getResource(
            this.getClass().getSimpleName() + ".class").getFile());
    String path = cfile.getAbsolutePath();
    return new File(path.substring(0, path.indexOf(req.getContextPath())));
    return new File(path.substring(0, path.indexOf(getRequest().getContextPath())));
  }
  @Override
  public ServletContext getServletContext() {
    return ctx;
  }
  @Override
  public void setServletContext(ServletContext servletContext) {
    this.ctx = servletContext;
  }
  @Override
  public HttpServletRequest getRequest() {
    return req;
  }
  @Override
  public void setRequest(HttpServletRequest r) {
    this.req = r;
  }
  
}