Persoenliche Mediazentrale
ulrich
2021-04-11 78d7078d97625e4a8d2d6863318c192f910f2ec9
commit | author | age
86bbf7 1 /*
cf6509 2   Mediazentrale - Personal Media Center
U 3   Copyright (C) 2021  Ulrich Hilger
4
5   This program is free software: you can redistribute it and/or modify
6   it under the terms of the GNU Affero General Public License as
7   published by the Free Software Foundation, either version 3 of the
8   License, or (at your option) any later version.
9
10   This program is distributed in the hope that it will be useful,
11   but WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   GNU Affero General Public License for more details.
14
15   You should have received a copy of the GNU Affero General Public License
16   along with this program.  If not, see <https://www.gnu.org/licenses/>.
86bbf7 17  */
U 18 package de.uhilger.mediaz.api;
19
20 import com.sun.net.httpserver.HttpExchange;
21 import de.uhilger.mediaz.App;
22 import de.uhilger.mediaz.Server;
dce2c7 23 import de.uhilger.mediaz.entity.Einstellung;
U 24 import de.uhilger.mediaz.entity.Entity;
25 import de.uhilger.mediaz.store.FileStorage;
26 import de.uhilger.mediaz.store.Storage;
86bbf7 27 import de.uhilger.mediaz.store.StorageFile;
37eadf 28 import de.uhilger.mediaz.store.Track;
86bbf7 29 import java.io.File;
U 30 import java.io.IOException;
31 import java.io.OutputStream;
32 import java.util.ArrayList;
d4d091 33 import java.util.Arrays;
U 34 import java.util.Collections;
dce2c7 35 import java.util.HashMap;
U 36 import java.util.Map;
37 import java.util.logging.Level;
86bbf7 38 import java.util.logging.Logger;
U 39
40 /**
41  *
42  * @author ulrich
43  */
44 public class ListFileHandler extends FileHandler {
45   
46   /* Der Logger fuer diesen ListFileHandler */
47   private static final Logger logger = Logger.getLogger(ListFileHandler.class.getName());
48   
dce2c7 49   Map extMap = new HashMap();
U 50   
86bbf7 51   public ListFileHandler(String absoluteDirectoryPathAndName) {
U 52     super(absoluteDirectoryPathAndName);
dce2c7 53     /*
0e9cd3 54       Ermittlung von Dateifiltern. 
dce2c7 55       Sie werden erwartet in den Einstellungen 'audioexts' und 'videoexts'
U 56       jeweils als Dateierweiterungen mit Komma getrennt
57       z.B. "mp4,m4v"
58     */
59     FileStorage fs = new FileStorage(App.getInitParameter(App.getRs(App.RB_AP_CONF)));
0e9cd3 60     initMap(fs, App.getRs(App.RB_AUDIOEXTS), StorageFile.TYP_AUDIO);
U 61     initMap(fs, App.getRs(App.RB_VIDEOEXTS), StorageFile.TYP_VIDEO);
86bbf7 62   }
U 63
dce2c7 64   private void initMap(Storage s, String key, String typ) {
U 65     Entity e = s.read(Einstellung.class.getSimpleName(), key);
66     if(e instanceof Einstellung) {
67       String[] exts = ((Einstellung) e).getValue().split(",");
68       for(String ext : exts) {
69         extMap.put(ext, typ);
70       }
71     }
72   }
73   
86bbf7 74   @Override
U 75   public void handle(HttpExchange e) throws IOException {
76     String path = e.getRequestURI().toString();
77     logger.fine(path);
0e9cd3 78     if(path.endsWith(Server.SLASH)) {
86bbf7 79       String fName = getFileName(e);
U 80       logger.fine(fName);
81       File dir = new File(fileBase, fName);
82       logger.fine(dir.getAbsolutePath());
83       File[] files = dir.listFiles();
d4d091 84       Arrays.sort(files);      
86bbf7 85       ArrayList list = new ArrayList();
U 86       if(files != null) {
87         for(File file : files) {
88           StorageFile sf = new StorageFile();
7c22a2 89           String fname = file.getName();
U 90           sf.setName(fname);
b56bb3 91           sf.setTitelAnzName(fname);
86bbf7 92           if(file.isDirectory()) {
822ddf 93             sf.setTyp(StorageFile.TYP_FOLDER);
86bbf7 94           } else {
dce2c7 95             int pos = fname.lastIndexOf(".");
U 96             String ext = fname.substring(pos+1);
97             logger.log(Level.FINE, "ext: {0}", ext);
98             Object o = extMap.get(ext);
99             if(o instanceof String) {
100               sf.setTyp(o.toString());
37eadf 101               getTrack(file, sf);
7c22a2 102             } else {
U 103               sf.setTyp(StorageFile.TYP_FILE);
104             }
86bbf7 105           }
U 106           list.add(sf);
107         }
108       }
d4d091 109       //Collections.sort(list);
86bbf7 110       String json = jsonWithCustomType(list, "Medialiste");
U 111       logger.fine(json);
112       e.sendResponseHeaders(200, json.length());
113       OutputStream os = e.getResponseBody();
114       os.write(json.getBytes());
115       os.close();        
116     } else {
117       super.handle(e);
118     }
119   }
120   
37eadf 121   private void getTrack(File file, StorageFile sf) {
U 122     if(sf.getTyp().equalsIgnoreCase(StorageFile.TYP_AUDIO)) {
123       Track track = new Track(file);
124       sf.setInterpret(track.getArtist());
b56bb3 125       String trackTitel = track.getTitle();
U 126       if(trackTitel != null && trackTitel.length() > 0) {
127         sf.setTitelAnzName(trackTitel);
128       }
37eadf 129       sf.setAlbum(track.getAlbum());
U 130     }
131   }
86bbf7 132   
U 133 }