[internal] Add logo size check to all chains, logos. (#3133)

Co-authored-by: Catenocrypt <catenocrypt@users.noreply.github.com>
This commit is contained in:
Adam R 2020-08-07 16:39:46 +02:00 committed by GitHub
parent bda2c0a236
commit c408d033e4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 89 additions and 54 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.8 KiB

After

Width:  |  Height:  |  Size: 10 KiB

View File

@ -21,16 +21,15 @@ import {
} from "../common/filesystem";
import { isChecksum, toChecksum } from "../common/eth-web3";
import { ActionInterface, CheckStepInterface } from "./interface";
import { isLogoOK } from "../common/image";
import { isAssetInfoOK } from "../common/asset-info";
import * as bluebird from "bluebird";
function formatInfos() {
async function formatInfos() {
console.log(`Formatting info files...`);
ethForkChains.forEach(chain => {
await bluebird.each(ethForkChains, async (chain) => {
let count: number = 0;
const chainAssets = getChainAssetsList(chain);
chainAssets.forEach(address => {
await bluebird.each(chainAssets, async (address) => {
if (isChainAssetInfoExistSync(chain, address)) {
const chainAssetInfoPath = getChainAssetInfoPath(chain, address);
formatJsonFile(chainAssetInfoPath, true);
@ -49,13 +48,13 @@ function checkAddressChecksum(assetsFolderPath: string, address: string) {
}
}
function checkAddressChecksums() {
async function checkAddressChecksums() {
console.log(`Checking for checksum formats ...`);
ethForkChains.forEach(chain => {
await bluebird.each(ethForkChains, async (chain) => {
const assetsPath = getChainAssetsPath(chain);
readDirSync(assetsPath).forEach(address => {
getChainAssetFilesList(chain, address).forEach(file => {
await bluebird.each(readDirSync(assetsPath), async (address) => {
await bluebird.each(getChainAssetFilesList(chain, address), async (file) => {
if (getFileName(file) == logoName && getFileExt(file) !== logoExtension) {
console.log(`Renaming incorrect asset logo extension ${file} ...`);
gitMove(getChainAssetPath(chain, address), file, logoFullName);
@ -92,10 +91,6 @@ export class EthForks implements ActionInterface {
if (!isPathExistsSync(assetLogoPath)) {
error += `Missing file at path '${assetLogoPath}'\n`;
}
const [isOK, dimensionMsg] = await isLogoOK(assetLogoPath);
if (!isOK) {
error += dimensionMsg + "\n";
}
const [isInfoOK, infoMsg] = isAssetInfoOK(chain, address);
if (!isInfoOK) {
error += infoMsg + "\n";
@ -110,8 +105,8 @@ export class EthForks implements ActionInterface {
}
async fix(): Promise<void> {
formatInfos();
checkAddressChecksums();
await formatInfos();
await checkAddressChecksums();
}
update = null;

View File

@ -12,20 +12,27 @@ import {
readFileSync,
isPathExistsSync
} from "../common/filesystem";
import { resizeIfTooLarge } from "../common/image";
import { ActionInterface } from "./interface";
import { checkResizeIfTooLarge } from "../common/image";
import { ActionInterface, CheckStepInterface } from "./interface";
async function downsize(chains) {
console.log(`Checking all logos for downsizing ...`);
// return name of large logo, or empty
async function checkDownsize(chains, checkOnly: boolean): Promise<string> {
console.log(`Checking all logos for size ...`);
let totalCountChecked: number = 0;
let totalCountTooLarge: number = 0;
let totalCountUpdated: number = 0;
let largePath = "";
await bluebird.map(chains, async chain => {
let countChecked: number = 0;
let countTooLarge: number = 0;
let countUpdated: number = 0;
const path = getChainLogoPath(chain);
countChecked++;
countUpdated += await resizeIfTooLarge(path) ? 1 : 0;
const [tooLarge, updated] = await checkResizeIfTooLarge(path, checkOnly);
if (tooLarge) { largePath = path; }
countTooLarge += tooLarge ? 1 : 0;
countUpdated += updated ? 1 : 0;
// Check and resize if needed chain assets
const assetsPath = getChainAssetsPath(chain);
@ -33,7 +40,10 @@ async function downsize(chains) {
await bluebird.mapSeries(readDirSync(assetsPath), async asset => {
const path = getChainAssetLogoPath(chain, asset);
countChecked++;
countUpdated += await resizeIfTooLarge(path) ? 1 : 0;
const [tooLarge, updated] = await checkResizeIfTooLarge(path, checkOnly);
if (tooLarge) { largePath = path; }
countTooLarge += tooLarge ? 1 : 0;
countUpdated += updated ? 1 : 0;
})
}
@ -44,25 +54,47 @@ async function downsize(chains) {
await bluebird.mapSeries(validatorsList, async ({ id }) => {
const path = getChainValidatorAssetLogoPath(chain, id);
countChecked++;
countUpdated += await resizeIfTooLarge(path) ? 1 : 0;
const [tooLarge, updated] = await checkResizeIfTooLarge(path, checkOnly);
if (tooLarge) { largePath = path; }
countTooLarge += tooLarge ? 1 : 0;
countUpdated += updated ? 1 : 0;
})
}
totalCountChecked += countChecked;
totalCountTooLarge += countTooLarge;
totalCountUpdated += countUpdated;
if (countUpdated > 0) {
console.log(`Checking logos on chain ${chain} completed, ${countChecked} checked, ${countUpdated} logos updated`);
if (countTooLarge > 0 || countUpdated > 0) {
console.log(`Checking logos on chain ${chain} completed, ${countChecked} checked, ${countTooLarge} too large, ${largePath}, ${countUpdated} logos updated`);
}
});
console.log(`Checking logos completed, ${totalCountChecked} logos checked, ${totalCountUpdated} logos updated`);
console.log(`Checking logos completed, ${totalCountChecked} logos checked, ${totalCountTooLarge} too large, ${totalCountUpdated} logos updated`);
return largePath;
}
export class LogoSize implements ActionInterface {
getName(): string { return "Logo sizes"; }
getChecks = null;
getChecks(): CheckStepInterface[] {
return [
{
getName: () => { return "Check that logos are not too large"},
check: async () => {
const foundChains = readDirSync(chainsPath);
var largePath = await checkDownsize(foundChains, true);
if (largePath.length > 0) {
return `Found at least one logo that is too large: ${largePath}`;
}
return "";
}
},
];
}
async fix(): Promise<void> {
const foundChains = readDirSync(chainsPath);
await downsize(foundChains);
await checkDownsize(foundChains, false);
}
update = null;
}

View File

@ -4,7 +4,6 @@ import { Tron } from "../common/blockchains";
import { readDirSync, isPathExistsSync } from "../common/filesystem";
import { getChainAssetLogoPath, getChainValidatorsAssets } from "../common/repo-structure";
import { isLowerCase, isUpperCase } from "../common/types";
import { isLogoOK } from "../common/image";
import * as bluebird from "bluebird";
export function isTRC10(str: string): boolean {
@ -37,10 +36,6 @@ export class TronAction implements ActionInterface {
if (!isPathExistsSync(assetsLogoPath)) {
error += `Missing file at path '${assetsLogoPath}'\n`;
}
const [isOk, sizeMsg] = await isLogoOK(assetsLogoPath);
if (!isOk) {
error += sizeMsg + "\n";
}
});
return error;
}

View File

@ -71,37 +71,50 @@ async function isLogoSizeOK(path: string): Promise<[boolean, string]> {
return [true, ''];
}
// return true if image updated
export async function resizeIfTooLarge(path: string): Promise<boolean> {
// return if image if too large, and if image has been updated
export async function checkResizeIfTooLarge(path: string, checkOnly: boolean): Promise<[boolean, boolean]> {
let tooLarge = false;
let updated: boolean = false;
const { width: srcWidth, height: srcHeight } = getImageDimensions(path);
if (!isDimensionOK(srcWidth, srcHeight)) {
tooLarge = true; // may be too small as well
}
if (isDimensionTooLarge(srcWidth, srcHeight)) {
const { width, height } = calculateTargetSize(srcWidth, srcHeight, maxLogoWidth, maxLogoHeight);
console.log(`Resizing image at ${path} from ${srcWidth}x${srcHeight} => ${width}x${height}`)
await sharp(path).resize(width, height).toBuffer()
.then(data => {
writeFileSync(path, data);
updated = true;
})
.catch(e => {
console.log(chalk.red(e.message));
});
tooLarge = true;
if (!checkOnly) {
// resize
const { width, height } = calculateTargetSize(srcWidth, srcHeight, maxLogoWidth, maxLogoHeight);
console.log(`Resizing image at ${path} from ${srcWidth}x${srcHeight} => ${width}x${height}`)
await sharp(path).resize(width, height).toBuffer()
.then(data => {
writeFileSync(path, data);
updated = true;
})
.catch(e => {
console.log(chalk.red(e.message));
});
}
}
// If file size > max limit, compress with tinypng
const sizeKilobyte = getFileSizeInKilobyte(path);
if (sizeKilobyte > maxLogoSizeInKilobyte) {
console.log(`Resizing image at path ${path} from ${sizeKilobyte} kB`);
await compressTinyPNG(path)
.then(() => {
updated = true;
console.log(`Resized image at path ${path} from ${sizeKilobyte} kB => ${getFileSizeInKilobyte(path)} kB`);
})
.catch(e => {
console.log(chalk.red(e.message));
});
tooLarge = true;
if (!checkOnly) {
console.log(`Resizing image at path ${path} from ${sizeKilobyte} kB`);
await compressTinyPNG(path)
.then(() => {
updated = true;
console.log(`Resized image at path ${path} from ${sizeKilobyte} kB => ${getFileSizeInKilobyte(path)} kB`);
})
.catch(e => {
console.log(chalk.red(e.message));
});
}
}
return updated;
return [tooLarge, updated];
}