/*
|
fm - File management class library
|
Copyright (C) 2024 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 <https://www.gnu.org/licenses/>.
|
*/
|
package de.uhilger.fm;
|
|
import com.google.gson.Gson;
|
import java.io.File;
|
import java.io.IOException;
|
import java.util.ArrayList;
|
import java.util.Arrays;
|
|
|
/**
|
* Eine Klasse zur Bildung einer Liste mit den Dateien eines Verzeichnisses.
|
*
|
* Varianten von Bilddateien werden ausgeblendet. Enthaelt ein Ordner die Dateien
|
* mein-bild.jpg
|
* mein-bild_tn.jpg
|
* mein-bild_kl.jpg
|
* erscheint nur die Datei mein-bild.jpg in der Liste.
|
*
|
*
|
* @author Ulrich Hilger
|
* @version 0.1, 05.11.2024
|
*/
|
public class Lister {
|
|
public static final String STR_DOT = ".";
|
|
public String liste(String fName, String ctxPath, String base/*, String path*/) throws IOException {
|
File[] files = new File(base, fName).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);
|
}
|
String lowerName = dateiName.toLowerCase();
|
if (lowerName.endsWith(ImageFileFilter.JPEG)
|
|| lowerName.endsWith(ImageFileFilter.JPG)
|
|| lowerName.endsWith(ImageFileFilter.PNG)) {
|
datei.setBild(true);
|
String ext = dateiName.substring(dateiName.lastIndexOf(STR_DOT));
|
String ohneExt = dateiName.substring(0, dateiName.lastIndexOf(STR_DOT));
|
datei.setMiniurl(ctxPath + /*"/" + */ fName + ohneExt + ImageFileFilter.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.isEmpty()) {
|
DirList list = new DirList();
|
list.setPfad(ctxPath + fName);
|
list.setDateien(liste);
|
Gson gson = new Gson();
|
String json = gson.toJson(list);
|
return json;
|
} else {
|
return null;
|
}
|
} else {
|
return null;
|
}
|
//} else {
|
//}
|
}
|
|
}
|