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 |
|
095119
|
20 |
import com.sun.net.httpserver.Headers; |
86bbf7
|
21 |
import com.sun.net.httpserver.HttpExchange; |
U |
22 |
import de.uhilger.mediaz.App; |
|
23 |
import de.uhilger.mediaz.Server; |
dce2c7
|
24 |
import de.uhilger.mediaz.entity.Einstellung; |
U |
25 |
import de.uhilger.mediaz.entity.Entity; |
|
26 |
import de.uhilger.mediaz.store.FileStorage; |
|
27 |
import de.uhilger.mediaz.store.Storage; |
86bbf7
|
28 |
import de.uhilger.mediaz.store.StorageFile; |
37eadf
|
29 |
import de.uhilger.mediaz.store.Track; |
86bbf7
|
30 |
import java.io.File; |
be4056
|
31 |
import java.io.FileFilter; |
86bbf7
|
32 |
import java.io.IOException; |
U |
33 |
import java.io.OutputStream; |
|
34 |
import java.util.ArrayList; |
d4d091
|
35 |
import java.util.Arrays; |
dce2c7
|
36 |
import java.util.HashMap; |
U |
37 |
import java.util.Map; |
be4056
|
38 |
import java.util.Set; |
dce2c7
|
39 |
import java.util.logging.Level; |
86bbf7
|
40 |
import java.util.logging.Logger; |
U |
41 |
|
|
42 |
/** |
|
43 |
* |
|
44 |
* @author ulrich |
|
45 |
*/ |
|
46 |
public class ListFileHandler extends FileHandler { |
|
47 |
|
|
48 |
/* Der Logger fuer diesen ListFileHandler */ |
|
49 |
private static final Logger logger = Logger.getLogger(ListFileHandler.class.getName()); |
|
50 |
|
630b44
|
51 |
private static final String[] specialChars = {new String("\u00c4"), new String("\u00d6"), |
U |
52 |
new String("\u00dc"), new String("\u00e4"), new String("\u00f6"), new String("\u00fc"), new String("\u00df")}; |
|
53 |
|
dce2c7
|
54 |
Map extMap = new HashMap(); |
U |
55 |
|
86bbf7
|
56 |
public ListFileHandler(String absoluteDirectoryPathAndName) { |
U |
57 |
super(absoluteDirectoryPathAndName); |
dce2c7
|
58 |
/* |
0e9cd3
|
59 |
Ermittlung von Dateifiltern. |
dce2c7
|
60 |
Sie werden erwartet in den Einstellungen 'audioexts' und 'videoexts' |
U |
61 |
jeweils als Dateierweiterungen mit Komma getrennt |
|
62 |
z.B. "mp4,m4v" |
|
63 |
*/ |
|
64 |
FileStorage fs = new FileStorage(App.getInitParameter(App.getRs(App.RB_AP_CONF))); |
0e9cd3
|
65 |
initMap(fs, App.getRs(App.RB_AUDIOEXTS), StorageFile.TYP_AUDIO); |
U |
66 |
initMap(fs, App.getRs(App.RB_VIDEOEXTS), StorageFile.TYP_VIDEO); |
86bbf7
|
67 |
} |
U |
68 |
|
dce2c7
|
69 |
private void initMap(Storage s, String key, String typ) { |
U |
70 |
Entity e = s.read(Einstellung.class.getSimpleName(), key); |
|
71 |
if(e instanceof Einstellung) { |
|
72 |
String[] exts = ((Einstellung) e).getValue().split(","); |
|
73 |
for(String ext : exts) { |
|
74 |
extMap.put(ext, typ); |
|
75 |
} |
|
76 |
} |
|
77 |
} |
|
78 |
|
86bbf7
|
79 |
@Override |
U |
80 |
public void handle(HttpExchange e) throws IOException { |
|
81 |
String path = e.getRequestURI().toString(); |
|
82 |
logger.fine(path); |
0e9cd3
|
83 |
if(path.endsWith(Server.SLASH)) { |
86bbf7
|
84 |
String fName = getFileName(e); |
U |
85 |
logger.fine(fName); |
|
86 |
File dir = new File(fileBase, fName); |
|
87 |
logger.fine(dir.getAbsolutePath()); |
be4056
|
88 |
File[] files = dir.listFiles(new FileFilter() { |
U |
89 |
@Override |
|
90 |
public boolean accept(File pathname) { |
|
91 |
Set keys = extMap.keySet(); |
|
92 |
String fname = pathname.getName(); |
|
93 |
int pos = fname.lastIndexOf("."); |
|
94 |
String ext = fname.substring(pos+1); |
|
95 |
return keys.contains(ext) || pathname.isDirectory(); |
|
96 |
} |
|
97 |
}); |
d4d091
|
98 |
Arrays.sort(files); |
86bbf7
|
99 |
ArrayList list = new ArrayList(); |
U |
100 |
if(files != null) { |
|
101 |
for(File file : files) { |
|
102 |
StorageFile sf = new StorageFile(); |
7c22a2
|
103 |
String fname = file.getName(); |
U |
104 |
sf.setName(fname); |
b56bb3
|
105 |
sf.setTitelAnzName(fname); |
86bbf7
|
106 |
if(file.isDirectory()) { |
822ddf
|
107 |
sf.setTyp(StorageFile.TYP_FOLDER); |
86bbf7
|
108 |
} else { |
dce2c7
|
109 |
int pos = fname.lastIndexOf("."); |
U |
110 |
String ext = fname.substring(pos+1); |
|
111 |
logger.log(Level.FINE, "ext: {0}", ext); |
|
112 |
Object o = extMap.get(ext); |
|
113 |
if(o instanceof String) { |
|
114 |
sf.setTyp(o.toString()); |
37eadf
|
115 |
getTrack(file, sf); |
7c22a2
|
116 |
} else { |
U |
117 |
sf.setTyp(StorageFile.TYP_FILE); |
|
118 |
} |
86bbf7
|
119 |
} |
U |
120 |
list.add(sf); |
|
121 |
} |
|
122 |
} |
d4d091
|
123 |
//Collections.sort(list); |
630b44
|
124 |
String json = escapeHtml(jsonWithCustomType(list, "Medialiste")); |
U |
125 |
|
86bbf7
|
126 |
logger.fine(json); |
095119
|
127 |
Headers headers = e.getResponseHeaders(); |
630b44
|
128 |
headers.add("Content-Type", "application/json; charset=UTF-8"); |
86bbf7
|
129 |
e.sendResponseHeaders(200, json.length()); |
U |
130 |
OutputStream os = e.getResponseBody(); |
|
131 |
os.write(json.getBytes()); |
|
132 |
os.close(); |
|
133 |
} else { |
|
134 |
super.handle(e); |
|
135 |
} |
|
136 |
} |
|
137 |
|
630b44
|
138 |
public String escapeHtml(String text) { |
U |
139 |
text = text.replace(specialChars[0], "Ae"); |
|
140 |
text = text.replace(specialChars[1], "Oe"); |
|
141 |
text = text.replace(specialChars[2], "Ue"); |
|
142 |
text = text.replace(specialChars[3], "ae"); |
|
143 |
text = text.replace(specialChars[4], "oe"); |
|
144 |
text = text.replace(specialChars[5], "ue"); |
|
145 |
text = text.replace(specialChars[6], "ss"); |
|
146 |
|
|
147 |
/* |
|
148 |
text = text.replace(specialChars[0], "Ä"); |
|
149 |
text = text.replace(specialChars[1], "Ö"); |
|
150 |
text = text.replace(specialChars[2], "Ü"); |
|
151 |
text = text.replace(specialChars[3], "ä"); |
|
152 |
text = text.replace(specialChars[4], "ö"); |
|
153 |
text = text.replace(specialChars[5], "ü"); |
|
154 |
text = text.replace(specialChars[6], "ß"); |
|
155 |
*/ |
|
156 |
return text; |
|
157 |
} |
|
158 |
|
37eadf
|
159 |
private void getTrack(File file, StorageFile sf) { |
U |
160 |
if(sf.getTyp().equalsIgnoreCase(StorageFile.TYP_AUDIO)) { |
|
161 |
Track track = new Track(file); |
|
162 |
sf.setInterpret(track.getArtist()); |
b56bb3
|
163 |
String trackTitel = track.getTitle(); |
U |
164 |
if(trackTitel != null && trackTitel.length() > 0) { |
|
165 |
sf.setTitelAnzName(trackTitel); |
|
166 |
} |
37eadf
|
167 |
sf.setAlbum(track.getAlbum()); |
U |
168 |
} |
|
169 |
} |
86bbf7
|
170 |
|
U |
171 |
} |