|
@@ -5,12 +5,18 @@ import cn.nosum.http.SimpleDownloadRequestExecutor;
|
|
|
import cn.nosum.http.SimpleUploadRequestExecutor;
|
|
|
import cn.nosum.http.entity.DownloadResponse;
|
|
|
import cn.nosum.http.entity.UploadRequest;
|
|
|
+import cn.nosum.http.exception.HttpError;
|
|
|
+import cn.nosum.http.exception.HttpErrorException;
|
|
|
import cn.nosum.wx.common.entity.result.WxMediaUploadResult;
|
|
|
import cn.nosum.wx.common.error.WxErrorException;
|
|
|
import cn.nosum.wx.cp.api.WxCpMediaService;
|
|
|
import cn.nosum.wx.cp.api.WxCpService;
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
|
|
|
+import java.io.ByteArrayInputStream;
|
|
|
+import java.io.ByteArrayOutputStream;
|
|
|
+import java.io.IOException;
|
|
|
+
|
|
|
import static cn.nosum.wx.cp.constant.WxCpApiPathConsts.Media.*;
|
|
|
|
|
|
|
|
@@ -25,33 +31,38 @@ public class WxCpMediaServiceImpl implements WxCpMediaService {
|
|
|
private final WxCpService mainService;
|
|
|
|
|
|
@Override
|
|
|
+ public String uploadImg(String url) throws WxErrorException {
|
|
|
+ try {
|
|
|
+ DownloadResponse execute = SimpleDownloadRequestExecutor.create(this.mainService.getRequestHttp()).execute(url, null);
|
|
|
+ UploadRequest uploadRequest = UploadRequest.builder().inputStream(new ByteArrayInputStream(((ByteArrayOutputStream) execute.getOutputStream()).toByteArray())).build();
|
|
|
+ return this.uploadImg(uploadRequest);
|
|
|
+ } catch (HttpErrorException e) {
|
|
|
+ throw new WxErrorException(e.getError());
|
|
|
+ } catch (IOException e) {
|
|
|
+ throw new WxErrorException(e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
public String uploadImg(UploadRequest request) throws WxErrorException {
|
|
|
final String url = this.mainService.getWxCpConfigStorage().getApiUrl(IMG_UPLOAD);
|
|
|
- return WxMediaUploadResult.fromJson(
|
|
|
- this.mainService.execute(SimpleUploadRequestExecutor.create(this.mainService.getRequestHttp()), url, request)
|
|
|
- ).getUrl();
|
|
|
+ return WxMediaUploadResult.fromJson(this.mainService.execute(SimpleUploadRequestExecutor.create(this.mainService.getRequestHttp()), url, request)).getUrl();
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public WxMediaUploadResult upload(String mediaType, UploadRequest request) throws WxErrorException {
|
|
|
RequestExecutor<UploadRequest, String> executor = SimpleUploadRequestExecutor.create(this.mainService.getRequestHttp());
|
|
|
- return WxMediaUploadResult.fromJson(
|
|
|
- this.mainService.execute(executor, this.mainService.getWxCpConfigStorage().getApiUrl(MEDIA_UPLOAD + mediaType), request
|
|
|
- ));
|
|
|
+ return WxMediaUploadResult.fromJson(this.mainService.execute(executor, this.mainService.getWxCpConfigStorage().getApiUrl(MEDIA_UPLOAD + mediaType), request));
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public DownloadResponse download(String mediaId) throws WxErrorException {
|
|
|
- return this.mainService.execute(
|
|
|
- SimpleDownloadRequestExecutor.create(this.mainService.getRequestHttp()),
|
|
|
- this.mainService.getWxCpConfigStorage().getApiUrl(MEDIA_GET), "media_id=" + mediaId);
|
|
|
+ return this.mainService.execute(SimpleDownloadRequestExecutor.create(this.mainService.getRequestHttp()), this.mainService.getWxCpConfigStorage().getApiUrl(MEDIA_GET), "media_id=" + mediaId);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public DownloadResponse getJsSdk(String mediaId) throws WxErrorException {
|
|
|
- return this.mainService.execute(
|
|
|
- SimpleDownloadRequestExecutor.create(this.mainService.getRequestHttp()),
|
|
|
- this.mainService.getWxCpConfigStorage().getApiUrl(JS_SDK_MEDIA_GET), "media_id=" + mediaId);
|
|
|
+ return this.mainService.execute(SimpleDownloadRequestExecutor.create(this.mainService.getRequestHttp()), this.mainService.getWxCpConfigStorage().getApiUrl(JS_SDK_MEDIA_GET), "media_id=" + mediaId);
|
|
|
}
|
|
|
|
|
|
}
|