package com.backendsys.modules.common.utils;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.UUID;
import org.springframework.web.multipart.MultipartFile;

public class CommonUtil {

    // [文件名] 获得文件后缀名
    public static String getFilenameSuffix(MultipartFile multipartFile, boolean includeDot) {
        String originalFileName = multipartFile.getOriginalFilename();
        int dotIndex = originalFileName.lastIndexOf(".");
        // 根据 includeDot 参数决定是否包含点
        if (includeDot) {
            return originalFileName.substring(dotIndex);
        } else {
            return originalFileName.substring(dotIndex + 1);
        }
    }

    // [文件名] 生成新的文件名 (UUID + 后缀名)
    public static String generateUUIDFilename(MultipartFile multipartFile) {
        return Convert.toStr(UUID.randomUUID()) + getFilenameSuffix(multipartFile, true);
    }

    // [文件名] 插入 '-thumb' 文件名后缀
    public static String insertThumbSuffix(String filePath) {
        int dotIndex = filePath.lastIndexOf('.');
        if (dotIndex != -1) {
            return filePath.substring(0, dotIndex) + "-thumb" + filePath.substring(dotIndex);
        } else {
            return filePath;
        }
    }

}
