-
Notifications
You must be signed in to change notification settings - Fork 420
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #180 from jpush/feature/20201214_image_api
新增图片操作接口
- Loading branch information
Showing
13 changed files
with
827 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package cn.jpush.api.image; | ||
|
||
import cn.jiguang.common.resp.APIConnectionException; | ||
import cn.jiguang.common.resp.APIRequestException; | ||
import cn.jpush.api.image.model.ImageFilePayload; | ||
import cn.jpush.api.image.model.ImageType; | ||
import cn.jpush.api.image.model.ImageUploadResult; | ||
import cn.jpush.api.image.model.ImageUrlPayload; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public class ImageExample { | ||
protected static final Logger LOG = LoggerFactory.getLogger(ImageExample.class); | ||
|
||
// demo App defined in resources/jpush-api.conf | ||
protected static final String APP_KEY = "e4ceeaf7a53ad745dd4728f2"; | ||
protected static final String MASTER_SECRET = "1582b986adeaf48ceec1e354"; | ||
protected static final String GROUP_PUSH_KEY = "2c88a01e073a0fe4fc7b167c"; | ||
protected static final String GROUP_MASTER_SECRET = "b11314807507e2bcfdeebe2e"; | ||
|
||
public static final String TITLE = "Test from API example"; | ||
public static final String ALERT = "Test from API Example - alert"; | ||
public static final String MSG_CONTENT = "Test from API Example - msgContent"; | ||
public static final String REGISTRATION_ID = "0900e8d85ef"; | ||
public static final String TAG = "tag_api"; | ||
public static long sendCount = 0; | ||
private static long sendTotalTime = 0; | ||
|
||
public static void main(String[] args) throws APIConnectionException, APIRequestException { | ||
testUploadImageByFile(); | ||
testUploadImageByUrl(); | ||
} | ||
|
||
public static void testUploadImageByUrl() throws APIConnectionException, APIRequestException { | ||
ImageClient client = new ImageClient(MASTER_SECRET, APP_KEY); | ||
ImageUrlPayload payload = ImageUrlPayload.newBuilder() | ||
.setImageType(ImageType.LARGE_ICON) | ||
.setImageUrl("http://xxx.com/image/a.jpg") | ||
.build(); | ||
ImageUploadResult imageUploadResult = client.uploadImage(payload); | ||
String mediaId = imageUploadResult.getMediaId(); | ||
} | ||
|
||
public static void testUploadImageByFile() { | ||
ImageClient client = new ImageClient(MASTER_SECRET, APP_KEY); | ||
ImageFilePayload payload = ImageFilePayload.newBuilder() | ||
.setImageType(ImageType.BIG_PICTURE) | ||
// 本地文件路径 | ||
.setOppoFileName("/MyDir/a.jpg") | ||
.setXiaomiFileName("/MyDir/a.jpg") | ||
.build(); | ||
ImageUploadResult imageUploadResult = client.uploadImage(payload); | ||
String mediaId = imageUploadResult.getMediaId(); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
package cn.jpush.api.image; | ||
|
||
import cn.jiguang.common.ClientConfig; | ||
import cn.jiguang.common.ServiceHelper; | ||
import cn.jiguang.common.connection.HttpProxy; | ||
import cn.jiguang.common.connection.IHttpClient; | ||
import cn.jiguang.common.connection.NativeHttpClient; | ||
import cn.jiguang.common.resp.APIConnectionException; | ||
import cn.jiguang.common.resp.APIRequestException; | ||
import cn.jiguang.common.resp.ResponseWrapper; | ||
import cn.jiguang.common.utils.Preconditions; | ||
import cn.jiguang.common.utils.StringUtils; | ||
import cn.jpush.api.image.model.ImageFilePayload; | ||
import cn.jpush.api.image.model.ImageSource; | ||
import cn.jpush.api.image.model.ImageUploadResult; | ||
import cn.jpush.api.image.model.ImageUrlPayload; | ||
import com.google.gson.Gson; | ||
import com.google.gson.JsonElement; | ||
import com.google.gson.JsonSyntaxException; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
/** | ||
* Provide the ability to upload images to the Jiguang server. Only images in JPG, JPEG and PNG format are supported. | ||
* | ||
* @author fuyx | ||
* @version 2020-12-14 | ||
*/ | ||
public class ImageClient { | ||
|
||
protected static final Logger LOG = LoggerFactory.getLogger(ImageClient.class); | ||
|
||
private IHttpClient _httpClient; | ||
private String _baseUrl; | ||
private String _imagesPath; | ||
private Gson _gson = new Gson(); | ||
|
||
public ImageClient(String masterSecret, String appKey) { | ||
this(masterSecret, appKey, null, ClientConfig.getInstance()); | ||
} | ||
|
||
public ImageClient(String masterSecret, String appKey, HttpProxy proxy, ClientConfig conf) { | ||
_baseUrl = (String) conf.get(ClientConfig.PUSH_HOST_NAME); | ||
_imagesPath = (String) conf.get(ClientConfig.V3_IMAGES_PATH); | ||
String authCode = ServiceHelper.getBasicAuthorization(appKey, masterSecret); | ||
this._httpClient = new NativeHttpClient(authCode, proxy, conf); | ||
} | ||
|
||
/** | ||
* Upload image by url. Require at least one non-null url. | ||
*/ | ||
public ImageUploadResult uploadImage(ImageUrlPayload imageUrlPayload) | ||
throws APIConnectionException, APIRequestException { | ||
Preconditions.checkArgument(imageUrlPayload.getImageType() != null, "Image type should not be null"); | ||
checkImageUrlPayload(imageUrlPayload); | ||
NativeHttpClient client = (NativeHttpClient) _httpClient; | ||
String url = _baseUrl + _imagesPath + "/" + ImageSource.URL.value(); | ||
JsonElement jsonElement = imageUrlPayload.toJSON(); | ||
String content = _gson.toJson(jsonElement); | ||
ResponseWrapper responseWrapper = client.sendPost(url, content); | ||
if (responseWrapper.responseCode != 200) { | ||
LOG.error("upload image failed: {}", responseWrapper); | ||
} | ||
ImageUploadResult imageUploadResult = _gson.fromJson(responseWrapper.responseContent, ImageUploadResult.class); | ||
|
||
LOG.info("upload image result:{}", imageUploadResult); | ||
return imageUploadResult; | ||
} | ||
|
||
/** | ||
* Upload image by file. Require at least 1 non-null fileName. Currently only support Xiaomi and OPPO | ||
*/ | ||
public ImageUploadResult uploadImage(ImageFilePayload imageFilePayload) { | ||
Preconditions.checkArgument(imageFilePayload.getImageType() != null, "Image type should not be null"); | ||
checkImageFilePayload(imageFilePayload); | ||
NativeHttpClient client = (NativeHttpClient) _httpClient; | ||
String url = _baseUrl + _imagesPath + "/" + ImageSource.FILE.value(); | ||
|
||
Map<String, String> textMap = new HashMap<>(); | ||
textMap.put("image_type", String.valueOf(imageFilePayload.getImageType().value())); | ||
|
||
Map<String, String> fileMap = imageFilePayload.toFileMap(); | ||
LOG.debug("upload fileMap: {}", fileMap); | ||
String response = client.formUploadByPost(url, textMap, fileMap, null); | ||
LOG.debug("upload image result: {}", response); | ||
ImageUploadResult imageUploadResult; | ||
try { | ||
imageUploadResult = _gson.fromJson(response, ImageUploadResult.class); | ||
} catch (JsonSyntaxException e) { | ||
LOG.error("could not parse response: {}", response); | ||
throw new IllegalStateException("could not parse response", e); | ||
} | ||
LOG.info("upload image result:{}", imageUploadResult); | ||
return imageUploadResult; | ||
} | ||
|
||
/** | ||
* Modify image by url. Require at least one non-null url. | ||
*/ | ||
public ImageUploadResult modifyImage(String mediaId, ImageUrlPayload imageUrlPayload) | ||
throws APIConnectionException, APIRequestException { | ||
Preconditions.checkArgument(StringUtils.isNotEmpty(mediaId), "mediaId should not be empty"); | ||
checkImageUrlPayload(imageUrlPayload); | ||
NativeHttpClient client = (NativeHttpClient) _httpClient; | ||
String url = _baseUrl + _imagesPath + "/" + ImageSource.URL.value() + "/" + mediaId; | ||
JsonElement jsonElement = imageUrlPayload.toJSON(); | ||
String content = _gson.toJson(jsonElement); | ||
ResponseWrapper responseWrapper = client.sendPut(url, content); | ||
if (responseWrapper.responseCode != 200) { | ||
LOG.error("upload image failed: {}", responseWrapper); | ||
} | ||
ImageUploadResult imageUploadResult = _gson.fromJson(responseWrapper.responseContent, ImageUploadResult.class); | ||
|
||
LOG.info("upload image result:{}", imageUploadResult); | ||
return imageUploadResult; | ||
} | ||
|
||
/** | ||
* Modify image by file. Require at least 1 non-null fileName. Currently only support Xiaomi and OPPO | ||
*/ | ||
public ImageUploadResult modifyImage(String mediaId, ImageFilePayload imageFilePayload) { | ||
Preconditions.checkArgument(StringUtils.isNotEmpty(mediaId), "mediaId should not be empty"); | ||
checkImageFilePayload(imageFilePayload); | ||
NativeHttpClient client = (NativeHttpClient) _httpClient; | ||
String url = _baseUrl + _imagesPath + "/" + ImageSource.FILE.value() + "/" + mediaId; | ||
|
||
Map<String, String> fileMap = imageFilePayload.toFileMap(); | ||
LOG.debug("upload image fileMap: {}", fileMap); | ||
String response = client.formUploadByPut(url, null, fileMap, null); | ||
LOG.debug("upload image result: {}", response); | ||
ImageUploadResult imageUploadResult; | ||
try { | ||
imageUploadResult = _gson.fromJson(response, ImageUploadResult.class); | ||
} catch (JsonSyntaxException e) { | ||
LOG.error("could not parse response: {}", response); | ||
throw new IllegalStateException("could not parse response", e); | ||
} | ||
LOG.info("upload image result:{}", imageUploadResult); | ||
return imageUploadResult; | ||
} | ||
|
||
private void checkImageUrlPayload(ImageUrlPayload imageUrlPayload) { | ||
boolean anyUrlNotEmpty = StringUtils.isNotEmpty(imageUrlPayload.getImageUrl()) | ||
|| StringUtils.isNotEmpty(imageUrlPayload.getFcmImageUrl()) | ||
|| StringUtils.isNotEmpty(imageUrlPayload.getHuaweiImageUrl()) | ||
|| StringUtils.isNotEmpty(imageUrlPayload.getOppoImageUrl()) | ||
|| StringUtils.isNotEmpty(imageUrlPayload.getXiaomiImageUrl()) | ||
|| StringUtils.isNotEmpty(imageUrlPayload.getJiguangImageUrl()) ; | ||
Preconditions.checkArgument(anyUrlNotEmpty, "Require at least 1 non-empty url"); | ||
} | ||
|
||
private void checkImageFilePayload(ImageFilePayload imageFilePayload) { | ||
boolean anyFileNotEmpty = StringUtils.isNotEmpty(imageFilePayload.getOppoFileName() ) | ||
|| StringUtils.isNotEmpty(imageFilePayload.getXiaomiFileName() ); | ||
Preconditions.checkArgument(anyFileNotEmpty, "Require at least 1 non-empty fileName. Currently only support Xiaomi and OPPO"); | ||
} | ||
|
||
|
||
} |
Oops, something went wrong.