Skip to content

Commit 2c65d08

Browse files
authored
Merge pull request #18 from javadev/add-new-api
Added support for new api convert(types) and transform(options).
2 parents c38ccd3 + 7b4b878 commit 2c65d08

File tree

5 files changed

+96
-1
lines changed

5 files changed

+96
-1
lines changed

CHANGES.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
## 1.8.0
22
* Added new property extension().
3+
* Added new methods convert(new Options().with("type", "image/webp")) and
4+
transform(new Options().with("background", "black")).
5+
6+
## 1.7.0
7+
* Updated dependent libraries.
8+
* Minimum java version is 1.8.
39

410
## 1.6.1
511
* Fixes to depedency paths in OSGi imported packages.

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
The MIT License
22

3-
Copyright (c) 2013-2018 Tinify
3+
Copyright (c) 2013-2022 Tinify
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

src/main/java/com/tinify/Source.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,14 @@ public final Source resize(final Options options) {
3636
return new Source(url, new Options(commands).with("resize", options));
3737
}
3838

39+
public final Source convert(final Options options) {
40+
return new Source(url, new Options(commands).with("convert", options));
41+
}
42+
43+
public final Source transform(final Options options) {
44+
return new Source(url, new Options(commands).with("transform", options));
45+
}
46+
3947
public final ResultMeta store(final Options options) {
4048
Options params = new Options(commands).with("store", options);
4149
Client.Response response = Tinify.client().request(Client.Method.POST, url, params);

src/test/java/com/tinify/Integration.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,4 +129,42 @@ public void shouldPreserveMetadata() throws java.lang.Exception {
129129
assertThat(contents, containsString(new String(new byte[] {0, 0, 0, (byte)0x89})));
130130
assertThat(contents, containsString(("Copyright Voormedia")));
131131
}
132+
133+
@Test
134+
public void shouldConvertFile() throws java.lang.Exception {
135+
Path tempFile = Files.createTempFile("tinify_", null);
136+
tempFile.toFile().deleteOnExit();
137+
138+
Result result = optimized.convert(new Options().with("type", "image/webp")).result();
139+
result.toFile(tempFile.toString());
140+
141+
long size = new File(tempFile.toString()).length();
142+
143+
assertThat(result.width(), is(equalTo(137)));
144+
assertThat(result.height(), is(equalTo(21)));
145+
assertThat(result.mediaType(), is(equalTo("image/webp")));
146+
147+
assertThat(size, greaterThan((long) 1000));
148+
assertThat(size, lessThan((long) 2000));
149+
}
150+
151+
@Test
152+
public void shouldTransformFile() throws java.lang.Exception {
153+
Path tempFile = Files.createTempFile("tinify_", null);
154+
tempFile.toFile().deleteOnExit();
155+
Result result = optimized.transform(new Options().with("background", "black")).result();
156+
result.toFile(tempFile.toString());
157+
158+
long size = new File(tempFile.toString()).length();
159+
String contents = new String(Files.readAllBytes(Paths.get(tempFile.toString())));
160+
161+
assertThat(result.width(), is(equalTo(137)));
162+
assertThat(result.height(), is(equalTo(21)));
163+
164+
assertThat(size, greaterThan((long) 1000));
165+
assertThat(size, lessThan((long) 2000));
166+
167+
/* width == 137 */
168+
assertThat(contents, containsString(new String(new byte[] {0, 0, 0, (byte)0x89})));
169+
}
132170
}

src/test/java/com/tinify/SourceTest.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,25 @@ public void withValidApiKeyPreserveShouldReturnSource() throws Exception, Interr
242242
assertEquals("png file", request1.getBody().readUtf8());
243243
}
244244

245+
@Test
246+
public void withValidApiKeyConvertShouldReturnSource() throws Exception, InterruptedException {
247+
Tinify.setKey("valid");
248+
249+
server.enqueue(new MockResponse()
250+
.setResponseCode(201)
251+
.addHeader("Location", "https://api.tinify.com/some/location"));
252+
253+
server.enqueue(new MockResponse()
254+
.setResponseCode(200)
255+
.setBody("copyrighted file"));
256+
257+
assertThat(Source.fromBuffer("png file".getBytes()).convert(new Options().with("type", "image/webp")),
258+
isA(Source.class));
259+
260+
RecordedRequest request1 = server.takeRequest(3, TimeUnit.SECONDS);
261+
assertEquals("png file", request1.getBody().readUtf8());
262+
}
263+
245264
@Test
246265
public void withValidApiKeyPreserveShouldReturnSourceWithData() throws Exception, IOException, InterruptedException {
247266
Tinify.setKey("valid");
@@ -356,6 +375,30 @@ public void withValidApiKeyResizeShouldReturnSourceWithData() throws Exception,
356375
assertJsonEquals("{\"resize\":{\"width\":100,\"height\":60}}", request2.getBody().readUtf8());
357376
}
358377

378+
@Test
379+
public void withValidApiKeyTransformShouldReturnSourceWithData() throws Exception, IOException, InterruptedException {
380+
Tinify.setKey("valid");
381+
382+
server.enqueue(new MockResponse()
383+
.setResponseCode(201)
384+
.addHeader("Location", "https://api.tinify.com/some/location"));
385+
386+
server.enqueue(new MockResponse()
387+
.setResponseCode(200)
388+
.setBody("small file"));
389+
390+
Options options = new Options().with("background", "black");
391+
392+
assertThat(Source.fromBuffer("png file".getBytes()).transform(options).toBuffer(),
393+
is(equalTo("small file".getBytes())));
394+
395+
RecordedRequest request1 = server.takeRequest(3, TimeUnit.SECONDS);
396+
assertEquals("png file", request1.getBody().readUtf8());
397+
398+
RecordedRequest request2 = server.takeRequest(3, TimeUnit.SECONDS);
399+
assertJsonEquals("{\"transform\":{\"background\":\"black\"}}", request2.getBody().readUtf8());
400+
}
401+
359402
@Test
360403
public void withValidApiKeyStoreShouldReturnResultMeta() throws Exception, InterruptedException {
361404
Tinify.setKey("valid");

0 commit comments

Comments
 (0)