forked from j62/ctbrec
1
0
Fork 0

Code cleanup

This commit is contained in:
0xboobface 2020-06-12 18:24:44 +02:00
parent 43d2676e11
commit 447ab46e39
1 changed files with 36 additions and 25 deletions

View File

@ -4,6 +4,7 @@ import static ctbrec.Model.State.*;
import static ctbrec.io.HttpConstants.*; import static ctbrec.io.HttpConstants.*;
import java.io.IOException; import java.io.IOException;
import java.util.Locale;
import java.util.Objects; import java.util.Objects;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
@ -328,35 +329,45 @@ public class ThumbCell extends StackPane {
if (!Objects.equals(System.getenv("CTBREC_DEV"), "1")) { if (!Objects.equals(System.getenv("CTBREC_DEV"), "1")) {
boolean updateThumbs = Config.getInstance().getSettings().updateThumbnails; boolean updateThumbs = Config.getInstance().getSettings().updateThumbnails;
if (updateThumbs || iv.getImage() == null) { if (updateThumbs || iv.getImage() == null) {
imageLoadingThreadPool.submit(() -> { imageLoadingThreadPool.submit(createThumbDownload(url));
Request req = new Request.Builder().url(url).header(USER_AGENT, Config.getInstance().getSettings().httpUserAgent).build();
try (Response resp = CamrecApplication.httpClient.execute(req)) {
if (resp.isSuccessful()) {
Image img = new Image(resp.body().byteStream(), 0, 360, true, true);
if (img.progressProperty().get() == 1.0) {
Platform.runLater(() -> {
iv.setImage(img);
setThumbWidth(Config.getInstance().getSettings().thumbWidth);
});
} else {
img.progressProperty().addListener((ChangeListener<Number>) (observable, oldValue, newValue) -> {
if (newValue.doubleValue() == 1.0) {
iv.setImage(img);
setThumbWidth(Config.getInstance().getSettings().thumbWidth);
}
});
}
} else {
throw new HttpException(resp.code(), resp.message());
}
} catch (IOException e) {
LOG.warn("Error loading thumbnail: {}", e.getLocalizedMessage());
}
});
} }
} }
} }
private Runnable createThumbDownload(String url) {
return () -> {
Request req = new Request.Builder()
.url(url)
.header(ACCEPT, "*/*")
.header(ACCEPT_LANGUAGE, Locale.ENGLISH.getLanguage())
.header(USER_AGENT, Config.getInstance().getSettings().httpUserAgent)
.header(REFERER, getModel().getSite().getBaseUrl())
.build();
try (Response resp = CamrecApplication.httpClient.execute(req)) {
if (resp.isSuccessful()) {
Image img = new Image(resp.body().byteStream(), 0, 360, true, true);
if (img.progressProperty().get() == 1.0) {
Platform.runLater(() -> {
iv.setImage(img);
setThumbWidth(Config.getInstance().getSettings().thumbWidth);
});
} else {
img.progressProperty().addListener((ChangeListener<Number>) (observable, oldValue, newValue) -> {
if (newValue.doubleValue() == 1.0) {
iv.setImage(img);
setThumbWidth(Config.getInstance().getSettings().thumbWidth);
}
});
}
} else {
throw new HttpException(resp.code(), resp.message());
}
} catch (IOException e) {
LOG.warn("Error loading thumbnail: {}", e.getLocalizedMessage());
}
};
}
Image getImage() { Image getImage() {
return iv.getImage(); return iv.getImage();
} }