diff --git a/gse-app/src/main/java/com/powsybl/gse/app/ProjectPane.java b/gse-app/src/main/java/com/powsybl/gse/app/ProjectPane.java index 4449171d..a7655e68 100644 --- a/gse-app/src/main/java/com/powsybl/gse/app/ProjectPane.java +++ b/gse-app/src/main/java/com/powsybl/gse/app/ProjectPane.java @@ -12,6 +12,7 @@ import com.powsybl.afs.*; import com.powsybl.afs.storage.AppStorage; import com.powsybl.afs.storage.NodeInfo; +import com.powsybl.afs.storage.Utils; import com.powsybl.afs.storage.events.*; import com.powsybl.commons.util.ServiceLoaderCache; import com.powsybl.gse.copy_paste.afs.CopyManager; @@ -41,6 +42,7 @@ import javafx.scene.layout.StackPane; import javafx.scene.paint.Color; import javafx.stage.DirectoryChooser; +import javafx.stage.FileChooser; import javafx.stage.Stage; import javafx.util.Callback; import javafx.util.Pair; @@ -48,6 +50,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.io.IOException; import java.lang.reflect.Field; import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; @@ -85,7 +88,6 @@ public class ProjectPane extends Tab { private final TaskItemList taskItems; private final TaskMonitorPane taskMonitorPane; private final Map lCache = new HashMap<>(); - private final CopyManager localArchiveManager = CopyManager.getInstance(); private final AppStorageListener appStorageListener; public ProjectPane(Scene scene, Project project, GseContext context) { @@ -721,9 +723,12 @@ private void archive(AbstractNodeBase item) { context.getExecutor().execute(() -> { TaskMonitor.Task task = project.getFileSystem().getTaskMonitor().startTask(String.format(RESOURCE_BUNDLE.getString("ArchiveTask"), selectedDirectory.getName()), project); try { - localArchiveManager.copy(Collections.singletonList(item), selectedDirectory); - } catch (CopyPasteException e) { + Utils.checkDiskSpace(selectedDirectory.toPath()); + item.archive(selectedDirectory.toPath(), true); + LOGGER.info("Archiving node {} ({}) is complete", item.getName(), item.getId()); + } catch (AfsException | IOException e) { GseAlerts.showDialogError(e.getMessage()); + LOGGER.error("Archiving has failed for node {}", item.getId(), e); } finally { project.getFileSystem().getTaskMonitor().stopTask(task.getId()); } @@ -737,13 +742,14 @@ private void unarchiveItems(TreeItem folderItem) { throw new IllegalStateException("Can't unarchive item if target is not a project folder!"); } - DirectoryChooser directoryChooser = new DirectoryChooser(); - java.io.File selectedDirectory = directoryChooser.showDialog(getContent().getScene().getWindow()); - if (selectedDirectory != null) { + FileChooser fileChooser = new FileChooser(); + fileChooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("ZIP", "*.zip")); + java.io.File selectedFile = fileChooser.showOpenDialog(getContent().getScene().getWindow()); + if (selectedFile != null) { context.getExecutor().execute(() -> { - TaskMonitor.Task task = project.getFileSystem().getTaskMonitor().startTask(String.format(RESOURCE_BUNDLE.getString("UnarchiveTask"), selectedDirectory.getName()), project); + TaskMonitor.Task task = project.getFileSystem().getTaskMonitor().startTask(String.format(RESOURCE_BUNDLE.getString("UnarchiveTask"), selectedFile.getName()), project); try { - ((ProjectFolder) folder).unarchive(selectedDirectory.toPath()); + ((ProjectFolder) folder).unarchive(selectedFile.toPath(), true); Platform.runLater(() -> refresh(folderItem)); } catch (Exception e) { Platform.runLater(() -> GseAlerts.showDialogError(e.getMessage())); diff --git a/gse-copy-paste-afs/src/main/java/com/powsybl/gse/copy_paste/afs/CopyManager.java b/gse-copy-paste-afs/src/main/java/com/powsybl/gse/copy_paste/afs/CopyManager.java index 45897005..4673574f 100644 --- a/gse-copy-paste-afs/src/main/java/com/powsybl/gse/copy_paste/afs/CopyManager.java +++ b/gse-copy-paste-afs/src/main/java/com/powsybl/gse/copy_paste/afs/CopyManager.java @@ -7,6 +7,7 @@ package com.powsybl.gse.copy_paste.afs; import com.powsybl.afs.*; +import com.powsybl.afs.storage.Utils; import com.powsybl.commons.util.ServiceLoaderCache; import com.powsybl.computation.local.LocalComputationManager; import com.powsybl.gse.copy_paste.afs.exceptions.*; @@ -37,7 +38,6 @@ public final class CopyManager { private static final String NODE_COPY_TYPE = "@NODE@"; private static final String COPY_SIGNATURE = "@COPY_SIGNATURE@"; private static final long COPY_EXPIRATION_TIME = 6; - private static final long MIN_DISK_SPACE_THRESHOLD = 10; private static final Logger LOGGER = LoggerFactory.getLogger(CopyManager.class); private static final String TEMP_DIR_PREFIX = "powsybl_node_export"; private static final long CLEANUP_DELAY = 36000; @@ -105,14 +105,6 @@ public Map copy(List nodes, @Nulla copyInfo.nodeId = node.getId(); copyInfo.node = node; copyInfo.archivePath = resolveArchiveTargetDirectory(targetDirectory); - - File archiveFile = copyInfo.archivePath.toFile(); - long freeSpacePercent = 100 * archiveFile.getFreeSpace() / archiveFile.getTotalSpace(); - LOGGER.info("Copying into drive with {}% free space", freeSpacePercent); - if (freeSpacePercent < MIN_DISK_SPACE_THRESHOLD) { - throw new CopyPasteException("Not enough space"); - } - copyInfo.expirationDate = ZonedDateTime.now().plusHours(COPY_EXPIRATION_TIME); currentCopies.put(copyInfo.nodeId, copyInfo); @@ -120,6 +112,7 @@ public Map copy(List nodes, @Nulla LOGGER.info("Copying (archiving) node {} ({})", node.getName(), node.getId()); logger.accept(String.format("Copying node %s", copyInfo.getNode().getName())); try { + Utils.checkDiskSpace(copyInfo.archivePath); node.archive(copyInfo.archivePath); copyInfo.archiveSuccess = true; LOGGER.info("Copying (archiving) node {} ({}) is complete", node.getName(), node.getId()); @@ -276,7 +269,7 @@ private String renameAndPaste(AbstractNodeBase folder, List extends GridP private boolean success; private Set openedProjects = new HashSet<>(); private SimpleBooleanProperty deleteMenuItemDisableProperty = new SimpleBooleanProperty(false); - private final CopyManager localArchiveManager = CopyManager.getInstance(); private BooleanProperty copied = new SimpleBooleanProperty(false); @@ -691,11 +688,14 @@ private void archive(AbstractNodeBase item) { context.getExecutor().execute(() -> { InfoDialog infoDialog = new InfoDialog(String.format(RESOURCE_BUNDLE.getString("ArchiveTask"), item.getName()), true); try { - localArchiveManager.copy(Collections.singletonList(item), selectedDirectory); + Utils.checkDiskSpace(selectedDirectory.toPath()); + item.archive(selectedDirectory.toPath(), true); infoDialog.updateStage(RESOURCE_BUNDLE.getString("CompleteTask")); - } catch (CopyPasteException e) { + LOGGER.info("Archiving node {} ({}) is complete", item.getName(), item.getId()); + } catch (AfsException | IOException e) { Platform.runLater(() -> GseAlerts.showDialogError(e.getMessage())); infoDialog.updateStage(RESOURCE_BUNDLE.getString("ErrorTask"), Color.RED); + LOGGER.error("Archiving has failed for node {}", item.getId(), e); } }); } @@ -707,13 +707,14 @@ private void unarchiveItems(TreeItem folderItem) { throw new IllegalStateException("Can't unarchive item if target is not a folder!"); } - DirectoryChooser directoryChooser = new DirectoryChooser(); - java.io.File selectedDirectory = directoryChooser.showDialog(window); - if (selectedDirectory != null) { + FileChooser fileChooser = new FileChooser(); + fileChooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("ZIP", "*.zip")); + java.io.File selectedFile = fileChooser.showOpenDialog(window); + if (selectedFile != null) { context.getExecutor().execute(() -> { - InfoDialog infoDialog = new InfoDialog(String.format(RESOURCE_BUNDLE.getString("UnarchiveTask"), selectedDirectory.getName()), true); + InfoDialog infoDialog = new InfoDialog(String.format(RESOURCE_BUNDLE.getString("UnarchiveTask"), selectedFile.getName()), true); try { - ((Folder) folder).unarchive(selectedDirectory.toPath()); + ((Folder) folder).unarchive(selectedFile.toPath(), true); infoDialog.updateStage(RESOURCE_BUNDLE.getString("CompleteTask")); Platform.runLater(() -> refresh(folderItem)); } catch (Exception e) {