2013-04-12 19:32:25 -07:00
|
|
|
package magic.data;
|
|
|
|
|
|
|
|
import java.io.BufferedOutputStream;
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileOutputStream;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.io.OutputStream;
|
|
|
|
import java.net.Proxy;
|
|
|
|
import java.net.URL;
|
2015-12-12 01:26:22 -08:00
|
|
|
import java.nio.file.Files;
|
|
|
|
import java.nio.file.StandardCopyOption;
|
2020-01-15 12:02:42 -08:00
|
|
|
|
2014-08-02 11:26:46 -07:00
|
|
|
import org.apache.commons.io.FileUtils;
|
2013-04-12 19:32:25 -07:00
|
|
|
|
2015-12-12 01:26:22 -08:00
|
|
|
public class DownloadableFile {
|
2013-06-23 18:33:35 -07:00
|
|
|
|
2017-02-20 12:59:08 -08:00
|
|
|
private static final GeneralConfig CONFIG = GeneralConfig.getInstance();
|
|
|
|
|
|
|
|
private static String proxySettings = "";
|
|
|
|
private static Proxy proxy = Proxy.NO_PROXY;
|
|
|
|
|
2015-12-12 01:26:22 -08:00
|
|
|
private final File localFile;
|
|
|
|
private final URL remoteFile;
|
2013-06-23 18:33:35 -07:00
|
|
|
|
2015-12-12 01:26:22 -08:00
|
|
|
public DownloadableFile(File aFile, URL aUrl) {
|
|
|
|
this.localFile = aFile;
|
|
|
|
this.remoteFile = aUrl;
|
|
|
|
}
|
2013-04-12 19:32:25 -07:00
|
|
|
|
2015-12-12 01:26:22 -08:00
|
|
|
public File getLocalFile() {
|
|
|
|
return localFile;
|
|
|
|
}
|
2014-08-16 03:22:28 -07:00
|
|
|
|
2015-12-12 01:26:22 -08:00
|
|
|
public URL getUrl() {
|
|
|
|
return remoteFile;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getFilename() {
|
|
|
|
return localFile.getName();
|
|
|
|
}
|
|
|
|
|
2017-02-20 12:59:08 -08:00
|
|
|
public void doDownload() throws IOException {
|
2015-12-12 01:26:22 -08:00
|
|
|
final File tempFile = new File(localFile.getParent(), "~" + localFile.getName());
|
2017-02-20 12:59:08 -08:00
|
|
|
downloadToFile(remoteFile, tempFile);
|
2015-12-12 01:26:22 -08:00
|
|
|
Files.move(tempFile.toPath(), localFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
|
|
|
|
}
|
2013-06-23 18:33:35 -07:00
|
|
|
|
2017-02-20 12:59:08 -08:00
|
|
|
private static Proxy getProxy() {
|
|
|
|
if (!proxySettings.equals(CONFIG.getProxySettings())) {
|
|
|
|
proxySettings = CONFIG.getProxySettings();
|
|
|
|
proxy = CONFIG.getProxy();
|
2014-08-02 11:26:46 -07:00
|
|
|
}
|
2017-02-20 12:59:08 -08:00
|
|
|
return proxy;
|
2014-08-02 11:26:46 -07:00
|
|
|
}
|
|
|
|
|
2017-02-20 12:59:08 -08:00
|
|
|
private static void downloadUsingProxy(URL url, File file) throws IOException {
|
2014-08-02 11:26:46 -07:00
|
|
|
try (
|
|
|
|
final OutputStream outputStream = new BufferedOutputStream(new FileOutputStream(file));
|
2017-02-20 12:59:08 -08:00
|
|
|
final InputStream inputStream = url.openConnection(getProxy()).getInputStream()) {
|
2015-12-12 01:26:22 -08:00
|
|
|
final byte[] buffer = new byte[65536];
|
|
|
|
while (true) {
|
|
|
|
final int len = inputStream.read(buffer);
|
|
|
|
if (len < 0) {
|
|
|
|
break;
|
2013-04-12 19:32:25 -07:00
|
|
|
}
|
2015-12-12 01:26:22 -08:00
|
|
|
outputStream.write(buffer, 0, len);
|
|
|
|
}
|
2014-08-02 11:26:46 -07:00
|
|
|
} catch (final Exception ex) {
|
|
|
|
file.delete();
|
|
|
|
throw ex;
|
2013-04-12 19:32:25 -07:00
|
|
|
}
|
|
|
|
}
|
2013-06-23 18:33:35 -07:00
|
|
|
|
2017-02-20 12:59:08 -08:00
|
|
|
public static void downloadToFile(URL url, File file) throws IOException {
|
|
|
|
if (getProxy() != Proxy.NO_PROXY && getProxy().type() != Proxy.Type.DIRECT) {
|
|
|
|
downloadUsingProxy(url, file);
|
|
|
|
} else {
|
|
|
|
FileUtils.copyURLToFile(url, file, 10000, 5000);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-12 19:32:25 -07:00
|
|
|
}
|