🎨 重构 redis 配置存储与媒体下载执行器中的重复代码
This commit is contained in:
committed by
GitHub
parent
8833bce0cf
commit
f95fd76b05
@@ -8,8 +8,6 @@ import me.chanjar.weixin.common.util.http.RequestHttp;
|
||||
import me.chanjar.weixin.common.util.http.ResponseHandler;
|
||||
import me.chanjar.weixin.common.util.http.apache.InputStreamResponseHandler;
|
||||
import me.chanjar.weixin.common.util.http.apache.Utf8ResponseHandler;
|
||||
import org.apache.commons.io.FilenameUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.http.Header;
|
||||
import org.apache.http.HttpHost;
|
||||
import org.apache.http.client.config.RequestConfig;
|
||||
@@ -30,14 +28,7 @@ public class ApacheHttpChannelMediaDownloadRequestExecutor extends ChannelMediaD
|
||||
|
||||
@Override
|
||||
public ChannelImageResponse execute(String uri, String data, WxType wxType) throws WxErrorException, IOException {
|
||||
if (data != null) {
|
||||
if (uri.indexOf('?') == -1) {
|
||||
uri += '?';
|
||||
}
|
||||
uri += uri.endsWith("?") ? data : '&' + data;
|
||||
}
|
||||
|
||||
HttpGet httpGet = new HttpGet(uri);
|
||||
HttpGet httpGet = new HttpGet(this.appendDataToUri(uri, data));
|
||||
if (requestHttp.getRequestHttpProxy() != null) {
|
||||
RequestConfig config = RequestConfig.custom().setProxy(requestHttp.getRequestHttpProxy()).build();
|
||||
httpGet.setConfig(config);
|
||||
@@ -56,20 +47,7 @@ public class ApacheHttpChannelMediaDownloadRequestExecutor extends ChannelMediaD
|
||||
}
|
||||
}
|
||||
|
||||
String fileName = this.getFileName(response);
|
||||
if (StringUtils.isBlank(fileName)) {
|
||||
fileName = String.valueOf(System.currentTimeMillis());
|
||||
}
|
||||
|
||||
String baseName = FilenameUtils.getBaseName(fileName);
|
||||
if (StringUtils.isBlank(fileName) || baseName.length() < 3) {
|
||||
baseName = String.valueOf(System.currentTimeMillis());
|
||||
}
|
||||
String extension = FilenameUtils.getExtension(fileName);
|
||||
if (StringUtils.isBlank(extension)) {
|
||||
extension = "unknown";
|
||||
}
|
||||
File file = createTmpFile(inputStream, baseName, extension, tmpDirFile);
|
||||
File file = this.saveTmpFile(inputStream, this.getFileName(response));
|
||||
return new ChannelImageResponse(file, contentType);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,7 +3,9 @@ package me.chanjar.weixin.channel.executor;
|
||||
import me.chanjar.weixin.channel.bean.image.ChannelImageResponse;
|
||||
import me.chanjar.weixin.common.util.http.RequestExecutor;
|
||||
import me.chanjar.weixin.common.util.http.RequestHttp;
|
||||
import org.apache.commons.io.FilenameUtils;
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
@@ -64,6 +66,45 @@ public abstract class ChannelMediaDownloadRequestExecutor<H, P> implements Reque
|
||||
return resultFile;
|
||||
}
|
||||
|
||||
/**
|
||||
* 把请求参数拼接到请求地址上
|
||||
*
|
||||
* @param uri 请求地址
|
||||
* @param data 请求参数,可为空
|
||||
* @return 拼接后的请求地址
|
||||
*/
|
||||
protected String appendDataToUri(String uri, String data) {
|
||||
if (data == null) {
|
||||
return uri;
|
||||
}
|
||||
String result = uri;
|
||||
if (result.indexOf('?') == -1) {
|
||||
result += '?';
|
||||
}
|
||||
return result + (result.endsWith("?") ? data : '&' + data);
|
||||
}
|
||||
|
||||
/**
|
||||
* 按照响应中的文件名把媒体内容写入临时文件
|
||||
*
|
||||
* @param inputStream 媒体内容
|
||||
* @param fileName 响应中解析出的文件名,可为空
|
||||
* @return 临时文件
|
||||
*/
|
||||
protected File saveTmpFile(InputStream inputStream, String fileName) throws IOException {
|
||||
String name = StringUtils.isBlank(fileName) ? String.valueOf(System.currentTimeMillis()) : fileName;
|
||||
|
||||
String baseName = FilenameUtils.getBaseName(name);
|
||||
if (StringUtils.isBlank(baseName) || baseName.length() < 3) {
|
||||
baseName = String.valueOf(System.currentTimeMillis());
|
||||
}
|
||||
String extension = FilenameUtils.getExtension(name);
|
||||
if (StringUtils.isBlank(extension)) {
|
||||
extension = "unknown";
|
||||
}
|
||||
return createTmpFile(inputStream, baseName, extension, tmpDirFile);
|
||||
}
|
||||
|
||||
protected String createDefaultFileName() {
|
||||
return UUID.randomUUID().toString();
|
||||
}
|
||||
|
||||
@@ -8,8 +8,6 @@ import me.chanjar.weixin.common.util.http.RequestHttp;
|
||||
import me.chanjar.weixin.common.util.http.ResponseHandler;
|
||||
import me.chanjar.weixin.common.util.http.hc.InputStreamResponseHandler;
|
||||
import me.chanjar.weixin.common.util.http.hc.Utf8ResponseHandler;
|
||||
import org.apache.commons.io.FilenameUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.hc.client5.http.ClientProtocolException;
|
||||
import org.apache.hc.client5.http.classic.methods.HttpGet;
|
||||
import org.apache.hc.client5.http.config.RequestConfig;
|
||||
@@ -32,14 +30,7 @@ public class HttpComponentsChannelMediaDownloadRequestExecutor extends ChannelMe
|
||||
|
||||
@Override
|
||||
public ChannelImageResponse execute(String uri, String data, WxType wxType) throws WxErrorException, IOException {
|
||||
if (data != null) {
|
||||
if (uri.indexOf('?') == -1) {
|
||||
uri += '?';
|
||||
}
|
||||
uri += uri.endsWith("?") ? data : '&' + data;
|
||||
}
|
||||
|
||||
HttpGet httpGet = new HttpGet(uri);
|
||||
HttpGet httpGet = new HttpGet(this.appendDataToUri(uri, data));
|
||||
if (requestHttp.getRequestHttpProxy() != null) {
|
||||
RequestConfig config = RequestConfig.custom().setProxy(requestHttp.getRequestHttpProxy()).build();
|
||||
httpGet.setConfig(config);
|
||||
@@ -58,20 +49,7 @@ public class HttpComponentsChannelMediaDownloadRequestExecutor extends ChannelMe
|
||||
}
|
||||
}
|
||||
|
||||
String fileName = this.getFileName(response);
|
||||
if (StringUtils.isBlank(fileName)) {
|
||||
fileName = String.valueOf(System.currentTimeMillis());
|
||||
}
|
||||
|
||||
String baseName = FilenameUtils.getBaseName(fileName);
|
||||
if (StringUtils.isBlank(fileName) || baseName.length() < 3) {
|
||||
baseName = String.valueOf(System.currentTimeMillis());
|
||||
}
|
||||
String extension = FilenameUtils.getExtension(fileName);
|
||||
if (StringUtils.isBlank(extension)) {
|
||||
extension = "unknown";
|
||||
}
|
||||
File file = createTmpFile(inputStream, baseName, extension, tmpDirFile);
|
||||
File file = this.saveTmpFile(inputStream, this.getFileName(response));
|
||||
return new ChannelImageResponse(file, contentType);
|
||||
} catch (HttpException httpException) {
|
||||
throw new ClientProtocolException(httpException.getMessage(), httpException);
|
||||
|
||||
Reference in New Issue
Block a user