|
|
package com.yohoufo.resource.util;
|
|
|
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
import java.io.File;
|
|
|
import java.util.*;
|
|
|
|
|
|
/**
|
|
|
* 七牛云图片上传辅助工具类 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(ImagesConstant.BUCKET_YHB_IMG01, ImagesConstant.BUCKET_YHB_IMG01);
|
|
|
BUCKET_LIST.put(ImagesConstant.BUCKET_YHB_IMG02, ImagesConstant.BUCKET_YHB_IMG02);
|
|
|
BUCKET_LIST.put(ImagesConstant.BUCKET_PASSPORT, ImagesConstant.YHB_HEAD);
|
|
|
BUCKET_LIST.put(ImagesConstant.BUCKET_GOODS_IMG, ImagesConstant.BUCKET_GOODS_IMG);
|
|
|
BUCKET_LIST.put(ImagesConstant.BUCKET_SNS, ImagesConstant.BUCKET_SNS);
|
|
|
BUCKET_LIST.put(ImagesConstant.BUCKET_EVIDENCE, ImagesConstant.BUCKET_EVIDENCE);
|
|
|
BUCKET_LIST.put(ImagesConstant.BUCKET_BRAND_LOGO, ImagesConstant.BUCKET_BRAND_LOGO);
|
|
|
BUCKET_LIST.put(ImagesConstant.BUCKET_GOODS_VIDEO, ImagesConstant.BUCKET_GOODS_VIDEO);
|
|
|
BUCKET_LIST.put(ImagesConstant.BUCKET_ACTIVITY_PIC, ImagesConstant.BUCKET_ACTIVITY_PIC);
|
|
|
BUCKET_LIST.put(ImagesConstant.BUCKET_PLUSTAR, ImagesConstant.BUCKET_PLUSTAR);
|
|
|
BUCKET_LIST.put(ImagesConstant.BUCKET_TAGS, ImagesConstant.BUCKET_TAGS);
|
|
|
BUCKET_LIST.put(ImagesConstant.BUCKET_RELATIONSHIP, ImagesConstant.BUCKET_RELATIONSHIP);
|
|
|
BUCKET_LIST.put(ImagesConstant.BUCKET_AUTHOR, ImagesConstant.BUCKET_AUTHOR);
|
|
|
BUCKET_LIST.put(ImagesConstant.BUCKET_ARTICLE, ImagesConstant.BUCKET_ARTICLE);
|
|
|
BUCKET_LIST.put(ImagesConstant.BUCKET_MODELCARD, ImagesConstant.BUCKET_MODELCARD);
|
|
|
BUCKET_LIST.put(ImagesConstant.BUCKET_BRANDBANNER, ImagesConstant.BUCKET_BRANDBANNER);
|
|
|
BUCKET_LIST.put(ImagesConstant.BUCKET_COLOR, ImagesConstant.BUCKET_COLOR);
|
|
|
BUCKET_LIST.put(ImagesConstant.BUCKET_SALES_CATEGORY, ImagesConstant.BUCKET_SALES_CATEGORY);
|
|
|
BUCKET_LIST.put(ImagesConstant.BUCKET_ZIP, ImagesConstant.BUCKET_ZIP);
|
|
|
BUCKET_LIST.put(ImagesConstant.BUCKET_COUPON_IMAGE, ImagesConstant.BUCKET_COUPON_IMAGE);
|
|
|
BUCKET_LIST.put(ImagesConstant.BUCKET_COUPON_BG_IMAGE, ImagesConstant.BUCKET_COUPON_BG_IMAGE);
|
|
|
BUCKET_LIST.put(ImagesConstant.BUCKET_HELPER_LOGO, ImagesConstant.BUCKET_HELPER_LOGO);
|
|
|
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 场景:前台传的是图片绝对路径,数据库存储需要图片相对路径 获取图片相对路径
|
|
|
*
|
|
|
* @param fileName
|
|
|
* 图片的绝对地址
|
|
|
* http://img01.static.yhbimg.com/goodsimg/2016/03/07/13/01f
|
|
|
* 0024f3dc6f0885f370281a2b41c8bfb.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('/'));
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 场景:前台传的是图片绝对路径,数据库存储需要图片相对路径 批量图片处理 获取图片相对路径
|
|
|
*
|
|
|
* @param fileName
|
|
|
* 图片的绝对地址
|
|
|
* @param bucket
|
|
|
* bucket, 由ImageConstant中定义
|
|
|
*
|
|
|
*/
|
|
|
public static String getBatchImageRelativeUrls(String fileName, String bucket) {
|
|
|
if (StringUtils.isEmpty(fileName) || !isValidBucket(bucket)) {
|
|
|
logger.warn("getImageRelativeUrls fail, invalid param[file:{}, bucket:{}].", fileName, bucket);
|
|
|
return null;
|
|
|
}
|
|
|
logger.info("getImageRelativeUrls by fileName[{}] bucket[{}].", fileName, bucket);
|
|
|
String[] imagePaths = fileName.split(",");
|
|
|
List<String> imageRelativeUrls = new ArrayList<String>(imagePaths.length);
|
|
|
for (String imagePath : imagePaths) {
|
|
|
String subImagePath = imagePath.substring(fileName.indexOf(bucket));
|
|
|
imageRelativeUrls.add(subImagePath.substring(subImagePath.indexOf('/')));
|
|
|
}
|
|
|
return StringUtils.join(imageRelativeUrls, ",");
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 场景:数据库存储是图片相对路径,前台需要图片绝对路径 获取图片绝对路径
|
|
|
*
|
|
|
* @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);
|
|
|
|
|
|
if (fileName.length() <= 17) { // 不合法的脏数据,直接返回空
|
|
|
return "";
|
|
|
}
|
|
|
|
|
|
String fileMode = "";
|
|
|
try {
|
|
|
fileMode = fileName.substring(15, 17);
|
|
|
if (DOMAIN_LIST.get(fileMode) == null) {
|
|
|
logger.warn("getImageAbsoluteUrl by fileName[{}] bucket[{}] fail.", fileName, bucket);
|
|
|
return "";
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
logger.warn("getImageAbsoluteUrl exception:", e);
|
|
|
return fileName;
|
|
|
}
|
|
|
|
|
|
// 随机从两个域名中获取
|
|
|
String[] domains = DOMAIN_LIST.get(fileMode);
|
|
|
int domainMode = new Random().nextInt(2);
|
|
|
return "http://" + domains[domainMode] + File.separator + bucket + fileName;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 场景:数据库存储是图片相对路径,前台需要图片绝对路径 批量图片处理 获取图片绝对路径
|
|
|
*
|
|
|
* @param fileName
|
|
|
* 图片的相对地址 /2016/03/07/13/01f0024f3dc6f0885f370281a2b41c8bfb.png
|
|
|
* @param bucket
|
|
|
* bucket, 由ImageConstant中定义
|
|
|
*
|
|
|
*/
|
|
|
public static String getBatchImageAbsoluteUrls(String fileName, String bucket) {
|
|
|
if (StringUtils.isEmpty(fileName) || !isValidBucket(bucket)) {
|
|
|
logger.warn("getImageAbsoluteUrls fail, invalid param[file:{}, bucket:{}].", fileName, bucket);
|
|
|
return "";
|
|
|
}
|
|
|
logger.info("getImageAbsoluteUrls by fileName[{}] bucket[{}].", fileName, bucket);
|
|
|
String[] imagePaths = fileName.split(",");
|
|
|
List<String> imageAbsoluteUrls = new ArrayList<String>(imagePaths.length);
|
|
|
int domainMode = new Random().nextInt(2);
|
|
|
for (String imagePath : imagePaths) {
|
|
|
String fileMode = imagePath.substring(15, 17);
|
|
|
if (DOMAIN_LIST.get(fileMode) == null) {
|
|
|
logger.warn("getImageAbsoluteUrls by fileName[{}] bucket[{}] fail.", fileName, bucket);
|
|
|
return "";
|
|
|
}
|
|
|
// 随机从两个域名中获取
|
|
|
String[] domains = DOMAIN_LIST.get(fileMode);
|
|
|
imageAbsoluteUrls.add("http://" + domains[domainMode] + File.separator + bucket + imagePath);
|
|
|
}
|
|
|
return StringUtils.join(imageAbsoluteUrls, ",");
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 检查bucket是否有效
|
|
|
*
|
|
|
* @param bucket
|
|
|
*/
|
|
|
public static boolean isValidBucket(String bucket) {
|
|
|
return BUCKET_LIST.containsKey(bucket);
|
|
|
}
|
|
|
} |
|
|
\ No newline at end of file |