/*
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 static de.uhilger.httpserver.cm.FileManager.OP_MOVE;
import static de.uhilger.httpserver.cm.FileManager.STR_DOT;
import de.uhilger.httpserver.cm.FileTransporter;
import de.uhilger.httpserver.cm.OrdnerBearbeiter;
import de.uhilger.httpserver.image.ImageActor;
import java.io.File;
import java.io.IOException;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
/**
* Eine Klasse mit Methoden zum Kopieren und Verschieben von Dateien
*
* @author Ulrich Hilger, 15. Janaur 2024
*/
public class Mover {
public String copyOrMoveFiles(String fromPath, String toPath, String[] fileNames, int operation, String base) throws IOException {
String result = null;
File srcDir = new File(base, fromPath);
File targetDir = new File(base, toPath);
for (String fileName : fileNames) {
File srcFile = new File(srcDir, fileName);
//logger.fine("srcFile: " + srcFile);
if (srcFile.isDirectory()) {
//logger.fine("srcFile is directory.");
OrdnerBearbeiter bearbeiter = new OrdnerBearbeiter();
bearbeiter.setTargetDir(targetDir.toPath());
bearbeiter.setOperation(operation);
Files.walkFileTree(srcFile.toPath(), bearbeiter);
} else {
Path source = srcFile.toPath();
File destFile = targetDir.toPath().resolve(source.getFileName()).toFile();
if (destFile.exists()) {
FileTransporter trans = new FileTransporter();
destFile = trans.getNewFileName(destFile);
}
if (operation == OP_MOVE) {
String fname = srcFile.getName().toLowerCase();
if (fname.endsWith(ImageActor.JPEG)
|| fname.endsWith(ImageActor.JPG)
|| fname.endsWith(ImageActor.PNG)) {
moveImgFilesToDirectory(srcFile, srcDir, targetDir, false);
} else {
Files.move(source, destFile.toPath());
}
} else {
Files.copy(source, destFile.toPath());
}
}
}
return result;
}
private void moveImgFilesToDirectory(File srcFile, File srcDir, File targetDir, boolean createDestDir) throws IOException {
String fnameext = srcFile.getName();
int dotpos = fnameext.lastIndexOf(STR_DOT);
String fname = fnameext.substring(0, dotpos);
String ext = fnameext.substring(dotpos);
//logger.fine("fname: " + fname + ", ext: " + ext);
Path targetPath = targetDir.toPath();
DirectoryStream stream = Files.newDirectoryStream(srcDir.toPath(), fname + "*" + ext); //"*.{txt,doc,pdf,ppt}"
for (Path path : stream) {
//logger.fine(path.getFileName().toString());
//Files.delete(path);
Files.move(path, targetPath.resolve(path.getFileName()));
}
stream.close();
}
public String duplizieren(String base, String relPfad) throws IOException {
File srcFile = new File(base, relPfad);
String fnameext = srcFile.getName();
int dotpos = fnameext.lastIndexOf(STR_DOT);
String fname = fnameext.substring(0, dotpos);
String ext = fnameext.substring(dotpos);
File srcDir = srcFile.getParentFile();
File destFile = new File(srcDir, fname + "-Kopie" + ext);
int i = 1;
while (destFile.exists()) {
destFile = new File(srcDir, fname + "-Kopie-" + Integer.toString(++i) + ext);
}
Files.copy(srcFile.toPath(), destFile.toPath());
return destFile.getName();
}
}