-
-
Save OMGZui/2e4a263a36b4962cbac4782c7631be93 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface IcompressSvga { | |
(path: string, source: Buffer): () => Promise<Idetail> | |
} | |
let compressSvga: IcompressSvga | |
compressSvga = (path, source) => { | |
return async () => { | |
const result = { | |
input: 0, | |
output: 0, | |
ratio: 0, | |
path, | |
file: source, | |
msg: '' | |
} | |
try { | |
const data = ProtoMovieEntity.decode( | |
pako.inflate(toArrayBuffer(source)) | |
) as unknown as IsvgaData | |
const { images } = data | |
const list = Object.keys(images).map(path => { | |
return compressImg({ path, file: toBuffer(images[path]) }) | |
}) | |
const detail = await Promise.all(list.map(fn => fn())) | |
detail.forEach(({ path, file }) => { | |
data.images[path] = file | |
}) | |
const file = pako.deflate( | |
toArrayBuffer(ProtoMovieEntity.encode(data).finish() as Buffer) | |
) | |
result.input = source.length | |
result.output = file.length | |
result.ratio = 1 - file.length / source.length | |
result.file = file | |
} catch (err) { | |
result.msg = `[${chalk.blue(path)}] ${chalk.red(JSON.stringify(err))}` | |
} | |
return result | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment