Persoenliche Mediazentrale
ulrich
2021-04-07 f45e203a66a5471a0dd05e0362566ef96a2b6b31
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*
  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.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 java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
 
/**
 *
 * @author ulrich
 */
public class ListFileHandler extends FileHandler {
  
  /* Der Logger fuer diesen ListFileHandler */
  private static final Logger logger = Logger.getLogger(ListFileHandler.class.getName());
  
  Map extMap = new HashMap();
  
  public ListFileHandler(String absoluteDirectoryPathAndName) {
    super(absoluteDirectoryPathAndName);
    /*
      Nachfolgend hart codiert die 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);
  }
 
  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))) {
      String fName = getFileName(e);
      logger.fine(fName);
      File dir = new File(fileBase, fName);
      logger.fine(dir.getAbsolutePath());
      File[] files = dir.listFiles();
      ArrayList list = new ArrayList();
      if(files != null) {
        for(File file : files) {
          StorageFile sf = new StorageFile();
          String fname = file.getName();
          sf.setName(fname);
          if(file.isDirectory()) {
            sf.setTyp(StorageFile.TYP_FOLDER);
          } else {
            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());
            } else {
              sf.setTyp(StorageFile.TYP_FILE);
            }
          }
          list.add(sf);
        }
      }
      String json = jsonWithCustomType(list, "Medialiste");
      logger.fine(json);
      e.sendResponseHeaders(200, json.length());
      OutputStream os = e.getResponseBody();
      os.write(json.getBytes());
      os.close();        
    } else {
      super.handle(e);
    }
  }
  
  
}