11// @flow
22require ( 'now-env' ) ;
33import ImgixClient from 'imgix-core-js' ;
4+ import decodeUriComponent from 'decode-uri-component' ;
45
56const IS_PROD = process . env . NODE_ENV === 'production' ;
67const LEGACY_PREFIX = 'https://spectrum.imgix.net/' ;
@@ -12,6 +13,7 @@ const isLocalUpload = (url: string): boolean => url.startsWith('/uploads/', 0) &
1213const hasLegacyPrefix = ( url : string ) : boolean => url . startsWith ( LEGACY_PREFIX , 0 )
1314// prettier-ignore
1415const useProxy = ( url : string ) : boolean => url . indexOf ( 'spectrum.imgix.net' ) < 0 && url . startsWith ( 'http' , 0 )
16+ const isEncoded = ( url : string ) : boolean => url . indexOf ( '%' ) >= 0 ;
1517
1618/*
1719 When an image is uploaded to s3, we generate a url to be stored in our db
@@ -50,8 +52,19 @@ export const signImageUrl = (url: string, opts: Opts) => {
5052
5153 if ( isLocalUpload ( url ) ) return url ;
5254
53- let processedUrl = hasLegacyPrefix ( url ) ? stripLegacyPrefix ( url ) : url ;
55+ const processedUrl = hasLegacyPrefix ( url ) ? stripLegacyPrefix ( url ) : url ;
5456
57+ // we never have to worry about escaping or unescaping proxied urls e.g. twitter images
5558 if ( useProxy ( url ) ) return signProxy ( processedUrl , opts ) ;
56- return signPrimary ( processedUrl , opts ) ;
59+
60+ let decoded = processedUrl ;
61+ if ( isEncoded ( processedUrl ) ) {
62+ const pathParts = decoded . split ( '/' ) ;
63+ const filename = pathParts . pop ( ) ;
64+ const bucketPath = pathParts . join ( '/' ) ;
65+ decoded = bucketPath + '/' + encodeURIComponent ( filename ) ;
66+ decoded = decodeUriComponent ( decoded ) ;
67+ }
68+
69+ return signPrimary ( decoded , opts ) ;
5770} ;
0 commit comments