@@ -34,7 +34,9 @@ class ImageServer {
3434 }
3535
3636 // validate signature token
37- this . _signatureStrategy . verifyToken ( request . hasQuery ( this . _config . SIGNATURE_PARAMETER_NAME ) ? request . getQuery ( this . _config . SIGNATURE_PARAMETER_NAME ) : null , path ) ;
37+ const signatureToken = request . hasQuery ( this . _config . SIGNATURE_PARAMETER_NAME ) ? request . getQuery ( this . _config . SIGNATURE_PARAMETER_NAME ) : null ;
38+
39+ this . _signatureStrategy . verifyToken ( signatureToken , path ) ;
3840
3941 // parse image info & modifiers
4042 const parts = path . split ( '/' ) ;
@@ -49,6 +51,10 @@ class ImageServer {
4951
5052 const info = new ImageInfo ( parts . join ( '/' ) ) ;
5153
54+ if ( request . hasQuery ( this . _config . VERSION_PARAMETER_NAME ) ) {
55+ info . version = request . getQuery ( this . _config . VERSION_PARAMETER_NAME ) ;
56+ }
57+
5258 if ( null === info . extension ) {
5359 throw new InvalidStateError ( 'Missing file extension in requested path.' ) ;
5460 }
@@ -72,7 +78,7 @@ class ImageServer {
7278 CacheControl : `public, max-age=${ this . _config . CACHE_MAX_AGE . toString ( ) } ` ,
7379 Expires : new Date ( now . getTime ( ) + ( this . _config . CACHE_MAX_AGE * 1000 ) )
7480 } ) . promise ( ) . then (
75- ( ) => this . _responseFactory . createRedirectResponse ( this . _createLink ( resource . info , modifierString ) ) ,
81+ ( ) => this . _responseFactory . createRedirectResponse ( this . _createLink ( resource . info , modifierString , signatureToken ) ) ,
7682 e => {
7783 throw new InvalidStateError ( e . message ) ;
7884 }
@@ -96,15 +102,39 @@ class ImageServer {
96102 return `${ basePath } ${ info . createCachedPath ( modifierString ) } ` ;
97103 }
98104
99- _createLink ( info , modifierString , addSignature = false ) {
100- const host = null !== this . _config . HOST ? this . _config . HOST : `https://${ this . _config . CACHE_BUCKET } .s3.${ this . _s3 . config . region } .amazonaws.com` ;
101- let path = this . _createCachedPath ( info , modifierString ) ;
105+ _getHostUrl ( ) {
106+ return new URL (
107+ null !== this . _config . HOST ? this . _config . HOST : `https://${ this . _config . CACHE_BUCKET } .s3.${ this . _s3 . config . region } .amazonaws.com`
108+ ) ;
109+ }
110+
111+ _createLink ( info , modifierString , signatureToken = null ) {
112+ const url = this . _getHostUrl ( ) ;
113+ url . pathname = this . _createCachedPath ( info , modifierString ) ;
114+
115+ if ( null !== signatureToken ) {
116+ url . searchParams . set ( this . _config . SIGNATURE_PARAMETER_NAME , signatureToken ) ;
117+ }
118+
119+ if ( null !== info . version ) {
120+ url . searchParams . set ( this . _config . SIGNATURE_PARAMETER_NAME , info . version ) ;
121+ }
122+
123+ return url . toString ( ) ;
124+ }
125+
126+ _createNoImageLink ( info , modifierString ) {
127+ const url = this . _getHostUrl ( ) ;
128+ const path = this . _createCachedPath ( info , modifierString ) ;
129+ const signatureToken = this . _signatureStrategy . createToken ( path ) ;
130+
131+ url . pathname = path ;
102132
103- if ( addSignature ) {
104- path += `? ${ this . _config . SIGNATURE_PARAMETER_NAME } = ${ this . _signatureStrategy . createToken ( path ) } ` ;
133+ if ( null !== signatureToken ) {
134+ url . searchParams . set ( this . _config . SIGNATURE_PARAMETER_NAME , signatureToken ) ;
105135 }
106136
107- return ` ${ host } / ${ path } ` ;
137+ return url . toString ( ) ;
108138 }
109139
110140 _getNoImageResponse ( info , modifierString ) {
@@ -114,7 +144,7 @@ class ImageServer {
114144 noImageInfo . extension = info . extension ;
115145
116146 return this . _responseFactory . createRedirectResponse (
117- this . _createLink ( noImageInfo , modifierString , true )
147+ this . _createNoImageLink ( noImageInfo , modifierString )
118148 ) ;
119149 } catch ( e ) {
120150 return null ;
0 commit comments