From aa7eec382d53c85c4e5a2d8c6ec351aba85b6136 Mon Sep 17 00:00:00 2001 From: ulrich <undisclosed> Date: Thu, 05 Apr 2018 18:33:20 +0000 Subject: [PATCH] file content bei collectFiles hinzugenommen --- src/de/uhilger/wbx/WbxUtils.java | 90 +++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 90 insertions(+), 0 deletions(-) diff --git a/src/de/uhilger/wbx/WbxUtils.java b/src/de/uhilger/wbx/WbxUtils.java index 9705fce..d10955b 100644 --- a/src/de/uhilger/wbx/WbxUtils.java +++ b/src/de/uhilger/wbx/WbxUtils.java @@ -18,7 +18,13 @@ package de.uhilger.wbx; +import de.uhilger.wbx.data.Inhalt; +import java.io.BufferedReader; import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.IOException; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; @@ -43,9 +49,93 @@ public static final String NO_STRING = " ist kein String"; public static final String EMPTY_STRING = ""; + public static final String WBX_FILE_BASE = "wbxFileBase"; + public static final String WBX_PUB_DIR_NAME = "wbxPubDirName"; public static final String WBX_DEFAULT_PUB_DIR_NAME = "/www"; + public static final String WBX_PUB_URL_NAME = "wbxPubUrlName"; + public static final String WBX_DEFAULT_PUB_URL_NAME = "/data"; + + + public List<Inhalt> collectFiles(String requestUrl, String contextPath, + String relativePath, int maxTiefe, int maxAnzahl, int length) { + Bild bild = new Bild(); + //WbxUtils wu = new WbxUtils(); + String basis = getJNDIParameter(WBX_FILE_BASE, WbxUtils.EMPTY_STRING); + String pubDirName = getJNDIParameter(WbxUtils.WBX_PUB_DIR_NAME, WbxUtils.WBX_DEFAULT_PUB_DIR_NAME); + String pubUrlName = getJNDIParameter(WbxUtils.WBX_PUB_URL_NAME, WbxUtils.WBX_DEFAULT_PUB_URL_NAME); + String relPath = relativePath.replace(pubUrlName, pubDirName); + String absPath = basis + relPath; + + ArrayList beitraege = new ArrayList(); + ArrayList<Inhalt> files = new ArrayList<>(); + collectFiles(new File(absPath), 0, beitraege, maxTiefe, maxAnzahl); + + Iterator i = beitraege.iterator(); + while(i.hasNext()) { + File beitrag = (File) i.next(); + Inhalt cont = new Inhalt(); + cont.setMimetype(bild.getMimeType(beitrag)); + cont.setIsDirectory(beitrag.isDirectory()); + cont.setIsHidden(beitrag.isHidden()); + cont.setLastModified(beitrag.lastModified()); + cont.setLength(beitrag.length()); + if(length > 0) { + cont.setAbst(getFileContent(beitrag, length)); + } + + /* + den 'https://..'-Teil bis vor dem + ContextPath ermitteln + */ + //String requestUrl = getRequest().getRequestURL().toString(); + //String contextPath = getRequest().getContextPath(); + int pos = requestUrl.indexOf(contextPath); + + /* + den Teil des Pfades ermitteln, der zwischen dem + ContextPath zum oeffentlichen Ordner und dem Dateiname + steht + */ + String absolutePath = beitrag.getAbsolutePath(); + absolutePath = absolutePath.replace(beitrag.getName(), ""); + absolutePath = absolutePath.replace(pubDirName, ""); + String part = relativePath.replace(pubUrlName, ""); + int pos2 = absolutePath.indexOf(part); + String mittelteil = absolutePath.substring(pos2); + mittelteil = mittelteil.replace(part, ""); + cont.setBase(requestUrl.substring(0, pos)); + + cont.setUrl(/*requestUrl.substring(0, pos) + "/data" + */ mittelteil + beitrag.getName()); + files.add(cont); + } + return files; + } + + private String getFileContent(File file, int len) { + try { + StringBuffer readBuffer = new StringBuffer(); + char[] buf = new char[1]; + FileReader fr = new FileReader(file); + int bytesRead = fr.read(buf); + int read = 0; + while(bytesRead > -1 && read < len) { + read += bytesRead; + readBuffer.append(buf); + bytesRead = fr.read(buf); + } + readBuffer.append(buf); + fr.close(); + logger.fine("read: " + read + ", readBuffer.len: " + readBuffer.length()); + return readBuffer.toString(); + } catch (Exception ex) { + logger.log(Level.SEVERE, ex.getMessage(), ex); + return EMPTY_STRING; + } + } + + /** * Diese Methode funktioniert nur, wenn entweder ein Ordner uebergeben * wird, der keine Unterordner enthaelt wie zum Beispiel der Ordner 'neu' -- Gitblit v1.9.3