From: Sweidan Omar Date: Mon, 24 Jan 2022 13:45:35 +0000 (+0000) Subject: git-tfs-id: [http://tfs.userrendszerhaz.hu:8080/tfs/DefaultCollection]$/MediaCube... X-Git-Url: http://git.useribm.hu/?a=commitdiff_plain;h=0f145e0b4220f393965b7b1e3f776e5a023a425d;p=mediacube.git git-tfs-id: [tfs.userrendszerhaz.hu:8080/tfs/DefaultCollection]$/MediaCube;C32873 --- diff --git a/server/user.jobengine.executors/src/user/jobengine/server/steps/MoveJpegToIsilonStep.java b/server/user.jobengine.executors/src/user/jobengine/server/steps/MoveJpegToIsilonStep.java index 27f7d0e1..78ca210f 100644 --- a/server/user.jobengine.executors/src/user/jobengine/server/steps/MoveJpegToIsilonStep.java +++ b/server/user.jobengine.executors/src/user/jobengine/server/steps/MoveJpegToIsilonStep.java @@ -7,66 +7,146 @@ import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.SimpleFileVisitor; +import java.nio.file.StandardCopyOption; import java.nio.file.attribute.BasicFileAttributes; +import java.text.ParseException; +import java.text.SimpleDateFormat; import java.util.ArrayList; +import java.util.Date; import java.util.List; -import java.util.regex.Matcher; -import java.util.regex.Pattern; +import org.apache.commons.io.FilenameUtils; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import user.commons.StoreUri; +import user.commons.remotestore.RemoteStoreProtocol; public class MoveJpegToIsilonStep extends JobStep { private static final Logger logger = LogManager.getLogger(); @StepEntry - public Object[] execute(StoreUri uri, String protocol) throws Exception { - // TODO szerezzük a path-t meg az uri-ból - Path sourcePath; - List files = new ArrayList<>(); - String fileNamePattern = "yyyymmdd"; + public Object[] execute(String sourceUri, String sourceProtocol, String sourceFolder, String targetUri, + String targetProtocol, String targetFolder, String userName, String password, int port) throws Exception { + StoreUri sourceStoreUri = new StoreUri(sourceUri); +// StoreUri targetStoreUri = new StoreUri(targetUri); + if (sourceFolder.endsWith("/")) { + sourceFolder = sourceFolder.substring(0, sourceFolder.length() - 1); + } - try { - sourcePath = Paths.get(uri.toString()); +// logger.info("sourceFolder: {}", sourceFolder); + List foundItems = new ArrayList<>(); + sourceStoreUri.setRootPath(sourceFolder); + sourceStoreUri.setPortNumber(port); + sourceStoreUri.setPassword(password); + sourceStoreUri.setUserName(userName); + sourceStoreUri.setProtocol(RemoteStoreProtocol.valueOf(sourceProtocol)); +// logger.info("sourceStoreUri.toString(): {}", sourceStoreUri.toString()); + +// targetStoreUri.setProtocol(RemoteStoreProtocol.valueOf(targetProtocol)); - Pattern pattern = Pattern.compile(fileNamePattern, Pattern.CASE_INSENSITIVE); + FileVisitor visitor = new SimpleFileVisitor() { + String fileNamePattern = "yyyymmdd"; + SimpleDateFormat dateFormatter = new SimpleDateFormat(fileNamePattern); + Date currentDate = new Date(); // initializes with the current date + Date dateFromFileName; - FileVisitor visitor = new SimpleFileVisitor() { + @Override + public FileVisitResult visitFile(Path path, BasicFileAttributes attrs) throws IOException { + try { + if (Files.isDirectory(path)) { + dateFromFileName = dateFormatter.parse(path.toString().substring(0, 8)); + if (!(dateFromFileName.compareTo(currentDate) > 0)) { + return FileVisitResult.SKIP_SUBTREE; + } else { + return FileVisitResult.CONTINUE; + } + } else { + String fileExtension = FilenameUtils.getExtension(path.toString()); - @Override - public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { - if (!file.getFileName().toString().contains("_DONE")) { - Matcher matcher = pattern.matcher(file.getFileName().toString()); - if (matcher.find()) - files.add(file.toString()); + if (fileExtension.equals("jpg") || fileExtension.equals("jpeg")) { + logger.info("path: {}", path.toString()); + foundItems.add(path.toString()); +// logger.info("getRootPath(): {}", sourceStoreUri.getRootPath()); + StoreUri targetStoreUri = getManager() + .createStoreUri(RemoteStoreProtocol.valueOf(targetProtocol), targetUri); + targetStoreUri.setProtocol(RemoteStoreProtocol.valueOf(targetProtocol)); + targetStoreUri.setRootPath(targetFolder); + +// if (!targetStoreUri.fileExists(URLEncoder.encode(path.toString(), "UTF-8"))) { + logger.info("Paths.get(targetStoreUri.getRootPath()): {}", targetStoreUri.getRootPath()); + copyFile(path, Paths.get(targetStoreUri.getRootPath())); +// getManager().createStore(); +// getPathUntilCurrentFile(sourceStoreUri.getRootPath().toString(), path), "" ); +// } + } } - return FileVisitResult.CONTINUE; + } catch (ParseException e) { + logger.info("Illegal argument to parse as date: {}", path.getFileName().toString().substring(0, 8)); + } catch (Exception e) { + logger.info("Exception: {}", e.getStackTrace()); } + return FileVisitResult.CONTINUE; + } - @Override - public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException { - return FileVisitResult.CONTINUE; - } - }; - - try { - Files.walkFileTree(sourcePath, visitor); - } catch (Exception e) { - logger.error(getMarker(), "Error processing '{}'. System message: {}", sourcePath, e.getMessage()); - logger.catching(e); - throw e; - } finally { + @Override + public FileVisitResult visitFileFailed(Path path, IOException exc) throws IOException { + return FileVisitResult.CONTINUE; } + }; + + try { + Files.walkFileTree(Paths.get(sourceStoreUri.getRootPath()), visitor); + } catch (IOException e) { + logger.info("Error processing Paths.get(sourceStoreUri.getRootPath()) '{}'. System message: {}", + Paths.get(sourceStoreUri.getRootPath()), e.getStackTrace()); + logger.catching(e); + throw e; + } catch (SecurityException se) { + logger.info("SecurityException: {}", se.getStackTrace()); + } finally { + } + + logger.info("Found {} files", foundItems.size()); + logger.info("files: {}", foundItems); + return new Object[] { foundItems }; + } - logger.info(getMarker(), "Found {} files", files.size()); + private void copyFile(Path sourceFullPath, Path targetRootPath) { + Path targetPath = Paths.get(targetRootPath.toString(), sourceFullPath.getFileName().toString()); + logger.info(getMarker(), "Root {} exists {}", targetRootPath, Files.exists(targetRootPath)); + if (Files.exists(targetPath)) { + logger.info(getMarker(), "Skipping {}, target exists", targetPath); + return; + } + + logger.info(getMarker(), "Target {} synchronization required", targetPath); + + try { + Files.copy(sourceFullPath, targetPath, StandardCopyOption.REPLACE_EXISTING); } catch (Exception e) { - logger.debug("e.stackTrace: {}", e.getStackTrace()); + logger.error(getMarker(), "Error synchronize {} to {}. System message: {}", sourceFullPath, targetPath, + e.getMessage()); + } + } + + protected String getPathUntilCurrentFile(String rootFolder, Path fullPath) { + String tempRootfolder = ""; + if (rootFolder.contains("/") && fullPath.toString().contains("\\")) { + tempRootfolder = rootFolder.replace('/', '\\'); + if (!tempRootfolder.endsWith("\\")) { + tempRootfolder = tempRootfolder.concat("\\"); + } + } + if (rootFolder.contains("\\") && fullPath.toString().contains("/")) { + tempRootfolder = rootFolder.replace('\\', '/'); + if (!tempRootfolder.endsWith("/")) { + tempRootfolder = tempRootfolder.concat("/"); + } } -// return new Object[] { files }; - return null; +// logger.info("Path until file: {}", fullPath.toString().replace(tempRootfolder, "")); + return fullPath.toString().replace(rootFolder, "").substring(1); } -} \ No newline at end of file +}