mirror of
https://github.com/MineLittlePony/MineLittlePony.git
synced 2025-04-01 01:05:27 +02:00
Delete the cache file when a 404 error occurs.
This will prevent the file from being loaded again if the connection fails in the future.
This commit is contained in:
parent
a194aed282
commit
4fbf5b8a22
1 changed files with 35 additions and 12 deletions
|
@ -102,26 +102,35 @@ public class ThreadDownloadImageETag extends SimpleTexture {
|
||||||
HttpClient client = HttpClientBuilder.create().build();
|
HttpClient client = HttpClientBuilder.create().build();
|
||||||
response = client.execute(new HttpGet(imageUrl));
|
response = client.execute(new HttpGet(imageUrl));
|
||||||
int status = response.getStatusLine().getStatusCode();
|
int status = response.getStatusLine().getStatusCode();
|
||||||
if (status == HttpStatus.SC_NOT_FOUND)
|
if (status == HttpStatus.SC_NOT_FOUND) {
|
||||||
return;
|
// delete the cache files in case we can't connect in the future
|
||||||
if (checkEtag(response)) {
|
clearCache();
|
||||||
|
} else if (checkETag(response)) {
|
||||||
LOGGER.debug("Loading http texture from local cache ({})", cacheFile);
|
LOGGER.debug("Loading http texture from local cache ({})", cacheFile);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
bufferedImage = ImageIO.read(cacheFile);
|
// e-tag check passed. Load the local file
|
||||||
|
setLocalCache();
|
||||||
if (imageBuffer != null) {
|
|
||||||
setBufferedImage(imageBuffer.parseUserSkin(bufferedImage));
|
|
||||||
}
|
|
||||||
} catch (IOException ioexception) {
|
} catch (IOException ioexception) {
|
||||||
|
// Nope. Local cache is corrupt. Re-download it.
|
||||||
LOGGER.error("Couldn't load skin {}", cacheFile, ioexception);
|
LOGGER.error("Couldn't load skin {}", cacheFile, ioexception);
|
||||||
loadTextureFromServer(response);
|
loadTextureFromServer(response);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
// there's an updated file. Download it again.
|
||||||
loadTextureFromServer(response);
|
loadTextureFromServer(response);
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
|
// connection failed
|
||||||
|
if (cacheFile.isFile()) {
|
||||||
|
try {
|
||||||
|
// try to load from cache anyway
|
||||||
|
setLocalCache();
|
||||||
|
return;
|
||||||
|
} catch (IOException ignored) {
|
||||||
|
}
|
||||||
|
}
|
||||||
LOGGER.error("Couldn't load skin {} ", imageUrl, e);
|
LOGGER.error("Couldn't load skin {} ", imageUrl, e);
|
||||||
} finally {
|
} finally {
|
||||||
if (response != null)
|
if (response != null)
|
||||||
|
@ -129,8 +138,22 @@ public class ThreadDownloadImageETag extends SimpleTexture {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void setLocalCache() throws IOException {
|
||||||
|
if (cacheFile.isFile()) {
|
||||||
|
BufferedImage image = ImageIO.read(cacheFile);
|
||||||
|
if (imageBuffer != null) {
|
||||||
|
image = imageBuffer.parseUserSkin(image);
|
||||||
|
}
|
||||||
|
setBufferedImage(image);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private boolean checkEtag(HttpResponse response) {
|
private void clearCache() {
|
||||||
|
FileUtils.deleteQuietly(this.cacheFile);
|
||||||
|
FileUtils.deleteQuietly(this.eTagFile);
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
@ -147,23 +170,23 @@ public class ThreadDownloadImageETag extends SimpleTexture {
|
||||||
|
|
||||||
private 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 {
|
||||||
|
|
||||||
if (response.getStatusLine().getStatusCode() / 100 == 2) {
|
if (response.getStatusLine().getStatusCode() / 100 == 2) {
|
||||||
BufferedImage bufferedimage;
|
BufferedImage bufferedimage;
|
||||||
|
|
||||||
|
// write the image to disk
|
||||||
FileUtils.copyInputStreamToFile(response.getEntity().getContent(), cacheFile);
|
FileUtils.copyInputStreamToFile(response.getEntity().getContent(), cacheFile);
|
||||||
bufferedimage = ImageIO.read(cacheFile);
|
bufferedimage = ImageIO.read(cacheFile);
|
||||||
|
|
||||||
|
// maybe write the etag to disk
|
||||||
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
setBufferedImage(bufferedimage);
|
setBufferedImage(bufferedimage);
|
||||||
}
|
}
|
||||||
} catch (Exception exception) {
|
} catch (Exception exception) {
|
||||||
|
|
Loading…
Add table
Reference in a new issue