Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
fs.readFile(name, function(err, sourceData) {
if (err) throw err;
tinify.fromBuffer(sourceData).toBuffer(function(err, resultData) {
if (err) throw err;
filePath = path.join(filePath, name);
const writerStream = fs.createWriteStream(filePath);
// 标记文件末尾
writerStream.write(resultData,'binary');
writerStream.end();
// 处理流事件 --> data, end, and error
writerStream.on('finish', function() {
failsList.push(null);
record(name, true, failsList.length, fullLen);
if (failsList.length === fullLen) {
finishcb(failsList, filePath);
}
});
fs.readFile(input, function(err, sourceData) {
if (err){
throw err;
}
tinifypng.fromBuffer(sourceData).toBuffer(function(err, resultData) {
if (err){
throw err;
}
fs.writeFile(output, resultData, function(err) {
if(err) {
return console.log(err);
}
//- - - - - - - - - - - - - - - - - - - - - - - -
//Block statistic
//- - - - - - - - - - - - - - - - - - - - - - - -
//Узнаем размер файла после сжатия
size_output = getFilesizeInBytes(output);
//Находим на сколько процентов удалось сжать файл
percent = size_output / size_in;
percent = percent * 100;
fs.readFile(input, function(err, sourceData) {
if(err === null){
if (err){
throw err;
}
tinifypng.fromBuffer(sourceData).toBuffer(function(err, resultData) {
if (err){
throw err;
}
fs.writeFile(output, resultData, function(err) {
if(err) {
return console.log(err);
}
//- - - - - - - - - - - - - - - - - - - - - - - -
//Block statistic
//- - - - - - - - - - - - - - - - - - - - - - - -
//Узнаем размер файла после сжатия
size_output = getFilesizeInBytes(output);
//Находим на сколько процентов удалось сжать файл
percent = size_output / size_in;
percent = percent * 100;
fs.readFile(filePathAll, (err, sourceData) => {
if (err) {
console.log(chalk.red(`${file} 压缩失败`));
reject(err);
} else {
let fileSize = fs.statSync(filePathAll).size;
tinify.fromBuffer(sourceData).toBuffer((err, resultData) => {
if (err) {
console.log(chalk.red(`${file} 压缩失败`));
reject(err);
}
//将压缩后的文件保存覆盖
fs.writeFile(filePathAll, resultData, (err) => {
let compressFileSize = fs.statSync(filePathAll).size;
console.log(
chalk.green(
`${file} ${(fileSize / 1024).toFixed(2)}kb 压缩成功 ${(
compressFileSize / 1024
).toFixed(2)}kb ${(
((compressFileSize - fileSize) * 100) /
fileSize
).toFixed(1)}%`
)
static tinifyImage(buffer, options, callback) {
if(!options.tinify) {
callback(buffer);
return;
}
tinify.key = options.tinifyKey;
tinify.fromBuffer(buffer).toBuffer(function(err, result) {
if (err) throw err;
callback(result);
});
}
}
ipcMain.on('tinify', (e, data) => {
tinify.key = data.key;
tinify.fromBuffer(Buffer.from(data.imageData, 'base64')).toBuffer((err, res) => {
if (err) {
e.sender.send('tinify-complete', {
success: false,
uid: data.uid,
error: err.message
});
return;
}
e.sender.send('tinify-complete', {
success: true,
uid: data.uid,
data: res.toString('base64')
});
});
});
return new Promise((resolve, reject) => {
let sourceData = fs.readFileSync(path)
tinify.fromBuffer(sourceData).toBuffer((err, resultData) => {
if (err instanceof tinify.AccountError) {
reject(err)
return new Notification('compress filed', {
body: 'Verify your API key and account limit.'
})
} else if (err instanceof tinify.ServerError) {
reject(err)
return new Notification('compress filed', {
body: 'Temporary issue with the Tinify API.'
})
} else if (err instanceof tinify.ConnectionError) {
reject(err)
return new Notification('compress filed', {
body: 'A network connection error occurred.'
})
} else {
(function tinifyCompress() {
tinify.key = __keys[0];
tinify.fromBuffer(fileInfo.data).toBuffer(function(err, resultData) {
if (err instanceof tinify.AccountError && __keys.length > 1) {
//更换秘钥
__keys.splice(0, 1);
tinifyCompress();
} else if (err instanceof tinify.ServerError && __serverErrorRetry -- > 0) {
tinifyCompress();
} else if(err){
reject(err);
} else {
resolve({
dir: fileInfo.dir,
compressionData: resultData
});
}
});
})();
let compressImg = yield new Promise((resolve, reject) => {
tinify.fromBuffer(fileInfo.source.source()).toBuffer((err, resultData) => {
if (err) {
reject(err);
} else {
resolve(resultData);
}
})
});
//压缩图片成功