/*
http-cm - File management extensions to jdk.httpserver
Copyright (C) 2021 Ulrich Hilger
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see .
*/
package de.uhilger.httpserver.cm.actor;
import com.google.gson.Gson;
import com.sun.net.httpserver.HttpExchange;
import de.uhilger.httpserver.base.HttpHelper;
import de.uhilger.httpserver.base.HttpResponder;
import de.uhilger.httpserver.base.handler.FileHandler;
import static de.uhilger.httpserver.base.handler.FileHandler.SC_OK;
import de.uhilger.httpserver.cm.DirList;
import de.uhilger.httpserver.cm.FileManager;
import static de.uhilger.httpserver.cm.FileManager.STR_DOT;
import static de.uhilger.httpserver.cm.FileManager.STR_SLASH;
import de.uhilger.httpserver.cm.ImageFileFilter;
import de.uhilger.httpserver.image.Datei;
import de.uhilger.httpserver.image.ImageActor;
import de.uhilger.httpserver.image.ImageThread;
import de.uhilger.httpserver.image.ImageThread.ThreadListener;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* Klasse zum Erzeugen von Dateilisten
*
* @author Ulrich Hilger, 15. Januar 2024
*/
public class Lister implements ThreadListener {
private final List waitingThreads;
private final int maxThreads;
private int threadCount;
public Lister() {
waitingThreads = new ArrayList();
maxThreads = 4;
threadCount = 0;
}
public String liste(String fName, String ctxPath, String base, String path) throws IOException {
String dirListPath = ctxPath + fName;
//if (path.endsWith(STR_SLASH)) {
//logger.fine("fName: " + fName);
File dir = new File(base, fName);
//logger.fine("absPath: " + dir.getAbsolutePath());
File[] files = dir.listFiles(new ImageFileFilter());
if(files != null && files.length > 0) {
Arrays.sort(files);
ArrayList liste = new ArrayList();
for (File file : files) {
Datei datei = new Datei();
String dateiName = file.getName();
datei.setName(dateiName);
if (file.isDirectory()) {
datei.setTyp(Datei.TYP_ORDNER);
} else {
datei.setTyp(Datei.TYP_DATEI);
}
//datei.setPfad(e.getHttpContext().getPath() + fName);
String lowerName = dateiName.toLowerCase();
if (lowerName.endsWith(ImageActor.JPEG)
|| lowerName.endsWith(ImageActor.JPG)
|| lowerName.endsWith(ImageActor.PNG)) {
datei.setBild(true);
String ext = dateiName.substring(dateiName.lastIndexOf(STR_DOT));
String ohneExt = dateiName.substring(0, dateiName.lastIndexOf(STR_DOT));
datei.setMiniurl(ohneExt + ImageActor.TN + ext);
buildImgSrc(file, datei, ohneExt, ext);
}
liste.add(datei);
}
while(threadCount > 0) {
try {
Thread.sleep(50);
} catch (InterruptedException ex) {
Logger.getLogger(FileManager.class.getName()).log(Level.SEVERE, null, ex);
}
}
if(liste.size() > 0) {
DirList list = new DirList();
list.setPfad(dirListPath);
list.setDateien(liste);
Gson gson = new Gson();
//String json = gson.toJson(liste);
String json = gson.toJson(list);
//byte[] bytes = json.getBytes();
//logger.fine("json: '" + json + "'");
return json;
} else {
return null;
}
} else {
return null;
}
//} else {
//}
}
public void b64Action(String fName, String base) throws IOException {
String lowerName = fName.toLowerCase();
if(lowerName.contains(ImageActor.B64)) {
ImageActor actor = new ImageActor();
String fromName = fName.replace(ImageActor.B64, "");
File fromFile = new File(base, fromName);
File toFile = new File(base, fName);
//logger.fine("from " + fromFile.getAbsolutePath() + ", to " + toFile.getAbsolutePath());
if(!toFile.exists()) {
actor.b64Image(fromFile, toFile);
}
}
}
// data:[][;charset=][;base64],
/*
[So. Juni 13 13:23:32 MESZ 2021] FEIN:
file: /home/ulrich/helix-files/bild-test/10419903-14-2-1920-r.jpg,
relname: bild-test/10419903-14-2-1920-r.jpg, ohneExt: 10419903-14-2-1920-r, ext: .jpg (de.uhilger.helix.FileManager buildImgSrc)
*/
private void buildImgSrc(File file, Datei datei, String ohneExt, String ext) throws IOException {
//logger.fine("file: " + file.getAbsolutePath() + ", ohneExt: " + ohneExt + ", ext: " + ext);
File dir = file.getParentFile();
String newRelName = ohneExt + ImageActor.TN + ImageActor.B64 + ext;
File b64File = new File(dir, newRelName);
//logger.fine("b64File: " + b64File.getAbsolutePath());
if(!b64File.exists()) {
//BildErzeuger be = new BildErzeuger();
//be.bildErzeugen(dir, newRelName, BildErzeuger.TN, 120, b64File);
ImageThread it = new ImageThread(dir, newRelName, ImageActor.TN, 120, b64File, datei, ext);
it.addListener(this);
if(threadCount < maxThreads) {
++threadCount;
//logger.fine("Thread started, threadCount: " + threadCount);
it.start();
} else {
waitingThreads.add(it);
//logger.fine("Thread added to wait queue.");
}
} else {
ImageActor be = new ImageActor();
be.setImgSrc(datei, ext, b64File);
}
}
@Override
public void finished() {
--threadCount;
//logger.fine("Thread finished, threadCound now: " + threadCount);
if (threadCount < maxThreads) {
if (waitingThreads.size() > 0) {
Object o = waitingThreads.get(0);
if (o instanceof ImageThread) {
waitingThreads.remove(o);
ImageThread it = (ImageThread) o;
++threadCount;
//logger.fine("Thread started from wait queue, threadCount now: " + threadCount);
it.start();
}
}
}
}
}