forked from yuzutech/kroki
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Install msc-generator [1] into the server docker image and provide a corresponding under "/mscgen". [1] https://gitlab.com/msc-generator/msc-generator
- Loading branch information
Showing
3 changed files
with
88 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
server/src/main/java/io/kroki/server/service/MscGenerator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package io.kroki.server.service; | ||
|
||
import io.kroki.server.action.Commander; | ||
import io.kroki.server.decode.DiagramSource; | ||
import io.kroki.server.decode.SourceDecoder; | ||
import io.kroki.server.error.DecodeException; | ||
import io.kroki.server.format.FileFormat; | ||
import io.vertx.core.AsyncResult; | ||
import io.vertx.core.Handler; | ||
import io.vertx.core.Vertx; | ||
import io.vertx.core.buffer.Buffer; | ||
import io.vertx.core.json.JsonObject; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
public class MscGenerator implements DiagramService { | ||
|
||
private static final List<FileFormat> SUPPORTED_FORMATS = Arrays.asList(FileFormat.PNG, FileFormat.SVG, FileFormat.PDF); | ||
|
||
private final Vertx vertx; | ||
private final String binPath; | ||
private final SourceDecoder sourceDecoder; | ||
private final Commander commander; | ||
|
||
public MscGenerator(Vertx vertx, JsonObject config, Commander commander) { | ||
this.vertx = vertx; | ||
this.binPath = config.getString("KROKI_MSCGEN_BIN_PATH", "msc-gen"); | ||
this.sourceDecoder = new SourceDecoder() { | ||
@Override | ||
public String decode(String encoded) throws DecodeException { | ||
return DiagramSource.decode(encoded); | ||
} | ||
}; | ||
this.commander = commander; | ||
} | ||
|
||
@Override | ||
public List<FileFormat> getSupportedFormats() { | ||
return SUPPORTED_FORMATS; | ||
} | ||
|
||
@Override | ||
public SourceDecoder getSourceDecoder() { | ||
return sourceDecoder; | ||
} | ||
|
||
@Override | ||
public String getVersion() { | ||
try { | ||
Process process = new ProcessBuilder(binPath, "--version").start(); | ||
String line = new BufferedReader(new InputStreamReader(process.getInputStream())).readLine(); | ||
return line.split("\\s+")[1].substring(1); | ||
} catch (IOException e) { | ||
return "unknown"; | ||
} | ||
} | ||
|
||
@Override | ||
public void convert(String sourceDecoded, String serviceName, FileFormat fileFormat, JsonObject options, Handler<AsyncResult<Buffer>> handler) { | ||
vertx.executeBlocking(future -> { | ||
try { | ||
byte[] result = mscgen(sourceDecoded.getBytes(), fileFormat.getName(), options); | ||
future.complete(result); | ||
} catch (IOException | InterruptedException | IllegalStateException e) { | ||
future.fail(e); | ||
} | ||
}, res -> handler.handle(res.map(o -> Buffer.buffer((byte[]) o)))); | ||
} | ||
|
||
private byte[] mscgen(byte[] source, String format, JsonObject options) throws IOException, InterruptedException, IllegalStateException { | ||
return commander.execute( | ||
source, | ||
binPath, | ||
"-S", options.getString("lang", "signalling"), // Supported languages: signalling | msc, graph, block | ||
"-T", format, | ||
"-s=" + options.getFloat("scale", 1.f), | ||
"-" | ||
); | ||
} | ||
} |