Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
return new Promise((resolve, reject) => {
const gotStream = got.stream(toDownload.browser_download_url)
// eslint-disable-next-line
const extractionStream = gotStream.pipe(unzipper.Extract({ path: target }))
extractionStream.on('close', resolve)
extractionStream.on('error', reject)
})
})
function unzipFile(response, locales, path ) {
return response && response.pipe(unZipper.Parse())
.on('entry', function (entry) {
const fileName = entry.path;
const type = entry.type; // 'Directory' or 'File'
const existLocales = locales.filter(lang => fileName.includes(`${lang}/`));
if (!existLocales.length) {
console.warn('[skip lang]', fileName);
return entry;
}
if (type === 'Directory') {
const name = `${path}/${fileName}`;
if (!fs.existsSync(name)) {
fs.mkdirSync(name);
initialPromise.then(() => {
fs.createReadStream(filepath)
.pipe(unzip.Parse())
.pipe(etl.map(entry => {
// 适配iconPath为ic_launcher.xml的情况
const entryPath = entry.path
const isXml = entryPath.indexOf('.xml') >= 0
if ( (!isXml && entryPath.indexOf(iconPath) != -1) || (isXml && entry.path.indexOf(maxSizePath) != -1)) {
console.log(entry.path)
entry.pipe(etl.toFile(tempOut))
resolve({ 'success': true, fileName: realPath })
} else {
resolve({ 'success': true, fileName: realPath })
entry.autodrain()
}
}))
})
})
encoding: 'utf8'
}).trim();
}
const libffmpegZipPath = await new Promise((resolve, reject) => {
downloadElectron({
// `version` usually starts with a `v`, which already gets added by `electron-download`.
version: electronVersion.replace(/^v/i, ''),
ffmpeg: true,
}, (error, path) => {
if (error) reject(error);
else resolve(path);
});
});
const libffmpegZip = await unzipper.Open.file(libffmpegZipPath);
file = libffmpegZip.files.find(file => file.path.endsWith(libffmpegFileName));
if (!file) {
throw new Error(`Archive did not contain "${libffmpegFileName}".`);
}
// Extract file to cache.
await new Promise((resolve, reject) => {
file.stream()
.pipe(fs.createWriteStream(libffmpegCachedPath))
.on('finish', resolve)
.on('error', reject);
});
console.info(`Downloaded ffmpeg shared library { version: "${electronVersion}", dist: "${electronDist}" }.`);
}
// .pipe(etl.map(entry => {
// // 适配iconPath为ic_launcher.xml的情况
// const entryPath = entry.path
// // const isXml = entryPath.indexOf('.xml') >= 0
// // if ( (!isXml && entryPath.indexOf(iconPath) != -1) || (isXml && entry.path.indexOf(maxSizePath) != -1)) {
// // console.log(entry.path)
// entry.pipe(etl.toFile(tempOut))
// resolve({ 'success': true, fileName: realPath })
// // } else {
// // entry.autodrain()
// // }
// }))
// }
const initialPromise = ext === '.xml' ?
unzip.Open.file(filepath).then(directory => {
const getMaxSizeImagePath = compose(get('path'), maxBy('compressedSize'),
filter(entry => entry.path.indexOf(dir) >= 0 && entry.path.indexOf('.png') >= 0), get('files'));
maxSizePath = getMaxSizeImagePath(directory)
}) : new Promise((resolve) => resolve())
initialPromise.then(() => {
fs.createReadStream(filepath)
.pipe(unzip.Parse())
.pipe(etl.map(entry => {
// 适配iconPath为ic_launcher.xml的情况
const entryPath = entry.path
const isXml = entryPath.indexOf('.xml') >= 0
if ( (!isXml && entryPath.indexOf(iconPath) != -1) || (isXml && entry.path.indexOf(maxSizePath) != -1)) {
console.log(entry.path)
entry.pipe(etl.toFile(tempOut))
resolve({ 'success': true, fileName: realPath })
} else {
function unzipAndMove(params: Params): void {
fs.createReadStream(params.filename)
.pipe(Extract({ path: `${process.cwd()}/${params.extractDir}` }))
.on(
"finish",
(): void => {
fs.renameSync(
`./${params.extractDir}/config.json`,
`./${params.extractDir}/config.json.default`
);
// Avoid "EPERM: operation not permitted" on Windows
setTimeout(function(): void {
fs.renameSync(`./${params.extractDir}`, `./${params.targetDir}`);
}, 1000);
}
);
}
// It would be simpler if the standard TMPDIR directory could be used
// to extract the zip files, but Docker on Mac is often not configured with
// access to the Mac's /var temp directory location
const baseDir = mountpointParent || process.cwd();
const tempDir = await tmp.dir({ dir: baseDir, mode: '0755', prefix: LAMBDA_TOOLS_WORK_PREFIX });
const tempDirName = tempDir.path;
const cleanup = async () => {
// Delete unzipped files
await fs.emptyDir(tempDirName);
await tempDir.cleanup();
};
try {
// eslint-disable-next-line security/detect-non-literal-fs-filename
const fsStream = fs.createReadStream(zipfile);
const unzipper = fsStream.pipe(unzip.Extract({
path: tempDirName
}));
await new Promise((resolve, reject) => {
const endOnError = (error) => reject(error);
unzipper.on('close', () => resolve());
fsStream.on('error', endOnError);
unzipper.on('error', endOnError);
});
return {
mountpoint: tempDirName,
cleanup
};
} catch (e) {
await cleanup();
progress(request(url), {throttle: 200}
).on('progress', function (state) {
event.sender.send('file-callback', JSON.stringify({type: 'progress', state: state}));
}).on('error', (err) => {
event.sender.send('file-callback', JSON.stringify({type: 'error', error: err}));
}).on('end', () => {
try {
if (!!chmodTarget) {
// Make the target executable
fs.chmodSync(chmodTarget, '755');
}
} catch (e) {
}
event.sender.send('file-callback', JSON.stringify({type: 'extracted', extracted: true}));
}).pipe(unzip.Extract({path: destination}));
});
return new Promise((resolve, reject) => {
const filePromises = [];
stream
.pipe(unzip.Parse())
.on('error', reject)
.on('close', () => {
Promise.all(filePromises).then(() => resolve(files));
})
// For some reason `end` event is not emitted.
// .on('end', () => {
// Promise.all(filePromises).then(() => resolve(files));
// })
.on('entry', (entry) => {
const file = files[entry.path];
if (file) {
let contents = '';
filePromises.push(new Promise((resolve) => {
entry
.on('data', data => contents += data.toString())
.on('end', () => {
resolve()
}
if (src.endsWith('.tar.gz')) {
targz.decompress(
{
src,
dest: destFolder,
tar: {
strip: 1,
},
},
err => (err ? reject(err) : finish()),
)
} else if (src.endsWith('.zip')) {
fs.createReadStream(src)
.pipe(unzipper.Parse())
.on('entry', function(entry) {
if (entry.type === 'Directory') {
return
}
// Do strip and resolve
const strip = entry.path.replace(/.*?\//, '')
entry.pipe(Writer(path.resolve(destFolder, strip)))
})
.on('error', reject)
.on('close', finish)
}
})
}