11import { ImageKitOptions , UrlOptions } from "../interfaces" ;
22import { Transformation } from "../interfaces/Transformation" ;
33import transformationUtils from "../utils/transformation" ;
4+ import { safeBtoa } from "../utils/transformation" ;
45const TRANSFORMATION_PARAMETER = "tr" ;
56
67function removeTrailingSlash ( str : string ) {
@@ -73,20 +74,106 @@ export const buildURL = (opts: UrlOptions & ImageKitOptions) => {
7374 return urlObj . href ;
7475} ;
7576
77+
78+ function processOverlay ( overlay : Transformation [ "overlay" ] ) : string | undefined {
79+ const entries = [ ] ;
80+ if ( ! overlay ) {
81+ return ;
82+ }
83+ const { type, position = { } , timing = { } , transformation = [ ] } = overlay ;
84+
85+ if ( ! type ) {
86+ throw new Error ( "Overlay type is required" ) ;
87+ }
88+
89+ switch ( type ) {
90+ case "text" :
91+ entries . push ( "l-text" ) ;
92+ if ( overlay . text ) {
93+ entries . push ( `ie-${ encodeURIComponent ( safeBtoa ( overlay . text ) ) } ` ) ;
94+ }
95+ break ;
96+ case "image" :
97+ entries . push ( "l-image" ) ;
98+ if ( overlay . input ) {
99+ entries . push ( `i-${ overlay . input } ` ) ;
100+ }
101+ break ;
102+ case "video" :
103+ entries . push ( "l-video" ) ;
104+ if ( overlay . input ) {
105+ entries . push ( `i-${ overlay . input } ` ) ;
106+ }
107+ break ;
108+ case "subtitle" :
109+ entries . push ( "l-subtitle" ) ;
110+ if ( overlay . input ) {
111+ entries . push ( `i-${ overlay . input } ` ) ;
112+ }
113+ break ;
114+ case "solidColor" :
115+ entries . push ( "l-image" ) ;
116+ entries . push ( `i-ik_canvas` ) ;
117+ if ( overlay . color ) {
118+ entries . push ( `bg-${ overlay . color } ` ) ;
119+ }
120+ break ;
121+ }
122+
123+ const { x, y, focus } = position ;
124+ if ( x ) {
125+ entries . push ( `lxo-${ x } ` ) ;
126+ }
127+ if ( y ) {
128+ entries . push ( `lyo-${ y } ` ) ;
129+ }
130+ if ( focus ) {
131+ entries . push ( `lfo-${ focus } ` ) ;
132+ }
133+
134+ const { start, end, duration } = timing ;
135+
136+ if ( start ) {
137+ entries . push ( `lso-${ start } ` ) ;
138+ }
139+ if ( end ) {
140+ entries . push ( `leo-${ end } ` ) ;
141+ }
142+ if ( duration ) {
143+ entries . push ( `ldu-${ duration } ` ) ;
144+ }
145+
146+ const transformationString = constructTransformationString ( transformation ) ;
147+
148+ if ( transformationString && transformationString . trim ( ) !== "" ) entries . push ( transformationString ) ;
149+
150+ entries . push ( "l-end" ) ;
151+
152+ return entries . join ( transformationUtils . getTransformDelimiter ( ) ) ;
153+ }
154+
76155function constructTransformationString ( transformation : Transformation [ ] | undefined ) {
77156 if ( ! Array . isArray ( transformation ) ) {
78157 return "" ;
79158 }
80159
81- var parsedTransforms = [ ] ;
160+ var parsedTransforms : string [ ] = [ ] ;
82161 for ( var i = 0 , l = transformation . length ; i < l ; i ++ ) {
83- var parsedTransformStep = [ ] ;
162+ var parsedTransformStep : string [ ] = [ ] ;
84163 for ( var key in transformation [ i ] ) {
85164 let value = transformation [ i ] [ key as keyof Transformation ] ;
86165 if ( value === undefined || value === null ) {
87166 continue ;
88167 }
89168
169+ if ( key === "overlay" && typeof value === "object" ) {
170+ var rawString = processOverlay ( value as Transformation [ "overlay" ] ) ;
171+ if ( rawString ) {
172+ parsedTransformStep . push ( rawString ) ;
173+ continue ;
174+ }
175+ }
176+
90177 var transformKey = transformationUtils . getTransformKey ( key ) ;
91178 if ( ! transformKey ) {
92179 transformKey = key ;
@@ -111,7 +198,7 @@ function constructTransformationString(transformation: Transformation[] | undefi
111198 ) {
112199 parsedTransformStep . push ( transformKey ) ;
113200 } else if ( key === "raw" ) {
114- parsedTransformStep . push ( transformation [ i ] [ key ] ) ;
201+ parsedTransformStep . push ( transformation [ i ] [ key ] as string ) ;
115202 } else {
116203 if ( transformKey === "di" ) {
117204 value = removeTrailingSlash ( removeLeadingSlash ( value as string || "" ) ) ;
0 commit comments