FilePathBuilder.java 4.1 KB
/*
 * Decompiled with CFR 0_118.
 */
package com.adobe.aemfd.watchfolder.job;

import com.adobe.aemfd.watchfolder.job.TextUtil;
import com.adobe.aemfd.watchfolder.job.WatchedFolderUtils;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;

public class FilePathBuilder {
    public static String getConcretePath(String filepathPattern) {
        return FilePathBuilder.getConcretePath(filepathPattern, null, 0, null);
    }

    public static String getConcretePath(String filepathPattern, String inputFilepath, long millis, String processId) {
        Calendar cal = Calendar.getInstance();
        if (millis > 0) {
            cal.setTimeInMillis(millis);
        }
        if (!TextUtil.isEmpty(inputFilepath)) {
            filepathPattern = TextUtil.replace(filepathPattern, "%F", WatchedFolderUtils.getFilenameWithNoExtension(inputFilepath));
            String _extension = WatchedFolderUtils.getFileExtensionLowerCase(inputFilepath);
            String _modExt = TextUtil.isNull(_extension, "");
            filepathPattern = TextUtil.replace(filepathPattern, "%E", _modExt);
        }
        filepathPattern = TextUtil.replace(filepathPattern, "%Y", String.valueOf(cal.get(1)));
        filepathPattern = TextUtil.replace(filepathPattern, "%y", String.valueOf(cal.get(1)).substring(2));
        filepathPattern = TextUtil.replace(filepathPattern, "%M", String.format("%1$tm", cal));
        filepathPattern = TextUtil.replace(filepathPattern, "%D", String.format("%1$td", cal));
        filepathPattern = TextUtil.replace(filepathPattern, "%d", String.format("%1$tj", cal));
        filepathPattern = TextUtil.replace(filepathPattern, "%H", String.format("%1$tH", cal));
        filepathPattern = TextUtil.replace(filepathPattern, "%h", String.format("%1$tI", cal));
        filepathPattern = TextUtil.replace(filepathPattern, "%m", String.format("%1$tM", cal));
        filepathPattern = TextUtil.replace(filepathPattern, "%s", String.format("%1$tS", cal));
        filepathPattern = TextUtil.replace(filepathPattern, "%l", String.format("%1$tL", cal));
        filepathPattern = TextUtil.replace(filepathPattern, "%R", String.valueOf((int)(Math.random() * 10.0)));
        if (!TextUtil.isEmpty(processId)) {
            filepathPattern = TextUtil.replace(filepathPattern, "%P", processId);
        } else if (!TextUtil.isEmpty(inputFilepath)) {
            filepathPattern = TextUtil.replace(filepathPattern, "%P", WatchedFolderUtils.getFilenameWithNoExtension(inputFilepath));
        }
        filepathPattern = FilePathBuilder.replaceFolderWithFileSeparator(filepathPattern);
        return filepathPattern;
    }

    public static boolean isFolderPath(String aFilePattern) {
        if (aFilePattern.endsWith("/") || aFilePattern.endsWith("\\")) {
            return true;
        }
        return false;
    }

    public static String replaceFolderWithFileSeparator(String aFilePattern) {
        String _filepathPattern = aFilePattern;
        if (FilePathBuilder.isFolderPath(_filepathPattern)) {
            _filepathPattern = _filepathPattern.substring(0, _filepathPattern.length() - 1) + WatchedFolderUtils.FILE_SEPARATOR;
        }
        return _filepathPattern;
    }

    public static boolean makeDirectoryPath(File node) {
        ArrayList<File> tree = new ArrayList<File>();
        File _path = node;
        while (!node.exists()) {
            tree.add(node);
            if ((node = node.getParentFile()) != null) continue;
            throw new RuntimeException("Invalid URL " + _path);
        }
        if (!tree.isEmpty()) {
            for (int i = tree.size() - 1; i >= 0; --i) {
                node = (File)tree.get(i);
                try {
                    node = new File(node.getParentFile().getCanonicalFile(), node.getName());
                }
                catch (IOException ioe) {
                    throw new RuntimeException("Error getting parent directory path!", ioe);
                }
                if (node.mkdir()) continue;
                throw new RuntimeException("Error creating directory " + node.getPath());
            }
        }
        return true;
    }
}