forked from j62/ctbrec
Load images with OkHttp instead of the built-in loader
The built-in loader does not allow control over http headers etc. That is why we use OkHttp now.
This commit is contained in:
parent
0c825237b2
commit
308a40210f
|
@ -5,6 +5,8 @@ import java.io.IOException;
|
||||||
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;
|
||||||
|
import java.util.concurrent.ExecutorService;
|
||||||
|
import java.util.concurrent.Executors;
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
|
@ -14,6 +16,7 @@ import com.iheartradio.m3u8.ParseException;
|
||||||
|
|
||||||
import ctbrec.Config;
|
import ctbrec.Config;
|
||||||
import ctbrec.Model;
|
import ctbrec.Model;
|
||||||
|
import ctbrec.io.HttpException;
|
||||||
import ctbrec.recorder.Recorder;
|
import ctbrec.recorder.Recorder;
|
||||||
import ctbrec.ui.controls.Toast;
|
import ctbrec.ui.controls.Toast;
|
||||||
import javafx.animation.FadeTransition;
|
import javafx.animation.FadeTransition;
|
||||||
|
@ -43,6 +46,8 @@ import javafx.scene.text.Font;
|
||||||
import javafx.scene.text.Text;
|
import javafx.scene.text.Text;
|
||||||
import javafx.scene.text.TextAlignment;
|
import javafx.scene.text.TextAlignment;
|
||||||
import javafx.util.Duration;
|
import javafx.util.Duration;
|
||||||
|
import okhttp3.Request;
|
||||||
|
import okhttp3.Response;
|
||||||
|
|
||||||
public class ThumbCell extends StackPane {
|
public class ThumbCell extends StackPane {
|
||||||
|
|
||||||
|
@ -74,6 +79,7 @@ public class ThumbCell extends StackPane {
|
||||||
private ObservableList<Node> thumbCellList;
|
private ObservableList<Node> thumbCellList;
|
||||||
private boolean mouseHovering = false;
|
private boolean mouseHovering = false;
|
||||||
private boolean recording = false;
|
private boolean recording = false;
|
||||||
|
private static ExecutorService imageLoadingThreadPool = Executors.newFixedThreadPool(10);
|
||||||
|
|
||||||
public ThumbCell(ThumbOverviewTab parent, Model model, Recorder recorder) {
|
public ThumbCell(ThumbOverviewTab parent, Model model, Recorder recorder) {
|
||||||
this.thumbCellList = parent.grid.getChildren();
|
this.thumbCellList = parent.grid.getChildren();
|
||||||
|
@ -267,18 +273,35 @@ 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) {
|
||||||
Image img = new Image(url, true);
|
imageLoadingThreadPool.submit(() -> {
|
||||||
|
Request req = new Request.Builder()
|
||||||
// wait for the image to load, otherwise the ImageView replaces the current image with an "empty" image,
|
.url(url)
|
||||||
// which causes to show the grey background until the image is loaded
|
.addHeader("User-Agent", Config.getInstance().getSettings().httpUserAgent)
|
||||||
img.progressProperty().addListener(new ChangeListener<Number>() {
|
.build();
|
||||||
@Override
|
try(Response resp = CamrecApplication.httpClient.execute(req)) {
|
||||||
public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
|
if(resp.isSuccessful()) {
|
||||||
if(newValue.doubleValue() == 1.0) {
|
Image img = new Image(resp.body().byteStream());
|
||||||
//imgAspectRatio = img.getHeight() / img.getWidth();
|
if(img.progressProperty().get() == 1.0) {
|
||||||
iv.setImage(img);
|
Platform.runLater(() -> {
|
||||||
setThumbWidth(Config.getInstance().getSettings().thumbWidth);
|
iv.setImage(img);
|
||||||
|
setThumbWidth(Config.getInstance().getSettings().thumbWidth);
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
img.progressProperty().addListener(new ChangeListener<Number>() {
|
||||||
|
@Override
|
||||||
|
public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number 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.error("Error loading image", e);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue