diff --git a/experiment/src/org/labkey/experiment/DataURLRelativizer.java b/experiment/src/org/labkey/experiment/DataURLRelativizer.java index 0c75bbb58dc..341668d4a63 100644 --- a/experiment/src/org/labkey/experiment/DataURLRelativizer.java +++ b/experiment/src/org/labkey/experiment/DataURLRelativizer.java @@ -22,6 +22,7 @@ import org.labkey.api.security.User; import org.labkey.api.util.FileUtil; import org.labkey.api.view.ActionURL; +import org.labkey.experiment.api.ExperimentServiceImpl; import org.labkey.experiment.controllers.exp.ExperimentController; import java.io.IOException; @@ -75,21 +76,16 @@ public URLRewriter createURLRewriter() @Override public String rewriteURL(Path path, ExpData data, String roleName, ExpRun expRun, User user, String rootFilePath) throws ExperimentException { - try - { - if (path == null) - return null; + if (path == null) + return null; - if (expRun == null || expRun.getFilePathRoot() == null) - { - return FileUtil.pathToString(path); - } - return FileUtil.relativizeUnix(expRun.getFilePathRootPath(), path, false); - } - catch (IOException e) + if (expRun == null || expRun.getFilePathRoot() == null) { - throw new ExperimentException(e); + return FileUtil.pathToString(path); } + + // NOTE: this will only write a relative path if the file is a direct descendant of the root. + return ExperimentServiceImpl.get().generatePathStringRelativeToRootIfUnderRoot(path, expRun.getFilePathRootPath()); } }; } diff --git a/experiment/src/org/labkey/experiment/api/ExperimentServiceImpl.java b/experiment/src/org/labkey/experiment/api/ExperimentServiceImpl.java index 705e05c82d6..f819696469c 100644 --- a/experiment/src/org/labkey/experiment/api/ExperimentServiceImpl.java +++ b/experiment/src/org/labkey/experiment/api/ExperimentServiceImpl.java @@ -768,33 +768,10 @@ public ExpDataImpl createData(URI uri, XarSource source) throws XarFormatExcepti String[] parts = pathStr.split("/"); String name = FileUtil.decodeSpaces(parts[parts.length - 1]); Path path = FileUtil.getPath(source.getXarContext().getContainer(), uri); - if (path != null) { - try - { - path = FileUtil.stringToPath(source.getXarContext().getContainer(), - source.getCanonicalDataFileURL(FileUtil.pathToString(path))); - - // Only convert to a relative path if this is a descendant of the root: - path = path.normalize(); - if (URIUtil.isDescendant(source.getRootPath().toUri(), path.toUri())) - { - pathStr = FileUtil.relativizeUnix(source.getRootPath(), path, false); - } - else - { - pathStr = FileUtil.pathToString(path); - } - } - catch (IOException e) - { - pathStr = FileUtil.pathToString(path); - } - } - else - { - pathStr = FileUtil.uriToString(uri); + path = FileUtil.stringToPath(source.getXarContext().getContainer(), source.getCanonicalDataFileURL(FileUtil.pathToString(path))); + pathStr = generatePathStringRelativeToRootIfUnderRoot(path, source.getRootPath()); } Lsid.LsidBuilder lsid = new Lsid.LsidBuilder(LsidUtils.resolveLsidFromTemplate(AutoFileLSIDReplacer.AUTO_FILE_LSID_SUBSTITUTION, source.getXarContext(), "Data", new AutoFileLSIDReplacer(pathStr, source.getXarContext().getContainer(), source))); @@ -816,6 +793,30 @@ public ExpDataImpl createData(URI uri, XarSource source) throws XarFormatExcepti return data; } + public String generatePathStringRelativeToRootIfUnderRoot(@NotNull Path path, @Nullable Path pipeRootPath) + { + String pathStr; + try + { + // Only convert to a relative path if this is a descendant of the root: + path = path.normalize(); + if (pipeRootPath != null && URIUtil.isDescendant(pipeRootPath.toUri(), path.toUri())) + { + pathStr = FileUtil.relativizeUnix(pipeRootPath, path, false); + } + else + { + pathStr = FileUtil.pathToString(path); + } + } + catch (IOException e) + { + pathStr = FileUtil.pathToString(path); + } + + return pathStr; + } + @Override public ExpDataImpl createData(Container container, @NotNull DataType type) {