2023-03-31 07:56:18 +13:00
|
|
|
package io.xpipe.app.browser;
|
|
|
|
|
2023-04-01 02:32:12 +13:00
|
|
|
import io.xpipe.app.issue.ErrorEvent;
|
2023-03-31 07:56:18 +13:00
|
|
|
import io.xpipe.app.util.BusyProperty;
|
|
|
|
import io.xpipe.core.impl.FileNames;
|
|
|
|
import io.xpipe.core.store.FileSystem;
|
|
|
|
import javafx.beans.property.BooleanProperty;
|
|
|
|
import javafx.beans.property.SimpleBooleanProperty;
|
|
|
|
import javafx.collections.FXCollections;
|
|
|
|
import javafx.collections.ObservableList;
|
|
|
|
import lombok.Value;
|
|
|
|
import org.apache.commons.io.FileUtils;
|
|
|
|
|
2023-07-17 13:03:12 +12:00
|
|
|
import java.io.IOException;
|
2023-03-31 07:56:18 +13:00
|
|
|
import java.nio.file.Path;
|
2023-07-17 13:03:12 +12:00
|
|
|
import java.util.ArrayList;
|
2023-03-31 07:56:18 +13:00
|
|
|
import java.util.List;
|
2023-04-01 02:32:12 +13:00
|
|
|
import java.util.concurrent.ExecutorService;
|
|
|
|
import java.util.concurrent.Executors;
|
2023-03-31 07:56:18 +13:00
|
|
|
|
|
|
|
@Value
|
2023-05-21 01:49:58 +12:00
|
|
|
public class BrowserTransferModel {
|
2023-03-31 07:56:18 +13:00
|
|
|
|
|
|
|
private static final Path TEMP =
|
|
|
|
FileUtils.getTempDirectory().toPath().resolve("xpipe").resolve("download");
|
|
|
|
|
2023-04-01 02:32:12 +13:00
|
|
|
ExecutorService executor = Executors.newSingleThreadExecutor(r -> {
|
|
|
|
Thread t = Executors.defaultThreadFactory().newThread(r);
|
|
|
|
t.setDaemon(true);
|
|
|
|
t.setName("file downloader");
|
|
|
|
return t;
|
|
|
|
});
|
|
|
|
|
2023-03-31 07:56:18 +13:00
|
|
|
@Value
|
|
|
|
public static class Item {
|
2023-04-01 02:32:12 +13:00
|
|
|
String name;
|
2023-03-31 07:56:18 +13:00
|
|
|
FileSystem.FileEntry fileEntry;
|
|
|
|
Path localFile;
|
|
|
|
BooleanProperty finishedDownload = new SimpleBooleanProperty();
|
|
|
|
}
|
|
|
|
|
2023-07-17 13:03:12 +12:00
|
|
|
BrowserModel browserModel;
|
2023-03-31 07:56:18 +13:00
|
|
|
ObservableList<Item> items = FXCollections.observableArrayList();
|
|
|
|
BooleanProperty downloading = new SimpleBooleanProperty();
|
2023-07-17 13:03:12 +12:00
|
|
|
BooleanProperty allDownloaded = new SimpleBooleanProperty();
|
|
|
|
|
|
|
|
public void clear() {
|
|
|
|
try {
|
|
|
|
FileUtils.deleteDirectory(TEMP.toFile());
|
|
|
|
} catch (IOException e) {
|
|
|
|
ErrorEvent.fromThrowable(e).handle();
|
|
|
|
}
|
|
|
|
items.clear();
|
|
|
|
}
|
2023-03-31 07:56:18 +13:00
|
|
|
|
|
|
|
public void drop(List<FileSystem.FileEntry> entries) {
|
|
|
|
entries.forEach(entry -> {
|
2023-04-01 02:32:12 +13:00
|
|
|
var name = FileNames.getFileName(entry.getPath());
|
|
|
|
if (items.stream().anyMatch(item -> item.getName().equals(name))) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Path file = TEMP.resolve(name);
|
|
|
|
var item = new Item(name, entry, file);
|
2023-03-31 07:56:18 +13:00
|
|
|
items.add(item);
|
2023-07-17 13:03:12 +12:00
|
|
|
allDownloaded.set(false);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public void download() {
|
|
|
|
executor.submit(() -> {
|
|
|
|
try {
|
|
|
|
FileUtils.forceMkdir(TEMP.toFile());
|
|
|
|
} catch (IOException e) {
|
|
|
|
ErrorEvent.fromThrowable(e).handle();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (Item item : new ArrayList<>(items)) {
|
2023-04-01 02:32:12 +13:00
|
|
|
try {
|
|
|
|
try (var b = new BusyProperty(downloading)) {
|
2023-07-17 13:03:12 +12:00
|
|
|
FileSystemHelper.dropFilesInto(
|
|
|
|
FileSystemHelper.getLocal(TEMP),
|
|
|
|
List.of(item.getFileEntry()),
|
|
|
|
true);
|
2023-04-01 02:32:12 +13:00
|
|
|
}
|
|
|
|
item.finishedDownload.set(true);
|
|
|
|
} catch (Throwable t) {
|
|
|
|
ErrorEvent.fromThrowable(t).handle();
|
2023-06-14 20:54:00 +12:00
|
|
|
items.remove(item);
|
2023-03-31 07:56:18 +13:00
|
|
|
}
|
2023-07-17 13:03:12 +12:00
|
|
|
}
|
|
|
|
allDownloaded.set(true);
|
2023-03-31 07:56:18 +13:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|