From 8472125b1ee526b2c73771abf20b1f282d759c68 Mon Sep 17 00:00:00 2001 From: "vasary.daniel" Date: Tue, 1 Feb 2022 10:34:29 +0000 Subject: [PATCH] git-tfs-id: [http://tfs.userrendszerhaz.hu:8080/tfs/DefaultCollection]$/MediaCube;C32950 --- .../run-mediacube-server-hirtv (1).launch | 44 +++++++++ .../server/steps/CopyImagesStep.java | 89 +++++++++++++++++++ 2 files changed, 133 insertions(+) create mode 100644 server/-configuration/run-mediacube-server-hirtv (1).launch create mode 100644 server/user.jobengine.executors/src/user/jobengine/server/steps/CopyImagesStep.java diff --git a/server/-configuration/run-mediacube-server-hirtv (1).launch b/server/-configuration/run-mediacube-server-hirtv (1).launch new file mode 100644 index 00000000..5d433363 --- /dev/null +++ b/server/-configuration/run-mediacube-server-hirtv (1).launch @@ -0,0 +1,44 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/server/user.jobengine.executors/src/user/jobengine/server/steps/CopyImagesStep.java b/server/user.jobengine.executors/src/user/jobengine/server/steps/CopyImagesStep.java new file mode 100644 index 00000000..12be74e7 --- /dev/null +++ b/server/user.jobengine.executors/src/user/jobengine/server/steps/CopyImagesStep.java @@ -0,0 +1,89 @@ +package user.jobengine.server.steps; + +import java.io.IOException; +import java.nio.file.FileVisitResult; +import java.nio.file.FileVisitor; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.nio.file.SimpleFileVisitor; +import java.nio.file.attribute.BasicFileAttributes; +import java.nio.file.attribute.FileTime; + +import org.apache.commons.io.FilenameUtils; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +public class CopyImagesStep extends JobStep { + private static final Logger logger = LogManager.getLogger(); + + @StepEntry + public Object[] execute(String sourceFolder, String targetFolder) throws Exception { + + FileVisitor visitor = new SimpleFileVisitor() { + @Override + public FileVisitResult preVisitDirectory(Path folder, BasicFileAttributes attrs) throws IOException { + return FileVisitResult.CONTINUE; + } + + @Override + public FileVisitResult visitFile(Path source, BasicFileAttributes attrs) throws IOException { + FileVisitResult result = FileVisitResult.CONTINUE; + + logger.info(getMarker(), "Checking path: {}", source.toString()); + try { + String fileName = source.getFileName().toString(); + String fileExtension = FilenameUtils.getExtension(source.toString()).toLowerCase(); + + Path target = Paths.get(targetFolder, fileName); + + boolean needCopy = false; + + if (!fileExtension.equals("jpg") && !fileExtension.equals("jpeg")) + return result; + + if (target.toFile().exists()) { + FileTime sourceLastModified = Files.readAttributes(source, BasicFileAttributes.class) + .lastModifiedTime(); + FileTime targetLastModified = Files.readAttributes(target, BasicFileAttributes.class) + .lastModifiedTime(); + if (sourceLastModified.compareTo(targetLastModified) > 0) + needCopy = true; + } else + needCopy = true; + + if (needCopy) + copyFile(source, target); + + } catch (Exception e) { + logger.catching(e); + } + return result; + } + + @Override + public FileVisitResult visitFileFailed(Path path, IOException exc) throws IOException { + return FileVisitResult.CONTINUE; + } + }; + + try { + Files.walkFileTree(Paths.get(sourceFolder), visitor); + } catch (Exception e) { + logger.info("Error processing '{}'. System message: {}", sourceFolder, e.getMessage()); + throw e; + } + + return null; + } + + private void copyFile(Path source, Path target) { + logger.info(getMarker(), "Copy needed for {}", source); + try { + Files.copy(source, target); + } catch (Exception e) { + logger.error(getMarker(), "Error synchronize {} to {}. System message: {}", source, target, e.getMessage()); + } + } + +} -- 2.54.0