package com.tiesheng.util.pojos;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;

public class FileUploadPath {

    public static String UPLOAD_FOLDER = "/upload/";

    /**
     * 绝对路径
     */
    private String absolutePath;

    /**
     * 访问路径
     */
    private String httpPath;

    private FileUploadPath() {
    }

    /**
     * 获得一个文件路径
     *
     * @param fileName
     * @return
     */
    public static FileUploadPath get(String fileName) {
        FileUploadPath pathBean = new FileUploadPath();

        String tempPath = fileName;
        if (!StrUtil.startWith(tempPath, UPLOAD_FOLDER)) {
            tempPath = UPLOAD_FOLDER + DateUtil.format(DateUtil.date(), "yyyy-MM") + "/" + fileName;
        }
        pathBean.setHttpPath(tempPath);
        pathBean.setAbsolutePath(String.format("%s/static%s", System.getProperty("user.dir"), tempPath));

        return pathBean;
    }


    /**
     * 随机生成一个文件路径
     *
     * @return
     */
    public static FileUploadPath random() {
        return get(IdUtil.simpleUUID());
    }


    /**
     * 获取一个目录
     *
     * @param folder
     * @return
     */
    public static FileUploadPath folder(String folder) {
        FileUploadPath pathBean = new FileUploadPath();

        String tempFolder = folder;
        if (!StrUtil.startWith(tempFolder, UPLOAD_FOLDER)) {
            tempFolder = UPLOAD_FOLDER + tempFolder;
        }
        if (!StrUtil.endWith(folder, "/")) {
            tempFolder = tempFolder + "/";
        }
        pathBean.setHttpPath(tempFolder);
        pathBean.setAbsolutePath(String.format("%s/static%s", System.getProperty("user.dir"), tempFolder));
        FileUtil.mkParentDirs(pathBean.getAbsolutePath());

        return pathBean;
    }


    ///////////////////////////////////////////////////////////////////////////
    // setter\getter
    ///////////////////////////////////////////////////////////////////////////

    public String getAbsolutePath() {
        return absolutePath;
    }

    public void setAbsolutePath(String absolutePath) {
        this.absolutePath = absolutePath;
    }

    public String getHttpPath() {
        return httpPath;
    }

    public void setHttpPath(String httpPath) {
        this.httpPath = httpPath;
    }
}
