|
| 1 | +package cn.jpush.api.files; |
| 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.file.model.FileModel; |
| 14 | +import cn.jpush.api.file.model.FileModelPage; |
| 15 | +import cn.jpush.api.file.model.FileType; |
| 16 | +import cn.jpush.api.file.model.FileUploadResult; |
| 17 | +import com.google.gson.Gson; |
| 18 | +import com.google.gson.reflect.TypeToken; |
| 19 | +import org.slf4j.Logger; |
| 20 | +import org.slf4j.LoggerFactory; |
| 21 | + |
| 22 | +import java.util.HashMap; |
| 23 | +import java.util.Map; |
| 24 | + |
| 25 | +/** |
| 26 | + * @author daixuan |
| 27 | + * @version 2020/2/23 19:38 |
| 28 | + */ |
| 29 | +public class FileClient2 { |
| 30 | + |
| 31 | + protected static final Logger LOG = LoggerFactory.getLogger(FileClient2.class); |
| 32 | + |
| 33 | + private IHttpClient _httpClient; |
| 34 | + private String _baseUrl; |
| 35 | + private String _filesPath; |
| 36 | + private Gson _gson = new Gson(); |
| 37 | + |
| 38 | + public FileClient2(String masterSecret, String appKey) { |
| 39 | + this(masterSecret, appKey, null, ClientConfig.getInstance()); |
| 40 | + } |
| 41 | + |
| 42 | + public FileClient2(String masterSecret, String appKey, HttpProxy proxy, ClientConfig conf) { |
| 43 | + _baseUrl = (String) conf.get(ClientConfig.PUSH_HOST_NAME); |
| 44 | + _filesPath = (String) conf.get(ClientConfig.V3_FILES_PATH); |
| 45 | + String authCode = ServiceHelper.getBasicAuthorization(appKey, masterSecret); |
| 46 | + this._httpClient = new NativeHttpClient(authCode, proxy, conf); |
| 47 | + } |
| 48 | + |
| 49 | + public String uploadFile(FileType type, String filename, Integer ttl) |
| 50 | + throws APIConnectionException, APIRequestException { |
| 51 | + Preconditions.checkArgument(type != null, "type should not be null"); |
| 52 | + Preconditions.checkArgument(StringUtils.isNotEmpty(filename), "filename should not be null"); |
| 53 | + Preconditions.checkArgument(ttl >= 1 && ttl <= 720,"TTL is not in the range of 1 to 720"); |
| 54 | + NativeHttpClient client = (NativeHttpClient) _httpClient; |
| 55 | + String typeStr = type.value(); |
| 56 | + String url = _baseUrl + _filesPath + "/" + typeStr; |
| 57 | + Map<String, String> fileMap = new HashMap<String,String>(); |
| 58 | + fileMap.put("filename", filename); |
| 59 | + Map<String, String> textMap = new HashMap<String,String>(); |
| 60 | + textMap.put("ttl",String.valueOf(ttl)); |
| 61 | + String response = client.formUploadByPost(url, textMap, fileMap, null); |
| 62 | + LOG.info("uploadFile:{}", response); |
| 63 | + return response; |
| 64 | + } |
| 65 | + |
| 66 | + public String uploadFile(FileType type, String filename) throws APIConnectionException, APIRequestException { |
| 67 | + return uploadFile(type,filename,720); |
| 68 | + } |
| 69 | + |
| 70 | + public String queryEffectFiles() throws APIConnectionException, APIRequestException { |
| 71 | + String url = _baseUrl + _filesPath + "/"; |
| 72 | + ResponseWrapper response = _httpClient.sendGet(url); |
| 73 | + LOG.info("queryEffFiles:{}", response); |
| 74 | + return response.responseContent; |
| 75 | + } |
| 76 | + |
| 77 | + public String queryFile(String fileId) throws APIConnectionException, APIRequestException { |
| 78 | + Preconditions.checkArgument(StringUtils.isNotEmpty(fileId), "fileId should not be null"); |
| 79 | + String url = _baseUrl + _filesPath + "/" + fileId; |
| 80 | + ResponseWrapper response = _httpClient.sendGet(url); |
| 81 | + LOG.info("queryFile:{}", response); |
| 82 | + return response.responseContent; |
| 83 | + } |
| 84 | + |
| 85 | + public ResponseWrapper deleteFile(String fileId) throws APIConnectionException, APIRequestException { |
| 86 | + Preconditions.checkArgument(StringUtils.isNotEmpty(fileId), "fileId should not be null"); |
| 87 | + String url = _baseUrl + _filesPath + "/" + fileId; |
| 88 | + ResponseWrapper response = _httpClient.sendDelete(url); |
| 89 | + LOG.info("deleteFile:{}", response); |
| 90 | + return response; |
| 91 | + } |
| 92 | +} |
0 commit comments