From 5c621434b008d1671accfc4b9c9c9016a256fd9f Mon Sep 17 00:00:00 2001
From: ulrich
Date: Wed, 21 Apr 2021 09:42:16 +0000
Subject: [PATCH] Ein/Aus-Schalter verbessert

---
 src/de/uhilger/mediaz/api/ListFileHandler.java |   44 +++++++++++++++++++++++++++++++++++++++++---
 1 files changed, 41 insertions(+), 3 deletions(-)

diff --git a/src/de/uhilger/mediaz/api/ListFileHandler.java b/src/de/uhilger/mediaz/api/ListFileHandler.java
index e6452eb..c54c9c7 100644
--- a/src/de/uhilger/mediaz/api/ListFileHandler.java
+++ b/src/de/uhilger/mediaz/api/ListFileHandler.java
@@ -17,6 +17,7 @@
  */
 package de.uhilger.mediaz.api;
 
+import com.sun.net.httpserver.Headers;
 import com.sun.net.httpserver.HttpExchange;
 import de.uhilger.mediaz.App;
 import de.uhilger.mediaz.Server;
@@ -27,13 +28,14 @@
 import de.uhilger.mediaz.store.StorageFile;
 import de.uhilger.mediaz.store.Track;
 import java.io.File;
+import java.io.FileFilter;
 import java.io.IOException;
 import java.io.OutputStream;
 import java.util.ArrayList;
 import java.util.Arrays;
-import java.util.Collections;
 import java.util.HashMap;
 import java.util.Map;
+import java.util.Set;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 
@@ -45,6 +47,9 @@
   
   /* Der Logger fuer diesen ListFileHandler */
   private static final Logger logger = Logger.getLogger(ListFileHandler.class.getName());
+  
+  private static final String[] specialChars = {new String("\u00c4"), new String("\u00d6"), 
+      new String("\u00dc"), new String("\u00e4"), new String("\u00f6"), new String("\u00fc"), new String("\u00df")};
   
   Map extMap = new HashMap();
   
@@ -80,7 +85,16 @@
       logger.fine(fName);
       File dir = new File(fileBase, fName);
       logger.fine(dir.getAbsolutePath());
-      File[] files = dir.listFiles();
+      File[] files = dir.listFiles(new FileFilter() {
+        @Override
+        public boolean accept(File pathname) {
+          Set keys = extMap.keySet();
+          String fname = pathname.getName();
+          int pos = fname.lastIndexOf(".");
+          String ext = fname.substring(pos+1);          
+          return keys.contains(ext) || pathname.isDirectory();
+        }
+      });
       Arrays.sort(files);      
       ArrayList list = new ArrayList();
       if(files != null) {
@@ -107,8 +121,11 @@
         }
       }
       //Collections.sort(list);
-      String json = jsonWithCustomType(list, "Medialiste");
+      String json = escapeHtml(jsonWithCustomType(list, "Medialiste"));
+      
       logger.fine(json);
+      Headers headers = e.getResponseHeaders();
+      headers.add("Content-Type", "application/json; charset=UTF-8");
       e.sendResponseHeaders(200, json.length());
       OutputStream os = e.getResponseBody();
       os.write(json.getBytes());
@@ -118,6 +135,27 @@
     }
   }
   
+  public String escapeHtml(String text) {
+    text = text.replace(specialChars[0], "Ae");
+    text = text.replace(specialChars[1], "Oe");
+    text = text.replace(specialChars[2], "Ue");
+    text = text.replace(specialChars[3], "ae");
+    text = text.replace(specialChars[4], "oe");
+    text = text.replace(specialChars[5], "ue");
+    text = text.replace(specialChars[6], "ss");
+    
+    /*
+    text = text.replace(specialChars[0], "Ä");
+    text = text.replace(specialChars[1], "Ö");
+    text = text.replace(specialChars[2], "Ü");
+    text = text.replace(specialChars[3], "ä");
+    text = text.replace(specialChars[4], "ö");
+    text = text.replace(specialChars[5], "ü");
+    text = text.replace(specialChars[6], "ß");
+    */
+    return text;
+  }
+  
   private void getTrack(File file, StorageFile sf) {
     if(sf.getTyp().equalsIgnoreCase(StorageFile.TYP_AUDIO)) {
       Track track = new Track(file);

--
Gitblit v1.9.3