mirror of
https://github.com/arduino/Arduino.git
synced 2025-01-31 20:52:13 +01:00
fix the misleading exception throw on windows
- The file will be close before delete it - Some refactoring of the downloader cache
This commit is contained in:
parent
934df81b23
commit
207128db91
@ -148,12 +148,9 @@ public class FileDownloader extends Observable {
|
||||
|
||||
final File settingsFolder = BaseNoGui.getPlatform().getSettingsFolder();
|
||||
final String cacheFolder = Paths.get(settingsFolder.getPath(), "cache").toString();
|
||||
final FileDownloaderCache fileDownloaderCache =
|
||||
new FileDownloaderCache(cacheFolder, downloadUrl);
|
||||
final FileDownloaderCache fileDownloaderCache = FileDownloaderCache.getFileCached(cacheFolder, downloadUrl);
|
||||
|
||||
final boolean isChanged = fileDownloaderCache.checkIfTheFileIsChanged();
|
||||
|
||||
if (!isChanged) {
|
||||
if (!fileDownloaderCache.isChange()) {
|
||||
try {
|
||||
final Optional<File> fileFromCache =
|
||||
fileDownloaderCache.getFileFromCache();
|
||||
@ -165,20 +162,17 @@ public class FileDownloader extends Observable {
|
||||
} catch (Exception e) {
|
||||
log.warn(
|
||||
"Cannot get the file from the cache, will be downloaded a new one ", e.getCause());
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
// Open file and seek to the end of it
|
||||
randomAccessOutputFile = new RandomAccessFile(outputFile, "rw");
|
||||
initialSize = randomAccessOutputFile.length();
|
||||
|
||||
initialSize = outputFile.length();
|
||||
if (noResume && initialSize > 0) {
|
||||
// delete file and restart downloading
|
||||
Files.delete(outputFile.toPath());
|
||||
Files.deleteIfExists(outputFile.toPath());
|
||||
initialSize = 0;
|
||||
}
|
||||
|
||||
// Open file and seek to the end of it
|
||||
randomAccessOutputFile = new RandomAccessFile(outputFile, "rw");
|
||||
randomAccessOutputFile.seek(initialSize);
|
||||
|
||||
final HttpURLConnection connection = new HttpConnectionManager(downloadUrl)
|
||||
@ -186,7 +180,8 @@ public class FileDownloader extends Observable {
|
||||
final int resp = connection.getResponseCode();
|
||||
|
||||
if (resp < 200 || resp >= 300) {
|
||||
Files.delete(outputFile.toPath());
|
||||
IOUtils.closeQuietly(randomAccessOutputFile);
|
||||
Files.deleteIfExists(outputFile.toPath());
|
||||
throw new IOException("Received invalid http status code from server: " + resp);
|
||||
}
|
||||
|
||||
|
@ -19,25 +19,28 @@ import java.util.Optional;
|
||||
|
||||
public class FileDownloaderCache {
|
||||
private static Logger log = LoggerFactory.getLogger(FileDownloaderCache.class);
|
||||
private final URL remoteURL;
|
||||
private final Path cacheFilePath;
|
||||
// Will be initialized by call the checkIfTheFileIsChanged function
|
||||
private String eTag;
|
||||
private final String remoteETag;
|
||||
private final String preferencesDataKey;
|
||||
|
||||
// BaseNoGui.getSettingsFolder()
|
||||
public FileDownloaderCache(String cacheFolder, URL remoteURL) {
|
||||
this.remoteURL = remoteURL;
|
||||
String[] splitPath = remoteURL.getPath().split("/");
|
||||
if (splitPath.length > 0) {
|
||||
this.cacheFilePath = Paths.get(cacheFolder, splitPath);
|
||||
} else {
|
||||
this.cacheFilePath = null;
|
||||
}
|
||||
private FileDownloaderCache(Path cacheFilePath, String remoteETag, String preferencesDataKey) {
|
||||
this.cacheFilePath = cacheFilePath;
|
||||
this.remoteETag = remoteETag;
|
||||
this.preferencesDataKey = preferencesDataKey;
|
||||
}
|
||||
|
||||
public boolean checkIfTheFileIsChanged()
|
||||
throws NoSuchMethodException, ScriptException, IOException,
|
||||
URISyntaxException {
|
||||
public static FileDownloaderCache getFileCached(String cacheFolder, URL remoteURL)
|
||||
throws IOException, NoSuchMethodException, ScriptException, URISyntaxException {
|
||||
|
||||
final String[] splitPath = remoteURL.getPath().split("/");
|
||||
final String preferencesDataKey = "cache.file." + remoteURL.getPath();
|
||||
final Path cacheFilePath;
|
||||
if (splitPath.length > 0) {
|
||||
cacheFilePath = Paths.get(cacheFolder, splitPath);
|
||||
} else {
|
||||
cacheFilePath = null;
|
||||
}
|
||||
|
||||
final HttpURLConnection headRequest = new HttpConnectionManager(remoteURL)
|
||||
.makeConnection((connection) -> {
|
||||
@ -52,20 +55,30 @@ public class FileDownloaderCache {
|
||||
// Something bad is happening return a conservative true to try to download the file
|
||||
if (responseCode < 200 || responseCode >= 300) {
|
||||
log.warn("The head request return a bad response code " + responseCode);
|
||||
return true;
|
||||
// if something bad happend
|
||||
return new FileDownloaderCache(cacheFilePath, null, preferencesDataKey);
|
||||
}
|
||||
|
||||
final String remoteETag = headRequest.getHeaderField("ETag");
|
||||
final String localETag = PreferencesData.get(getPreferencesDataKey());
|
||||
String remoteETagClean = null;
|
||||
if (remoteETag != null) {
|
||||
remoteETagClean = remoteETag.trim().replace("\"", "");
|
||||
}
|
||||
|
||||
return new FileDownloaderCache(cacheFilePath, remoteETagClean, preferencesDataKey);
|
||||
}
|
||||
|
||||
public boolean isChange() {
|
||||
|
||||
final String localETag = PreferencesData.get(preferencesDataKey);
|
||||
|
||||
// If the header doesn't exist or the local cache doesn't exist you need to download the file
|
||||
if (remoteETag == null || localETag == null) {
|
||||
if (cacheFilePath == null || remoteETag == null || localETag == null) {
|
||||
return true;
|
||||
}
|
||||
eTag = remoteETag.trim().replace("\"", "");
|
||||
|
||||
// If are different means that the file is change
|
||||
return !eTag.equals(localETag);
|
||||
return !remoteETag.equals(localETag);
|
||||
}
|
||||
|
||||
public Optional<File> getFileFromCache() {
|
||||
@ -77,10 +90,10 @@ public class FileDownloaderCache {
|
||||
}
|
||||
|
||||
public void fillCache(File fileToCache) throws Exception {
|
||||
if (Optional.ofNullable(eTag).isPresent() &&
|
||||
if (Optional.ofNullable(remoteETag).isPresent() &&
|
||||
Optional.ofNullable(cacheFilePath).isPresent()) {
|
||||
|
||||
PreferencesData.set(getPreferencesDataKey(), eTag);
|
||||
PreferencesData.set(preferencesDataKey, remoteETag);
|
||||
// If the cache directory does not exist create it
|
||||
if (!Files.exists(cacheFilePath.getParent())) {
|
||||
Files.createDirectories(cacheFilePath.getParent());
|
||||
@ -89,7 +102,4 @@ public class FileDownloaderCache {
|
||||
}
|
||||
}
|
||||
|
||||
private String getPreferencesDataKey() {
|
||||
return "cache.file." + remoteURL.getPath();
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user