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