log error when background can't be loaded

This commit is contained in:
Haowei Wen 2021-06-23 04:57:01 +08:00
parent ab1a2f6fcb
commit c329f1cafb
No known key found for this signature in database
GPG Key ID: 5BC167F73EA558E4

View File

@ -61,10 +61,12 @@ import java.util.Random;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.stream.Stream; import java.util.stream.Stream;
import static java.util.logging.Level.WARNING;
import static java.util.stream.Collectors.toList; import static java.util.stream.Collectors.toList;
import static org.jackhuang.hmcl.setting.ConfigHolder.config; import static org.jackhuang.hmcl.setting.ConfigHolder.config;
import static org.jackhuang.hmcl.ui.FXUtils.newImage; import static org.jackhuang.hmcl.ui.FXUtils.newImage;
import static org.jackhuang.hmcl.util.Logging.LOG; import static org.jackhuang.hmcl.util.Logging.LOG;
import static org.jackhuang.hmcl.util.io.FileUtils.getExtension;
public class DecoratorController { public class DecoratorController {
private static final String PROPERTY_DIALOG_CLOSE_HANDLER = DecoratorController.class.getName() + ".dialog.closeListener"; private static final String PROPERTY_DIALOG_CLOSE_HANDLER = DecoratorController.class.getName() + ".dialog.closeListener";
@ -174,10 +176,10 @@ public class DecoratorController {
List<Path> candidates; List<Path> candidates;
try (Stream<Path> stream = Files.list(imageDir)) { try (Stream<Path> stream = Files.list(imageDir)) {
candidates = stream candidates = stream
.filter(Files::isRegularFile) .filter(Files::isReadable)
.filter(it -> { .filter(it -> {
String filename = it.getFileName().toString(); String ext = getExtension(it).toLowerCase();
return filename.endsWith(".png") || filename.endsWith(".jpg"); return ext.equals("png") || ext.equals("jpg");
}) })
.collect(toList()); .collect(toList());
} catch (IOException e) { } catch (IOException e) {
@ -199,13 +201,23 @@ public class DecoratorController {
} }
private Optional<Image> tryLoadImage(Path path) { private Optional<Image> tryLoadImage(Path path) {
if (Files.isRegularFile(path)) { if (!Files.isReadable(path))
try { return Optional.empty();
return Optional.of(new Image(path.toAbsolutePath().toUri().toString()));
} catch (IllegalArgumentException ignored) { Image img;
} try {
img = new Image(path.toAbsolutePath().toUri().toString());
} catch (IllegalArgumentException e) {
LOG.log(WARNING, "Couldn't load background image", e);
return Optional.empty();
} }
return Optional.empty();
if (img.getException() != null) {
LOG.log(WARNING, "Couldn't load background image", img.getException());
return Optional.empty();
}
return Optional.of(img);
} }
// ==== Navigation ==== // ==== Navigation ====