@@ -165,8 +165,14 @@ function getImageDimensions(filePath: string): { width: number, height: number }
165165 return null ;
166166}
167167
168+ function isImage ( buffer : Buffer ) : boolean {
169+
170+ return constants . MAGIC_NUMBERS . some ( magic => buffer . slice ( 0 , magic . magic . length ) . equals ( magic . magic ) ) ;
171+ }
172+
168173export async function uploadScreenshots ( ctx : Context ) : Promise < void > {
169174 const allowedExtensions = ctx . options . fileExtension . map ( ext => `.${ ext . trim ( ) . toLowerCase ( ) } ` ) ;
175+ let noOfScreenshots = 0 ;
170176
171177 async function processDirectory ( directory : string , relativePath : string = '' ) : Promise < void > {
172178 const files = fs . readdirSync ( directory ) ;
@@ -177,6 +183,7 @@ export async function uploadScreenshots(ctx: Context): Promise<void> {
177183 const relativeFilePath = path . join ( relativePath , file ) ;
178184
179185 if ( stat . isDirectory ( ) && ctx . options . ignorePattern . includes ( relativeFilePath ) ) {
186+ ctx . log . debug ( `Ignoring Directory ${ relativeFilePath } ` )
180187 continue ; // Skip this path
181188 }
182189
@@ -185,6 +192,13 @@ export async function uploadScreenshots(ctx: Context): Promise<void> {
185192 } else {
186193 let fileExtension = path . extname ( file ) . toLowerCase ( ) ;
187194 if ( allowedExtensions . includes ( fileExtension ) ) {
195+ const fileBuffer = fs . readFileSync ( filePath ) ;
196+
197+ if ( ! isImage ( fileBuffer ) ) {
198+ ctx . log . debug ( `File ${ filePath } is not a valid ${ fileExtension } image. Skipping.` ) ;
199+ continue ;
200+ }
201+
188202 let ssId = relativeFilePath ;
189203 if ( ctx . options . stripExtension ) {
190204 ssId = path . join ( relativePath , path . basename ( file , fileExtension ) ) ;
@@ -195,18 +209,26 @@ export async function uploadScreenshots(ctx: Context): Promise<void> {
195209 if ( ! ctx . options . ignoreResolutions ) {
196210 const dimensions = getImageDimensions ( filePath ) ;
197211 if ( ! dimensions ) {
198- throw new Error ( `Unable to determine dimensions for image: ${ filePath } ` ) ;
212+ ctx . log . debug ( `Unable to determine dimensions for image: ${ filePath } ` )
213+ } else {
214+ const width = dimensions . width ;
215+ const height = dimensions . height ;
216+ viewport = `${ width } x${ height } ` ;
199217 }
200- const width = dimensions . width ;
201- const height = dimensions . height ;
202- viewport = `${ width } x${ height } ` ;
203218 }
204219
205220 await ctx . client . uploadScreenshot ( ctx . build , filePath , ssId , 'default' , viewport , ctx . log ) ;
221+ ctx . log . debug ( `${ filePath } : uploaded successfully` )
222+ noOfScreenshots ++ ;
206223 }
207224 }
208225 }
209226 }
210227
211228 await processDirectory ( ctx . uploadFilePath ) ;
229+ if ( noOfScreenshots == 0 ) {
230+ console . log ( `No screenshots uploaded.` ) ;
231+ } else {
232+ console . log ( `${ noOfScreenshots } screenshots uploaded successfully.` ) ;
233+ }
212234}
0 commit comments