Skip to content

Commit 750fa8d

Browse files
committed
Add GUIRunner
1 parent 1ca1f91 commit 750fa8d

File tree

9 files changed

+360
-191
lines changed

9 files changed

+360
-191
lines changed

src/main/java/net/cofcool/sourcebox/App.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,11 @@ public static void main(String[] args) throws Exception {
102102

103103
var notRun = new AtomicBoolean(true);
104104
try {
105-
var runner = RUNNER_MAP.get(RunnerType.valueOf(pArgs.readArg("mode").val()));
105+
var mode = pArgs.readArg("mode").val();
106+
var runner = RUNNER_MAP.get(RunnerType.valueOf(mode));
107+
if (runner == null) {
108+
throw new IllegalArgumentException("Unknown mode: " + mode);
109+
}
106110
notRun.set(!runner.run(pArgs));
107111
} catch (Exception e) {
108112
notRun.set(false);

src/main/java/net/cofcool/sourcebox/ToolContext.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ default Object toObject() {
1818
return null;
1919
}
2020

21+
default Tool owner() {
22+
return null;
23+
}
24+
2125
static String randomName() {
2226
return RandomStringUtils.randomAlphabetic(6);
2327
}

src/main/java/net/cofcool/sourcebox/internal/HtmlDownloader.java

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import javax.xml.transform.stream.StreamResult;
3131
import lombok.CustomLog;
3232
import net.cofcool.sourcebox.Tool;
33+
import net.cofcool.sourcebox.ToolContext;
3334
import net.cofcool.sourcebox.ToolName;
3435
import org.apache.commons.io.FileUtils;
3536
import org.apache.commons.io.FilenameUtils;
@@ -57,6 +58,7 @@ public class HtmlDownloader implements Tool {
5758
private Set<OutputType> outputTypes = EnumSet.of(OutputType.html);
5859
private Proxy proxy;
5960
private String filter;
61+
private ToolContext context;
6062

6163
private Connection connection;
6264

@@ -68,6 +70,7 @@ public ToolName name() {
6870
@Override
6971
public void run(Args args) throws Exception {
7072
var urls = new ArrayList<String>();
73+
context = args.getContext();
7174

7275
args.readArg("urlFile").ifPresent(a -> {
7376
try {
@@ -171,7 +174,7 @@ private void downloadUrl(String folder, String url, int depth, String expression
171174
var dir = Paths.get(folder, title).toFile();
172175
folder = dir.toString();
173176
FileUtils.forceMkdir(dir);
174-
log.info("Create dir {0}", dir);
177+
context.write(String.format("Create dir %s", dir));
175178
}
176179

177180
if (clean) {
@@ -185,7 +188,7 @@ private void downloadUrl(String folder, String url, int depth, String expression
185188
if (filter == null || title.contains(filter)) {
186189
for (OutputType type : outputTypes) {
187190
type.applyOutput(doc, folder, title);
188-
log.info("Save {0} file to {1} from <<{2}>>: {3}", type, folder, title, url);
191+
context.write(String.format("Save %s file to %s from <<%s>>: %s", type, folder, title, url));
189192
}
190193
}
191194

@@ -204,7 +207,7 @@ private void downloadUrl(String folder, String url, int depth, String expression
204207
try {
205208
downloadUrl(folder, href, depth, "false");
206209
} catch (Exception e) {
207-
log.error("Download " + href, e);
210+
context.write(String.format("Download %s error: %s", href, e.getMessage()));
208211
}
209212
}
210213
}
@@ -233,7 +236,7 @@ private void downloadImages(Elements imgs, String folder, String expression) thr
233236
var splitIdx = src.indexOf(",");
234237
imgData = Base64.getDecoder().decode(src.substring(splitIdx + 1));
235238
} else {
236-
log.info("Image url {0} is invalid", src);
239+
context.write(String.format("Image url %s is invalid", src));
237240
continue;
238241
}
239242

@@ -242,9 +245,9 @@ private void downloadImages(Elements imgs, String folder, String expression) thr
242245
File file = Paths.get(folder, name).toFile();
243246
FileUtils.writeByteArrayToFile(file, imgData);
244247
img.attr("src", String.join("/", ".", IMGS_FOLDER, name));
245-
log.info("Download image from {0}", file);
248+
context.write(String.format("Download image from %s", file));
246249
} catch (Exception e) {
247-
log.error("Download " + src + " image error", e);
250+
context.write(String.format("Download %s image error: %s", src, e.getMessage()));
248251
}
249252
}
250253
}
@@ -372,17 +375,17 @@ private static class ToEpub implements Output {
372375
private static final String START_HTML = """
373376
<?xml version='1.0' encoding='utf-8'?>
374377
<html xmlns="http://www.w3.org/1999/xhtml" lang="zh">
375-
378+
376379
<head>
377380
<title id="toc_1">%s</title>
378381
</head>
379-
382+
380383
<body>
381-
384+
382385
<h1>%s</h1>
383-
386+
384387
</body>
385-
388+
386389
</html>
387390
""";
388391

@@ -473,6 +476,7 @@ public void finished() {
473476
navMap.appendChild(navPoint);
474477
}
475478

479+
476480
var transformer = TransformerFactory.newInstance().newTransformer();
477481

478482
var outputStream = new ByteArrayOutputStream();
@@ -487,7 +491,7 @@ public void finished() {
487491

488492
IOUtils.closeQuietly(zos);
489493
} catch (Exception e) {
490-
log.error("Save epub file error", e);
494+
throw new RuntimeException("Save epub file error", e);
491495
}
492496
zos = null;
493497
files = null;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package net.cofcool.sourcebox.runner;
2+
3+
public record ActionEvent(Object source, String tool, String action) {
4+
5+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package net.cofcool.sourcebox.runner;
2+
3+
import io.vertx.core.Vertx;
4+
import io.vertx.core.VertxOptions;
5+
import io.vertx.core.json.JsonObject;
6+
import java.util.concurrent.TimeUnit;
7+
import lombok.CustomLog;
8+
import lombok.RequiredArgsConstructor;
9+
import net.cofcool.sourcebox.Tool;
10+
import net.cofcool.sourcebox.Tool.Args;
11+
import net.cofcool.sourcebox.Tool.RunnerType;
12+
import net.cofcool.sourcebox.ToolContext;
13+
import net.cofcool.sourcebox.ToolRunner;
14+
import net.cofcool.sourcebox.runner.CLIRunner.ConsoleToolContext;
15+
import net.cofcool.sourcebox.util.VertxUtils;
16+
17+
@CustomLog
18+
public class GUIRunner implements ToolRunner {
19+
20+
@Override
21+
public boolean run(Args args) throws Exception {
22+
Vertx v = Vertx.vertx(
23+
new VertxOptions()
24+
.setWarningExceptionTimeUnit(TimeUnit.SECONDS)
25+
.setWarningExceptionTime(120)
26+
);
27+
new WebVerticle(RunnerType.GUI, GUIContext::new).deploy(v, null, args)
28+
.onComplete(VertxUtils.logResult(log, e -> v.close()));
29+
return true;
30+
}
31+
32+
@RequiredArgsConstructor
33+
private static class GUIContext extends ConsoleToolContext {
34+
private final Tool tool;
35+
36+
@Override
37+
public RunnerType runnerType() {
38+
return RunnerType.GUI;
39+
}
40+
41+
@Override
42+
public ToolContext write(Object val) {
43+
super.write(val);
44+
WebVerticle.EVENT_QUEUE.offer(new ActionEvent(val, tool.name().name(), "write"));
45+
return this;
46+
}
47+
48+
@Override
49+
public ToolContext write(String name, String in) {
50+
super.write(name, in);
51+
WebVerticle.EVENT_QUEUE.offer(new ActionEvent(name, tool.name().name(), "writeFile"));
52+
return this;
53+
}
54+
55+
@Override
56+
public Object toObject() {
57+
return JsonObject.of("status", true);
58+
}
59+
60+
@Override
61+
public Tool owner() {
62+
return tool;
63+
}
64+
}
65+
}

0 commit comments

Comments
 (0)