Skip to content

Commit 296683e

Browse files
authored
Merge pull request #105 from parthlambdatest/Dot-3170
[Dot-3170] Handled non image files
2 parents 02ec122 + c000c21 commit 296683e

File tree

3 files changed

+43
-4
lines changed

3 files changed

+43
-4
lines changed

src/commander/upload.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import fs from 'fs';
2+
import path from 'path';
23
import { Command } from 'commander';
34
import { Context } from '../types.js';
45
import { color, Listr, ListrDefaultRendererLogLevels } from 'listr2';
@@ -8,6 +9,7 @@ import getGitInfo from '../tasks/getGitInfo.js';
89
import createBuild from '../tasks/createBuild.js';
910
import uploadScreenshots from '../tasks/uploadScreenshots.js';
1011
import finalizeBuild from '../tasks/finalizeBuild.js';
12+
import constants from '../lib/constants.js';
1113

1214
const command = new Command();
1315

@@ -31,6 +33,11 @@ command
3133
return;
3234
}
3335

36+
if (path.extname(directory).toLowerCase() === constants.ZIP_EXTENSION) {
37+
ctx.log.debug(`Zips are not accepted. ${directory}`)
38+
console.log(`Error: The provided directory ${directory} is a zip file. Zips are not accepted.`);
39+
return;
40+
}
3441
ctx.uploadFilePath = directory;
3542

3643
let tasks = new Listr<Context>(

src/lib/constants.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,16 @@ export default {
6767
// log file path
6868
LOG_FILE_PATH: '.smartui.log',
6969

70+
// Disallowed Zip Extension
71+
ZIP_EXTENSION: '.zip',
72+
73+
// Magic Numbers
74+
MAGIC_NUMBERS: [
75+
{ ext: 'jpg', magic: Buffer.from([0xFF, 0xD8, 0xFF]) },
76+
{ ext: 'jpeg', magic: Buffer.from([0xFF, 0xD8, 0xFF]) },
77+
{ ext: 'png', magic: Buffer.from([0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A]) },
78+
],
79+
7080
SUPPORTED_MOBILE_DEVICES: {
7181
'Blackberry KEY2 LE': { os: 'android', viewport: {width: 412, height: 618}},
7282
'Galaxy A12': { os: 'android', viewport: {width: 360, height: 800}},

src/lib/screenshot.ts

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
168173
export 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

Comments
 (0)