X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.fileimport%2Fsrc%2Forg%2Fsimantics%2Ffileimport%2Fscl%2FDropinsSCL.java;h=d3fa3d0f9b6b14302e37ee4bef804d174a57e06e;hp=0758d3e784ef735ad98471e0b5e34cafde33f748;hb=873afce76b92cb7cf9094fe60e407278f220e5ed;hpb=b35573372259ace60d8827766fe41443f4c57629 diff --git a/bundles/org.simantics.fileimport/src/org/simantics/fileimport/scl/DropinsSCL.java b/bundles/org.simantics.fileimport/src/org/simantics/fileimport/scl/DropinsSCL.java index 0758d3e78..d3fa3d0f9 100644 --- a/bundles/org.simantics.fileimport/src/org/simantics/fileimport/scl/DropinsSCL.java +++ b/bundles/org.simantics.fileimport/src/org/simantics/fileimport/scl/DropinsSCL.java @@ -1,37 +1,107 @@ package org.simantics.fileimport.scl; import java.io.IOException; +import java.nio.file.DirectoryStream; +import java.nio.file.Files; import java.nio.file.Path; +import java.util.HashMap; +import java.util.Iterator; import java.util.Map; import java.util.Optional; +import org.simantics.Simantics; import org.simantics.databoard.util.Base64; +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.common.request.UniqueRead; +import org.simantics.db.exception.DatabaseException; +import org.simantics.db.service.SerialisationSupport; import org.simantics.fileimport.Activator; import org.simantics.fileimport.FileImportService; import org.simantics.fileimport.dropins.FileImportDropins; +import org.simantics.layer0.Layer0; import org.simantics.utils.FileUtils; +/** + * SCL interface for Simantics File Import Functionality + * See "Dropins/Core" SCL module for more + * + * @author Jani Simomaa + * + */ public class DropinsSCL { + public static void watchDropinsFolder() { + FileImportDropins.watchDropinsFolder(); + } + + public static void unwatchDropinsFolder() { + FileImportDropins.unwatchDropinsFolder(); + } + public static void uploadToDropinsBase64(String base64, String fileName) { // ensure that watcher is awake FileImportDropins.watchDropinsFolder(); try { Path rootFolder = Activator.getDropinsFolder(); + Path newFile = rootFolder.resolve(fileName); + if (Files.exists(newFile)) { + newFile = findFreshFileName(rootFolder, fileName); + } byte[] bytes = Base64.decode(base64); - FileUtils.writeFile(rootFolder.resolve(fileName).toFile(), bytes); - + FileUtils.writeFile(newFile.toFile(), bytes); } catch (IOException e) { e.printStackTrace(); } } - - public static Map getUploadedFiles() { - return FileImportService.getPathsAndResources(); + + private static Path findFreshFileName(Path root, String fileName) throws IOException { + int ending = fileName.lastIndexOf('.'); + String glob = fileName; + String suffix = ""; + if (ending > -1) { + glob = fileName.substring(0, ending); + suffix = fileName.substring(ending); + } + int i = 0; + try (DirectoryStream stream = Files.newDirectoryStream(root, glob)) { + Iterator iter = stream.iterator(); + while (iter.hasNext()) { + iter.next(); + i++; + } + } + String newFileName = glob + "_" + i + suffix; + return root.resolve(newFileName); + } + + public static Map getUploadedFiles() throws DatabaseException { + Map results = FileImportService.getPathsAndResources(); + Map result = Simantics.getSession().syncRequest(new UniqueRead>() { + + @Override + public Map perform(ReadGraph graph) throws DatabaseException { + Map map = new HashMap<>(); + for (Map.Entry entry : results.entrySet()) { + String value = (String) entry.getValue(); + Long id = Long.valueOf(value); + SerialisationSupport ss = graph.getService(SerialisationSupport.class); + try { + Resource r = ss.getResource(id); + String name = graph.getRelatedValue(r, Layer0.getInstance(graph).HasName); + map.put(name, id); + } catch (DatabaseException e) { + e.printStackTrace(); + } + } + return map; + } + }); + return result; } - + public static void removeFileForId(long id) { FileImportService.removeFileForResource(id, Optional.empty()); } - + }