Skip to content

Commit

Permalink
Media handler #10869
Browse files Browse the repository at this point in the history
  • Loading branch information
anatol-sialitski committed Jan 23, 2025
1 parent 47ecf86 commit 6afa583
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import java.util.List;
import java.util.Objects;
import java.util.concurrent.Callable;
import java.util.function.Predicate;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -78,8 +77,6 @@ public class MediaHandler

private static final EnumSet<HttpMethod> ALLOWED_METHODS = EnumSet.of( HttpMethod.GET, HttpMethod.HEAD, HttpMethod.OPTIONS );

private static final Predicate<PortalRequest> IS_ALLOWED_METHOD = req -> ALLOWED_METHODS.contains( req.getMethod() );

private static final MediaType SVG_MEDIA_TYPE = MediaType.SVG_UTF_8.withoutParameters();

private static final int DEFAULT_BACKGROUND = 0xFFFFFF;
Expand Down Expand Up @@ -138,7 +135,7 @@ public WebResponse handle( final WebRequest webRequest )
throw createNotFoundException();
}

if ( !IS_ALLOWED_METHOD.test( portalRequest ) )
if ( !ALLOWED_METHODS.contains( portalRequest.getMethod() ) )
{
throw new WebException( HttpStatus.METHOD_NOT_ALLOWED, String.format( "Method %s not allowed", portalRequest.getMethod() ) );
}
Expand Down Expand Up @@ -173,9 +170,6 @@ public WebResponse handle( final WebRequest webRequest )
verifyMediaScope( matcher.group( "context" ), repositoryId, branch, portalRequest );
verifyAccessByBranch( branch );

portalRequest.setRepositoryId( repositoryId );
portalRequest.setBranch( branch );

return executeInContext( repositoryId, branch, () -> type.equals( "attachment" )
? doHandleAttachment( portalRequest, id, fingerprint, restPath )
: doHandleImage( portalRequest, id, fingerprint, restPath ) );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.enonic.xp.portal.PortalResponse;
import com.enonic.xp.portal.impl.PortalConfig;
import com.enonic.xp.portal.impl.VirtualHostContextHelper;
import com.enonic.xp.project.ProjectName;
import com.enonic.xp.repository.RepositoryId;
import com.enonic.xp.schema.content.ContentTypeName;
import com.enonic.xp.security.PrincipalKey;
Expand Down Expand Up @@ -379,24 +380,34 @@ void testMediaScope()
webResponse = this.handler.handle( this.request );
assertEquals( HttpStatus.OK, webResponse.getStatus() );

this.request.setRepositoryId( ProjectName.from( "myproject" ).getRepoId() );
this.request.setBranch( ContentConstants.BRANCH_DRAFT );

this.request.setBaseUri( "/admin/site/preview" );
this.request.setEndpointPath( "/_/media/image/myproject:draft/123456/scale-100-100/image-name.jpg" );
this.request.setRawPath(
"/admin/site/preview/myproject/draft/_/media/image/myproject:draft/123456/scale-100-100/image-name.jpg" );
webResponse = this.handler.handle( this.request );
assertEquals( HttpStatus.OK, webResponse.getStatus() );

this.request.setRepositoryId( ProjectName.from( "myproject" ).getRepoId() );
this.request.setBranch( ContentConstants.BRANCH_DRAFT );

this.request.setBaseUri( "/admin/site/preview" );
this.request.setEndpointPath( "/_/media/image/myproject:draft/123456/scale-100-100/image-name.jpg" );
this.request.setRawPath(
"/admin/site/preview/myproject/master/_/media/image/myproject:draft/123456/scale-100-100/image-name.jpg" );
"/admin/site/preview/myproject/draft/_/media/image/myproject:draft/123456/scale-100-100/image-name.jpg" );
webResponse = this.handler.handle( this.request );
assertEquals( HttpStatus.OK, webResponse.getStatus() );


this.request.setRepositoryId( ProjectName.from( "unknown" ).getRepoId() );
this.request.setBranch( ContentConstants.BRANCH_DRAFT );

this.request.setBaseUri( "/admin/site/preview" );
this.request.setEndpointPath( "/_/media/image/unknown:draft/123456/scale-100-100/image-name.jpg" );
this.request.setRawPath(
"/admin/site/preview/unknown/draft/_/media/image/unknown:draft/123456/scale-100-100/image-name.jpg" );
"/admin/site/preview/myproject/draft/_/media/image/unknown:draft/123456/scale-100-100/image-name.jpg" );
WebException ex = assertThrows( WebException.class, () -> this.handler.handle( this.request ) );
assertEquals( HttpStatus.NOT_FOUND, ex.getStatus() );
}
Expand Down

0 comments on commit 6afa583

Please sign in to comment.