Skip to content

Commit

Permalink
use substring-after LOADER-PATH for metadata path
Browse files Browse the repository at this point in the history
Resolves issue #86
  • Loading branch information
hansenmc committed Jun 11, 2019
1 parent 867e8b9 commit 1007d80
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -113,7 +115,13 @@ protected Document toLoaderDoc(Map<String, String> metadata, Node content, boole
protected Map<String, String> getMetadata(File file) throws IOException {
Map<String, String> 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);
Expand All @@ -125,6 +133,19 @@ protected Map<String, String> 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());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
}

}

0 comments on commit 1007d80

Please sign in to comment.