1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.argeo.cms.ui.workbench.internal.jcr.commands;
17
18 import java.io.InputStream;
19 import java.lang.reflect.Method;
20 import java.net.URI;
21 import java.nio.file.Files;
22 import java.nio.file.Path;
23 import java.nio.file.Paths;
24
25 import javax.jcr.Binary;
26 import javax.jcr.Node;
27 import javax.jcr.Property;
28 import javax.jcr.nodetype.NodeType;
29
30 import org.argeo.cms.ui.jcr.model.SingleJcrNodeElem;
31 import org.argeo.cms.ui.jcr.model.WorkspaceElem;
32 import org.argeo.cms.ui.workbench.WorkbenchUiPlugin;
33 import org.argeo.cms.ui.workbench.jcr.JcrBrowserView;
34 import org.argeo.eclipse.ui.EclipseUiUtils;
35 import org.argeo.eclipse.ui.TreeParent;
36 import org.argeo.eclipse.ui.dialogs.ErrorFeedback;
37 import org.eclipse.core.commands.AbstractHandler;
38 import org.eclipse.core.commands.ExecutionEvent;
39 import org.eclipse.core.commands.ExecutionException;
40 import org.eclipse.jface.dialogs.MessageDialog;
41 import org.eclipse.jface.viewers.ISelection;
42 import org.eclipse.jface.viewers.IStructuredSelection;
43 import org.eclipse.swt.SWT;
44 import org.eclipse.swt.widgets.FileDialog;
45 import org.eclipse.ui.handlers.HandlerUtil;
46
47
48 public class UploadFiles extends AbstractHandler {
49
50
51 public Object execute(ExecutionEvent event) throws ExecutionException {
52
53 ISelection selection = HandlerUtil.getActiveWorkbenchWindow(event).getActivePage().getSelection();
54 JcrBrowserView view = (JcrBrowserView) HandlerUtil.getActiveWorkbenchWindow(event).getActivePage()
55 .findView(HandlerUtil.getActivePartId(event));
56 if (selection != null && !selection.isEmpty() && selection instanceof IStructuredSelection) {
57 Object obj = ((IStructuredSelection) selection).getFirstElement();
58 try {
59 Node folder = null;
60 if (obj instanceof SingleJcrNodeElem) {
61 folder = ((SingleJcrNodeElem) obj).getNode();
62 } else if (obj instanceof WorkspaceElem) {
63 folder = ((WorkspaceElem) obj).getRootNode();
64 } else {
65 ErrorFeedback.show(WorkbenchUiPlugin.getMessage("warningInvalidNodeToImport"));
66 }
67 if (folder != null) {
68 FileDialog dialog = new FileDialog(HandlerUtil.getActiveShell(event), SWT.MULTI);
69 dialog.setText("Choose one or more files to upload");
70
71 if (EclipseUiUtils.notEmpty(dialog.open())) {
72 String[] names = dialog.getFileNames();
73
74
75
76
77
78 Method filterMethod = null;
79 Path parPath = null;
80
81 try {
82 filterMethod = dialog.getClass().getDeclaredMethod("getFilterPath");
83 String filterPath = (String) filterMethod.invoke(dialog);
84 parPath = Paths.get(filterPath);
85 } catch (NoSuchMethodException nsme) {
86 }
87 if (names.length == 0)
88 return null;
89 else {
90 loop: for (String name : names) {
91 Path path = Paths.get(name);
92 if (parPath != null)
93 path = parPath.resolve(path);
94 if (Files.exists(path)) {
95 URI uri = path.toUri();
96 String uriStr = uri.toString();
97 System.out.println(uriStr);
98
99 if (Files.isDirectory(path)) {
100 MessageDialog.openError(HandlerUtil.getActiveShell(event),
101 "Unimplemented directory import",
102 "Upload of directories in the system is not yet implemented");
103 continue loop;
104 }
105 Node fileNode = folder.addNode(path.getFileName().toString(), NodeType.NT_FILE);
106 Node resNode = fileNode.addNode(Property.JCR_CONTENT, NodeType.NT_RESOURCE);
107 Binary binary = null;
108 try (InputStream is = Files.newInputStream(path)) {
109 binary = folder.getSession().getValueFactory().createBinary(is);
110 resNode.setProperty(Property.JCR_DATA, binary);
111 }
112 folder.getSession().save();
113 } else {
114 String msg = "Cannot upload file at " + path.toString();
115 if (parPath != null)
116 msg += "\nPlease remember that file upload fails when choosing files from the \"Recently Used\" bookmarks on some OS";
117 MessageDialog.openError(HandlerUtil.getActiveShell(event), "Missing file", msg);
118 continue loop;
119 }
120 }
121 view.nodeAdded((TreeParent) obj);
122 return true;
123 }
124 }
125 }
126 } catch (Exception e) {
127 ErrorFeedback.show("Cannot import files to " + obj, e);
128 }
129 }
130 return null;
131 }
132 }