From a1333d058500619b1311e206127a1cc01b0be8f6 Mon Sep 17 00:00:00 2001
From: ulrich
Date: Fri, 11 Feb 2022 15:27:42 +0000
Subject: [PATCH] Erweiterung auf Kataloge fuer Fotografien

---
 src/de/uhilger/tango/api/ListFileHandler.java |   58 ++++++++++++++++++++++++++++++++++++++++++++--------------
 1 files changed, 44 insertions(+), 14 deletions(-)

diff --git a/src/de/uhilger/tango/api/ListFileHandler.java b/src/de/uhilger/tango/api/ListFileHandler.java
index ad074e3..2a3b45c 100644
--- a/src/de/uhilger/tango/api/ListFileHandler.java
+++ b/src/de/uhilger/tango/api/ListFileHandler.java
@@ -26,7 +26,6 @@
 import de.uhilger.tango.store.FileStorage;
 import de.uhilger.tango.store.Storage;
 import de.uhilger.tango.store.StorageFile;
-import de.uhilger.tango.store.Track;
 import java.io.File;
 import java.io.FileFilter;
 import java.io.IOException;
@@ -38,6 +37,10 @@
 import java.util.Set;
 import java.util.logging.Level;
 import java.util.logging.Logger;
+import org.farng.mp3.MP3File;
+import org.farng.mp3.TagException;
+import org.farng.mp3.id3.AbstractID3v2;
+import org.farng.mp3.id3.ID3v1;
 
 /**
  *
@@ -45,15 +48,23 @@
  */
 public class ListFileHandler extends FileHandler {
   
+  public static final String RB_AUDIOEXTS = "audioexts";
+  public static final String RB_VIDEOEXTS = "videoexts";
+  public static final String RB_FOTOEXTS = "imageexts";
+  
   /* 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")};
   
+  public static final String UNWANTED_PATTERN = "[^a-zA-Z_0-9 ]";
+  
   Map extMap = new HashMap();
   
-  public ListFileHandler(String absoluteDirectoryPathAndName) {
+  private String conf;
+  
+  public ListFileHandler(String absoluteDirectoryPathAndName, String conf) {
     super(absoluteDirectoryPathAndName);
     /*
       Ermittlung von Dateifiltern. 
@@ -61,9 +72,10 @@
       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);
+    FileStorage fs = new FileStorage(conf);
+    initMap(fs, getResString(RB_AUDIOEXTS), StorageFile.TYP_AUDIO);
+    initMap(fs, getResString(RB_VIDEOEXTS), StorageFile.TYP_VIDEO);
+    initMap(fs, getResString(RB_FOTOEXTS), StorageFile.TYP_FOTO);
   }
 
   private void initMap(Storage s, String key, String typ) {
@@ -112,7 +124,7 @@
             Object o = extMap.get(ext);
             if(o instanceof String) {
               sf.setTyp(o.toString());
-              getTrack(file, sf);
+              getMetadata(file, sf);
             } else {
               sf.setTyp(StorageFile.TYP_FILE);
             }
@@ -121,7 +133,8 @@
         }
       }
       //Collections.sort(list);
-      String json = escapeHtml(jsonWithCustomType(list, "Medialiste"));
+      String rawjson = jsonWithCustomType(list, "Medialiste");
+      String json = escapeHtml(rawjson);
       
       logger.fine(json);
       Headers headers = e.getResponseHeaders();
@@ -156,15 +169,32 @@
     return text;
   }
   
-  private void getTrack(File file, StorageFile sf) {
+  private void getMetadata(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);
+      try {
+        MP3File mp3 = new MP3File(file);
+        ID3v1 tag = mp3.getID3v1Tag();
+        if(tag == null) {
+          AbstractID3v2 tag2 = mp3.getID3v2Tag();
+          sf.setInterpret(tag2.getLeadArtist().replaceAll(UNWANTED_PATTERN, ""));
+          String trackTitel = tag2.getSongTitle().replaceAll(UNWANTED_PATTERN, "");
+          if(trackTitel != null && trackTitel.length() > 0) {
+            sf.setTitelAnzName(trackTitel);
+          }
+          sf.setAlbum(tag2.getAlbumTitle().replaceAll(UNWANTED_PATTERN, ""));
+        } else {
+          sf.setInterpret(tag.getArtist().replaceAll(UNWANTED_PATTERN, ""));
+          String trackTitel = tag.getTitle().replaceAll(UNWANTED_PATTERN, "");
+          if(trackTitel != null && trackTitel.length() > 0) {
+            sf.setTitelAnzName(trackTitel);
+          }
+          sf.setAlbum(tag.getAlbumTitle().replaceAll(UNWANTED_PATTERN, ""));
+        }
+      } catch (IOException ex) {
+        Logger.getLogger(ListFileHandler.class.getName()).log(Level.SEVERE, null, ex);
+      } catch (TagException ex) {
+        Logger.getLogger(ListFileHandler.class.getName()).log(Level.SEVERE, null, ex);
       }
-      sf.setAlbum(track.getAlbum());
     }
   }
   

--
Gitblit v1.9.3