From d769f3445ed4779715b5c2570731b4bb73d8d2aa Mon Sep 17 00:00:00 2001
From: ulrich
Date: Tue, 20 Apr 2021 17:15:03 +0000
Subject: [PATCH] Geraete-Liste mit Status (in Arbeit)

---
 src/de/uhilger/mediaz/api/ListFileHandler.java |  123 +++++++++++++++++++++++++++++++++++++---
 1 files changed, 113 insertions(+), 10 deletions(-)

diff --git a/src/de/uhilger/mediaz/api/ListFileHandler.java b/src/de/uhilger/mediaz/api/ListFileHandler.java
index f04db2b..c54c9c7 100644
--- a/src/de/uhilger/mediaz/api/ListFileHandler.java
+++ b/src/de/uhilger/mediaz/api/ListFileHandler.java
@@ -1,18 +1,42 @@
 /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
+  Mediazentrale - Personal Media Center
+  Copyright (C) 2021  Ulrich Hilger
+
+  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 <https://www.gnu.org/licenses/>.
  */
 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;
+import de.uhilger.mediaz.entity.Einstellung;
+import de.uhilger.mediaz.entity.Entity;
+import de.uhilger.mediaz.store.FileStorage;
+import de.uhilger.mediaz.store.Storage;
 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.HashMap;
+import java.util.Map;
+import java.util.Set;
+import java.util.logging.Level;
 import java.util.logging.Logger;
 
 /**
@@ -24,37 +48,84 @@
   /* 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();
+  
   public ListFileHandler(String absoluteDirectoryPathAndName) {
     super(absoluteDirectoryPathAndName);
+    /*
+      Ermittlung von Dateifiltern. 
+      Sie werden erwartet in den Einstellungen 'audioexts' und 'videoexts'
+      jeweils als Dateierweiterungen mit Komma getrennt
+      z.B. "mp4,m4v"
+    */
+    FileStorage fs = new FileStorage(App.getInitParameter(App.getRs(App.RB_AP_CONF)));
+    initMap(fs, App.getRs(App.RB_AUDIOEXTS), StorageFile.TYP_AUDIO);
+    initMap(fs, App.getRs(App.RB_VIDEOEXTS), StorageFile.TYP_VIDEO);
   }
 
+  private void initMap(Storage s, String key, String typ) {
+    Entity e = s.read(Einstellung.class.getSimpleName(), key);
+    if(e instanceof Einstellung) {
+      String[] exts = ((Einstellung) e).getValue().split(",");
+      for(String ext : exts) {
+        extMap.put(ext, typ);
+      }
+    }
+  }
+  
   @Override
   public void handle(HttpExchange e) throws IOException {
     String path = e.getRequestURI().toString();
     logger.fine(path);
-    if(path.endsWith(App.getRs(Server.RB_SLASH))) {
+    if(path.endsWith(Server.SLASH)) {
       String fName = getFileName(e);
       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) {
         for(File file : files) {
           StorageFile sf = new StorageFile();
-          sf.setName(file.getName());
+          String fname = file.getName();
+          sf.setName(fname);
+          sf.setTitelAnzName(fname);
           if(file.isDirectory()) {
             sf.setTyp(StorageFile.TYP_FOLDER);
           } else {
-            sf.setTyp(StorageFile.TYP_FILE);
+            int pos = fname.lastIndexOf(".");
+            String ext = fname.substring(pos+1);
+            logger.log(Level.FINE, "ext: {0}", ext);
+            Object o = extMap.get(ext);
+            if(o instanceof String) {
+              sf.setTyp(o.toString());
+              getTrack(file, sf);
+            } else {
+              sf.setTyp(StorageFile.TYP_FILE);
+            }
           }
           list.add(sf);
         }
       }
-      //Gson gson = new Gson();
-      //String json = gson.toJson(fileNames);
-      String json = jsonWithCustomType(list, "Medialiste");
+      //Collections.sort(list);
+      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());
@@ -64,5 +135,37 @@
     }
   }
   
+  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], "&Auml;");
+    text = text.replace(specialChars[1], "&Ouml;");
+    text = text.replace(specialChars[2], "&Uuml;");
+    text = text.replace(specialChars[3], "&auml;");
+    text = text.replace(specialChars[4], "&ouml;");
+    text = text.replace(specialChars[5], "&uuml;");
+    text = text.replace(specialChars[6], "&szlig;");
+    */
+    return text;
+  }
+  
+  private void getTrack(File file, StorageFile sf) {
+    if(sf.getTyp().equalsIgnoreCase(StorageFile.TYP_AUDIO)) {
+      Track track = new Track(file);
+      sf.setInterpret(track.getArtist());
+      String trackTitel = track.getTitle();
+      if(trackTitel != null && trackTitel.length() > 0) {
+        sf.setTitelAnzName(trackTitel);
+      }
+      sf.setAlbum(track.getAlbum());
+    }
+  }
   
 }

--
Gitblit v1.9.3