当前位置:网站首页>Node JS processes pictures asynchronously and JSON text asynchronously
Node JS processes pictures asynchronously and JSON text asynchronously
2022-07-21 05:28:00 【O sunflower o】
node-js Processing pictures asynchronously , Asynchronous processing json Text
var path = require("path");
var fs = require("fs");
// Create directory
function mkdirsSync(dirname) {
if (fs.existsSync(dirname)) {
return true;
} else {
if (mkdirsSync(path.dirname(dirname))) {
fs.mkdirSync(dirname);
return true;
}
}
}
let outdir = path.join(__dirname, 'out');
mkdirsSync(outdir);
/** Assignment copy move A to B */
function copyFile(fromUrl, outUrl) {
let file = fs.createReadStream(fromUrl);
let out = fs.createWriteStream(outUrl);
file.pipe(out);
}
/** Rename */
function rename(oldName) {
let newName = '';
let names = oldName.split(".")
for (let i = 0; i < names[0].length; i++) {
if (/^[a-zA-Z]*$/.test(names[0][i])) {// There are letters , The obtained string is cleared from
newName = '';
} else {
newName += names[0][i];
}
}
newName = Number(newName) - 6444;// rename ,8000->1556
if (names[1]) {
return `RND${newName}.${names[1]}`;
}
return `${newName}`;
}
// __dirname Current working directory
/** Processing images and json file */
var dirPath = path.join(__dirname, "img");
let fileNames = fs.readdirSync(dirPath);
let pngArr = [];
let jsonArr = []
for (let i = 0; i < fileNames.length; i++) {
let extname = path.extname(path.join(dirPath, fileNames[i]));
if (extname == ".png") {// Processing images , rename , And copy it to a new path
pngArr.push(fileNames[i]);
} else {
jsonArr.push(fileNames[i]);
}
}
let __time = new Date().getTime();
let _png = 0;
let _json = 0;
/** Copy pictures asynchronously , When one image is copied, the next image is processed */
var copyPngfile = () => {
let pngName = pngArr[_png];
if (!pngName) {
console.log(` Co processing files ${_png + _json} individual ,png file ${_png} individual . Total time : ${new Date().getTime() - __time}`);
return;
}
let _time = new Date().getTime();
let newName = rename(pngName);
let file = fs.createReadStream(path.join(dirPath, pngName));
let out = fs.createWriteStream(path.join(outdir, newName));
out.on("finish", () => {
console.log("Png Time consuming : ", new Date().getTime() - _time, " Total time : ", new Date().getTime() - __time);
_png++;
copyPngfile();
})
file.pipe(out);
}
copyPngfile();
/** Processing files asynchronously , After processing one file, you can process another file */
var copyOtherfile = () => {
let fileName = jsonArr[_json];
if (!fileName) {
console.log(` Co processing files ${_png + _json} individual , no suffix json${_json} individual . Total time : ${new Date().getTime() - __time}`);
return;
}
let newName = rename(fileName);
let _time = new Date().getTime();
fs.readFile(path.join(dirPath, fileName), "utf-8", (err, res) => {
let content;
try {
content = JSON.parse(res);
content.name = `#VCR ${newName}`;
content.image = `https://d9lekpvd0q3e4.cloudfront.net/img/RND${newName}.png`
} catch(e) {
content = res;
}
fs.writeFile(path.join(outdir, newName), JSON.stringify(content), "utf-8", (err) => {
if (err) {
console.error(err);
}
console.log("Other Time consuming : ", new Date().getTime() - _time, " Total time : ", new Date().getTime() - __time);
_json++;
copyOtherfile();
});
})
}
copyOtherfile();
json file 8000.json
{"name":"#VCR 8000","description":"We aim to be the Top crypto poker platform.The LIMITED collection of 10,000 unique NFTs will be the key to bond the community. The full version game itself will be launched no less than Q3 2022 with profit sharing scheme.","attributes":[{"trait_type":"background","value":"1"},{"trait_type":"behind","value":"24 "},{"trait_type":"body","value":"Franky"},{"trait_type":"expression","value":"Franky1"},{"trait_type":"tie","value":"necklace 8"},{"trait_type":"coat","value":"1"},{"trait_type":"hair","value":"1"},{"trait_type":"hand","value":"1"},{"trait_type":"hand-hold","value":"1"}],"image":"https://d9lekpvd0q3e4.cloudfront.net/img/RND8000.png"}
picture 8000.json
边栏推荐
猜你喜欢
【el-upload实现一个修改头像的功能】
2022年第二季度 Web3 成为加密风投最感兴趣的领域
Mysql 常用函数汇总,很实用,面试时常会遇到
Routing of upper layer loops in VRRP
Addition, deletion, query and modification of MySQL [advanced]
Common classes under JUC package
小程序毕设作品之微信运动场地预约小程序毕业设计(5)任务书
Entity annotation - batch generate 10000 test data
Qt 多线程实现的两种方式 线程实现
go runtime 包
随机推荐
控制台C#飞行棋小项目
go 原子操作
About the processing of single cell TPM and count data part1
Using hashes to solve problems
研究一下 JSON.parse(JSON.stringify(obj))
enable_if和偏特化
搜索引擎倒在Web3.0?
Emplacement non disponible - fichier ou répertoire corrompu et illisible
R语言 字符串提起数字,日期的最佳方法
Routing of upper layer loops in VRRP
含静态代码块及子父类的执行方法
Simple use of Supervisor
Openresty基本使用介绍
Role of GCC unsed and used
JUC包下的常见类
Addition, deletion, modification and query of MySQL table (primary level)
General packaging technology of GRE and mGRE
R 语言作业三
MySQL JDBC programming
Program yuan sent an article lamenting unemployment, which attracted program apes from various industries to comfort, laughing in the comment area