diff --git a/src/main/java/com/marklogic/developer/corb/AbstractFileUrisLoader.java b/src/main/java/com/marklogic/developer/corb/AbstractFileUrisLoader.java index 3fc88022..29c3bde8 100644 --- a/src/main/java/com/marklogic/developer/corb/AbstractFileUrisLoader.java +++ b/src/main/java/com/marklogic/developer/corb/AbstractFileUrisLoader.java @@ -25,6 +25,8 @@ import java.io.IOException; import java.io.InputStream; import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; import java.nio.file.attribute.FileTime; import java.time.Instant; import java.time.ZoneOffset; @@ -113,7 +115,13 @@ protected Document toLoaderDoc(Map metadata, Node content, boole protected Map getMetadata(File file) throws IOException { Map metadata = new HashMap<>(); metadata.put(META_FILENAME, file.getName()); - metadata.put(META_PATH, file.getCanonicalPath()); + metadata.put(META_PATH, getMetaPath(file)); + + String loaderPath = getLoaderPath(); + if (StringUtils.isNotBlank(loaderPath)) { + metadata.put(META_SOURCE, loaderPath); + } + String lastModified = this.toISODateTime(file.lastModified()); if (StringUtils.isNotBlank(lastModified)) { metadata.put(META_LAST_MODIFIED, lastModified); @@ -125,6 +133,19 @@ protected Map getMetadata(File file) throws IOException { return metadata; } + protected String getMetaPath(File file) throws IOException { + String path = file.getCanonicalPath(); + String loaderPath = getLoaderPath(); + + if (StringUtils.isNotBlank(loaderPath)) { + String loaderPathCanonicalPath = Paths.get(loaderPath).toFile().getCanonicalPath(); + if (path.startsWith(loaderPathCanonicalPath)) { + path = path.substring(loaderPathCanonicalPath.length() + 1); + } + } + return path; + } + protected String toISODateTime(FileTime fileTime) { return toISODateTime(fileTime.toInstant()); } diff --git a/src/main/java/com/marklogic/developer/corb/FileUrisDirectoryLoader.java b/src/main/java/com/marklogic/developer/corb/FileUrisDirectoryLoader.java index 2ca93a9f..c42d605e 100644 --- a/src/main/java/com/marklogic/developer/corb/FileUrisDirectoryLoader.java +++ b/src/main/java/com/marklogic/developer/corb/FileUrisDirectoryLoader.java @@ -53,7 +53,7 @@ public void open() throws CorbException { try { if (shouldSetBatchRef()) { - batchRef = dir.toFile().getCanonicalPath(); + batchRef = file.getCanonicalPath(); } fileStream = Files.walk(dir); fileIterator = fileStream.filter(this::accept).iterator(); diff --git a/src/test/java/com/marklogic/developer/corb/FileUrisDirectoryLoaderTest.java b/src/test/java/com/marklogic/developer/corb/FileUrisDirectoryLoaderTest.java index 0c82fa93..db78e5e4 100644 --- a/src/test/java/com/marklogic/developer/corb/FileUrisDirectoryLoaderTest.java +++ b/src/test/java/com/marklogic/developer/corb/FileUrisDirectoryLoaderTest.java @@ -18,6 +18,8 @@ */ package com.marklogic.developer.corb; +import java.io.File; +import java.io.IOException; import java.nio.file.Path; import java.nio.file.Paths; import java.util.Properties; @@ -80,4 +82,31 @@ public void testCloseWhenNotOpen() { } } + @Test + public void testGetMetaFilename() { + Properties properties = new Properties(); + File loaderDir = new File("/var/tmp"); + try (FileUrisDirectoryLoader loader = new FileUrisDirectoryLoader()) { + properties.setProperty(Options.LOADER_PATH, loaderDir.getCanonicalPath()); + loader.setProperties(properties); + File file = new File("/var/tmp/foo/bar.txt"); + String path = loader.getMetaPath(file); + assertEquals("foo/bar.txt", path); + assertNotEquals(file.getCanonicalPath(), path); + } catch (IOException ex) { + fail(); + } + } + + @Test + public void testGetMetaFilenameWithoutLoaderPath() { + File file = new File("/var/tmp/foo/bar.txt"); + try (FileUrisDirectoryLoader loader = new FileUrisDirectoryLoader()) { + String path = loader.getMetaPath(file); + assertEquals(file.getCanonicalPath(), path); + } catch (IOException ex) { + fail(); + } + } + }