@@ -58,24 +58,44 @@ export class Base {
5858 }
5959
6060 static parsePath ( path : string ) {
61- const transformation =
62- [ 'region' , 'size' , 'rotation' ]
63- . map ( ( type : ValidatorKey ) => this . _validator ( type ) )
64- . join ( '/' ) +
65- '/' +
66- this . _validator ( 'quality' ) +
67- '.' +
68- this . _validator ( 'format' ) ;
69- const re = new RegExp (
70- `^/?(?<id>.+?)/(?:(?<info>info.json)|${ transformation } )$`
61+ debug ( 'parsing IIIF path: %s' , path ) ;
62+ const idOnlyRe = new RegExp ( '^/?(?<id>.+)/?$' ) ;
63+ const infoJsonRe = new RegExp ( '^/?(?<id>.+)/(?<info>info.json)$' ) ;
64+ const transformRe = new RegExp (
65+ '^/?(?<id>.+)/(?<region>.+)/(?<size>.+)/(?<rotation>.+)/(?<quality>.+)\\.(?<format>.+)$'
7166 ) ;
72- const result = re . exec ( path ) ?. groups ;
73- if ( ! result ) {
74- throw new IIIFError ( `Not a valid IIIF path: ${ path } ` , {
75- statusCode : 400
76- } ) ;
67+
68+ let result = transformRe . exec ( path ) ?. groups ;
69+ debug ( 'transform match result: %j' , result ) ;
70+ if ( result ) {
71+ for ( const component of [
72+ 'region' ,
73+ 'size' ,
74+ 'rotation' ,
75+ 'quality' ,
76+ 'format'
77+ ] as ValidatorKey [ ] ) {
78+ const validator = new RegExp ( this . _validator ( component ) ) ;
79+ if ( ! validator . test ( result [ component ] as string ) ) {
80+ throw new IIIFError ( `Invalid ${ component } in IIIF path: ${ path } ` , {
81+ statusCode : 400
82+ } ) ;
83+ }
84+ }
85+ return result ;
7786 }
78- return result ;
87+
88+ result = infoJsonRe . exec ( path ) ?. groups ;
89+ debug ( 'info.json match result: %j' , result ) ;
90+ if ( result ) return result ;
91+
92+ result = idOnlyRe . exec ( path ) ?. groups ;
93+ debug ( 'ID only match result: %j' , result ) ;
94+ if ( result ) return result ;
95+
96+ throw new IIIFError ( `Not a valid IIIF path: ${ path } ` , {
97+ statusCode : 400
98+ } ) ;
7999 }
80100
81101 constructor ( dims : Dimensions , opts : CalculatorOptions = { } ) {
@@ -187,11 +207,10 @@ export class Base {
187207 const max : MaxDimensions = { ...( this . opts ?. max || { } ) } ;
188208 max . height = max . height || max . width ;
189209 this . _parsedInfo . size =
190- ( 'left' in v ) ? { width : v . width , height : v . height , fit : 'fill' } : { ...v } ;
210+ 'left' in v
211+ ? { width : v . width , height : v . height , fit : 'fill' }
212+ : { ...v } ;
191213 this . _constrainSize ( max ) ;
192- if ( ! this . _parsedInfo . upscale ) {
193- this . _constrainSize ( this . _sourceDims ) ;
194- }
195214 return this ;
196215 }
197216
0 commit comments