From: Sweidan Omar Date: Mon, 24 Jan 2022 13:48:40 +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=4cfb01e8f247a8ed52e1becef3386397030eb37a;p=mediacube.git git-tfs-id: [tfs.userrendszerhaz.hu:8080/tfs/DefaultCollection]$/MediaCube;C32877 --- diff --git a/server/-product/production/LOCAL/jobs/steps/MoveJpegToIsilonStep.java b/server/-product/production/LOCAL/jobs/steps/MoveJpegToIsilonStep.java index 24a178f4..78ca210f 100644 --- a/server/-product/production/LOCAL/jobs/steps/MoveJpegToIsilonStep.java +++ b/server/-product/production/LOCAL/jobs/steps/MoveJpegToIsilonStep.java @@ -1,62 +1,152 @@ package user.jobengine.server.steps; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; - -import java.net.URI; +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.StandardCopyOption; import java.nio.file.attribute.BasicFileAttributes; -import java.util.regex.Matcher; -import java.util.regex.Pattern; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; + +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 = Paths.get(sourceFolder); - List files = new ArrayList<>(); - - Pattern pattern = Pattern.compile(fileNamePattern, Pattern.CASE_INSENSITIVE); - + 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); + } + +// 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)); + FileVisitor visitor = new SimpleFileVisitor() { - - @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()); + String fileNamePattern = "yyyymmdd"; + SimpleDateFormat dateFormatter = new SimpleDateFormat(fileNamePattern); + Date currentDate = new Date(); // initializes with the current date + Date dateFromFileName; + + @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()); + + 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; + + @Override + public FileVisitResult visitFileFailed(Path path, 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()); + }; + + 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; - } - finally { } - - logger.info(getMarker(), "Found {} files", files.size()); - -// return new Object[] { files }; - return null; + } catch (SecurityException se) { + logger.info("SecurityException: {}", se.getStackTrace()); + } finally { + } + + logger.info("Found {} files", foundItems.size()); + logger.info("files: {}", foundItems); + return new Object[] { foundItems }; + } + + 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.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("/"); + } + } + +// logger.info("Path until file: {}", fullPath.toString().replace(tempRootfolder, "")); + return fullPath.toString().replace(rootFolder, "").substring(1); } -} \ No newline at end of file +}