Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use filename in URL when storing downloaded file on disk #32293

Merged
merged 2 commits into from
Aug 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}

}