mirror of
https://github.com/MineLittlePony/MineLittlePony.git
synced 2024-11-29 23:48:00 +01:00
Add a status check for 404 in the skin download.
Should not load from the cache if the skin was deleted.
This commit is contained in:
parent
e016f420a7
commit
a194aed282
1 changed files with 40 additions and 37 deletions
|
@ -11,6 +11,7 @@ import org.apache.commons.io.FileUtils;
|
||||||
import org.apache.http.Header;
|
import org.apache.http.Header;
|
||||||
import org.apache.http.HttpHeaders;
|
import org.apache.http.HttpHeaders;
|
||||||
import org.apache.http.HttpResponse;
|
import org.apache.http.HttpResponse;
|
||||||
|
import org.apache.http.HttpStatus;
|
||||||
import org.apache.http.client.HttpClient;
|
import org.apache.http.client.HttpClient;
|
||||||
import org.apache.http.client.methods.HttpGet;
|
import org.apache.http.client.methods.HttpGet;
|
||||||
import org.apache.http.impl.client.HttpClientBuilder;
|
import org.apache.http.impl.client.HttpClientBuilder;
|
||||||
|
@ -27,14 +28,17 @@ import java.io.IOException;
|
||||||
import java.util.concurrent.atomic.AtomicInteger;
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
|
|
||||||
public class ThreadDownloadImageETag extends SimpleTexture {
|
public class ThreadDownloadImageETag extends SimpleTexture {
|
||||||
|
|
||||||
private static final Logger LOGGER = LogManager.getLogger();
|
private static final Logger LOGGER = LogManager.getLogger();
|
||||||
private static final AtomicInteger THREAD_ID = new AtomicInteger(0);
|
private static final AtomicInteger THREAD_ID = new AtomicInteger(0);
|
||||||
|
|
||||||
@Nonnull
|
@Nonnull
|
||||||
private final File cacheFile;
|
private final File cacheFile;
|
||||||
private final File eTagFile;
|
private final File eTagFile;
|
||||||
private final String imageUrl;
|
private final String imageUrl;
|
||||||
@Nullable
|
@Nullable
|
||||||
private final IImageBuffer imageBuffer;
|
private final IImageBuffer imageBuffer;
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
private BufferedImage bufferedImage;
|
private BufferedImage bufferedImage;
|
||||||
@Nullable
|
@Nullable
|
||||||
|
@ -67,7 +71,7 @@ public class ThreadDownloadImageETag extends SimpleTexture {
|
||||||
return super.getGlTextureId();
|
return super.getGlTextureId();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setBufferedImage(BufferedImage bufferedImageIn) {
|
private void setBufferedImage(@Nonnull BufferedImage bufferedImageIn) {
|
||||||
this.bufferedImage = bufferedImageIn;
|
this.bufferedImage = bufferedImageIn;
|
||||||
|
|
||||||
if (this.imageBuffer != null) {
|
if (this.imageBuffer != null) {
|
||||||
|
@ -86,18 +90,20 @@ public class ThreadDownloadImageETag extends SimpleTexture {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.imageThread == null) {
|
if (this.imageThread == null) {
|
||||||
loadTexture();
|
this.imageThread = new Thread(this::loadTexture, "Texture Downloader #" + THREAD_ID.incrementAndGet());
|
||||||
|
this.imageThread.setDaemon(true);
|
||||||
|
this.imageThread.start();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void loadTexture() {
|
private void loadTexture() {
|
||||||
this.imageThread = new Thread("Texture Downloader #" + THREAD_ID.incrementAndGet()) {
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
HttpResponse response = null;
|
HttpResponse response = null;
|
||||||
try {
|
try {
|
||||||
HttpClient client = HttpClientBuilder.create().build();
|
HttpClient client = HttpClientBuilder.create().build();
|
||||||
response = client.execute(new HttpGet(ThreadDownloadImageETag.this.imageUrl));
|
response = client.execute(new HttpGet(imageUrl));
|
||||||
|
int status = response.getStatusLine().getStatusCode();
|
||||||
|
if (status == HttpStatus.SC_NOT_FOUND)
|
||||||
|
return;
|
||||||
if (checkEtag(response)) {
|
if (checkEtag(response)) {
|
||||||
LOGGER.debug("Loading http texture from local cache ({})", cacheFile);
|
LOGGER.debug("Loading http texture from local cache ({})", cacheFile);
|
||||||
|
|
||||||
|
@ -114,6 +120,7 @@ public class ThreadDownloadImageETag extends SimpleTexture {
|
||||||
} else {
|
} else {
|
||||||
loadTextureFromServer(response);
|
loadTextureFromServer(response);
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
LOGGER.error("Couldn't load skin {} ", imageUrl, e);
|
LOGGER.error("Couldn't load skin {} ", imageUrl, e);
|
||||||
} finally {
|
} finally {
|
||||||
|
@ -121,19 +128,15 @@ public class ThreadDownloadImageETag extends SimpleTexture {
|
||||||
EntityUtils.consumeQuietly(response.getEntity());
|
EntityUtils.consumeQuietly(response.getEntity());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
|
||||||
this.imageThread.setDaemon(true);
|
|
||||||
this.imageThread.start();
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
private boolean checkEtag(HttpResponse response) {
|
private boolean checkEtag(HttpResponse response) {
|
||||||
try {
|
try {
|
||||||
if (cacheFile.isFile()) {
|
if (cacheFile.isFile()) {
|
||||||
String localEtag = Files.readFirstLine(eTagFile, Charsets.UTF_8);
|
String localETag = Files.readFirstLine(eTagFile, Charsets.UTF_8);
|
||||||
Header remoteEtag = response.getFirstHeader(HttpHeaders.ETAG);
|
Header remoteETag = response.getFirstHeader(HttpHeaders.ETAG);
|
||||||
// true if no remote etag or does match
|
// true if no remote etag or does match
|
||||||
return remoteEtag == null || localEtag.equals(remoteEtag.getValue());
|
return remoteETag == null || localETag.equals(remoteETag.getValue());
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
|
@ -142,7 +145,7 @@ public class ThreadDownloadImageETag extends SimpleTexture {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void loadTextureFromServer(HttpResponse response) {
|
private void loadTextureFromServer(HttpResponse response) {
|
||||||
LOGGER.debug("Downloading http texture from {} to {}", imageUrl, cacheFile);
|
LOGGER.debug("Downloading http texture from {} to {}", imageUrl, cacheFile);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
@ -153,9 +156,9 @@ public class ThreadDownloadImageETag extends SimpleTexture {
|
||||||
FileUtils.copyInputStreamToFile(response.getEntity().getContent(), cacheFile);
|
FileUtils.copyInputStreamToFile(response.getEntity().getContent(), cacheFile);
|
||||||
bufferedimage = ImageIO.read(cacheFile);
|
bufferedimage = ImageIO.read(cacheFile);
|
||||||
|
|
||||||
Header etag = response.getFirstHeader(HttpHeaders.ETAG);
|
Header eTag = response.getFirstHeader(HttpHeaders.ETAG);
|
||||||
if (etag != null) {
|
if (eTag != null) {
|
||||||
FileUtils.write(eTagFile, etag.getValue(), Charsets.UTF_8);
|
FileUtils.write(eTagFile, eTag.getValue(), Charsets.UTF_8);
|
||||||
}
|
}
|
||||||
if (imageBuffer != null) {
|
if (imageBuffer != null) {
|
||||||
bufferedimage = imageBuffer.parseUserSkin(bufferedimage);
|
bufferedimage = imageBuffer.parseUserSkin(bufferedimage);
|
||||||
|
|
Loading…
Reference in a new issue