[Internal] Add checks for dapps folder files, extension, casing, image size (#5725)

* Add checks for dapps folder files, extension and casing.

* Add checks for dapps logo dimansions/size.

* Reduce size of large dapps logos

Co-authored-by: Catenocrypt <catenocrypt@users.noreply.github.com>
This commit is contained in:
Adam R 2021-02-26 12:39:39 +01:00 committed by GitHub
parent 43e87a878b
commit a6ab7933a9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 56 additions and 6 deletions

View File

@ -5,6 +5,7 @@ import {
import { CheckStepInterface, ActionInterface } from "../generic/interface";
import {
allChains,
dappsPath,
getChainLogoPath,
getChainAssetInfoPath,
getChainAssetsPath,
@ -90,7 +91,7 @@ export class FoldersFiles implements ActionInterface {
//console.log(msg);
warnings.push(msg);
}
}) ;
});
}
});
return [errors, warnings];
@ -109,7 +110,7 @@ export class FoldersFiles implements ActionInterface {
if (!isPathExistsSync(infoFullPath)) {
warnings.push(`Missing info file for asset '${chain}/${address}' -- ${infoFullPath}`);
}
}) ;
});
}
});
return [[], warnings];
@ -130,11 +131,28 @@ export class FoldersFiles implements ActionInterface {
errors.push(`File '${assetFolderFile}' not allowed at this path: ${assetsPath}`);
}
});
}) ;
});
}
});
return [errors, []];
}
},
{
getName: () => { return "Dapps folders contain only .png files, with all lowercase names"},
check: async () => {
const errors: string[] = [];
if (isPathExistsSync(dappsPath)) {
readDirSync(dappsPath).forEach(filename => {
if (!filename.endsWith('.png')) {
errors.push(`File '${filename}' has invalid extension; ${dappsPath}`);
}
if (filename.toLowerCase() != filename) {
errors.push(`File '${filename}' is not all-lowercase; ${dappsPath}`);
}
});
}
return [errors, []];
}
}
];
}

View File

@ -80,10 +80,12 @@ export async function checkResizeIfTooLarge(path: string, checkOnly: boolean): P
if (!isDimensionOK(srcWidth, srcHeight)) {
tooLarge = true; // may be too small as well
console.log(`Wrong image dimensions, ${srcWidth}x${srcHeight}, ${path}`);
}
if (isDimensionTooLarge(srcWidth, srcHeight)) {
tooLarge = true;
console.log(`Image too large, ${srcWidth}x${srcHeight}, ${path}`);
if (!checkOnly) {
// resize
const { width, height } = calculateTargetSize(srcWidth, srcHeight, maxLogoWidth, maxLogoHeight);
@ -103,6 +105,7 @@ export async function checkResizeIfTooLarge(path: string, checkOnly: boolean): P
const sizeKilobyte = getFileSizeInKilobyte(path);
if (sizeKilobyte > maxLogoSizeInKilobyte) {
tooLarge = true;
console.log(`Image too big, ${sizeKilobyte} kb, ${path}`);
if (!checkOnly) {
console.log(`Resizing image at path ${path} from ${sizeKilobyte} kB`);
await compressTinyPNG(path)

View File

@ -5,7 +5,8 @@ import {
getChainAssetsPath,
getChainAssetLogoPath,
getChainValidatorsListPath,
getChainValidatorAssetLogoPath
getChainValidatorAssetLogoPath,
dappsPath
} from "../generic/repo-structure";
import {
readDirSync,
@ -16,12 +17,14 @@ import { checkResizeIfTooLarge } from "../generic/image";
import { ActionInterface, CheckStepInterface } from "../generic/interface";
// return name of large logo, or empty
async function checkDownsize(chains, checkOnly: boolean): Promise<string[]> {
async function checkDownsize(chains: string[], checkOnly: boolean): Promise<string[]> {
console.log(`Checking all logos for size ...`);
let totalCountChecked = 0;
let totalCountTooLarge = 0;
let totalCountUpdated = 0;
const largePaths: string[] = [];
// Check asset logos, under given chains
await bluebird.map(chains, async chain => {
let countChecked = 0;
let countTooLarge = 0;
@ -58,7 +61,7 @@ async function checkDownsize(chains, checkOnly: boolean): Promise<string[]> {
if (tooLarge) { largePaths.push(path); }
countTooLarge += tooLarge ? 1 : 0;
countUpdated += updated ? 1 : 0;
})
});
}
totalCountChecked += countChecked;
@ -68,6 +71,30 @@ async function checkDownsize(chains, checkOnly: boolean): Promise<string[]> {
console.log(`Checking logos on chain ${chain} completed, ${countChecked} checked, ${countTooLarge} too large, ${largePaths}, ${countUpdated} logos updated`);
}
});
// Check dapps logos
if (isPathExistsSync(dappsPath)) {
let countChecked = 0;
let countTooLarge = 0;
let countUpdated = 0;
await bluebird.mapSeries(readDirSync(dappsPath), async filename => {
const path = dappsPath + `/` + filename;
countChecked++;
const [tooLarge, updated] = await checkResizeIfTooLarge(path, checkOnly);
if (tooLarge) { largePaths.push(path); }
countTooLarge += tooLarge ? 1 : 0;
countUpdated += updated ? 1 : 0;
});
totalCountChecked += countChecked;
totalCountTooLarge += countTooLarge;
totalCountUpdated += countUpdated;
if (countTooLarge > 0 || countUpdated > 0) {
console.log(`Checking dapps logos completed, ${countChecked} checked, ${countTooLarge} too large, ${largePaths}, ${countUpdated} logos updated`);
}
}
console.log(`Checking logos completed, ${totalCountChecked} logos checked, ${totalCountTooLarge} too large, ${totalCountUpdated} logos updated`);
return largePaths;
}

View File

@ -51,4 +51,6 @@ export const getChainAssetsList = (chain: string): string[] => readDirSync(getCh
export const getChainAssetFilesList = (chain: string, address: string): string[] => readDirSync(getChainAssetPath(chain, address));
export const getChainValidatorsAssets = (chain: string): string[] => readDirSync(getChainValidatorsAssetsPath(chain));
export const dappsPath: string = path.join(process.cwd(), '/dapps');
export const rootDirAllowedFiles = config.foldersRootdirAllowedFiles;