Persoenliche Mediazentrale
ulrich
2021-04-07 f45e203a66a5471a0dd05e0362566ef96a2b6b31
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;
U 28 import java.io.File;
29 import java.io.IOException;
30 import java.io.OutputStream;
31 import java.util.ArrayList;
dce2c7 32 import java.util.HashMap;
U 33 import java.util.Map;
34 import java.util.logging.Level;
86bbf7 35 import java.util.logging.Logger;
U 36
37 /**
38  *
39  * @author ulrich
40  */
41 public class ListFileHandler extends FileHandler {
42   
43   /* Der Logger fuer diesen ListFileHandler */
44   private static final Logger logger = Logger.getLogger(ListFileHandler.class.getName());
45   
dce2c7 46   Map extMap = new HashMap();
U 47   
86bbf7 48   public ListFileHandler(String absoluteDirectoryPathAndName) {
U 49     super(absoluteDirectoryPathAndName);
dce2c7 50     /*
U 51       Nachfolgend hart codiert die Ermittlung von Dateifiltern. 
52       Sie werden erwartet in den Einstellungen 'audioexts' und 'videoexts'
53       jeweils als Dateierweiterungen mit Komma getrennt
54       z.B. "mp4,m4v"
55     */
56     FileStorage fs = new FileStorage(App.getInitParameter(App.getRs(App.RB_AP_CONF)));
57     initMap(fs, "audioexts", StorageFile.TYP_AUDIO);
58     initMap(fs, "videoexts", StorageFile.TYP_VIDEO);
86bbf7 59   }
U 60
dce2c7 61   private void initMap(Storage s, String key, String typ) {
U 62     Entity e = s.read(Einstellung.class.getSimpleName(), key);
63     if(e instanceof Einstellung) {
64       String[] exts = ((Einstellung) e).getValue().split(",");
65       for(String ext : exts) {
66         extMap.put(ext, typ);
67       }
68     }
69   }
70   
86bbf7 71   @Override
U 72   public void handle(HttpExchange e) throws IOException {
73     String path = e.getRequestURI().toString();
74     logger.fine(path);
75     if(path.endsWith(App.getRs(Server.RB_SLASH))) {
76       String fName = getFileName(e);
77       logger.fine(fName);
78       File dir = new File(fileBase, fName);
79       logger.fine(dir.getAbsolutePath());
80       File[] files = dir.listFiles();
81       ArrayList list = new ArrayList();
82       if(files != null) {
83         for(File file : files) {
84           StorageFile sf = new StorageFile();
7c22a2 85           String fname = file.getName();
U 86           sf.setName(fname);
86bbf7 87           if(file.isDirectory()) {
822ddf 88             sf.setTyp(StorageFile.TYP_FOLDER);
86bbf7 89           } else {
dce2c7 90             int pos = fname.lastIndexOf(".");
U 91             String ext = fname.substring(pos+1);
92             logger.log(Level.FINE, "ext: {0}", ext);
93             Object o = extMap.get(ext);
94             if(o instanceof String) {
95               sf.setTyp(o.toString());
7c22a2 96             } else {
U 97               sf.setTyp(StorageFile.TYP_FILE);
98             }
86bbf7 99           }
U 100           list.add(sf);
101         }
102       }
103       String json = jsonWithCustomType(list, "Medialiste");
104       logger.fine(json);
105       e.sendResponseHeaders(200, json.length());
106       OutputStream os = e.getResponseBody();
107       os.write(json.getBytes());
108       os.close();        
109     } else {
110       super.handle(e);
111     }
112   }
113   
114   
115 }