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