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