|
| 1 | +package cn.jpush.api.image; |
| 2 | + |
| 3 | +import cn.jiguang.common.ClientConfig; |
| 4 | +import cn.jiguang.common.ServiceHelper; |
| 5 | +import cn.jiguang.common.connection.HttpProxy; |
| 6 | +import cn.jiguang.common.connection.IHttpClient; |
| 7 | +import cn.jiguang.common.connection.NativeHttpClient; |
| 8 | +import cn.jiguang.common.resp.APIConnectionException; |
| 9 | +import cn.jiguang.common.resp.APIRequestException; |
| 10 | +import cn.jiguang.common.resp.ResponseWrapper; |
| 11 | +import cn.jiguang.common.utils.Preconditions; |
| 12 | +import cn.jiguang.common.utils.StringUtils; |
| 13 | +import cn.jpush.api.image.model.ImageFilePayload; |
| 14 | +import cn.jpush.api.image.model.ImageSource; |
| 15 | +import cn.jpush.api.image.model.ImageUploadResult; |
| 16 | +import cn.jpush.api.image.model.ImageUrlPayload; |
| 17 | +import com.google.gson.Gson; |
| 18 | +import com.google.gson.JsonElement; |
| 19 | +import com.google.gson.JsonSyntaxException; |
| 20 | +import org.slf4j.Logger; |
| 21 | +import org.slf4j.LoggerFactory; |
| 22 | + |
| 23 | +import java.util.HashMap; |
| 24 | +import java.util.Map; |
| 25 | + |
| 26 | +/** |
| 27 | + * Provide the ability to upload images to the Jiguang server. Only images in JPG, JPEG and PNG format are supported. |
| 28 | + * |
| 29 | + * @author fuyx |
| 30 | + * @version 2020-12-14 |
| 31 | + */ |
| 32 | +public class ImageClient { |
| 33 | + |
| 34 | + protected static final Logger LOG = LoggerFactory.getLogger(ImageClient.class); |
| 35 | + |
| 36 | + private IHttpClient _httpClient; |
| 37 | + private String _baseUrl; |
| 38 | + private String _imagesPath; |
| 39 | + private Gson _gson = new Gson(); |
| 40 | + |
| 41 | + public ImageClient(String masterSecret, String appKey) { |
| 42 | + this(masterSecret, appKey, null, ClientConfig.getInstance()); |
| 43 | + } |
| 44 | + |
| 45 | + public ImageClient(String masterSecret, String appKey, HttpProxy proxy, ClientConfig conf) { |
| 46 | + _baseUrl = (String) conf.get(ClientConfig.PUSH_HOST_NAME); |
| 47 | + _imagesPath = (String) conf.get(ClientConfig.V3_IMAGES_PATH); |
| 48 | + String authCode = ServiceHelper.getBasicAuthorization(appKey, masterSecret); |
| 49 | + this._httpClient = new NativeHttpClient(authCode, proxy, conf); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Upload image by url. Require at least one non-null url. |
| 54 | + */ |
| 55 | + public ImageUploadResult uploadImage(ImageUrlPayload imageUrlPayload) |
| 56 | + throws APIConnectionException, APIRequestException { |
| 57 | + Preconditions.checkArgument(imageUrlPayload.getImageType() != null, "Image type should not be null"); |
| 58 | + checkImageUrlPayload(imageUrlPayload); |
| 59 | + NativeHttpClient client = (NativeHttpClient) _httpClient; |
| 60 | + String url = _baseUrl + _imagesPath + "/" + ImageSource.URL.value(); |
| 61 | + JsonElement jsonElement = imageUrlPayload.toJSON(); |
| 62 | + String content = _gson.toJson(jsonElement); |
| 63 | + ResponseWrapper responseWrapper = client.sendPost(url, content); |
| 64 | + if (responseWrapper.responseCode != 200) { |
| 65 | + LOG.error("upload image failed: {}", responseWrapper); |
| 66 | + } |
| 67 | + ImageUploadResult imageUploadResult = _gson.fromJson(responseWrapper.responseContent, ImageUploadResult.class); |
| 68 | + |
| 69 | + LOG.info("upload image result:{}", imageUploadResult); |
| 70 | + return imageUploadResult; |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Upload image by file. Require at least 1 non-null fileName. Currently only support Xiaomi and OPPO |
| 75 | + */ |
| 76 | + public ImageUploadResult uploadImage(ImageFilePayload imageFilePayload) { |
| 77 | + Preconditions.checkArgument(imageFilePayload.getImageType() != null, "Image type should not be null"); |
| 78 | + checkImageFilePayload(imageFilePayload); |
| 79 | + NativeHttpClient client = (NativeHttpClient) _httpClient; |
| 80 | + String url = _baseUrl + _imagesPath + "/" + ImageSource.FILE.value(); |
| 81 | + |
| 82 | + Map<String, String> textMap = new HashMap<>(); |
| 83 | + textMap.put("image_type", String.valueOf(imageFilePayload.getImageType().value())); |
| 84 | + |
| 85 | + Map<String, String> fileMap = imageFilePayload.toFileMap(); |
| 86 | + LOG.debug("upload fileMap: {}", fileMap); |
| 87 | + String response = client.formUploadByPost(url, textMap, fileMap, null); |
| 88 | + LOG.debug("upload image result: {}", response); |
| 89 | + ImageUploadResult imageUploadResult; |
| 90 | + try { |
| 91 | + imageUploadResult = _gson.fromJson(response, ImageUploadResult.class); |
| 92 | + } catch (JsonSyntaxException e) { |
| 93 | + LOG.error("could not parse response: {}", response); |
| 94 | + throw new IllegalStateException("could not parse response", e); |
| 95 | + } |
| 96 | + LOG.info("upload image result:{}", imageUploadResult); |
| 97 | + return imageUploadResult; |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Modify image by url. Require at least one non-null url. |
| 102 | + */ |
| 103 | + public ImageUploadResult modifyImage(String mediaId, ImageUrlPayload imageUrlPayload) |
| 104 | + throws APIConnectionException, APIRequestException { |
| 105 | + Preconditions.checkArgument(StringUtils.isNotEmpty(mediaId), "mediaId should not be empty"); |
| 106 | + checkImageUrlPayload(imageUrlPayload); |
| 107 | + NativeHttpClient client = (NativeHttpClient) _httpClient; |
| 108 | + String url = _baseUrl + _imagesPath + "/" + ImageSource.URL.value() + "/" + mediaId; |
| 109 | + JsonElement jsonElement = imageUrlPayload.toJSON(); |
| 110 | + String content = _gson.toJson(jsonElement); |
| 111 | + ResponseWrapper responseWrapper = client.sendPut(url, content); |
| 112 | + if (responseWrapper.responseCode != 200) { |
| 113 | + LOG.error("upload image failed: {}", responseWrapper); |
| 114 | + } |
| 115 | + ImageUploadResult imageUploadResult = _gson.fromJson(responseWrapper.responseContent, ImageUploadResult.class); |
| 116 | + |
| 117 | + LOG.info("upload image result:{}", imageUploadResult); |
| 118 | + return imageUploadResult; |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * Modify image by file. Require at least 1 non-null fileName. Currently only support Xiaomi and OPPO |
| 123 | + */ |
| 124 | + public ImageUploadResult modifyImage(String mediaId, ImageFilePayload imageFilePayload) { |
| 125 | + Preconditions.checkArgument(StringUtils.isNotEmpty(mediaId), "mediaId should not be empty"); |
| 126 | + checkImageFilePayload(imageFilePayload); |
| 127 | + NativeHttpClient client = (NativeHttpClient) _httpClient; |
| 128 | + String url = _baseUrl + _imagesPath + "/" + ImageSource.FILE.value() + "/" + mediaId; |
| 129 | + |
| 130 | + Map<String, String> fileMap = imageFilePayload.toFileMap(); |
| 131 | + LOG.debug("upload image fileMap: {}", fileMap); |
| 132 | + String response = client.formUploadByPut(url, null, fileMap, null); |
| 133 | + LOG.debug("upload image result: {}", response); |
| 134 | + ImageUploadResult imageUploadResult; |
| 135 | + try { |
| 136 | + imageUploadResult = _gson.fromJson(response, ImageUploadResult.class); |
| 137 | + } catch (JsonSyntaxException e) { |
| 138 | + LOG.error("could not parse response: {}", response); |
| 139 | + throw new IllegalStateException("could not parse response", e); |
| 140 | + } |
| 141 | + LOG.info("upload image result:{}", imageUploadResult); |
| 142 | + return imageUploadResult; |
| 143 | + } |
| 144 | + |
| 145 | + private void checkImageUrlPayload(ImageUrlPayload imageUrlPayload) { |
| 146 | + boolean anyUrlNotEmpty = StringUtils.isNotEmpty(imageUrlPayload.getImageUrl()) |
| 147 | + || StringUtils.isNotEmpty(imageUrlPayload.getFcmImageUrl()) |
| 148 | + || StringUtils.isNotEmpty(imageUrlPayload.getHuaweiImageUrl()) |
| 149 | + || StringUtils.isNotEmpty(imageUrlPayload.getOppoImageUrl()) |
| 150 | + || StringUtils.isNotEmpty(imageUrlPayload.getXiaomiImageUrl()) |
| 151 | + || StringUtils.isNotEmpty(imageUrlPayload.getJiguangImageUrl()) ; |
| 152 | + Preconditions.checkArgument(anyUrlNotEmpty, "Require at least 1 non-empty url"); |
| 153 | + } |
| 154 | + |
| 155 | + private void checkImageFilePayload(ImageFilePayload imageFilePayload) { |
| 156 | + boolean anyFileNotEmpty = StringUtils.isNotEmpty(imageFilePayload.getOppoFileName() ) |
| 157 | + || StringUtils.isNotEmpty(imageFilePayload.getXiaomiFileName() ); |
| 158 | + Preconditions.checkArgument(anyFileNotEmpty, "Require at least 1 non-empty fileName. Currently only support Xiaomi and OPPO"); |
| 159 | + } |
| 160 | + |
| 161 | + |
| 162 | +} |
0 commit comments