post.js
2.06 KB
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
const fs = require('fs');
const path = require('path');
const axios = require('axios');
const FormData = require('form-data');
const distFile = './fw-cms.tar.gz';
/**
* 上传apk文件
* @param {string} path apk文件信息
*/
async function uploadFile() {
if (!fs.existsSync(distFile)) {
console.error(' 🚨 文件不存在,请检查是否打包成功');
return;
}
const formData = new FormData();
formData.append('file', fs.createReadStream(distFile));
const config = {
method: 'post',
url: 'https://upload.feewee.cn/upload',
headers: {
...formData.getHeaders(),
},
onUploadProgress: (progressEvent) => {
const { loaded, total } = progressEvent;
console.info(`上传进度: ${Math.round((loaded / total) * 100)}%`);
},
maxContentLength: Infinity,
maxBodyLength: Infinity,
data: formData,
timeout: 600000,
};
try {
const response = await axios.request(config);
const res = response.data;
if (res.success) {
return res.data;
} else {
throw Error('文件上传失败');
}
} catch (error) {
console.error(error);
let code, errorMsg;
if (error.response) {
code = (error.response.data && error.response.data.code) || error.response.status;
errorMsg = (error.response.data && error.response.data.message) || error.response.statusText;
} else {
code = error.code || -1;
errorMsg = error.message || 'UNKNOWN ERROR';
}
console.error(code, errorMsg);
}
return '';
}
async function run() {
const fid = await uploadFile();
if (!fid) {
console.info('文件上传失败');
return;
}
// 写入文件fid到文件
if (fs.existsSync('./build.txt')) {
if (fs.statSync('./build.txt').isFile()) {
fs.unlinkSync('./build.txt'); //删除文件
}
}
fs.writeFileSync(path.join(__dirname, 'build.txt'), `https://gate.feewee.cn/file/show?fid=${fid}`);
// 写入文件fid到文件
if (fs.existsSync(distFile)) {
if (fs.statSync(distFile).isFile()) {
fs.unlinkSync(distFile); //删除文件
}
}
}
run();