mirror of https://github.com/micromata/borgbackup-butler.git

Kai Reinhard
15.39.2021 d80a4b3ab35af575fd06d4e2fb9a3726418c6727
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package de.micromata.borgbutler.server.rest;
 
import de.micromata.borgbutler.json.JsonUtils;
import de.micromata.borgbutler.server.RunningMode;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
import javax.servlet.http.HttpServletRequest;
import javax.swing.*;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import java.awt.*;
import java.io.File;
 
@Path("/files")
public class FilesystemBrowserRest {
    private Logger log = LoggerFactory.getLogger(FilesystemBrowserRest.class);
 
    /**
     * Opens a directory browser or file browser on the desktop app and returns the chosen dir/file. Works only if Browser and Desktop app are running
     * on the same host.
     *
     * @param current The current path of file. If not given the directory/file browser starts with the last used directory or user.home.
     * @return The chosen directory path (absolute path).
     */
    @GET
    @Path("/browse-local-filesystem")
    @Produces(MediaType.APPLICATION_JSON)
    public String browseLocalFilesystem(@Context HttpServletRequest requestContext, @QueryParam("current") String current) {
        String msg = RestUtils.checkLocalDesktopAvailable(requestContext);
        if (msg != null) {
            log.info(msg);
            return msg;
        }
        if (fileDialog != null || fileChooser != null) {
            log.warn("Cannot call already opened file choose twice. Close file chooser first.");
            return "{\"directory\": \"\"}";
        }
        File file = null;
        synchronized (FilesystemBrowserRest.class) {
            if (frame == null) {
                frame = new JFrame("BorgButler");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setSize(300, 100);
                frame.setResizable(false);
                frame.setLocationRelativeTo(null);
                frame.setBackground(Color.WHITE);
                frame.getContentPane().setBackground(Color.WHITE);
                JLabel label = new JLabel("Click for choosing directory...", SwingConstants.CENTER);
                frame.add(label);
            }
            if (RunningMode.getOSType() == RunningMode.OSType.MAC_OS) {
                // The JFileChooser will hang after several calls, use AWT file dialog instead for Mac OS:
                System.setProperty("apple.awt.fileDialogForDirectories", "true");
                frame.setAlwaysOnTop(true);
                frame.setVisible(true);
                try {
                    fileDialog = new FileDialog(frame, "Choose a directory", FileDialog.LOAD);
                    if (StringUtils.isNotBlank(current)) {
                        fileDialog.setDirectory(current);
                    }
                    fileDialog.toFront();
                    fileDialog.setVisible(true);
                    String filename = fileDialog.getFile();
                    String directory = fileDialog.getDirectory();
                    frame.setVisible(false);
                    if (filename == null) {
                        return "";
                    }
                    file = new File(directory, filename);
                    if (!file.isDirectory()) {
                        file = new File(directory);
                    }
                } finally {
                    fileDialog = null;
                }
            } else {
                try {
                    if (StringUtils.isNotBlank(current)) {
                        fileChooser = new JFileChooser(current);
                    } else {
                        fileChooser = new JFileChooser();
                    }
                    fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
                    frame.setVisible(true);
                    frame.setAlwaysOnTop(true);
                    int returnCode = fileChooser.showDialog(frame, "Choose");
                    frame.setVisible(false);
                    frame.setAlwaysOnTop(false);
                    if (returnCode == JFileChooser.APPROVE_OPTION) {
                        file = fileChooser.getSelectedFile();
                    }
                } finally {
                    fileChooser = null;
                }
            }
        }
        String filename = file != null ? JsonUtils.toJson(file.getAbsolutePath()) : "";
        String result = "{\"directory\":\"" + filename + "\"}";
        return result;
    }
 
    /**
     * @return OK, if the local desktop services such as open file browser etc. are available.
     */
    @GET
    @Path("/local-fileservices-available")
    @Produces(MediaType.TEXT_PLAIN)
    public String browseLocalFilesystem(@Context HttpServletRequest requestContext) {
        String msg = RestUtils.checkLocalDesktopAvailable(requestContext);
        if (msg != null) {
            log.info(msg);
            return msg;
        }
        return "OK";
    }
 
    private static JFrame frame;
    private static FileDialog fileDialog;
    private static JFileChooser fileChooser;
}