From 30a32f2235a73e7f7b360f99011a826ce7d25c72 Mon Sep 17 00:00:00 2001 From: ulrich <not disclosed> Date: Sat, 25 Feb 2017 09:58:36 +0000 Subject: [PATCH] readme hinzugefuegt --- src/java/de/uhilger/filecms/api/FileMgr.java | 102 +++++++++++++++++++++++++++++++++++--------------- 1 files changed, 71 insertions(+), 31 deletions(-) diff --git a/src/java/de/uhilger/filecms/api/FileMgr.java b/src/java/de/uhilger/filecms/api/FileMgr.java index 6a2be58..d70f816 100644 --- a/src/java/de/uhilger/filecms/api/FileMgr.java +++ b/src/java/de/uhilger/filecms/api/FileMgr.java @@ -22,8 +22,11 @@ import de.uhilger.filesystem.FileRef; import de.uhilger.filesystem.LocalFileSystem; import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; +import java.io.StringReader; import java.security.Principal; import java.util.ArrayList; import java.util.List; @@ -57,9 +60,9 @@ files.add(namedHomeFolder); files.add(namedPublicFolder); } else { - LocalFileSystem fs = new LocalFileSystem(); String path = getTargetDir(relPath).getAbsolutePath(); logger.fine(path); + LocalFileSystem fs = new LocalFileSystem(); FileRef[] fileRefs = fs.list(new FileRef(getTargetDir(relPath).getAbsolutePath(), true)); for(int i = 0; i < fileRefs.length; i++) { files.add(fileRefs[i]); @@ -85,44 +88,45 @@ return new FileRef(targetDir.getAbsolutePath(), true); } - 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() { - FileRef base = null; - Object o = getServletContext().getAttribute(Initialiser.FILE_BASE); - if(o instanceof FileRef) { - base = (FileRef) o; - } - return base; - } - - private String getUserName() { - String userName = null; + public String getCode(String relPath, String fileName) { + String code = null; + Object p = getRequest().getUserPrincipal(); if(p instanceof Principal) { - userName = ((Principal) p).getName(); - } - return userName; + FileReader reader = null; + try { + File targetFile = new File(getTargetDir(relPath), fileName); + 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); + } + } + + } + + return code; } public FileRef saveTextFile(String relPath, String fileName, String contents) { FileRef savedFile = null; try { FileRef datenRef = getBase(); - File daten = new File(datenRef.getAbsolutePath()); + //File daten = new File(datenRef.getAbsolutePath()); Object p = getRequest().getUserPrincipal(); if(p instanceof Principal) { File targetFile = new File(getTargetDir(relPath), fileName); @@ -154,12 +158,48 @@ } return savedFile; } + + /* ---- Hilfsfunktionen ---- */ + 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() { + FileRef base = null; + Object o = getServletContext().getAttribute(Initialiser.FILE_BASE); + if(o instanceof FileRef) { + base = (FileRef) o; + } + return base; + } + + private String getUserName() { + String userName = null; + Object p = getRequest().getUserPrincipal(); + if(p instanceof Principal) { + userName = ((Principal) p).getName(); + } + return userName; + } + + /* private File getWebappsDir() { File cfile = new File(this.getClass().getResource( this.getClass().getSimpleName() + ".class").getFile()); String path = cfile.getAbsolutePath(); return new File(path.substring(0, path.indexOf(getRequest().getContextPath()))); } + */ } -- Gitblit v1.9.3