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; |
|
23 |
import de.uhilger.mediaz.store.StorageFile; |
|
24 |
import java.io.File; |
|
25 |
import java.io.IOException; |
|
26 |
import java.io.OutputStream; |
|
27 |
import java.util.ArrayList; |
|
28 |
import java.util.logging.Logger; |
|
29 |
|
|
30 |
/** |
|
31 |
* |
|
32 |
* @author ulrich |
|
33 |
*/ |
|
34 |
public class ListFileHandler extends FileHandler { |
|
35 |
|
|
36 |
/* Der Logger fuer diesen ListFileHandler */ |
|
37 |
private static final Logger logger = Logger.getLogger(ListFileHandler.class.getName()); |
|
38 |
|
|
39 |
public ListFileHandler(String absoluteDirectoryPathAndName) { |
|
40 |
super(absoluteDirectoryPathAndName); |
|
41 |
} |
|
42 |
|
|
43 |
@Override |
|
44 |
public void handle(HttpExchange e) throws IOException { |
|
45 |
String path = e.getRequestURI().toString(); |
|
46 |
logger.fine(path); |
|
47 |
if(path.endsWith(App.getRs(Server.RB_SLASH))) { |
|
48 |
String fName = getFileName(e); |
|
49 |
logger.fine(fName); |
|
50 |
File dir = new File(fileBase, fName); |
|
51 |
logger.fine(dir.getAbsolutePath()); |
|
52 |
File[] files = dir.listFiles(); |
|
53 |
ArrayList list = new ArrayList(); |
|
54 |
if(files != null) { |
|
55 |
for(File file : files) { |
|
56 |
StorageFile sf = new StorageFile(); |
7c22a2
|
57 |
String fname = file.getName(); |
U |
58 |
sf.setName(fname); |
86bbf7
|
59 |
if(file.isDirectory()) { |
822ddf
|
60 |
sf.setTyp(StorageFile.TYP_FOLDER); |
86bbf7
|
61 |
} else { |
7c22a2
|
62 |
if(fname.endsWith(".mp3")) { |
U |
63 |
sf.setTyp(StorageFile.TYP_AUDIO); |
|
64 |
} else if(fname.endsWith(".mp4") || fname.endsWith(".m4v")) { |
|
65 |
sf.setTyp(StorageFile.TYP_VIDEO); |
|
66 |
} else { |
|
67 |
sf.setTyp(StorageFile.TYP_FILE); |
|
68 |
} |
86bbf7
|
69 |
} |
U |
70 |
list.add(sf); |
|
71 |
} |
|
72 |
} |
|
73 |
String json = jsonWithCustomType(list, "Medialiste"); |
|
74 |
logger.fine(json); |
|
75 |
e.sendResponseHeaders(200, json.length()); |
|
76 |
OutputStream os = e.getResponseBody(); |
|
77 |
os.write(json.getBytes()); |
|
78 |
os.close(); |
|
79 |
} else { |
|
80 |
super.handle(e); |
|
81 |
} |
|
82 |
} |
|
83 |
|
|
84 |
|
|
85 |
} |