Skip to content

Commit

Permalink
add system property to override download path
Browse files Browse the repository at this point in the history
Signed-off-by: Stephane Bouchet <[email protected]>
  • Loading branch information
sbouchet committed Jul 1, 2024
1 parent 43e00da commit 55ac365
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 9 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ target
build
.gradle
out
/cache/
/.run/
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

public class CommonConstants {
public static final String HOME_FOLDER = System.getProperty("user.home");
public static final String TOOLS_DOWNLOAD_PATH = System.getProperty("tools.dl.path");
public static final Key<Project> PROJECT = Key.create("com.redhat.devtools.intellij.common.project");
public static final Key<Long> LAST_MODIFICATION_STAMP = Key.create("com.redhat.devtools.intellij.common.last.modification.stamp");
public static final Key<Object> TARGET_NODE = Key.create("com.redhat.devtools.intellij.common.targetnode");
Expand All @@ -44,4 +45,7 @@ public class CommonConstants {
"selfLink",
"uid"
);

private CommonConstants() {}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@
import com.intellij.openapi.progress.Task;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.openapi.util.text.Strings;
import com.intellij.util.io.HttpRequests;
import com.redhat.devtools.intellij.common.CommonConstants;
import com.twelvemonkeys.lang.Platform;
import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.binary.Hex;
import org.apache.commons.compress.archivers.ArchiveEntry;
import org.apache.commons.compress.archivers.ArchiveInputStream;
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
Expand Down Expand Up @@ -51,6 +54,9 @@
import java.util.regex.Pattern;

public class DownloadHelper {

private static final int BUFFER_SIZE = 4096;

private static final UnaryOperator<InputStream> UNCOMPRESSOR = (input -> {
try {
return new CompressorStreamFactory().createCompressorInputStream(input);
Expand Down Expand Up @@ -141,7 +147,8 @@ private CompletableFuture<ToolInstance> downloadIfRequiredAsyncInner(String tool
String command = platform.getCmdFileName();
String version = getVersionFromPath(tool, platform);
if (!areCompatible(version, tool.getVersionMatchRegExpr())) {
Path path = Paths.get(tool.getBaseDir().replace("$HOME", CommonConstants.HOME_FOLDER), "cache", tool.getVersion(), command);
String replacement = Strings.isEmpty(CommonConstants.TOOLS_DOWNLOAD_PATH) ? CommonConstants.HOME_FOLDER : CommonConstants.TOOLS_DOWNLOAD_PATH;
Path path = Paths.get(tool.getBaseDir().replace("$HOME", replacement), "cache", tool.getVersion(), command);
final String cmd = path.toString();
if (!Files.exists(path)) {
result = downloadInBackground(toolName, platform, path, cmd, tool, version, platform.getSha256());
Expand Down Expand Up @@ -264,7 +271,7 @@ private String getVersionFromPath(ToolsConfig.Tool tool, ToolsConfig.Platform pl
}

private static void downloadFile(InputStream input, Path dlFileName, ProgressIndicator progressIndicator, long size) throws IOException {
byte[] buffer = new byte[4096];
byte[] buffer = new byte[BUFFER_SIZE];
Files.createDirectories(dlFileName.getParent());
try (OutputStream output = Files.newOutputStream(dlFileName)) {
int lg;
Expand Down Expand Up @@ -320,12 +327,17 @@ private void save(InputStream source, Path destination, long length) throws IOEx
}

private boolean verify(Path path, String checksum) throws IOException {
try {
try (InputStream stream = Files.newInputStream(path)) {
MessageDigest digest = MessageDigest.getInstance("SHA-256");
byte[] hash = digest.digest(Files.readAllBytes(path));
return MessageDigest.isEqual(hash, checksum.getBytes(StandardCharsets.UTF_8));
} catch (NoSuchAlgorithmException e) {
throw new IOException("Could not verify checksum for file " + path.toString(), e);
final byte[] buffer = new byte[BUFFER_SIZE];
int read = stream.read(buffer, 0, BUFFER_SIZE);
while (read > -1) {
digest.update(buffer, 0, read);
read = stream.read(buffer, 0, BUFFER_SIZE);
}
return MessageDigest.isEqual(digest.digest(), Hex.decodeHex(checksum));
} catch (NoSuchAlgorithmException | DecoderException e) {
throw new IOException("Could not verify checksum for file " + path, e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import com.intellij.openapi.ui.TestDialog;
import com.intellij.testFramework.LightPlatformTestCase;
import org.apache.commons.io.FileUtils;
import org.junit.rules.TemporaryFolder;

import java.io.File;
import java.io.IOException;
Expand All @@ -24,14 +25,13 @@ public class DownloadHelperTest extends LightPlatformTestCase {
public void setUp() throws Exception {
super.setUp();
previous = MessagesHelper.setTestDialog(TestDialog.OK);
FileUtils.deleteDirectory(new File("cache"));
}

@Override
protected void tearDown() throws Exception {
System.clearProperty("tools.dl.path");
MessagesHelper.setTestDialog(previous);
super.tearDown();
FileUtils.deleteDirectory(new File("cache"));
}

public void testThatGZIsDownloaded() throws IOException {
Expand All @@ -40,6 +40,7 @@ public void testThatGZIsDownloaded() throws IOException {
assertNotNull(toolInstance.getCommand());
assertEquals("." + File.separatorChar + "cache" + File.separatorChar + "0.5.0" + File.separatorChar + "tkn", toolInstance.getCommand());
assertEquals(17, new File(toolInstance.getCommand()).length());
FileUtils.deleteDirectory(Paths.get(toolInstance.getCommand()).toFile().getParentFile());
}

public void testThatTarGZIsDownloaded() throws IOException {
Expand All @@ -48,6 +49,7 @@ public void testThatTarGZIsDownloaded() throws IOException {
assertNotNull(toolInstance.getCommand());
assertEquals("." + File.separatorChar + "cache" + File.separatorChar + "0.5.0" + File.separatorChar + "tkn", toolInstance.getCommand());
assertEquals(17, new File(toolInstance.getCommand()).length());
FileUtils.deleteDirectory(Paths.get(toolInstance.getCommand()).toFile().getParentFile());
}

public void testThatPlainFileDownloaded() throws IOException {
Expand All @@ -56,6 +58,7 @@ public void testThatPlainFileDownloaded() throws IOException {
assertNotNull(toolInstance.getCommand());
assertEquals("." + File.separatorChar + "cache" + File.separatorChar + "0.5.0" + File.separatorChar + "tkn", toolInstance.getCommand());
assertEquals(17, new File(toolInstance.getCommand()).length());
FileUtils.deleteDirectory(Paths.get(toolInstance.getCommand()).toFile().getParentFile());
}

public void testThatChecksumIsValidForDownloadedTool() throws IOException {
Expand All @@ -74,4 +77,15 @@ public void testThatChecksumIsInValidForDownloadedTool() {
assertTrue(e.getMessage().contains("Error while setting tool"));
}
}

public void testThatPlainFileDownloadedInUserSpecificFolder() throws IOException {
TemporaryFolder temp = new TemporaryFolder();
temp.create();
System.setProperty("tools.dl.path", temp.getRoot().getAbsolutePath());
DownloadHelper.ToolInstance toolInstance = DownloadHelper.getInstance().downloadIfRequired("tkn", DownloadHelperTest.class.getResource("/tkn-test.json"));
assertNotNull(toolInstance);
assertNotNull(toolInstance.getCommand());
assertEquals(System.getProperty("tools.dl.path") + File.separatorChar + ".tekton" + File.separatorChar + "cache" + File.separatorChar + "0.5.0" + File.separatorChar + "tkn", toolInstance.getCommand());
temp.delete();
}
}

0 comments on commit 55ac365

Please sign in to comment.