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