Skip to content

Commit

Permalink
New update v1.5
Browse files Browse the repository at this point in the history
1. Update dependency
2. QR Code is now created in PNG format
  • Loading branch information
megoRU committed Mar 3, 2023
1 parent 31d7cbe commit 0d4c12c
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 11 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ https://jitpack.io/#megoRU/wg-easy-wrapper
<dependency>
<groupId>com.github.megoRU</groupId>
<artifactId>wg-easy-wrapper</artifactId>
<version>v1.1</version>
<version>v1.5</version>
</dependency>
```

Expand Down
13 changes: 10 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>org.megoru</groupId>
<artifactId>wg-easy-wrapper</artifactId>
<version>1.3</version>
<version>1.5</version>

<properties>
<maven.compiler.source>11</maven.compiler.source>
Expand All @@ -16,11 +16,18 @@
</properties>

<dependencies>
<!-- https://mvnrepository.com/artifact/org.apache.xmlgraphics/batik-codec -->
<dependency>
<groupId>org.apache.xmlgraphics</groupId>
<artifactId>batik-codec</artifactId>
<version>1.16</version>
</dependency>

<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.14.1</version>
<version>2.14.2</version>
</dependency>

<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
Expand Down Expand Up @@ -48,7 +55,7 @@
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20220924</version>
<version>20230227</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.jetbrains/annotations -->
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/org/megoru/impl/FileExtension.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package org.megoru.impl;

public enum FileExtension {
CONFIG,
QR_CODE
}
42 changes: 35 additions & 7 deletions src/main/java/org/megoru/impl/WgEasyAPIImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

import com.google.gson.*;
import okhttp3.HttpUrl;
import org.apache.batik.transcoder.Transcoder;
import org.apache.batik.transcoder.TranscoderInput;
import org.apache.batik.transcoder.TranscoderOutput;
import org.apache.batik.transcoder.image.PNGTranscoder;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHeaders;
import org.apache.http.client.CookieStore;
Expand All @@ -25,6 +29,7 @@
import org.megoru.io.ResponseTransformer;
import org.megoru.io.UnsuccessfulHttpException;

import javax.imageio.ImageIO;
import java.io.*;
import java.lang.reflect.Type;
import java.nio.charset.StandardCharsets;
Expand Down Expand Up @@ -104,7 +109,7 @@ public File getQRCode(String userId, String fileName) throws UnsuccessfulHttpExc
HttpGet request = new HttpGet(url.uri());
request.addHeader(HttpHeaders.CONTENT_TYPE, "application/json");

return execute(request, fileName + ".svg");
return execute(request, fileName + ".svg", FileExtension.QR_CODE);
}

@Override
Expand All @@ -121,7 +126,7 @@ public File getConfig(String userId, String fileName) throws UnsuccessfulHttpExc
HttpGet request = new HttpGet(url.uri());
request.addHeader(HttpHeaders.CONTENT_TYPE, "application/json");

return execute(request, fileName + ".conf");
return execute(request, fileName + ".conf", FileExtension.CONFIG);
}

@Override
Expand Down Expand Up @@ -372,19 +377,42 @@ private File writeToFile(String text, String fileName) {
return null;
}

private File execute(HttpRequestBase request, String fileName) throws UnsuccessfulHttpException {
private File svgToPng(File file, String fileName) {
Transcoder t = new PNGTranscoder();
t.addTranscodingHint(PNGTranscoder.KEY_WIDTH, (float) 512);
t.addTranscodingHint(PNGTranscoder.KEY_HEIGHT, (float) 512);
try (FileInputStream inputStream = new FileInputStream(file)) {
TranscoderInput input = new TranscoderInput(inputStream);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
TranscoderOutput output = new TranscoderOutput(outputStream);
t.transcode(input, output);
outputStream.flush();
outputStream.close();
byte[] imgData = outputStream.toByteArray();
int lastIndexOf = fileName.lastIndexOf(".");
File outputfile = new File(fileName.substring(0, lastIndexOf) + ".png");
ImageIO.write(ImageIO.read(new ByteArrayInputStream(imgData)), "png", outputfile);
return outputfile;
} catch (Exception e) {
e.printStackTrace();
}
throw new RuntimeException();
}

private File execute(HttpRequestBase request, String fileName, FileExtension fileExtension) throws UnsuccessfulHttpException {
try {
CloseableHttpResponse response = httpClient.execute(request);

HttpEntity entity = response.getEntity();
String body = EntityUtils.toString(entity);

if (response.getStatusLine().getStatusCode() == 200) {
if (response.getStatusLine().getStatusCode() == 200 && fileExtension.equals(FileExtension.CONFIG)) {
return writeToFile(body, fileName);
} else if (response.getStatusLine().getStatusCode() == 200 && fileExtension.equals(FileExtension.QR_CODE)) {
File file = writeToFile(body, fileName);
return svgToPng(file, fileName);
}

throw new UnsuccessfulHttpException(response.getStatusLine().getStatusCode(), "Client Not Found");

} catch (IOException e) {
e.printStackTrace();
}
Expand All @@ -399,7 +427,7 @@ private <E> E execute(HttpRequestBase request, ResponseTransformer<E> responseTr
HttpEntity entity = response.getEntity();
String body = "{}";
if (entity != null) {
body = EntityUtils.toString(entity);
body = EntityUtils.toString(entity);
}

if (devMode) {
Expand Down

0 comments on commit 0d4c12c

Please sign in to comment.