Skip to content

Commit

Permalink
expandPath relative to base template
Browse files Browse the repository at this point in the history
  • Loading branch information
bdw429s committed May 29, 2024
1 parent 839a676 commit a31fee1
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ public ExpandPath() {
public Object _invoke( IBoxContext context, ArgumentsScope arguments ) {
String path = arguments.getAsString( Key.path );
boolean hasTrailingSlash = path.endsWith( "/" ) || path.endsWith( "\\" );
String pathStr = FileSystemUtil.expandPath( context, path ).absolutePath().toString();
// base path is base template, or the original template that started the request, NOT the currently-executing template
String pathStr = FileSystemUtil.expandPath( context, path, context.findBaseTemplate() ).absolutePath().toString();

if ( hasTrailingSlash ) {
if ( !pathStr.endsWith( "/" ) || !pathStr.endsWith( "\\" ) ) {
Expand Down
22 changes: 18 additions & 4 deletions src/main/java/ortus/boxlang/runtime/util/FileSystemUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -814,6 +814,21 @@ public static String convertInputStreamToString( InputStream inputStream ) {
* @return The expanded path represented in a ResolvedFilePath record
*/
public static ResolvedFilePath expandPath( IBoxContext context, String path ) {
ResolvedFilePath resolvedFilePath = context.findClosestTemplate();
return expandPath( context, path, resolvedFilePath );
}

/**
* Expands a path to an absolute path. If the path is already absolute, it is
* returned as is.
*
* @param context The context in which the BIF is being invoked.
* @param path The path to expand
* @param basePath The base path to use for relative paths
*
* @return The expanded path represented in a ResolvedFilePath record
*/
public static ResolvedFilePath expandPath( IBoxContext context, String path, ResolvedFilePath basePath ) {
// This really isn't a valid path, but ColdBox does this by carelessly appending too many slashes to view paths
if ( path.startsWith( "//" ) ) {
// strip one of them off
Expand All @@ -836,11 +851,10 @@ public static ResolvedFilePath expandPath( IBoxContext context, String path ) {

// If the incoming path does NOT start with a /, then we make it relative to the current template (if there is one)
if ( !path.startsWith( SLASH_PREFIX ) ) {
ResolvedFilePath resolvedFilePath = context.findClosestTemplate();
if ( resolvedFilePath != null ) {
Path template = resolvedFilePath.absolutePath();
if ( basePath != null ) {
Path template = basePath.absolutePath();
if ( template != null ) {
return resolvedFilePath.newFromRelative( path );
return basePath.newFromRelative( path );
}
}
// No template, no problem. Slap a slash on, and we'll match it below
Expand Down

0 comments on commit a31fee1

Please sign in to comment.