Search in sources :

Example 1 with CommandExecutor

use of com.vaklinov.zcashui.CommandExecutor in project zencash-swing-wallet-ui by ZencashOfficial.

the class IPFSWrapper method shareFileViaIPFS.

// Returns null or [name](link)
public String shareFileViaIPFS() throws IOException, InterruptedException {
    JFileChooser fileChooser = new JFileChooser();
    fileChooser.setDialogTitle("Share file via IPFS...");
    fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
    int result = fileChooser.showOpenDialog(this.parentFrame);
    if (result != JFileChooser.APPROVE_OPTION) {
        return null;
    }
    File f = fileChooser.getSelectedFile();
    Cursor oldCursor = this.parentFrame.getCursor();
    try {
        this.parentFrame.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
        Log.info("Sharing file: {0}", f.getCanonicalPath());
        if (!this.ensureIPFSIsRunning()) {
            return null;
        }
        String ipfs = this.getIPFSFullExecutablePath();
        String pathParameter = ZCashClientCaller.wrapStringParameter(f.getCanonicalPath());
        CommandExecutor exec = new CommandExecutor(new String[] { ipfs, "add", "--quieter", pathParameter });
        String strResponse = exec.execute().trim();
        Log.info("IPFS hash of added file is: " + strResponse);
        // TODO: add via HTTP to some public writable IPFS gateway
        // this.uploadIPFSDataViaPost(f, "http://localhost:8080/ipfs/");
        this.parentFrame.setCursor(oldCursor);
        Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
        clipboard.setContents(new StringSelection("http://localhost:8080/ipfs/" + strResponse), null);
        JOptionPane.showMessageDialog(this.parentFrame, "The file " + f.getName() + " has been shared successfully via IPFS. It may be\n" + "reached by other users (who have a local IPFS server running) via IPFS link: \n" + "http://localhost:8080/ipfs/" + strResponse + "\n\n" + "The link has been added to the messaging text box and also copied to the clipboard.\n", "File shared successfully", JOptionPane.INFORMATION_MESSAGE);
        return "[" + f.getName() + "](" + "http://localhost:8080/ipfs/" + strResponse + ")";
    } catch (Exception wce) {
        Log.error("Unexpected error: ", wce);
        JOptionPane.showMessageDialog(this.parentFrame, "An unexpected error occurred while sharing file via IPFS!" + "\n" + wce.getMessage().replace(",", ",\n"), "Error in importing wallet private keys...", JOptionPane.ERROR_MESSAGE);
        return null;
    } finally {
        this.parentFrame.setCursor(oldCursor);
    }
}
Also used : JFileChooser(javax.swing.JFileChooser) CommandExecutor(com.vaklinov.zcashui.CommandExecutor) Clipboard(java.awt.datatransfer.Clipboard) Cursor(java.awt.Cursor) File(java.io.File) URISyntaxException(java.net.URISyntaxException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) ProtocolException(java.net.ProtocolException) StringSelection(java.awt.datatransfer.StringSelection)

Example 2 with CommandExecutor

use of com.vaklinov.zcashui.CommandExecutor in project zencash-swing-wallet-ui by ZencashOfficial.

the class IPFSWrapper method startIPFS.

// true if started OK
private boolean startIPFS() throws IOException, InterruptedException {
    // Warn user if executable and dir are missing!
    File dir = new File(this.getIPFSDirectory());
    if ((!dir.exists()) || (!dir.isDirectory())) {
        JOptionPane.showMessageDialog(this.parentFrame, "The IPFS executables are expected to be found in directory:\n" + dir.getCanonicalPath() + "\n" + "However this directory is missing! IPFS cannot be started!", "IPFS directory is not available", JOptionPane.ERROR_MESSAGE);
        return false;
    }
    File ipfsCmd = new File(this.getIPFSFullExecutablePath());
    if ((!ipfsCmd.exists()) || (!ipfsCmd.isFile())) {
        JOptionPane.showMessageDialog(this.parentFrame, "The IPFS command executable:\n" + ipfsCmd.getCanonicalPath() + "\n" + "needs to be available in order to start an IPFS Server on this PC." + "However this executable file is missing! IPFS cannot be started!", "IPFS executable is not available", JOptionPane.ERROR_MESSAGE);
        return false;
    }
    // Check IPFS config and possibly initialize it
    File userhome = OSUtil.getUserHomeDirectory();
    File ipfsConfig = new File(userhome, ".ipfs" + File.separator + "config");
    if (!ipfsConfig.exists()) {
        Log.info("IPFS configuration file {0} does not exist. IPFS will be initilaized!", ipfsConfig.getCanonicalPath());
        CommandExecutor initilaizer = new CommandExecutor(new String[] { this.getIPFSFullExecutablePath(), "init" });
        String initResponse = initilaizer.execute();
        Log.info("IPFS initilaization messages: {0}", initResponse);
    }
    // Finally start IPFS
    CommandExecutor starter = new CommandExecutor(new String[] { this.getIPFSFullExecutablePath(), "daemon" });
    this.IPFSProcess = starter.startChildProcess();
    // Wait 20 sec to make sure the daemon is started
    // TODO: better way to find out if it is started
    Cursor oldCursor = this.parentFrame.getCursor();
    this.parentFrame.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
    Thread.sleep(20 * 1000);
    this.parentFrame.setCursor(oldCursor);
    Runtime.getRuntime().addShutdownHook(new Thread() {

        public void run() {
            Log.info("Stopping IPFS...");
            try {
                IPFSWrapper.this.IPFSProcess.destroy();
            } catch (Exception bad) {
                Log.error("Couldn't stop IPFS!", bad);
            }
        }
    });
    return true;
}
Also used : CommandExecutor(com.vaklinov.zcashui.CommandExecutor) Cursor(java.awt.Cursor) File(java.io.File) URISyntaxException(java.net.URISyntaxException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) ProtocolException(java.net.ProtocolException)

Aggregations

CommandExecutor (com.vaklinov.zcashui.CommandExecutor)2 Cursor (java.awt.Cursor)2 File (java.io.File)2 IOException (java.io.IOException)2 MalformedURLException (java.net.MalformedURLException)2 ProtocolException (java.net.ProtocolException)2 URISyntaxException (java.net.URISyntaxException)2 Clipboard (java.awt.datatransfer.Clipboard)1 StringSelection (java.awt.datatransfer.StringSelection)1 JFileChooser (javax.swing.JFileChooser)1