Skip to content

Commit

Permalink
Merge pull request #1388 from tdrwenski/add-methods-to-mfile
Browse files Browse the repository at this point in the history
Add relativize method to MFile
  • Loading branch information
tdrwenski authored Sep 30, 2024
2 parents 283935e + 3fedc92 commit a87cba1
Show file tree
Hide file tree
Showing 10 changed files with 478 additions and 346 deletions.
8 changes: 8 additions & 0 deletions cdm/core/src/main/java/thredds/filesystem/MFileOS.java
Original file line number Diff line number Diff line change
Expand Up @@ -146,4 +146,12 @@ public File getFile() {
public MFileOS getChild(String newFilename) {
return new MFileOS(new File(file, newFilename));
}

@Override
public String relativize(MFile other) {
if (other instanceof MFileOS) {
return file.toPath().relativize(((MFileOS) other).getFile().toPath()).toString();
}
throw new IllegalArgumentException("Cannot relativize " + other + " against " + this);
}
}
8 changes: 8 additions & 0 deletions cdm/core/src/main/java/thredds/filesystem/MFileOS7.java
Original file line number Diff line number Diff line change
Expand Up @@ -149,4 +149,12 @@ public MFileOS7 getChild(String newFilename) {
public Path getNioPath() {
return path;
}

@Override
public String relativize(MFile other) {
if (other instanceof MFileOS7) {
return path.relativize(((MFileOS7) other).path).toString();
}
throw new IllegalArgumentException("Cannot relativize " + other + " against " + this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,11 @@ public void writeToStream(OutputStream outputStream, long offset, long maxBytes)
public MFileRemote getChild(String newFilename) {
throw new UnsupportedOperationException("MFileRemote::getChild not implemented. Filename: " + getName());
}

@Override
public String relativize(MFile other) {
throw new UnsupportedOperationException("MFileRemote::relativize not implemented. Filename: " + getName());
}
}

///////////////////////////////
Expand Down
8 changes: 8 additions & 0 deletions cdm/core/src/main/java/thredds/inventory/MFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,12 @@ default boolean isReadable() {
*/
@Nullable
MFile getChild(String newFileName);

/**
* Construct a relative path between this MFile and a given MFile.
*
* @param other the MFile to relativize against this MFile's path
* @return the resulting relative path as a String, or an empty path if both paths are equal
*/
String relativize(MFile other);
}
8 changes: 8 additions & 0 deletions cdm/core/src/test/java/thredds/filesystem/TestMFileOS.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,14 @@ public void shouldGetChildMFile() {
assertThat(newMFile.getParent().getPath()).isEqualTo(mFile.getPath());
assertThat(newMFile.getPath()).isEqualTo(Paths.get(mFile.getPath(), "newFile").toString());
}

@Test
public void shouldGetRelativePath() {
final MFileOS mFile = new MFileOS("/an/absolute/path/a/");
final MFileOS mFile2 = new MFileOS("/an/absolute/path/foo/bar/");
assertThat(mFile.relativize(mFile2)).isEqualTo("../foo/bar");
assertThat(mFile2.relativize(mFile)).isEqualTo("../../a");
}
}

private static File createTemporaryFile(int size) throws IOException {
Expand Down
8 changes: 8 additions & 0 deletions cdm/core/src/test/java/thredds/filesystem/TestMFileOS7.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,14 @@ public void shouldGetInputStream() throws IOException {
assertThat(inputStream.read()).isNotEqualTo(-1);
}
}

@Test
public void shouldGetRelativePath() throws IOException {
final MFileOS7 mFile = new MFileOS7(Paths.get("/an/absolute/path"), null);
final MFileOS7 mFile2 = new MFileOS7(Paths.get("/an/absolute/path/foo"), null);
assertThat(mFile.relativize(mFile2)).isEqualTo("foo");
assertThat(mFile2.relativize(mFile)).isEqualTo("..");
}
}

private static File createTemporaryFile(int size) throws IOException {
Expand Down
26 changes: 26 additions & 0 deletions cdm/s3/src/main/java/thredds/inventory/s3/MFileS3.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.io.IOException;
import java.io.OutputStream;
import java.net.URISyntaxException;
import java.nio.file.Paths;
import java.util.Objects;
import java.util.function.Supplier;
import javax.annotation.Nonnull;
Expand Down Expand Up @@ -382,6 +383,31 @@ public MFileS3 getChild(String newFilename) {
}
}

/**
* Construct a relative key path (as if it were a file path) between this MFile and a given MFile.
* They must have same delimiter and bucket, otherwise the path returned is empty.
*
* @param other the MFile to relativize against this MFile's path
* @return the resulting relative path as a String, or an empty path if both paths are equal
*/
@Override
public String relativize(MFile other) {
if (!(other instanceof MFileS3)) {
throw new IllegalArgumentException("Cannot relativize " + other + " against " + this);
}
final MFileS3 otherS3 = (MFileS3) other;

if (getDelimiter() != null && getDelimiter().equals("/") && getDelimiter().equals(otherS3.getDelimiter())
&& cdmS3Uri.getBucket().equals(otherS3.cdmS3Uri.getBucket())) {
final String key = getKey();
final String otherKey = otherS3.getKey();
return key == null || otherKey == null ? ""
: Paths.get("/" + key).relativize(Paths.get("/" + otherKey)).toString();
}

return "";
}

public static class Provider implements MFileProvider {

private static String protocol = CdmS3Uri.SCHEME_CDM_S3;
Expand Down
Loading

0 comments on commit a87cba1

Please sign in to comment.