mirror of
https://github.com/HMCL-dev/HMCL.git
synced 2025-09-10 12:26:16 -04:00
Fixed failing to download file when cached entry was removed by user
This commit is contained in:
parent
eaa6790013
commit
16ea88a659
@ -222,8 +222,14 @@ public class FileDownloadTask extends Task {
|
|||||||
|
|
||||||
if (con.getResponseCode() == HttpURLConnection.HTTP_NOT_MODIFIED) {
|
if (con.getResponseCode() == HttpURLConnection.HTTP_NOT_MODIFIED) {
|
||||||
// Handle cache
|
// Handle cache
|
||||||
|
try {
|
||||||
Path cache = repository.getCachedRemoteFile(con);
|
Path cache = repository.getCachedRemoteFile(con);
|
||||||
FileUtils.copyFile(cache.toFile(), file);
|
FileUtils.copyFile(cache.toFile(), file);
|
||||||
|
return;
|
||||||
|
} catch (IOException e) {
|
||||||
|
Logging.LOG.log(Level.WARNING, "Unable to use cached file, redownload it", e);
|
||||||
|
repository.removeRemoteEntry(con);
|
||||||
|
}
|
||||||
} else if (con.getResponseCode() / 100 != 2) {
|
} else if (con.getResponseCode() / 100 != 2) {
|
||||||
throw new IOException("Server error, response code: " + con.getResponseCode());
|
throw new IOException("Server error, response code: " + con.getResponseCode());
|
||||||
}
|
}
|
||||||
|
@ -96,11 +96,17 @@ public final class GetTask extends TaskResult<String> {
|
|||||||
if (checkETag) repository.injectConnection(conn);
|
if (checkETag) repository.injectConnection(conn);
|
||||||
conn.connect();
|
conn.connect();
|
||||||
|
|
||||||
if (conn.getResponseCode() == 304) {
|
if (conn.getResponseCode() == HttpURLConnection.HTTP_NOT_MODIFIED) {
|
||||||
// Handle cache
|
// Handle cache
|
||||||
|
try {
|
||||||
Path cache = repository.getCachedRemoteFile(conn);
|
Path cache = repository.getCachedRemoteFile(conn);
|
||||||
setResult(FileUtils.readText(cache));
|
setResult(FileUtils.readText(cache));
|
||||||
return;
|
return;
|
||||||
|
} catch (IOException e) {
|
||||||
|
Logging.LOG.log(Level.WARNING, "Unable to use cached file, redownload it", e);
|
||||||
|
repository.removeRemoteEntry(conn);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
} else if (conn.getResponseCode() / 100 != 2) {
|
} else if (conn.getResponseCode() / 100 != 2) {
|
||||||
throw new IOException("Server error, response code: " + conn.getResponseCode());
|
throw new IOException("Server error, response code: " + conn.getResponseCode());
|
||||||
}
|
}
|
||||||
|
@ -164,6 +164,16 @@ public class CacheRepository {
|
|||||||
return file;
|
return file;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void removeRemoteEntry(URLConnection conn) {
|
||||||
|
String url = conn.getURL().toString();
|
||||||
|
lock.readLock().lock();
|
||||||
|
try {
|
||||||
|
index.remove(url);
|
||||||
|
} finally {
|
||||||
|
lock.readLock().unlock();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public void injectConnection(URLConnection conn) {
|
public void injectConnection(URLConnection conn) {
|
||||||
String url = conn.getURL().toString();
|
String url = conn.getURL().toString();
|
||||||
lock.readLock().lock();
|
lock.readLock().lock();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user