Skip to content

Commit

Permalink
Merge pull request #32293 from vespa-engine/hmusum/keep-filename-when…
Browse files Browse the repository at this point in the history
…-downloading

Use filename in URL when storing downloaded file on disk
  • Loading branch information
Harald Musum authored Aug 30, 2024
2 parents 598aa2b + a1a6c42 commit 3a87783
Showing 1 changed file with 27 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
Expand All @@ -25,31 +27,48 @@ class UrlDownloader implements Downloader {
private static final String USER_AGENT_MODEL_DOWNLOADER = "Vespa/8.x (model download - https://github.com/vespa-engine/vespa)";

@Override
public Optional<File> downloadFile(String url, File downloadDir) throws IOException {
public Optional<File> downloadFile(String urlString, File downloadDir) throws IOException {
long start = System.currentTimeMillis();
HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
URI uri = getUri(urlString);
HttpURLConnection connection = (HttpURLConnection) uri.toURL().openConnection();
connection.setRequestProperty("User-Agent", USER_AGENT_MODEL_DOWNLOADER);
if (connection.getResponseCode() != 200)
throw new RuntimeException("Download of URL '" + url + "' failed, got response code " + connection.getResponseCode());
throw new RuntimeException("Download of URL '" + uri + "' failed, got response code " + connection.getResponseCode());

log.log(Level.INFO, "Downloading URL '" + url + "'");
File contentsPath = new File(downloadDir, CONTENTS_FILE_NAME);
log.log(Level.INFO, "Downloading URL '" + uri + "'");
File contentsPath = new File(downloadDir, filename(uri));
try (ReadableByteChannel rbc = Channels.newChannel(connection.getInputStream())) {
try (FileOutputStream fos = new FileOutputStream((contentsPath.getAbsolutePath()))) {
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);

if (contentsPath.exists() && contentsPath.length() > 0) {
new RequestTracker().trackRequest(downloadDir);
log.log(Level.FINE, () -> "URL '" + url + "' available at " + contentsPath);
log.log(Level.FINE, () -> "URL '" + uri + "' available at " + contentsPath);
log.log(Level.INFO, String.format("Download of URL '%s' done in %.3f seconds",
url, (System.currentTimeMillis() - start) / 1000.0));
uri, (System.currentTimeMillis() - start) / 1000.0));
return Optional.of(contentsPath);
} else {
log.log(Level.SEVERE, "Downloaded URL '" + url + "' not found, returning error");
log.log(Level.SEVERE, "Downloaded URL '" + uri + "' not found, returning error");
return Optional.empty();
}
}
}
}

private static URI getUri(String urlString) {
URI uri;
try {
uri = new URI(urlString);
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
return uri;
}

private String filename(URI uri) {
String path = uri.getPath();
var fileName = path.substring(path.lastIndexOf('/') + 1);
return fileName.isEmpty() ? CONTENTS_FILE_NAME : fileName;
}

}

0 comments on commit 3a87783

Please sign in to comment.