xpipe/app/src/main/java/io/xpipe/app/browser/BrowserTransferModel.java

67 lines
2.2 KiB
Java
Raw Normal View History

2023-03-31 07:56:18 +13:00
package io.xpipe.app.browser;
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;
import java.nio.file.Path;
import java.util.List;
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");
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 {
String name;
2023-03-31 07:56:18 +13:00
FileSystem.FileEntry fileEntry;
Path localFile;
BooleanProperty finishedDownload = new SimpleBooleanProperty();
}
ObservableList<Item> items = FXCollections.observableArrayList();
BooleanProperty downloading = new SimpleBooleanProperty();
public void drop(List<FileSystem.FileEntry> entries) {
entries.forEach(entry -> {
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);
executor.submit(() -> {
try {
2023-04-01 04:23:58 +13:00
FileUtils.forceMkdir(TEMP.toFile());
try (var b = new BusyProperty(downloading)) {
FileSystemHelper.dropFilesInto(FileSystemHelper.getLocal(TEMP), List.of(entry), false);
}
item.finishedDownload.set(true);
} catch (Throwable t) {
ErrorEvent.fromThrowable(t).handle();
2023-03-31 07:56:18 +13:00
}
});
});
}
}