commit | author | age
|
cc154b
|
1 |
/* |
U |
2 |
http-cm - File management extensions to jdk.httpserver |
|
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/>. |
|
17 |
*/ |
|
18 |
package de.uhilger.httpserver.cm.actor; |
|
19 |
|
|
20 |
import com.google.gson.Gson; |
|
21 |
import com.sun.net.httpserver.HttpExchange; |
|
22 |
import de.uhilger.httpserver.base.HttpHelper; |
|
23 |
import de.uhilger.httpserver.base.HttpResponder; |
|
24 |
import de.uhilger.httpserver.base.handler.FileHandler; |
|
25 |
import static de.uhilger.httpserver.base.handler.FileHandler.SC_OK; |
|
26 |
import de.uhilger.httpserver.cm.DirList; |
|
27 |
import de.uhilger.httpserver.cm.FileManager; |
|
28 |
import static de.uhilger.httpserver.cm.FileManager.STR_DOT; |
|
29 |
import static de.uhilger.httpserver.cm.FileManager.STR_SLASH; |
|
30 |
import de.uhilger.httpserver.cm.ImageFileFilter; |
|
31 |
import de.uhilger.httpserver.image.Datei; |
|
32 |
import de.uhilger.httpserver.image.ImageActor; |
|
33 |
import de.uhilger.httpserver.image.ImageThread; |
|
34 |
import de.uhilger.httpserver.image.ImageThread.ThreadListener; |
|
35 |
import java.io.File; |
|
36 |
import java.io.IOException; |
|
37 |
import java.util.ArrayList; |
|
38 |
import java.util.Arrays; |
|
39 |
import java.util.List; |
|
40 |
import java.util.logging.Level; |
|
41 |
import java.util.logging.Logger; |
|
42 |
|
|
43 |
/** |
|
44 |
* Klasse zum Erzeugen von Dateilisten |
|
45 |
* |
|
46 |
* @author Ulrich Hilger, 15. Januar 2024 |
|
47 |
*/ |
|
48 |
public class Lister implements ThreadListener { |
|
49 |
|
|
50 |
private final List waitingThreads; |
|
51 |
private final int maxThreads; |
|
52 |
private int threadCount; |
|
53 |
|
|
54 |
public Lister() { |
|
55 |
waitingThreads = new ArrayList(); |
|
56 |
maxThreads = 4; |
|
57 |
threadCount = 0; |
|
58 |
} |
|
59 |
|
|
60 |
public String liste(String fName, String ctxPath, String base, String path) throws IOException { |
|
61 |
String dirListPath = ctxPath + fName; |
|
62 |
//if (path.endsWith(STR_SLASH)) { |
|
63 |
//logger.fine("fName: " + fName); |
|
64 |
File dir = new File(base, fName); |
|
65 |
//logger.fine("absPath: " + dir.getAbsolutePath()); |
|
66 |
File[] files = dir.listFiles(new ImageFileFilter()); |
|
67 |
if(files != null && files.length > 0) { |
|
68 |
Arrays.sort(files); |
|
69 |
ArrayList liste = new ArrayList(); |
|
70 |
for (File file : files) { |
|
71 |
Datei datei = new Datei(); |
|
72 |
String dateiName = file.getName(); |
|
73 |
datei.setName(dateiName); |
|
74 |
if (file.isDirectory()) { |
|
75 |
datei.setTyp(Datei.TYP_ORDNER); |
|
76 |
} else { |
|
77 |
datei.setTyp(Datei.TYP_DATEI); |
|
78 |
} |
|
79 |
//datei.setPfad(e.getHttpContext().getPath() + fName); |
|
80 |
String lowerName = dateiName.toLowerCase(); |
|
81 |
if (lowerName.endsWith(ImageActor.JPEG) |
|
82 |
|| lowerName.endsWith(ImageActor.JPG) |
|
83 |
|| lowerName.endsWith(ImageActor.PNG)) { |
|
84 |
datei.setBild(true); |
|
85 |
String ext = dateiName.substring(dateiName.lastIndexOf(STR_DOT)); |
|
86 |
String ohneExt = dateiName.substring(0, dateiName.lastIndexOf(STR_DOT)); |
|
87 |
datei.setMiniurl(ohneExt + ImageActor.TN + ext); |
|
88 |
buildImgSrc(file, datei, ohneExt, ext); |
|
89 |
} |
|
90 |
liste.add(datei); |
|
91 |
} |
|
92 |
while(threadCount > 0) { |
|
93 |
try { |
|
94 |
Thread.sleep(50); |
|
95 |
} catch (InterruptedException ex) { |
|
96 |
Logger.getLogger(FileManager.class.getName()).log(Level.SEVERE, null, ex); |
|
97 |
} |
|
98 |
} |
|
99 |
if(liste.size() > 0) { |
|
100 |
DirList list = new DirList(); |
|
101 |
list.setPfad(dirListPath); |
|
102 |
list.setDateien(liste); |
|
103 |
Gson gson = new Gson(); |
|
104 |
//String json = gson.toJson(liste); |
|
105 |
String json = gson.toJson(list); |
|
106 |
//byte[] bytes = json.getBytes(); |
|
107 |
//logger.fine("json: '" + json + "'"); |
|
108 |
return json; |
|
109 |
} else { |
|
110 |
return null; |
|
111 |
} |
|
112 |
} else { |
|
113 |
return null; |
|
114 |
} |
|
115 |
//} else { |
|
116 |
//} |
|
117 |
} |
|
118 |
|
|
119 |
public void b64Action(String fName, String base) throws IOException { |
|
120 |
String lowerName = fName.toLowerCase(); |
|
121 |
if(lowerName.contains(ImageActor.B64)) { |
|
122 |
ImageActor actor = new ImageActor(); |
|
123 |
String fromName = fName.replace(ImageActor.B64, ""); |
|
124 |
File fromFile = new File(base, fromName); |
|
125 |
File toFile = new File(base, fName); |
|
126 |
//logger.fine("from " + fromFile.getAbsolutePath() + ", to " + toFile.getAbsolutePath()); |
|
127 |
if(!toFile.exists()) { |
|
128 |
actor.b64Image(fromFile, toFile); |
|
129 |
} |
|
130 |
} |
|
131 |
} |
|
132 |
|
|
133 |
// data:[<mime type>][;charset=<Zeichensatz>][;base64],<Daten> |
|
134 |
/* |
|
135 |
[So. Juni 13 13:23:32 MESZ 2021] FEIN: |
|
136 |
file: /home/ulrich/helix-files/bild-test/10419903-14-2-1920-r.jpg, |
|
137 |
relname: bild-test/10419903-14-2-1920-r.jpg, ohneExt: 10419903-14-2-1920-r, ext: .jpg (de.uhilger.helix.FileManager buildImgSrc) |
|
138 |
|
|
139 |
*/ |
|
140 |
private void buildImgSrc(File file, Datei datei, String ohneExt, String ext) throws IOException { |
|
141 |
//logger.fine("file: " + file.getAbsolutePath() + ", ohneExt: " + ohneExt + ", ext: " + ext); |
|
142 |
File dir = file.getParentFile(); |
|
143 |
String newRelName = ohneExt + ImageActor.TN + ImageActor.B64 + ext; |
|
144 |
File b64File = new File(dir, newRelName); |
|
145 |
//logger.fine("b64File: " + b64File.getAbsolutePath()); |
|
146 |
if(!b64File.exists()) { |
|
147 |
//BildErzeuger be = new BildErzeuger(); |
|
148 |
//be.bildErzeugen(dir, newRelName, BildErzeuger.TN, 120, b64File); |
|
149 |
ImageThread it = new ImageThread(dir, newRelName, ImageActor.TN, 120, b64File, datei, ext); |
|
150 |
it.addListener(this); |
|
151 |
if(threadCount < maxThreads) { |
|
152 |
++threadCount; |
|
153 |
//logger.fine("Thread started, threadCount: " + threadCount); |
|
154 |
it.start(); |
|
155 |
} else { |
|
156 |
waitingThreads.add(it); |
|
157 |
//logger.fine("Thread added to wait queue."); |
|
158 |
} |
|
159 |
} else { |
|
160 |
ImageActor be = new ImageActor(); |
|
161 |
be.setImgSrc(datei, ext, b64File); |
|
162 |
} |
|
163 |
} |
|
164 |
|
|
165 |
@Override |
|
166 |
public void finished() { |
|
167 |
--threadCount; |
|
168 |
//logger.fine("Thread finished, threadCound now: " + threadCount); |
|
169 |
if (threadCount < maxThreads) { |
|
170 |
if (waitingThreads.size() > 0) { |
|
171 |
Object o = waitingThreads.get(0); |
|
172 |
if (o instanceof ImageThread) { |
|
173 |
waitingThreads.remove(o); |
|
174 |
ImageThread it = (ImageThread) o; |
|
175 |
++threadCount; |
|
176 |
//logger.fine("Thread started from wait queue, threadCount now: " + threadCount); |
|
177 |
it.start(); |
|
178 |
} |
|
179 |
} |
|
180 |
} |
|
181 |
} |
|
182 |
|
|
183 |
|
|
184 |
} |