Skip to content

Make Result class containt Java's BufferedImage field #30

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion imagekit-sdk/src/main/java/io/imagekit/sdk/ImageKit.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import io.imagekit.sdk.tasks.RestClient;
import io.imagekit.sdk.tasks.UrlGen;

import java.net.MalformedURLException;
import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -88,7 +89,7 @@ public String getUrl(Map<String, Object> options) {
* @return object of Result class
*/
public Result upload(FileCreateRequest fileCreateRequest) throws InternalServerException, BadRequestException,
UnknownException, ForbiddenException, TooManyRequestsException, UnauthorizedException {
UnknownException, ForbiddenException, TooManyRequestsException, UnauthorizedException, MalformedURLException {
return restClient.upload(fileCreateRequest);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,27 @@ public class FileCreateRequest {
public Boolean overwriteTags;
public Boolean overwriteCustomMetadata;
public JsonObject customMetadata;
public boolean isReadableImage;

public FileCreateRequest(URL url, String fileName) {
this.url = url;
this.fileName = fileName;
this.useUniqueFileName = true;
this.isReadableImage = Utils.isReadableImage(url);
}

public FileCreateRequest(String base64, String fileName) {
this.base64 = base64;
this.fileName = fileName;
this.useUniqueFileName = true;
this.isReadableImage = Utils.isReadableImage(base64);
}

public FileCreateRequest(byte[] bytes, String fileName) {
this.bytes = bytes;
this.fileName = fileName;
this.useUniqueFileName = true;
this.isReadableImage = Utils.isReadableImage(bytes);
}

public String getFileName() {
Expand Down Expand Up @@ -160,6 +164,14 @@ public void setCustomMetadata(JsonObject customMetadata) {
this.customMetadata = customMetadata;
}

public boolean isReadableImage() {
return isReadableImage;
}

public void setReadableImage(boolean readableImage) {
isReadableImage = readableImage;
}

@Override
public String toString() {
return "FileCreateRequest{" + "fileName='" + fileName + '\'' + ", useUniqueFileName=" + useUniqueFileName
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,26 @@
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;

import io.imagekit.sdk.models.BaseFile;
import io.imagekit.sdk.models.ResponseMetaData;

import io.imagekit.sdk.utils.Utils;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Result extends BaseFile {
private BufferedImage image;
private String help;
@Deprecated
private String raw;
Expand Down Expand Up @@ -50,6 +60,14 @@ public Result(String fileId, String name, String url, String thumbnail, int heig
this.updatedAt = updatedAt;
}

public BufferedImage getImage() {
return this.image;
}

public void setImage(BufferedImage image) {
this.image = image;
}

public String getHelp() {
return help;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@

import java.io.IOException;
import java.lang.reflect.Field;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
Expand All @@ -62,7 +64,7 @@ public RestClient(ImageKit imageKit) {
}

public Result upload(FileCreateRequest fileCreateRequest) throws InternalServerException, BadRequestException,
UnknownException, ForbiddenException, TooManyRequestsException, UnauthorizedException {
UnknownException, ForbiddenException, TooManyRequestsException, UnauthorizedException, MalformedURLException {
Result result = null;
Map<String, String> headers = Utils.getHeaders(imageKit);

Expand All @@ -87,6 +89,9 @@ public Result upload(FileCreateRequest fileCreateRequest) throws InternalServerE
} catch (IOException e) {
throw new UnknownException(e.getMessage(), e.getCause());
}
if(fileCreateRequest.isReadableImage()) {
result.setImage(Utils.createImage(new URL(result.getUrl())));
}
return result;
}

Expand Down
28 changes: 28 additions & 0 deletions imagekit-sdk/src/main/java/io/imagekit/sdk/utils/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@
import okhttp3.Credentials;
import okhttp3.Response;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.URL;
import java.util.ArrayList;
import java.util.Base64;
import java.util.HashMap;
Expand Down Expand Up @@ -182,4 +185,29 @@ public static ResultException populateResult(Response response) throws IOExcepti
return result;
}

public static BufferedImage createImage(URL url) {
try {
return ImageIO.read(url);
} catch (IOException e) {
throw new RuntimeException(e);
}
}

public static boolean isReadableImage(String base64) {
byte[] bytes = Base64.getDecoder().decode(base64);
return isReadableImage(bytes);
}

public static boolean isReadableImage(byte[] bytes) {
InputStream inputStream = new ByteArrayInputStream(bytes);
try {
return ImageIO.read(inputStream) != null;
} catch (IOException e) {
throw new RuntimeException(e);
}
}

public static boolean isReadableImage(URL url) {
return createImage(url) != null;
}
}
44 changes: 44 additions & 0 deletions imagekit-sdk/src/test/java/io/imagekit/sdk/utils/UtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,48 @@ public void test_getSystemConfig_expectedSuccess() throws IOException {
Configuration config=Utils.getSystemConfig(UtilsTest.class);
assertNotNull(config);
}

@Test
public void validImageURL_isReadableImage_expectedTrue() {
URL imageURL = UtilsTest.class.getClassLoader().getResource("sample1.jpg");
assertTrue(Utils.isReadableImage(imageURL));
}

@Test
public void nonImageURL_isReadableImage_expectedFalse() {
URL nonImageURL = UtilsTest.class.getClassLoader().getResource("not_an_image.txt");
assertFalse(Utils.isReadableImage(nonImageURL));
}

@Test
public void validImageByteArray_isReadableImage_expectedTrue() {
URL imageURL = UtilsTest.class.getClassLoader().getResource("sample1.jpg");
File file=new File(imageURL.getPath());
byte[] bytes = Utils.fileToBytes(file);
assertTrue(Utils.isReadableImage(bytes));
}

@Test
public void nonImageByteArray_isReadableImage_expectedFalse() {
URL imageURL = UtilsTest.class.getClassLoader().getResource("not_an_image.txt");
File file=new File(imageURL.getPath());
byte[] bytes = Utils.fileToBytes(file);
assertFalse(Utils.isReadableImage(bytes));
}

@Test
public void validImageBase64_isReadableImage_expectedTrue() {
URL imageURL = UtilsTest.class.getClassLoader().getResource("sample1.jpg");
File file=new File(imageURL.getPath());
String base64 = Utils.fileToBase64(file);
assertTrue(Utils.isReadableImage(base64));
}

@Test
public void nonImageBase64_isReadableImage_expectedFalse() {
URL imageURL = UtilsTest.class.getClassLoader().getResource("not_an_image.txt");
File file=new File(imageURL.getPath());
String base64 = Utils.fileToBase64(file);
assertFalse(Utils.isReadableImage(base64));
}
}
1 change: 1 addition & 0 deletions imagekit-sdk/src/test/resources/not_an_image.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This file is not an image.