Skip to content

Commit 00b3cf9

Browse files
committed
Improve script detection
1 parent 14db14b commit 00b3cf9

1 file changed

Lines changed: 30 additions & 3 deletions

File tree

src/main/java/edu/harvard/mcz/imagecapture/CandidateImageFile.java

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import java.nio.file.Files;
3939
import java.nio.file.Path;
4040
import java.nio.file.Paths;
41+
import java.nio.file.StandardCopyOption;
4142
import java.text.SimpleDateFormat;
4243
import java.util.ArrayList;
4344
import java.util.Arrays;
@@ -2436,7 +2437,30 @@ private String findChromiumScanPage() {
24362437
return path.toString();
24372438
}
24382439
}
2439-
return null;
2440+
return copyClasspathResourceToTempFile(
2441+
"qrscan_chromium.html",
2442+
"qrscan_chromium_",
2443+
".html");
2444+
}
2445+
2446+
private String copyClasspathResourceToTempFile(String resourceName,
2447+
String tempPrefix,
2448+
String tempSuffix) {
2449+
try (InputStream stream = CandidateImageFile.class
2450+
.getClassLoader()
2451+
.getResourceAsStream(resourceName)) {
2452+
if (stream == null) {
2453+
return null;
2454+
}
2455+
Path tempFile = Files.createTempFile(tempPrefix, tempSuffix);
2456+
Files.copy(stream, tempFile, StandardCopyOption.REPLACE_EXISTING);
2457+
tempFile.toFile().deleteOnExit();
2458+
return tempFile.toString();
2459+
} catch (IOException e) {
2460+
log.debug("Unable to stage classpath resource {}: {}", resourceName,
2461+
e.getMessage());
2462+
return null;
2463+
}
24402464
}
24412465

24422466
private String getOsKey() {
@@ -2624,8 +2648,11 @@ private String findPythonScript() {
26242648
return path;
26252649
}
26262650
}
2627-
2628-
return null;
2651+
2652+
return copyClasspathResourceToTempFile(
2653+
"qrscan_using_python.py",
2654+
"qrscan_using_python_",
2655+
".py");
26292656
}
26302657

26312658
/**

0 commit comments

Comments
 (0)