From cfc1869716f245658559acebf5651ca0968107a8 Mon Sep 17 00:00:00 2001 From: Ulrich <undisclosed> Date: Tue, 28 Feb 2017 11:28:14 +0000 Subject: [PATCH] Bestimmung des Orts der Dateiablage ueber Parameter oder hart kodiert fuer WebBox-Struktur --- src/java/de/uhilger/filecms/api/FileMgr.java | 128 ++++++++++++++++++++++++++++++++---------- 1 files changed, 98 insertions(+), 30 deletions(-) diff --git a/src/java/de/uhilger/filecms/api/FileMgr.java b/src/java/de/uhilger/filecms/api/FileMgr.java index 6a2be58..d045623 100644 --- a/src/java/de/uhilger/filecms/api/FileMgr.java +++ b/src/java/de/uhilger/filecms/api/FileMgr.java @@ -20,8 +20,11 @@ import de.uhilger.filecms.web.Initialiser; 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; import java.io.FileWriter; import java.io.IOException; import java.security.Principal; @@ -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,70 @@ 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(); + 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 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++) { + 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()); + delRefs[i] = new FileRef(targetFile.getAbsolutePath(), targetFile.isDirectory()); + } + } + FileSystem fs = new LocalFileSystem(); + fs.delete(delRefs); + result = "deleted"; + } catch (Throwable ex) { + logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex); } - return userName; + return result; } 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 +183,51 @@ } return savedFile; } + + /* ---- Hilfsfunktionen ---- */ + private File getTargetDir(String relPath) { + logger.fine(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.fine(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 String) { + String baseStr = (String) o; + logger.fine(baseStr); + File file = new File(baseStr); + base = new FileRef(file.getAbsolutePath(), file.isDirectory()); + } + 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