-
Notifications
You must be signed in to change notification settings - Fork 0
/
imageHandler.js
111 lines (107 loc) · 3.35 KB
/
imageHandler.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
const fse = require('fs-extra');
const { unique, downloadImg } = require('@imgcook/cli-utils');
const UPLOAD = require('./utils/upload');
const upload = new UPLOAD();
const uploadData = (file, filepath, option) => {
return new Promise(resolve => {
upload.uploadUrl = option.uploadUrl;
upload
.start(file, {
filepath: filepath
})
.then(res => {
resolve(res.data);
})
.catch(err => {
// console.log(JSON.stringify(err));
});
});
};
const loader = async options => {
let imgArr = [];
let { data, filePath } = options;
const { config } = options;
const panelDisplay = data.code.panelDisplay;
const moduleData = data.moduleData;
let index = 0;
if (config.value) {
filePath = filePath.replace(config.value, '')
}
for (const item of panelDisplay) {
let fileValue = item.panelValue;
const temporaryImages = `${(
new Date().getTime() + Math.floor(Math.random() * 10000)
).toString(30)}`;
imgArr = fileValue.match(
/(https?):\/\/[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|](\.png|\.jpg)/g
);
if (imgArr && imgArr.length > 0) {
imgArr = unique(imgArr);
const imgPath = `${filePath}/public/images`;
fse.ensureDirSync(imgPath);
let imgObj = [];
const imgrc = `${imgPath}/.imgrc`;
if (fse.existsSync(imgrc)) {
let imgConfig = fse.readFileSync(imgrc, 'utf8');
imgObj = JSON.parse(imgConfig) || [];
}
for (let idx = 0; idx < imgArr.length; idx++) {
if (!fse.existsSync(imgPath)) {
fse.mkdirSync(imgPath);
}
let suffix = imgArr[idx].split('.');
suffix = suffix[suffix.length - 1];
const imgName = `img_${moduleData.id}_${index}_${idx}.${suffix}`;
const imgPathItem = `${imgPath}/${imgName}`;
let curImgObj = {};
for (const item of imgObj) {
if (item.imgUrl === imgArr[idx]) {
curImgObj = item;
}
}
const reg = new RegExp(imgArr[idx], 'g');
if (!curImgObj.imgPath) {
await downloadImg(imgArr[idx], imgPathItem);
let newImgUrl = '';
if (options.config && options.config.uploadUrl !== '') {
const udata = await uploadData(
imgPathItem,
`imgcook-cli/${temporaryImages}/`,
options.config
);
fileValue = fileValue.replace(reg, udata.url);
newImgUrl = udata.url;
} else {
fileValue = fileValue.replace(reg, `/images/${imgName}`);
}
imgObj.push({
newImgUrl,
imgUrl: imgArr[idx],
imgPath: `/images/${imgName}`
});
} else {
if (options.config && options.config.uploadUrl !== '') {
fileValue = fileValue.replace(reg, curImgObj.newImgUrl);
} else {
fileValue = fileValue.replace(reg, curImgObj.imgPath);
}
}
}
if (imgObj.length > 0) {
fse.writeFileSync(imgrc, JSON.stringify(imgObj), 'utf8');
}
}
item.panelValue = fileValue;
index++;
}
const imgrcPath = `${filePath}/public/images/.imgrc`;
if (fse.existsSync(imgrcPath)) {
fse.unlinkSync(imgrcPath);
}
return { ...data, filePath, config };
};
module.exports = (...args) => {
return loader(...args).catch(err => {
console.log(err);
});
};