2019-11-19 20:22:38 +00:00
|
|
|
import {
|
|
|
|
maxLogoWidth,
|
|
|
|
maxLogoHeight,
|
|
|
|
readDirSync,
|
|
|
|
chainsFolderPath,
|
|
|
|
getChainLogoPath,
|
|
|
|
calculateAspectRatioFit,
|
|
|
|
getImageDimentions,
|
|
|
|
getChainAssetsPath,
|
|
|
|
getChainAssetLogoPath,
|
|
|
|
isPathExistsSync,
|
|
|
|
writeFileSync,
|
|
|
|
readFileSync,
|
|
|
|
getChainValidatorsListPath,
|
2020-04-11 03:07:01 +00:00
|
|
|
getChainValidatorAssetLogoPath,
|
|
|
|
maxAssetLogoSizeInKilobyte,
|
|
|
|
getFileSizeInKilobyte
|
2019-11-19 20:22:38 +00:00
|
|
|
} from "../src/test/helpers"
|
|
|
|
const sharp = require('sharp')
|
|
|
|
const bluebird = require("bluebird")
|
|
|
|
const foundChains = readDirSync(chainsFolderPath)
|
2020-04-11 03:07:01 +00:00
|
|
|
const tinify = require("tinify");
|
|
|
|
tinify.key = "MXxhvmhjMkMM6CVccGrfyQm2RHpTf1G7"; // Key is free to get, gives 500 uploads per month
|
2019-11-19 20:22:38 +00:00
|
|
|
|
|
|
|
function downsize() {
|
|
|
|
console.log(`Start resizing`)
|
2020-04-11 20:24:56 +00:00
|
|
|
bluebird.map(foundChains, async chain => {
|
|
|
|
console.log(`Resizing assets on chain ${chain}`)
|
2019-11-19 20:22:38 +00:00
|
|
|
const chainLogoPath = getChainLogoPath(chain)
|
|
|
|
const { width: srcWidth, heigth: srcHeight } = getImageDimentions(chainLogoPath)
|
|
|
|
|
|
|
|
// Check and resize if needed chain logo
|
|
|
|
if (isDownsizing(srcWidth, srcHeight)) {
|
|
|
|
await resize(srcWidth, srcHeight, chainLogoPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check and resize if needed chain assets
|
|
|
|
const assetsPath = getChainAssetsPath(chain)
|
|
|
|
if (isPathExistsSync(assetsPath)) {
|
|
|
|
bluebird.mapSeries(readDirSync(assetsPath), async asset => {
|
|
|
|
const assetPath = getChainAssetLogoPath(chain, asset)
|
|
|
|
const { width: srcWidth, height: srcHeight } = getImageDimentions(assetPath)
|
|
|
|
if (isDownsizing(srcWidth, srcHeight)) {
|
|
|
|
await resize(srcWidth, srcHeight, assetPath)
|
|
|
|
}
|
2020-04-11 03:07:01 +00:00
|
|
|
|
|
|
|
// If size still > max limit, compress with tinypng
|
|
|
|
const sizeKilobyte = getFileSizeInKilobyte(assetPath)
|
|
|
|
if (sizeKilobyte > maxAssetLogoSizeInKilobyte) {
|
|
|
|
await compressTinyPNG(assetPath)
|
|
|
|
console.log(`Successfully resized iamge at path ${assetPath} from ${sizeKilobyte} => ${getFileSizeInKilobyte(assetPath)}`)
|
|
|
|
}
|
2019-11-19 20:22:38 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-04-11 03:07:01 +00:00
|
|
|
|
2019-11-19 20:22:38 +00:00
|
|
|
// Check and resize if needed chain validators image
|
|
|
|
const chainValidatorsList = getChainValidatorsListPath(chain)
|
|
|
|
if (isPathExistsSync(chainValidatorsList)) {
|
|
|
|
const validatorsList = JSON.parse(readFileSync(getChainValidatorsListPath(chain)))
|
|
|
|
bluebird.mapSeries(validatorsList, async ({ id }) => {
|
|
|
|
const path = getChainValidatorAssetLogoPath(chain, id)
|
|
|
|
const { width: srcWidth, height: srcHeight } = getImageDimentions(path)
|
|
|
|
if (isDownsizing(srcWidth, srcHeight)) {
|
|
|
|
await resize(srcWidth, srcHeight, path)
|
|
|
|
}
|
2020-04-11 03:07:01 +00:00
|
|
|
|
|
|
|
// If size still > max limit, compress with tinypng
|
|
|
|
const sizeKilobyte = getFileSizeInKilobyte(path)
|
|
|
|
if (sizeKilobyte > maxAssetLogoSizeInKilobyte) {
|
|
|
|
await compressTinyPNG(path)
|
|
|
|
}
|
2019-11-19 20:22:38 +00:00
|
|
|
})
|
|
|
|
}
|
2020-04-11 20:24:56 +00:00
|
|
|
|
|
|
|
console.log(` Resizing assets on chain ${chain} completed`)
|
2019-11-19 20:22:38 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
downsize()
|
|
|
|
|
|
|
|
function isDownsizing(srcWidth: number, srcHeight: number): boolean {
|
|
|
|
return (srcWidth > maxLogoWidth) || (srcHeight > maxLogoHeight)
|
|
|
|
}
|
|
|
|
|
|
|
|
async function resize(srcWidth: number, srcHeight: number, path: string) {
|
|
|
|
const { width, height } = calculateAspectRatioFit(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))
|
2020-01-16 21:10:59 +00:00
|
|
|
.catch(e => {
|
|
|
|
console.log(e.message)
|
|
|
|
})
|
2020-04-11 03:07:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export async function compressTinyPNG(path: string) {
|
|
|
|
const source = await tinify.fromFile(path);
|
|
|
|
await source.toFile(path);
|
|
|
|
|
2019-11-19 20:22:38 +00:00
|
|
|
}
|