|
| 1 | +package io.imagekit.sdk; |
| 2 | + |
| 3 | +import io.imagekit.sdk.config.Configuration; |
| 4 | +import io.imagekit.sdk.models.*; |
| 5 | +import io.imagekit.sdk.models.results.*; |
| 6 | +import io.imagekit.sdk.tasks.Calculation; |
| 7 | +import io.imagekit.sdk.tasks.RestClient; |
| 8 | +import io.imagekit.sdk.tasks.UrlGen; |
| 9 | + |
| 10 | +import java.util.ArrayList; |
| 11 | +import java.util.HashMap; |
| 12 | +import java.util.List; |
| 13 | +import java.util.Map; |
| 14 | + |
| 15 | +public final class ImageKit { |
| 16 | + private static ImageKit imageKit; |
| 17 | + private Configuration configuration; |
| 18 | + private RestClient restClient; |
| 19 | + private ImageKit(){ |
| 20 | + configuration=new Configuration(); |
| 21 | + } |
| 22 | + |
| 23 | + /** |
| 24 | + * getInstance() method will return single instance all over the application |
| 25 | + * @return object ImageKit |
| 26 | + */ |
| 27 | + public static synchronized ImageKit getInstance(){ |
| 28 | + if (imageKit==null) { |
| 29 | + imageKit = new ImageKit(); |
| 30 | + imageKit.restClient =new RestClient(imageKit); |
| 31 | + } |
| 32 | + return imageKit; |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * setRestClient(RestClient restClient) it takes object of RestClient class |
| 37 | + * @param restClient to set restClient |
| 38 | + */ |
| 39 | + void setRestClient(RestClient restClient){ |
| 40 | + this.restClient = restClient; |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * setConfig(Configuration config) |
| 45 | + * @param config will save new configuration |
| 46 | + */ |
| 47 | + public void setConfig(Configuration config){ |
| 48 | + this.configuration=config; |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * getConfig() |
| 53 | + * @return it will return current configuration |
| 54 | + */ |
| 55 | + public Configuration getConfig() { |
| 56 | + return configuration; |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * |
| 61 | + * @param options is a HashMap of Objects it can contains following keys - |
| 62 | + * ["path","src","urlEndpoint","transformation", |
| 63 | + * "transformationPosition","queryParameters", |
| 64 | + * "signed","expireSeconds"] |
| 65 | + * where transformation is an List of HashMap, signed is boolean and expireSeconds is integer. |
| 66 | + * @return String new generated url |
| 67 | + */ |
| 68 | + |
| 69 | + public String getUrl(Map<String, Object> options){ |
| 70 | + return UrlGen.getUrl(options); |
| 71 | + } |
| 72 | + |
| 73 | + |
| 74 | + /** |
| 75 | + * |
| 76 | + * @param fileCreateRequest is a object which contains file and other parameters |
| 77 | + * @return object of Result class |
| 78 | + */ |
| 79 | + public Result upload(FileCreateRequest fileCreateRequest){ |
| 80 | + return restClient.upload(fileCreateRequest); |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * |
| 85 | + * @param fileUpdateRequest is a object which contains parameters and fileId |
| 86 | + * @return object of Result class |
| 87 | + */ |
| 88 | + public Result updateFileDetail(FileUpdateRequest fileUpdateRequest){ |
| 89 | + return restClient.updateDetail(fileUpdateRequest); |
| 90 | + } |
| 91 | + |
| 92 | + |
| 93 | + /** |
| 94 | + * |
| 95 | + * @param options is an map it may contain keys [ |
| 96 | + * "path", "fileType", "tags", "includeFolder", "name", "limit", "skip"] |
| 97 | + * @return ResultList class that contains list of BaseFile |
| 98 | + */ |
| 99 | + public ResultList getFileList(Map<String, String> options){ |
| 100 | + return restClient.getFileList(options); |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * |
| 105 | + * @param fileId is a unique file id |
| 106 | + * @return Result class |
| 107 | + */ |
| 108 | + public Result getFileDetail(String fileId){ |
| 109 | + return restClient.getFileDetail(fileId); |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * |
| 114 | + * @param fileId is a unique file id |
| 115 | + * @return ResultMetaData class |
| 116 | + */ |
| 117 | + public ResultMetaData getFileMetadata(String fileId){ |
| 118 | + return restClient.getFileMetaData(fileId); |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * |
| 123 | + * @param url is a remote image url |
| 124 | + * @return ResultMetaData class |
| 125 | + */ |
| 126 | + public ResultMetaData getRemoteFileMetadata(String url){ |
| 127 | + return restClient.getRemoteFileMetaData(url); |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * |
| 132 | + * @param fileId is a unique file id |
| 133 | + * @return Result class |
| 134 | + */ |
| 135 | + public Result deleteFile(String fileId){ |
| 136 | + return restClient.deleteFile(fileId); |
| 137 | + } |
| 138 | + |
| 139 | + /** |
| 140 | + * |
| 141 | + * @param fileIds is a list of unique file id |
| 142 | + * @return Result class |
| 143 | + */ |
| 144 | + public ResultFileDelete bulkDeleteFiles(List<String> fileIds){ |
| 145 | + return restClient.bulkDeleteFiles(fileIds); |
| 146 | + } |
| 147 | + |
| 148 | + /** |
| 149 | + * |
| 150 | + * @param url is image url |
| 151 | + * @return ResultCache class |
| 152 | + */ |
| 153 | + public ResultCache purgeCache(String url){ |
| 154 | + return restClient.purgeCache(url); |
| 155 | + } |
| 156 | + |
| 157 | + /** |
| 158 | + * |
| 159 | + * @param requestId is cache request id |
| 160 | + * @return ResultCacheStatus class |
| 161 | + */ |
| 162 | + public ResultCacheStatus getPurgeCacheStatus(String requestId){ |
| 163 | + return restClient.getPurgeCacheStatus(requestId); |
| 164 | + } |
| 165 | + |
| 166 | + /** |
| 167 | + * |
| 168 | + * @return a map that contains token, expire and signature |
| 169 | + */ |
| 170 | + public Map<String, String> getAuthenticationParameters(){ |
| 171 | + return Calculation.getAuthenticatedParams(null,0,configuration.getPrivateKey()); |
| 172 | + } |
| 173 | + |
| 174 | + /** |
| 175 | + * |
| 176 | + * @param token take as a argument |
| 177 | + * @return a map that contains token, expire and signature |
| 178 | + */ |
| 179 | + public Map<String, String> getAuthenticationParameters(String token){ |
| 180 | + return Calculation.getAuthenticatedParams(token,0,configuration.getPrivateKey()); |
| 181 | + } |
| 182 | + |
| 183 | + /** |
| 184 | + * |
| 185 | + * @param token take as first argument |
| 186 | + * @param expire is a Timestamp in milliseconds take as second argument |
| 187 | + * @return a map that contains token, expire and signature |
| 188 | + */ |
| 189 | + public Map<String, String> getAuthenticationParameters(String token, long expire){ |
| 190 | + return Calculation.getAuthenticatedParams(token,expire,configuration.getPrivateKey()); |
| 191 | + } |
| 192 | + |
| 193 | + /** |
| 194 | + * firstHex and secondHex must have equal length |
| 195 | + * @param firstHex take Hex as a String argument |
| 196 | + * @param secondHex take Hex as a String argument |
| 197 | + * @return it's distance |
| 198 | + */ |
| 199 | + public int pHashDistance(String firstHex, String secondHex){ |
| 200 | + return Calculation.getHammingDistance(firstHex,secondHex); |
| 201 | + } |
| 202 | +} |
0 commit comments