From 785c4b02c06f4547fc8cc505f329aeb75e394cd8 Mon Sep 17 00:00:00 2001 From: bbimber Date: Fri, 12 Dec 2025 09:57:18 -0800 Subject: [PATCH 1/2] Do not relativize ExpData URIs when making the LSID unless under file root --- .../experiment/api/ExperimentServiceImpl.java | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/experiment/src/org/labkey/experiment/api/ExperimentServiceImpl.java b/experiment/src/org/labkey/experiment/api/ExperimentServiceImpl.java index f3d51bf42b7..705e05c82d6 100644 --- a/experiment/src/org/labkey/experiment/api/ExperimentServiceImpl.java +++ b/experiment/src/org/labkey/experiment/api/ExperimentServiceImpl.java @@ -230,13 +230,13 @@ import org.labkey.api.util.StringUtilsLabKey; import org.labkey.api.util.SubstitutionFormat; import org.labkey.api.util.TestContext; +import org.labkey.api.util.URIUtil; import org.labkey.api.util.UnexpectedException; import org.labkey.api.util.logging.LogHelper; import org.labkey.api.view.ActionURL; import org.labkey.api.view.HttpView; import org.labkey.api.view.JspTemplate; import org.labkey.api.view.JspView; -import org.labkey.api.view.NotFoundException; import org.labkey.api.view.UnauthorizedException; import org.labkey.api.view.ViewBackgroundInfo; import org.labkey.api.view.ViewContext; @@ -244,7 +244,6 @@ import org.labkey.experiment.FileLinkFileListener; import org.labkey.experiment.MissingFilesCheckInfo; import org.labkey.experiment.XarExportType; -import org.labkey.experiment.XarExporter; import org.labkey.experiment.XarReader; import org.labkey.experiment.api.property.DomainPropertyManager; import org.labkey.experiment.controllers.exp.ExperimentController; @@ -253,13 +252,11 @@ import org.labkey.experiment.pipeline.ExperimentPipelineJob; import org.labkey.experiment.pipeline.MoveRunsPipelineJob; import org.labkey.experiment.xar.AutoFileLSIDReplacer; -import org.labkey.experiment.xar.XarExportSelection; import org.labkey.vfs.FileLike; import org.springframework.dao.DataIntegrityViolationException; import org.springframework.dao.PessimisticLockingFailureException; import java.io.File; -import java.io.FileOutputStream; import java.io.IOException; import java.io.UncheckedIOException; import java.net.MalformedURLException; @@ -778,7 +775,17 @@ public ExpDataImpl createData(URI uri, XarSource source) throws XarFormatExcepti { path = FileUtil.stringToPath(source.getXarContext().getContainer(), source.getCanonicalDataFileURL(FileUtil.pathToString(path))); - pathStr = FileUtil.relativizeUnix(source.getRootPath(), path, false); + + // 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) { From 3ea164bdec4dc61b03056e63a14b06c9182752ad Mon Sep 17 00:00:00 2001 From: bbimber Date: Sun, 14 Dec 2025 08:39:11 -0800 Subject: [PATCH 2/2] Add second check for relativeURLs when data not under root --- .../labkey/experiment/DataURLRelativizer.java | 20 +++----- .../experiment/api/ExperimentServiceImpl.java | 51 ++++++++++--------- 2 files changed, 34 insertions(+), 37 deletions(-) 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) {