2020-06-12 17:40:17 +03:00
|
|
|
/*
|
2023-12-26 14:39:05 +03:00
|
|
|
Copyright 2019-2023 Dmitry Isaenko
|
2020-06-12 17:40:17 +03:00
|
|
|
|
|
|
|
This file is part of NS-USBloader.
|
|
|
|
|
|
|
|
NS-USBloader is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
NS-USBloader is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with NS-USBloader. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
package nsusbloader.Utilities.WindowsDrivers;
|
|
|
|
|
|
|
|
import javafx.concurrent.Task;
|
|
|
|
|
|
|
|
import java.io.*;
|
|
|
|
import java.net.URL;
|
|
|
|
|
|
|
|
public class DownloadDriversTask extends Task<String> {
|
|
|
|
|
2023-12-26 14:39:05 +03:00
|
|
|
public static final long DRIVERS_FILE_SIZE = 3857375;
|
2020-06-12 17:40:17 +03:00
|
|
|
private static final String driverFileLocationURL = "https://github.com/developersu/NS-Drivers/releases/download/v1.0/Drivers_set.exe";
|
|
|
|
|
|
|
|
private static File driversInstallerFile;
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected String call() {
|
2020-07-12 05:34:57 +03:00
|
|
|
if (isDriversDownloaded() || downloadDrivers())
|
2020-06-12 17:40:17 +03:00
|
|
|
return driversInstallerFile.getAbsolutePath();
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean isDriversDownloaded(){
|
2023-12-26 14:39:05 +03:00
|
|
|
return driversInstallerFile != null && driversInstallerFile.length() == DRIVERS_FILE_SIZE;
|
2020-06-12 17:40:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
private boolean downloadDrivers(){
|
|
|
|
try {
|
|
|
|
File tmpDirectory = File.createTempFile("nsul", null);
|
|
|
|
if (! tmpDirectory.delete())
|
|
|
|
return false;
|
|
|
|
if (! tmpDirectory.mkdirs())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
tmpDirectory.deleteOnExit();
|
|
|
|
|
|
|
|
URL url = new URL(driverFileLocationURL);
|
|
|
|
BufferedInputStream bis = new BufferedInputStream(url.openStream());
|
|
|
|
|
|
|
|
driversInstallerFile = new File(tmpDirectory, "drivers.exe");
|
|
|
|
FileOutputStream fos = new FileOutputStream(driversInstallerFile);
|
|
|
|
|
|
|
|
byte[] dataBuffer = new byte[1024];
|
|
|
|
int bytesRead;
|
|
|
|
double totalRead = 0;
|
|
|
|
|
|
|
|
while ((bytesRead = bis.read(dataBuffer, 0, 1024)) != -1) {
|
|
|
|
fos.write(dataBuffer, 0, bytesRead);
|
|
|
|
totalRead += bytesRead;
|
2023-12-26 14:39:05 +03:00
|
|
|
updateProgress(totalRead, DRIVERS_FILE_SIZE);
|
2020-06-12 17:40:17 +03:00
|
|
|
if (this.isCancelled()) {
|
|
|
|
bis.close();
|
|
|
|
fos.close();
|
|
|
|
updateProgress(0, 0);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
bis.close();
|
|
|
|
fos.close();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
catch (IOException | SecurityException e){
|
|
|
|
updateMessage("Error: "+e.toString().replaceAll(":.*$", ""));
|
|
|
|
e.printStackTrace();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|