-
-
Save OMGZui/24fa58a7b51e3b20f1c5328e6682e069 to your computer and use it in GitHub Desktop.
This file contains hidden or 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