From be4056eedcfdbe6e7aaa081eb2028f4e4518f577 Mon Sep 17 00:00:00 2001
From: ulrich
Date: Sun, 11 Apr 2021 11:57:41 +0000
Subject: [PATCH] Dateifilter fuer Kataloge

---
 src/de/uhilger/mediaz/api/ListFileHandler.java |   42 ++++++++++++++++++++++++++++++++++++------
 1 files changed, 36 insertions(+), 6 deletions(-)

diff --git a/src/de/uhilger/mediaz/api/ListFileHandler.java b/src/de/uhilger/mediaz/api/ListFileHandler.java
index acd3243..0b780aa 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;
@@ -25,14 +26,17 @@
 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.Collections;
 import java.util.HashMap;
-import java.util.List;
 import java.util.Map;
+import java.util.Set;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 
@@ -50,14 +54,14 @@
   public ListFileHandler(String absoluteDirectoryPathAndName) {
     super(absoluteDirectoryPathAndName);
     /*
-      Nachfolgend hart codiert die Ermittlung von Dateifiltern. 
+      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, "audioexts", StorageFile.TYP_AUDIO);
-    initMap(fs, "videoexts", StorageFile.TYP_VIDEO);
+    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) {
@@ -74,18 +78,29 @@
   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();
           String fname = file.getName();
           sf.setName(fname);
+          sf.setTitelAnzName(fname);
           if(file.isDirectory()) {
             sf.setTyp(StorageFile.TYP_FOLDER);
           } else {
@@ -95,6 +110,7 @@
             Object o = extMap.get(ext);
             if(o instanceof String) {
               sf.setTyp(o.toString());
+              getTrack(file, sf);
             } else {
               sf.setTyp(StorageFile.TYP_FILE);
             }
@@ -102,8 +118,11 @@
           list.add(sf);
         }
       }
+      //Collections.sort(list);
       String json = jsonWithCustomType(list, "Medialiste");
       logger.fine(json);
+      Headers headers = e.getResponseHeaders();
+      headers.add("Content-Type", "application/json");
       e.sendResponseHeaders(200, json.length());
       OutputStream os = e.getResponseBody();
       os.write(json.getBytes());
@@ -113,5 +132,16 @@
     }
   }
   
+  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