package cc.smtweb.system.bpm.util;

import lombok.extern.slf4j.Slf4j;

import java.io.File;

/**
 * 〈文件路径〉
 *
 * @author kevin
 * @since 1.0.0
 */
@Slf4j
public class FileFixPath {
    protected String path;
    protected String rootPath;
    protected String typeDir;

    public FileFixPath(String rootPath, String typeDir) {
        this.rootPath = rootPath;
        this.typeDir = typeDir;
    }

    public FilePathInfo makeDatePath(long fileId, String fileName) {
        long now = System.currentTimeMillis();
        this.path = this.typeDir + "/";
        createFolder(rootPath + this.path);

        return new FilePathInfo(rootPath, this.path, now, fileName, fileId);
    }

    private boolean createFolder(String path) {
        File file = new File(path);
        if (file.exists()) {
            if (!file.isDirectory()) {
                return false;
            }

            return true;
        }

        if (!file.mkdirs()) {
            log.error("unable to create folders {}.", rootPath + this.path);
            return false;
        }

        log.debug("create folders {}.", file);

        return true;
    }
}
