package com.nbport.zgwl.utils;

import cn.hutool.core.util.StrUtil;
import com.nbport.zgwl.exception.ServiceException;

import java.nio.file.Path;

public class FileStorageUtils {

    private FileStorageUtils() {
    }

    public static String getFileSuffix(String originalFilename) {
        if (StrUtil.isBlank(originalFilename) || !originalFilename.contains(".")) {
            return "";
        }
        return originalFilename.substring(originalFilename.lastIndexOf("."));
    }

    public static Path resolveFilePath(Path rootPath, String relativePath) {
        String normalizedRelativePath = relativePath.replace("\\", "/");
        Path targetPath = rootPath.resolve(normalizedRelativePath).normalize();
        if (!targetPath.startsWith(rootPath)) {
            throw new ServiceException("非法文件路径");
        }
        return targetPath;
    }
}
