package com.ycwl.basic.storage.utils;

import org.apache.commons.lang3.StringUtils;

public class StorageUtil {
    public static String joinPath(String path, String subPath) {
        if (StringUtils.isBlank(path)) {
            return subPath;
        }
        if (subPath.startsWith("/")) {
            subPath = subPath.substring(1);
        }
        if (path.endsWith("/")) {
            path = path.substring(0, path.length() - 1);
        }
        if (StringUtils.isBlank(subPath)) {
            return path;
        }
        if (path.endsWith("/")) {
            return path + subPath;
        } else {
            return path + "/" + subPath;
        }
    }

    public static String joinPath(String ...names) {
        String name = names[0];
        for (int i = 1; i < names.length; i++) {
            name = joinPath(name, names[i]);
        }
        return name;
    }


    public static String joinPath(String name, String ...names) {
        for (String s : names) {
            name = joinPath(name, s);
        }
        return name;
    }

    public static String getRelativePath(String fullPath, String relativeOn) {
        if (StringUtils.isNotBlank(relativeOn)) {
            fullPath = fullPath.replace(relativeOn, "");
        }
        if (fullPath.startsWith("/")) {
            fullPath = fullPath.substring(1);
        }
        return fullPath;
    }
}
