ImageUrlAssist.java
4.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package com.yoho.search.common.utils;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.List;
public class ImageUrlAssist {
private static final String INIT_POSITION = "center";
private static final String INIT_BACKGROUND = "d2hpdGU=";
private static final String INIT_SIZE = "source";
private static final Logger logger = LoggerFactory.getLogger(ImageUrlAssist.class);
/**
* 图片尺寸字符串的分隔符
*/
private static final String SEPRATOR_SIZE = "x";
/**
* 获取图片的完整路径 包含参数
* @param imageUrl 相对路径
* @param bucket
* @param position
* @param background
* @return 图片的完整路径
*/
public static String getAllProductPicUrl(Object imageUrl, String bucket, String position, String background) {
if (!(imageUrl instanceof String) || StringUtils.isEmpty((String)imageUrl)) {
return "";
}
if (StringUtils.isEmpty(background)) {
background = INIT_BACKGROUND;
}
if (StringUtils.isEmpty(position)) {
position = INIT_POSITION;
}
return getUrl((String)imageUrl, bucket, null, null) + "?imageMogr2/thumbnail/{width}x{height}/background/"
+ background + "/position/" + position + "/quality/80";
}
/**
* 获取图片的路径,不含参数
* @param imageUrl
* @param bucket
* @return
*/
public static String getUrl(String imageUrl, String bucket, String source, Integer mode) {
if (StringUtils.isEmpty(imageUrl)) {
return "";
}
source = StringUtils.isEmpty(source) ? INIT_SIZE : source;
mode = null == mode ? 1 : mode;
Integer width = null;
Integer height = null;
if (StringUtils.isEmpty(source) && !INIT_SIZE.equals(source)) {
String[] split = source.split(SEPRATOR_SIZE);
if (split.length == 2) {
try {
width = Integer.valueOf(split[0]);
height = Integer.valueOf(split[1]);
} catch (NumberFormatException e) {
logger.warn("The source of goodsPic is not number. imageUrl is " + imageUrl + "; source : " + source, e);
}
}
}
String domain = getDomain(imageUrl);
if (StringUtils.isEmpty(domain)) {
return "";
}
return getImgPrivateUrl(bucket + imageUrl, width, height, mode, domain);
}
/**
* 获取私有的图片地址
* @param fileName
* @param width
* @param height
* @param mode
* @param domain
* @return
*/
private static String getImgPrivateUrl(String fileName, Integer width, Integer height, Integer mode, String domain) {
if (null == mode) {
mode = 1;
}
if (StringUtils.isEmpty(domain)) {
domain = "yhfair.qiniudn.com";
}
return makeRequest("http://" + domain + "/" + fileName.replaceAll("%2F", "/"), mode, width, height, domain, null, null);
}
/**
* 拼接图片的URL参数
* @param baseUrl
* @param mode
* @param width
* @param height
* @param domain
* @param quality
* @param format
* @return
*/
private static String makeRequest(String baseUrl, Integer mode, Integer width, Integer height, String domain,
String quality, String format) {
StringBuilder sb = new StringBuilder();
if (null != mode) {
sb.append(mode);
}
if (null != width) {
sb.append("/w/").append(width);
}
if (null != height) {
sb.append("/h/").append(height);
}
if (null != quality) {
sb.append("/q/").append(quality);
}
if (null != format) {
sb.append("/format/").append(format);
}
if (0 == sb.length()) {
return baseUrl;
}
if (null == width || null == height) {
return baseUrl;
}
return baseUrl + "?imageView/" + sb.toString();
}
private static String getDomain(String imageUrl) {
if (imageUrl.length() < 17) {
return "";
}
String node = imageUrl.substring(15, 17);
List<String> domainList = getDomainList(node);
if (domainList.isEmpty()) {
return "";
}
return domainList.get(Math.random() > 0.5 ? 1 : 0);
}
/**
* 图片的节点服务器列表
* @param node
* @return
*/
private static List<String> getDomainList(String node) {
if ("01".equals(node)) {
return Lists.newArrayList("img10.static.yhbimg.com", "img11.static.yhbimg.com");
}else if("02".equals(node)) {
return Lists.newArrayList("img12.static.yhbimg.com", "img13.static.yhbimg.com");
}
return new ArrayList<String>(0);
}
}