use of com.intellij.util.download.DownloadableFileDescription in project intellij-community by JetBrains.
the class FindJarFix method downloadJar.
private void downloadJar(String jarUrl, String jarName) {
final Project project = myModule.getProject();
final String dirPath = PropertiesComponent.getInstance(project).getValue("findjar.last.used.dir");
VirtualFile toSelect = dirPath == null ? null : LocalFileSystem.getInstance().findFileByIoFile(new File(dirPath));
final VirtualFile file = FileChooser.chooseFile(FileChooserDescriptorFactory.createSingleFolderDescriptor(), project, toSelect);
if (file != null) {
PropertiesComponent.getInstance(project).setValue("findjar.last.used.dir", file.getPath());
final DownloadableFileService downloader = DownloadableFileService.getInstance();
final DownloadableFileDescription description = downloader.createFileDescription(jarUrl, jarName);
final List<VirtualFile> jars = downloader.createDownloader(Arrays.asList(description), jarName).downloadFilesWithProgress(file.getPath(), project, myEditorComponent);
if (jars != null && jars.size() == 1) {
WriteAction.run(() -> OrderEntryFix.addJarToRoots(jars.get(0).getPresentableUrl(), myModule, myRef));
}
}
}
use of com.intellij.util.download.DownloadableFileDescription in project intellij-community by JetBrains.
the class FileDownloaderImpl method moveToDir.
private static List<Pair<File, DownloadableFileDescription>> moveToDir(List<Pair<File, DownloadableFileDescription>> downloadedFiles, final File targetDir) throws IOException {
FileUtil.createDirectory(targetDir);
List<Pair<File, DownloadableFileDescription>> result = new ArrayList<>();
for (Pair<File, DownloadableFileDescription> pair : downloadedFiles) {
final DownloadableFileDescription description = pair.getSecond();
final String fileName = description.generateFileName(s -> !new File(targetDir, s).exists());
final File toFile = new File(targetDir, fileName);
FileUtil.rename(pair.getFirst(), toFile);
result.add(Pair.create(toFile, description));
}
return result;
}
use of com.intellij.util.download.DownloadableFileDescription in project intellij-community by JetBrains.
the class FileDownloaderImpl method download.
@NotNull
@Override
public List<Pair<File, DownloadableFileDescription>> download(@NotNull final File targetDir) throws IOException {
List<Pair<File, DownloadableFileDescription>> downloadedFiles = Collections.synchronizedList(new ArrayList<>());
List<Pair<File, DownloadableFileDescription>> existingFiles = Collections.synchronizedList(new ArrayList<>());
ProgressIndicator parentIndicator = ProgressManager.getInstance().getProgressIndicator();
if (parentIndicator == null) {
parentIndicator = new EmptyProgressIndicator();
}
try {
final ConcurrentTasksProgressManager progressManager = new ConcurrentTasksProgressManager(parentIndicator, myFileDescriptions.size());
parentIndicator.setText(IdeBundle.message("progress.downloading.0.files.text", myFileDescriptions.size()));
int maxParallelDownloads = Runtime.getRuntime().availableProcessors();
LOG.debug("Downloading " + myFileDescriptions.size() + " files using " + maxParallelDownloads + " threads");
long start = System.currentTimeMillis();
ExecutorService executor = AppExecutorUtil.createBoundedApplicationPoolExecutor("FileDownloaderImpl pool", maxParallelDownloads);
List<Future<Void>> results = new ArrayList<>();
final AtomicLong totalSize = new AtomicLong();
for (final DownloadableFileDescription description : myFileDescriptions) {
results.add(executor.submit(() -> {
SubTaskProgressIndicator indicator = progressManager.createSubTaskIndicator();
indicator.checkCanceled();
final File existing = new File(targetDir, description.getDefaultFileName());
final String url = description.getDownloadUrl();
if (url.startsWith(LIB_SCHEMA)) {
final String path = FileUtil.toSystemDependentName(StringUtil.trimStart(url, LIB_SCHEMA));
final File file = PathManager.findFileInLibDirectory(path);
existingFiles.add(Pair.create(file, description));
} else if (url.startsWith(LocalFileSystem.PROTOCOL_PREFIX)) {
String path = FileUtil.toSystemDependentName(StringUtil.trimStart(url, LocalFileSystem.PROTOCOL_PREFIX));
File file = new File(path);
if (file.exists()) {
existingFiles.add(Pair.create(file, description));
}
} else {
File downloaded;
try {
downloaded = downloadFile(description, existing, indicator);
} catch (IOException e) {
throw new IOException(IdeBundle.message("error.file.download.failed", description.getDownloadUrl(), e.getMessage()), e);
}
if (FileUtil.filesEqual(downloaded, existing)) {
existingFiles.add(Pair.create(existing, description));
} else {
totalSize.addAndGet(downloaded.length());
downloadedFiles.add(Pair.create(downloaded, description));
}
}
indicator.finished();
return null;
}));
}
for (Future<Void> result : results) {
try {
result.get();
} catch (InterruptedException e) {
throw new ProcessCanceledException();
} catch (ExecutionException e) {
Throwables.propagateIfInstanceOf(e.getCause(), IOException.class);
Throwables.propagateIfInstanceOf(e.getCause(), ProcessCanceledException.class);
LOG.error(e);
}
}
long duration = System.currentTimeMillis() - start;
LOG.debug("Downloaded " + StringUtil.formatFileSize(totalSize.get()) + " in " + StringUtil.formatDuration(duration) + "(" + duration + "ms)");
List<Pair<File, DownloadableFileDescription>> localFiles = new ArrayList<>();
localFiles.addAll(moveToDir(downloadedFiles, targetDir));
localFiles.addAll(existingFiles);
return localFiles;
} catch (ProcessCanceledException | IOException e) {
deleteFiles(downloadedFiles);
throw e;
}
}
use of com.intellij.util.download.DownloadableFileDescription in project intellij-community by JetBrains.
the class FileDownloaderImpl method downloadWithProcess.
@Nullable
private List<Pair<VirtualFile, DownloadableFileDescription>> downloadWithProcess(final File targetDir, Project project, JComponent parentComponent) {
final Ref<List<Pair<File, DownloadableFileDescription>>> localFiles = Ref.create(null);
final Ref<IOException> exceptionRef = Ref.create(null);
boolean completed = ProgressManager.getInstance().runProcessWithProgressSynchronously(() -> {
try {
localFiles.set(download(targetDir));
} catch (IOException e) {
exceptionRef.set(e);
}
}, myDialogTitle, true, project, parentComponent);
if (!completed) {
return null;
}
@SuppressWarnings("ThrowableResultOfMethodCallIgnored") Exception exception = exceptionRef.get();
if (exception != null) {
final boolean tryAgain = IOExceptionDialog.showErrorDialog(myDialogTitle, exception.getMessage());
if (tryAgain) {
return downloadWithProcess(targetDir, project, parentComponent);
}
return null;
}
return findVirtualFiles(localFiles.get());
}
use of com.intellij.util.download.DownloadableFileDescription in project android by JetBrains.
the class DistributionService method getInstance.
public static DistributionService getInstance() {
if (ourInstance == null) {
DownloadableFileDescription description = DownloadableFileService.getInstance().createFileDescription(STATS_URL, DOWNLOAD_FILENAME);
FileDownloader downloader = DownloadableFileService.getInstance().createDownloader(ImmutableList.of(description), "Distribution Stats");
ourInstance = new DistributionService(downloader, CACHE_PATH, FALLBACK_URL);
}
return ourInstance;
}
Aggregations