Skip to content
Snippets Groups Projects

Fix spaces in file paths breaking jobs

4 files
+ 44
32
Compare changes
  • Side-by-side
  • Inline

Files

@@ -3,7 +3,8 @@ package nl.tudelft.ewi.auta.worker.files;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Path;
@@ -40,21 +41,21 @@ public class JobDownloader {
}
/**
* Downloads the file at the given URL.
* Downloads the file at the given URI.
*
* @param urlstr the URL to download
* @param uri the URI to download
*
* @return the path to the downloaded archive
*
* @throws IOException if the job could not be downloaded
*/
public Path download(final String urlstr) throws IOException {
public Path download(final URI uri) throws IOException {
try {
logger.info("Trying to download from {}", urlstr);
final var url = (HttpURLConnection) new URL(urlstr).openConnection();
logger.info("Trying to download from {}", uri);
final var url = (HttpURLConnection) uri.toURL().openConnection();
final var file = Files.createTempFile(
this.settings.getTemp(), "job-", this.getExtension(urlstr)
this.settings.getTemp(), "job-", this.getExtension(uri.getPath())
);
url.setRequestProperty("Auth-Token", this.settings.getApiAuthToken());
@@ -62,7 +63,7 @@ public class JobDownloader {
if (url.getResponseCode() != 200) {
logger.error("Partial, no or error response {} {} from {}",
url.getResponseCode(), url.getResponseMessage(), urlstr
url.getResponseCode(), url.getResponseMessage(), uri
);
throw new BadJobException("Incomplete or error response " + url.getResponseCode());
}
@@ -71,7 +72,7 @@ public class JobDownloader {
var in = url.getInputStream()) {
final var contentLength = url.getContentLengthLong();
logger.debug("Downloading {} bytes from {}", contentLength, urlstr);
logger.debug("Downloading {} bytes from {}", contentLength, uri);
byte[] buf = new byte[BUFFER_SIZE];
@@ -101,11 +102,15 @@ public class JobDownloader {
*
* @return the URL
*/
public String getDownloadUrl(final Message job) {
return String.format("%s://%s:%s/api/v1/files/get%s",
this.settings.getApiProtocol(), this.settings.getHost(),
this.settings.getApiPort(), job.getData()
);
public URI getDownloadUri(final Message job) {
try {
return new URI(
this.settings.getApiProtocol(), "", this.settings.getHost(),
this.settings.getApiPort(), "/api/v1/files/get" + job.getData(), "", ""
);
} catch (final URISyntaxException ex) {
throw new RuntimeException(ex);
}
}
/**
Loading