package com.ruoyi.cai.util;

import com.ruoyi.common.utils.StringUtils;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.net.URL;

@Slf4j
public class CaiFileUtils {

    @Data
    public static class FileSize {
        private int width = 500;
        private int height = 500;
        private String suffix;
    }

    private static String BASE_URL = "https://nono-1257812345.cos.ap-shanghai.myqcloud.com/";

    public static FileSize getFileSize(String url){
        FileSize fileSize = new FileSize();
        String suffix = StringUtils.substring(url, url.lastIndexOf("."), url.length());
        fileSize.setSuffix(suffix);
        try {
            URL imageUrl = new URL(BASE_URL+url); // 替换为实际的图片 URL
            BufferedImage image = ImageIO.read(imageUrl);
            int width = image.getWidth();
            int height = image.getHeight();
            fileSize.setWidth(width);
            fileSize.setHeight(height);
            return fileSize;
        }catch (Exception e){
            log.error("获取文件图片大小失败",e);
        }
        return new FileSize();

    }
}
