1 package org.simantics.fileimport.scl;
3 import java.io.IOException;
4 import java.nio.file.DirectoryStream;
5 import java.nio.file.Files;
6 import java.nio.file.Path;
7 import java.util.HashMap;
8 import java.util.Iterator;
10 import java.util.Optional;
12 import org.simantics.Simantics;
13 import org.simantics.databoard.util.Base64;
14 import org.simantics.db.ReadGraph;
15 import org.simantics.db.Resource;
16 import org.simantics.db.common.request.UniqueRead;
17 import org.simantics.db.exception.DatabaseException;
18 import org.simantics.db.service.SerialisationSupport;
19 import org.simantics.fileimport.Activator;
20 import org.simantics.fileimport.FileImportService;
21 import org.simantics.fileimport.dropins.FileImportDropins;
22 import org.simantics.layer0.Layer0;
23 import org.simantics.utils.FileUtils;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
28 * SCL interface for Simantics File Import Functionality
29 * See <code>"Dropins/Core"</code> SCL module for more
31 * @author Jani Simomaa
34 public class DropinsSCL {
36 private static final Logger LOGGER = LoggerFactory.getLogger(DropinsSCL.class);
38 public static void watchDropinsFolder() {
39 FileImportDropins.watchDropinsFolder();
42 public static void unwatchDropinsFolder() {
43 FileImportDropins.unwatchDropinsFolder();
46 public static void uploadToDropinsBase64(String base64, String fileName) {
47 // ensure that watcher is awake
48 FileImportDropins.watchDropinsFolder();
49 Path rootFolder = null;
51 rootFolder = Activator.getDropinsFolder();
52 Path newFile = rootFolder.resolve(fileName);
53 if (Files.exists(newFile)) {
54 newFile = findFreshFileName(rootFolder, fileName);
56 byte[] bytes = Base64.decode(base64);
57 FileUtils.writeFile(newFile.toFile(), bytes);
58 } catch (IOException e) {
59 LOGGER.error("Could not upload base64 to file " + (rootFolder != null ? rootFolder.resolve(fileName).toAbsolutePath() : ""), e);
63 private static Path findFreshFileName(Path root, String fileName) throws IOException {
64 int ending = fileName.lastIndexOf('.');
65 String glob = fileName;
68 glob = fileName.substring(0, ending);
69 suffix = fileName.substring(ending);
72 try (DirectoryStream<Path> stream = Files.newDirectoryStream(root, glob)) {
73 Iterator<Path> iter = stream.iterator();
74 while (iter.hasNext()) {
79 String newFileName = glob + "_" + i + suffix;
80 return root.resolve(newFileName);
83 public static Map<String, Long> getUploadedFiles() throws DatabaseException {
84 Map<String, String> results = FileImportService.getPathsAndResources();
85 Map<String, Long> result = Simantics.getSession().syncRequest(new UniqueRead<Map<String, Long>>() {
88 public Map<String, Long> perform(ReadGraph graph) throws DatabaseException {
89 Map<String, Long> map = new HashMap<>();
90 for (Map.Entry<String, String> entry : results.entrySet()) {
91 String value = (String) entry.getValue();
92 Long id = Long.valueOf(value);
93 SerialisationSupport ss = graph.getService(SerialisationSupport.class);
95 Resource r = ss.getResource(id);
96 String name = graph.getRelatedValue(r, Layer0.getInstance(graph).HasName);
98 } catch (DatabaseException e) {
108 public static void removeFileForId(long id) {
109 FileImportService.removeFileForResource(id, Optional.empty());