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