mirror of
https://github.com/arduino/Arduino.git
synced 2025-02-08 02:54:24 +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 File settingsFolder = BaseNoGui.getPlatform().getSettingsFolder();
|
||||||
final String cacheFolder = Paths.get(settingsFolder.getPath(), "cache").toString();
|
final String cacheFolder = Paths.get(settingsFolder.getPath(), "cache").toString();
|
||||||
final FileDownloaderCache fileDownloaderCache =
|
final FileDownloaderCache fileDownloaderCache = FileDownloaderCache.getFileCached(cacheFolder, downloadUrl);
|
||||||
new FileDownloaderCache(cacheFolder, downloadUrl);
|
|
||||||
|
|
||||||
final boolean isChanged = fileDownloaderCache.checkIfTheFileIsChanged();
|
if (!fileDownloaderCache.isChange()) {
|
||||||
|
|
||||||
if (!isChanged) {
|
|
||||||
try {
|
try {
|
||||||
final Optional<File> fileFromCache =
|
final Optional<File> fileFromCache =
|
||||||
fileDownloaderCache.getFileFromCache();
|
fileDownloaderCache.getFileFromCache();
|
||||||
@ -165,20 +162,17 @@ public class FileDownloader extends Observable {
|
|||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.warn(
|
log.warn(
|
||||||
"Cannot get the file from the cache, will be downloaded a new one ", e.getCause());
|
"Cannot get the file from the cache, will be downloaded a new one ", e.getCause());
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Open file and seek to the end of it
|
initialSize = outputFile.length();
|
||||||
randomAccessOutputFile = new RandomAccessFile(outputFile, "rw");
|
|
||||||
initialSize = randomAccessOutputFile.length();
|
|
||||||
|
|
||||||
if (noResume && initialSize > 0) {
|
if (noResume && initialSize > 0) {
|
||||||
// delete file and restart downloading
|
// delete file and restart downloading
|
||||||
Files.delete(outputFile.toPath());
|
Files.deleteIfExists(outputFile.toPath());
|
||||||
initialSize = 0;
|
initialSize = 0;
|
||||||
}
|
}
|
||||||
|
// Open file and seek to the end of it
|
||||||
|
randomAccessOutputFile = new RandomAccessFile(outputFile, "rw");
|
||||||
randomAccessOutputFile.seek(initialSize);
|
randomAccessOutputFile.seek(initialSize);
|
||||||
|
|
||||||
final HttpURLConnection connection = new HttpConnectionManager(downloadUrl)
|
final HttpURLConnection connection = new HttpConnectionManager(downloadUrl)
|
||||||
@ -186,7 +180,8 @@ public class FileDownloader extends Observable {
|
|||||||
final int resp = connection.getResponseCode();
|
final int resp = connection.getResponseCode();
|
||||||
|
|
||||||
if (resp < 200 || resp >= 300) {
|
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);
|
throw new IOException("Received invalid http status code from server: " + resp);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -19,25 +19,28 @@ import java.util.Optional;
|
|||||||
|
|
||||||
public class FileDownloaderCache {
|
public class FileDownloaderCache {
|
||||||
private static Logger log = LoggerFactory.getLogger(FileDownloaderCache.class);
|
private static Logger log = LoggerFactory.getLogger(FileDownloaderCache.class);
|
||||||
private final URL remoteURL;
|
|
||||||
private final Path cacheFilePath;
|
private final Path cacheFilePath;
|
||||||
// Will be initialized by call the checkIfTheFileIsChanged function
|
private final String remoteETag;
|
||||||
private String eTag;
|
private final String preferencesDataKey;
|
||||||
|
|
||||||
// BaseNoGui.getSettingsFolder()
|
// BaseNoGui.getSettingsFolder()
|
||||||
public FileDownloaderCache(String cacheFolder, URL remoteURL) {
|
private FileDownloaderCache(Path cacheFilePath, String remoteETag, String preferencesDataKey) {
|
||||||
this.remoteURL = remoteURL;
|
this.cacheFilePath = cacheFilePath;
|
||||||
String[] splitPath = remoteURL.getPath().split("/");
|
this.remoteETag = remoteETag;
|
||||||
if (splitPath.length > 0) {
|
this.preferencesDataKey = preferencesDataKey;
|
||||||
this.cacheFilePath = Paths.get(cacheFolder, splitPath);
|
|
||||||
} else {
|
|
||||||
this.cacheFilePath = null;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean checkIfTheFileIsChanged()
|
public static FileDownloaderCache getFileCached(String cacheFolder, URL remoteURL)
|
||||||
throws NoSuchMethodException, ScriptException, IOException,
|
throws IOException, NoSuchMethodException, ScriptException, URISyntaxException {
|
||||||
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)
|
final HttpURLConnection headRequest = new HttpConnectionManager(remoteURL)
|
||||||
.makeConnection((connection) -> {
|
.makeConnection((connection) -> {
|
||||||
@ -52,20 +55,30 @@ public class FileDownloaderCache {
|
|||||||
// Something bad is happening return a conservative true to try to download the file
|
// Something bad is happening return a conservative true to try to download the file
|
||||||
if (responseCode < 200 || responseCode >= 300) {
|
if (responseCode < 200 || responseCode >= 300) {
|
||||||
log.warn("The head request return a bad response code " + responseCode);
|
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 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 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;
|
return true;
|
||||||
}
|
}
|
||||||
eTag = remoteETag.trim().replace("\"", "");
|
|
||||||
|
|
||||||
// If are different means that the file is change
|
// If are different means that the file is change
|
||||||
return !eTag.equals(localETag);
|
return !remoteETag.equals(localETag);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Optional<File> getFileFromCache() {
|
public Optional<File> getFileFromCache() {
|
||||||
@ -77,10 +90,10 @@ public class FileDownloaderCache {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void fillCache(File fileToCache) throws Exception {
|
public void fillCache(File fileToCache) throws Exception {
|
||||||
if (Optional.ofNullable(eTag).isPresent() &&
|
if (Optional.ofNullable(remoteETag).isPresent() &&
|
||||||
Optional.ofNullable(cacheFilePath).isPresent()) {
|
Optional.ofNullable(cacheFilePath).isPresent()) {
|
||||||
|
|
||||||
PreferencesData.set(getPreferencesDataKey(), eTag);
|
PreferencesData.set(preferencesDataKey, remoteETag);
|
||||||
// If the cache directory does not exist create it
|
// If the cache directory does not exist create it
|
||||||
if (!Files.exists(cacheFilePath.getParent())) {
|
if (!Files.exists(cacheFilePath.getParent())) {
|
||||||
Files.createDirectories(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