Dateiverwaltung für die WebBox
ulrich
2017-02-20 2121ccd4015d7c0dc81485fcaf8655bde28ea396
Dateien bearbeiten in Arbeit
5 files modified
2 files added
299 ■■■■ changed files
src/java/de/uhilger/filecms/api/FileMgr.java 49 ●●●● patch | view | raw | blame | history
src/java/de/uhilger/filecms/web/HomeFilter.java 98 ●●●●● patch | view | raw | blame | history
src/java/de/uhilger/filecms/web/Initialiser.java 4 ●●●● patch | view | raw | blame | history
src/java/logging.properties 67 ●●●●● patch | view | raw | blame | history
web/WEB-INF/web.xml 9 ●●●●● patch | view | raw | blame | history
web/ui/index.html 4 ●●●● patch | view | raw | blame | history
web/ui/ui.js 68 ●●●● patch | view | raw | blame | history
src/java/de/uhilger/filecms/api/FileMgr.java
@@ -26,7 +26,6 @@
import java.io.IOException;
import java.security.Principal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
@@ -43,10 +42,10 @@
  public static final String PUB_DIR_NAME = "Oeffentlich";
  public static final String HOME_DIR_NAME = "Persoenlicher Ordner";
  
  private FileRef homeFolder;
  private FileRef publicFolder;
  private FileRef namedHomeFolder;
  private FileRef namedPublicFolder;
  //private FileRef homeFolder;
  //private FileRef publicFolder;
  //private FileRef namedHomeFolder;
  //private FileRef namedPublicFolder;
  
      
  public String hallo() {
@@ -63,15 +62,16 @@
      homeFolder = new FileRef(getUserHomeDir().getAbsolutePath(), true);
      logger.info(homeFolder.getAbsolutePath());
      */
      namedPublicFolder = new FileRef(PUB_DIR_NAME, true);
      FileRef namedPublicFolder = new FileRef(PUB_DIR_NAME, true);
      logger.finer(namedPublicFolder.getAbsolutePath());
      namedHomeFolder = new FileRef(HOME_DIR_NAME, true);
      FileRef namedHomeFolder = new FileRef(HOME_DIR_NAME, true);
      logger.finer(namedHomeFolder.getAbsolutePath());
      files = new ArrayList<FileRef>();
      files = new ArrayList();
      files.add(namedHomeFolder);
      files.add(namedPublicFolder);
    } else {
      logger.info(relPath);
      /*
      logger.finer(relPath);
      String targetPath = null;
      if(relPath.startsWith(PUB_DIR_NAME)) {
        targetPath = PUB_DIR_PATH + getUserName() + "/" + relPath.substring(PUB_DIR_NAME.length());
@@ -80,18 +80,37 @@
      } else {
        // kann eigentlich nicht sein..
      }
      logger.info(targetPath);
      logger.finer(targetPath);
      File targetDir = new File(getBase().getAbsolutePath(), targetPath);
      */
      
      LocalFileSystem fs = new LocalFileSystem();
      FileRef[] fileRefs = fs.list(new FileRef(targetDir.getAbsolutePath(), true));
      String path = getTargetDir(relPath).getAbsolutePath();
      logger.fine(path);
      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());
      }
      //files = Arrays.asList(fileRefs);
    }
    
    return files;
  }
  private File getTargetDir(String relPath) {
      logger.finer(relPath);
      String targetPath = null;
      if(relPath.startsWith(PUB_DIR_NAME)) {
        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());
      } else {
        // kann eigentlich nicht sein..
      }
      logger.finer(targetPath);
      File targetDir = new File(getBase().getAbsolutePath(), targetPath);
      return targetDir;
  }
  
  private FileRef getBase() {
@@ -103,6 +122,7 @@
    return base;
  }
  
  /*
  private File getUserPubDir() {
    File userDir = null;
    File daten = new File(getBase().getAbsolutePath());
@@ -112,6 +132,7 @@
    }
    return userDir;
  }
  */
  
  private String getUserName() {
    String userName = null;
@@ -122,6 +143,7 @@
    return userName;
  }
  
  /*
  private File getUserHomeDir() {
    File userDir = null;
    File daten = new File(getBase().getAbsolutePath());
@@ -131,6 +153,7 @@
    }
    return userDir;
  }
  */
  
  public FileRef saveTextFile(String relPath, String fileName, String contents) {
    FileRef savedFile = null;
@@ -139,9 +162,11 @@
      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);
        */
        File targetFile = new File(getTargetDir(relPath), fileName);
        if(targetFile.exists()) {
          /*
            muss delete() sein?
src/java/de/uhilger/filecms/web/HomeFilter.java
New file
@@ -0,0 +1,98 @@
/*
    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.web;
import java.io.IOException;
import java.util.logging.Logger;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
/**
 * Filter fuer das home-Verzeichnis
 *
 * Nur Inhalte des angemeldeten Benutzers werden ausgegeben
 *
 * @author Copyright (c) Ulrich Hilger, http://uhilger.de
 * @author Published under the terms and conditions of the
 * <a href="http://www.gnu.org/licenses/agpl-3.0" target="_blank">GNU Affero
 * General Public License</a>
 *
 * @version 2, February 1, 2017
 */
public class HomeFilter implements Filter {
  private static final Logger logger = Logger.getLogger(HomeFilter.class.getName());
  private static final String ZIEL = "../api?c=de.uhilger.um.api.UserMgr&m=";
  @Override
  public void init(FilterConfig filterConfig) throws ServletException {
    // ...
  }
  /*
    Dieser Filter laesst nur Anfragen durch, die in username
    denselben Eintrag haben wie der Name des angemeldeten Benutzers.
    http]://example.com/file-cms/home/username/test.txt
    hier wird ein URL wie z.B.
    http]://example.com/um/svc/testmethode
    umgesetzt auf
    http]://example.com/um/api?c=de.uhilger.um.api.UserMgr&m=testmethode
    der Teil 'svc' ist im Filter Mapping des Deployment Descriptor
    deklariert. Der Teil 'api' zeigt mit Hilfe eines Servlet
    Mappings im Deployment Descriptor auf das Transit-Servlet.
    der statische Teil des Ziel-URL ist hier hart kodiert, das kann man
    freilich je nach Bedarf noch dynamisch gestalten
  */
  @Override
  public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
    if(request instanceof HttpServletRequest) {
      HttpServletRequest hr = (HttpServletRequest) request;
      logger.info("requestURL: " + hr.getRequestURL().toString());
      chain.doFilter(request, response);
    }
    /*
    if(request instanceof HttpServletRequest) {
      HttpServletRequest hr = (HttpServletRequest) request;
      String servletPath = hr.getServletPath();
      String[] path = servletPath.split("/");
      request.getRequestDispatcher(ZIEL + path[2]).forward(request, response);
    }
    */
  }
  @Override
  public void destroy() {
    // ...
  }
}
src/java/de/uhilger/filecms/web/Initialiser.java
@@ -87,7 +87,7 @@
    */
    
    File file = new File(".");
    logger.info(file.getAbsolutePath());
    logger.finer(file.getAbsolutePath());
    String path = file.getAbsolutePath();
    path = path.substring(0, path.length() - 1);
    file = new File(path);
@@ -98,7 +98,7 @@
    }
    file = new File(file, "daten/");
    ref = new FileRef(file.getAbsolutePath(), file.isDirectory());
    logger.info(ref.getAbsolutePath());
    logger.finer(ref.getAbsolutePath());
    return ref;
  }
  
src/java/logging.properties
New file
@@ -0,0 +1,67 @@
############################################################
#      Default Logging Configuration File
#
# You can use a different file by specifying a filename
# with the java.util.logging.config.file system property.
# For example java -Djava.util.logging.config.file=myfile
############################################################
############################################################
#      Global properties
############################################################
# "handlers" specifies a comma separated list of log Handler
# classes.  These handlers will be installed during VM startup.
# Note that these classes must be on the system classpath.
# By default we only configure a ConsoleHandler, which will only
# show messages at the INFO and above levels.
# handlers= java.util.logging.ConsoleHandler
# To also add the FileHandler, use the following line instead.
handlers= java.util.logging.FileHandler, java.util.logging.ConsoleHandler
# Default global logging level.
# This specifies which kinds of events are logged across
# all loggers.  For any given facility this global level
# can be overriden by a facility specific level
# Note that the ConsoleHandler also has a separate level
# setting to limit messages printed to the console.
# .level= FINE
.level = NONE
############################################################
# Handler specific properties.
# Describes specific configuration info for Handlers.
############################################################
# default file output is in user's home directory.
# java.util.logging.FileHandler.pattern = %h/java%u.log
# java.util.logging.FileHandler.pattern = /media/extmirror/tomcat747/logs/tv_%u.log
java.util.logging.FileHandler.pattern = ${catalina.base}/logs/file-cms_%u.log
java.util.logging.FileHandler.limit = 50000
# java.util.logging.FileHandler.count = 1
java.util.logging.FileHandler.count = 2
# java.util.logging.FileHandler.formatter = java.util.logging.XMLFormatter
java.util.logging.FileHandler.formatter = java.util.logging.SimpleFormatter
java.util.logging.FileHandler.level = OFF
# Limit the message that are printed on the console to INFO and above.
# java.util.logging.ConsoleHandler.level = INFO
java.util.logging.ConsoleHandler.level = FINER
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter
# Example to customize the SimpleFormatter output format
# to print one-line log message like this:
#     <level>: <log message> [<date/time>]
#
# java.util.logging.SimpleFormatter.format=%4$s: %5$s [%1$tc]%n
############################################################
# Facility specific properties.
# Provides extra control for each logger.
############################################################
# For example, set the com.xyz.foo logger to only log SEVERE
# messages:
# com.xyz.foo.level = SEVERE
de.uhilger.filecms.level = FINER
web/WEB-INF/web.xml
@@ -6,6 +6,15 @@
        <param-name>fileBase</param-name>
        <param-value>daten</param-value>
    </context-param>
    <filter>
        <description>Inhalte ausgeben</description>
        <filter-name>HomeFilter</filter-name>
        <filter-class>de.uhilger.filecms.web.HomeFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>HomeFilter</filter-name>
        <url-pattern>/home/*</url-pattern>
    </filter-mapping>
    <listener>
        <description>Der Initialiser setzt globale Variable fuer die Dateiverwaltung</description>
        <listener-class>de.uhilger.filecms.web.Initialiser</listener-class>
web/ui/index.html
@@ -13,7 +13,7 @@
      {{#files}}          
        <figure class="figure datei-figure text-center text-warning align-top">
          <i class="fa {{ typeClass }} fa-3x"></i>
          <figcaption class="figure-caption">{{ fr.absolutePath }}</figcaption>
          <figcaption class="figure-caption">{{ fileName }}</figcaption>
        </figure>   
      {{/files}}
          
@@ -47,7 +47,7 @@
              <a class="dropdown-item" href="#">Ansehen als Code</a>
              <a class="dropdown-item" href="#">Ansehen als Dokument</a>
              <div class="dropdown-divider"></div>
              <a class="dropdown-item" href="#">Bearbeiten als Code</a>
              <a id="editTextFile" class="dropdown-item" href="#">Bearbeiten als Code</a>
              <a class="dropdown-item" href="#">Bearbeiten als Dokument</a>
              <div class="dropdown-divider"></div>
              <a id="saveFile" class="dropdown-item" href="#">Speichern</a>
web/ui/ui.js
@@ -5,6 +5,7 @@
  $('.codeeditor-space').hide();
  $('.code-editor-container').hide();
  $('#newTextFile').on('click', fm_menu_neue_textdatei);
  $('#editTextFile').on('click', fm_menu_textdatei_bearbeiten);
  $('#saveFile').on('click', fm_menu_datei_speichern);
  $('#closeFile').on('click', fm_menu_datei_schliessen);
  $('#myModal').on('hidden.bs.modal', function (e) {
@@ -20,6 +21,16 @@
  $('.codeeditor-space').show();
  $('.code-editor-container').show();
  fm_code_edit('Test');
}
function fm_menu_textdatei_bearbeiten() {
  /*
  $('#dateiansicht').hide();
  $('.codeeditor-space').show();
  $('.code-editor-container').show();
  fm_code_edit('Test');
  */
 fm_get_file_content();
}
function fm_menu_datei_schliessen() {
@@ -58,28 +69,47 @@
  });  
}
function fm_get_file_content() {
  var fname = $('.datei-gewaehlt').text();
  console.log(pfad + '/' + fname);
  /*
  var m = '?c=de.uhilger.filecms.api.FileMgr&m=list&p=' + relPath;
  var u = '../svc' + m;
  fm_get(u, "json", function(resp) {
  });
  */
}
// http://localhost:8079/file-cms/svc?c=de.uhilger.filecms.api.FileMgr&f=JSONNICE&m=list&p=
function fm_get_list(relPath) {
  var m = '?c=de.uhilger.filecms.api.FileMgr&m=list&p=' + relPath;
  var u = '../svc' + m;
  fm_get(u, "json", function(resp) {
    //if(resp)
    var files = new Array();
    for(var i = 0; i < resp.List[0].FileRef.length; i++) {
      files.push(new FileRef(resp.List[0].FileRef[i]));
    if(resp.List[0].FileRef !== undefined) {
      var files = new Array();
      for(var i = 0; i < resp.List[0].FileRef.length; i++) {
        files.push(new FileRef(resp.List[0].FileRef[i]));
      }
      var fl = new FileList(files);
      var template = $('#tpl-kacheln').html();
      Mustache.parse(template);   // optional, speeds up future uses
      $('.figure').attr('onclick','').unbind('click');
      $('#dateien').empty();
      $('#dateien').html(Mustache.render(template, fl));
      $('.figure').click(fm_dateiwahl);
    } else {
      $('#dateien').empty();
    }
    var fl = new FileList(files);
    var template = $('#tpl-kacheln').html();
    Mustache.parse(template);   // optional, speeds up future uses
    $('.figure').attr('onclick','').unbind('click');
    $('#dateien').empty();
    $('#dateien').html(Mustache.render(template, fl));
    $('.figure').click(fm_dateiwahl);
    
    var dirList = new Array();
    var rp = '';
    var dirs = relPath.split('/');
    dirList.push(new BcrFile(rp, 'Home'))
    dirList.push(new BcrFile(rp, 'Home'));
    if(dirs.length > 0) {
      for(var i = 0; i < dirs.length; i++) {
        if(rp.length > 0 ) {
@@ -105,7 +135,7 @@
    // hier speichern
    var m = '?c=de.uhilger.filecms.api.FileMgr&m=saveTextFile';
    var u = '../svc' + m;
    fm_post(u, {p1: '', p2: $('#dateiname').val(), p3: cm.getValue()}, function(resp) {
    fm_post(u, {p1: pfad, p2: $('#dateiname').val(), p3: cm.getValue()}, function(resp) {
    });
  });
@@ -220,7 +250,17 @@
    } else {
      return 'fa-file';
    }
  }
  };
  this.fileName = function() {
    var namen = self.fr.absolutePath.split('/');
    if(namen.length > 0) {
      return namen[namen.length - 1];
    } else {
      return self.fr.absolutePath;
    }
  };
}
function BcrFiles(fl) {