ImagesHelper.java 5.11 KB
package com.yoho.unions.utils;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
 * 七牛云图片上传辅助工具类
 * qianjun
 */
public class ImagesHelper {
    private static final Logger logger = LoggerFactory.getLogger(ImagesHelper.class);

    // 当前域名列表
    private static final Map<String, String[]> DOMAIN_LIST;

    // 当前bucket列表
    private static final Map<String, String> BUCKET_LIST;
    static {
        DOMAIN_LIST = new HashMap<>();
        DOMAIN_LIST.put("01", new String[]{"img10.static.yhbimg.com", "img11.static.yhbimg.com"});
        DOMAIN_LIST.put("02", new String[]{"img12.static.yhbimg.com", "img13.static.yhbimg.com"});
        DOMAIN_LIST.put("03", new String[]{"flv01.static.yhbimg.com", "flv01.static.yhbimg.com"});
        DOMAIN_LIST.put("yhb-head", new String[]{"head.static.yhbimg.com", "head.static.yhbimg.com"});

        BUCKET_LIST = new HashMap<>();
        BUCKET_LIST.put("goodsimg", "goodsimg");
        BUCKET_LIST.put("unionimg", "unionimg");
    }

    /**
     * 场景:前台传的是图片绝对路径,数据库存储需要图片相对路径
     * 获取图片相对路径
     * @param fileName   图片的绝对地址 http://img01.static.yhbimg.com/goodsimg//2016/03/07/13/01f0024f3dc6f0885f370281a2b41c8bfb.png
     * @param bucket     bucket, 由ImageConstant中定义
     *
     */
    public static String getImageRelativeUrl(String fileName, String bucket) {
        if(fileName==null){
            logger.warn("getImageRelativeUrl fail, fileName is null");
            return  null;
        }

        if ( StringUtils.isEmpty(fileName) || !isValidBucket(bucket))
        {
            logger.warn("getImageRelativeUrl fail, invalid param[file:{}, bucket:{}].", fileName, bucket);
            return  "";
        }

        logger.info("getImageRelativeUrl by fileName[{}] bucket[{}].", fileName, bucket);

        String subfileName = fileName.substring(fileName.indexOf(bucket));
        return subfileName.substring(subfileName.indexOf("/"));
    }
    /**
     * 场景:从uic获取到的不是原始图片相对路径,数据库存储是图片相对路径
     * 获取图片相对路径
     * @param urlWithwh   图片的相对地址 http://head.static.yhbimg.com/yhb-head/2018/1/25/13/01abb7f1473ffc711a154c01091c29d9.648116.jpg?imageView2/{mode}/w/{width}/h/{height}
     *
     */
    public static String getImageUrl(String urlWithwh) {
        if (urlWithwh == null) {
            return null;
        }
        if (urlWithwh.contains("?")) {
            return urlWithwh.substring(0, urlWithwh.indexOf("?"));
        }
        return urlWithwh;
    }

    /**
     * 场景:从uic获取到的头像,数据库存储是图片绝对路径,给前台需要加上后缀
     * 获取图片相对路径
     * @param urlWithwh   图片的地址 http://head.static.yhbimg.com/yhb-head/2018/1/25/13/01abb7f1473ffc711a154c01091c29d9.648116.jpg
     * @return  urlWithwh   图片的地址 http://head.static.yhbimg.com/yhb-head/2018/1/25/13/01abb7f1473ffc711a154c01091c29d9.648116.jpg?imageView2/{mode}/w/{width}/h/{height}
     *
     */
    public static String getHeadImageUrl(String url) {
        if (StringUtils.isBlank(url)) {
            return null;
        }
        return url + "?imageView2/{mode}/w/{width}/h/{height}";
    }
    /**
     * 场景:数据库存储是图片相对路径,前台需要图片绝对路径
     * 获取图片绝对路径
     * @param fileName   图片的相对地址 /2016/03/07/13/01f0024f3dc6f0885f370281a2b41c8bfb.png
     * @param bucket     bucket, 由ImageConstant中定义
     *
     */
    public static String getImageAbsoluteUrl(String fileName, String bucket) {
        if ( StringUtils.isEmpty(fileName) || !isValidBucket(bucket))
        {
            logger.warn("getImageAbsoluteUrl fail, invalid param[file:{}, bucket:{}].", fileName, bucket);
            return  "";
        }

        // 兼容旧版本绝对URL地址
        if (fileName.trim().toLowerCase().startsWith("http://")) {
            // 旧版本url中可能会带参数,直接去掉
            if (-1 != fileName.indexOf("?")) {
                // 去掉后面的参数后返回
                return fileName.substring(0, fileName.indexOf("?"));
            }
            else {
                return fileName;
            }
        }

        logger.info("getImageAbsoluteUrl by fileName[{}] bucket[{}].", fileName, bucket);
        String fileMode = fileName.substring(15, 17);
        if (DOMAIN_LIST.get(fileMode) == null) {
            logger.warn("getImageAbsoluteUrl by fileName[{}] bucket[{}] fail.", fileName, bucket);
            return "";
        }

        //随机从两个域名中获取
        String[] domains = DOMAIN_LIST.get(fileMode);
        int domainMode = new Random().nextInt(2);
        return "http://" + domains[domainMode] + "/" + bucket + fileName;
    }
    
    /**
     * 检查bucket是否有效
     * @param bucket
     */
    public static boolean isValidBucket(String bucket) {
        return BUCKET_LIST.containsKey(bucket);
    }
}