package com.yuchen.saas.park.api.util;

import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.io.IOUtils;

import javax.annotation.Nullable;
import javax.validation.constraints.NotBlank;
import java.net.URL;

import static lombok.AccessLevel.PRIVATE;

@Slf4j
@NoArgsConstructor(access = PRIVATE)
public abstract class ImageUtils {
    @Nullable
    public static String getBase64StringFromUrl(@NotBlank String url) {
        try {
            return Base64.encodeBase64String(IOUtils.toByteArray(new URL(url).openStream()));
        } catch (Exception e) {
            log.error("获取图片 [" + url + "] 失败");
            return null;
        }
    }
}
