Resclone/src/main/java/io/gitlab/jfronny/resclone/fetchers/BasePackFetcher.java

50 lines
1.8 KiB
Java
Raw Normal View History

2020-12-29 16:14:53 +01:00
package io.gitlab.jfronny.resclone.fetchers;
2023-02-26 14:00:07 +01:00
import io.gitlab.jfronny.commons.HttpUtils;
2020-12-29 16:14:53 +01:00
import io.gitlab.jfronny.resclone.Resclone;
2023-02-26 14:00:07 +01:00
import java.io.*;
import java.nio.file.Files;
2020-12-29 16:14:53 +01:00
import java.nio.file.Path;
public abstract class BasePackFetcher implements PackFetcher {
2021-04-03 02:40:38 +02:00
abstract String getDownloadUrl(String baseUrl) throws Exception; // Return the actual download URL for the file based on the provided string
2021-02-19 12:39:22 +01:00
2021-04-03 02:40:38 +02:00
public Result get(String baseUrl, Path targetDir, boolean forceDownload) throws Exception {
String url;
try {
url = getDownloadUrl(baseUrl);
Resclone.urlCache.set(baseUrl, url);
2021-04-03 02:40:38 +02:00
} catch (Exception e) {
if (Resclone.urlCache.containsKey(baseUrl)) {
Resclone.LOGGER.error("Could not get download URL for " + baseUrl + ", using cached", e);
url = Resclone.urlCache.get(baseUrl);
2021-04-03 02:40:38 +02:00
} else {
throw e;
}
}
Path p = targetDir.resolve(Integer.toHexString(url.hashCode()));
2021-04-03 06:34:52 +02:00
if (!forceDownload && Files.exists(p)) {
Resclone.packCount++;
return new Result(p, false);
2021-04-03 06:34:52 +02:00
}
Resclone.LOGGER.info("Downloading pack: " + url);
2023-02-26 14:00:07 +01:00
try (InputStream is = HttpUtils.get(url).userAgent(Resclone.USER_AGENT).sendInputStream();
OutputStream os = Files.newOutputStream(p)) {
2020-12-29 16:14:53 +01:00
byte[] dataBuffer = new byte[1024];
int bytesRead;
while ((bytesRead = is.read(dataBuffer, 0, 1024)) != -1) {
os.write(dataBuffer, 0, bytesRead);
}
2021-04-03 03:54:24 +02:00
Resclone.LOGGER.info("Finished downloading.");
2021-04-03 02:40:38 +02:00
} catch (Throwable e) {
2023-02-26 14:00:07 +01:00
throw new IOException("Could not download pack", e);
2020-12-29 16:14:53 +01:00
}
Resclone.packCount++;
return new Result(p, true);
}
}