postcat/upload.js

88 lines
2.6 KiB
JavaScript
Raw Normal View History

2022-07-05 19:08:05 +08:00
const qiniu = require('qiniu');
2022-07-07 15:22:08 +08:00
const { AK, SK, bucket } = require('./qiniu_env.js');
2022-07-05 19:08:05 +08:00
const package = require('./package.json');
2022-07-07 15:22:08 +08:00
qiniu.conf.ACCESS_KEY = AK;
qiniu.conf.SECRET_KEY = SK;
2022-07-06 09:29:52 +08:00
2022-07-07 15:22:08 +08:00
// * 构建上传策略函数
2022-07-05 19:08:05 +08:00
const uptoken = (bucket, key) => new qiniu.rs.PutPolicy(bucket + ':' + key).token();
const toLatest = (name) => name.replace(/\d+\.\d+\.\d+/, 'latest');
2022-07-07 15:47:02 +08:00
const onlyName = (name) => name.replace(/release\//, '');
2022-07-05 19:08:05 +08:00
2022-07-07 15:22:08 +08:00
// * 构建客户端实例
2022-07-05 19:08:05 +08:00
const client = new qiniu.rs.Client();
// * 上传单个文件
const uploadFile = (token, file, localFile) =>
new Promise((resolve) => {
const extra = new qiniu.io.PutExtra();
qiniu.io.putFile(token, file, localFile, extra, (err) => {
2022-07-07 15:47:02 +08:00
console.log(err ? err : 'success');
2022-07-05 19:08:05 +08:00
return err ? resolve(false) : resolve(true);
});
});
// * 删除单个文件
2022-07-11 10:21:14 +08:00
const removeFile = (spaceName, file) =>
2022-07-05 19:08:05 +08:00
new Promise((resolve) => {
2022-07-11 10:21:14 +08:00
client.remove(spaceName, file, (err, ret) => {
2022-07-05 19:08:05 +08:00
return err ? resolve(false) : resolve(true);
});
});
// * 拷贝单个文件
const cpFile = (fromFile, toFile) =>
new Promise((resolve) => {
client.copy(bucket, fromFile, bucket, toFile, (err) => {
return err ? resolve(false) : resolve(true);
});
});
const version = package.version;
2022-07-11 00:02:27 +08:00
const fileList = [
'release/eoapi Setup ?.exe',
'release/eoapi-?.dmg',
'release/eoapi-?-arm64.dmg',
2022-07-11 10:49:34 +08:00
'release/eoapi-?-mac.zip',
2022-07-11 00:02:27 +08:00
'release/latest.yml',
'release/latest-mac.yml',
].map((it) => it.replace(/\?/, `${version}`));
2022-07-07 16:08:31 +08:00
// const fileList = ['releass/eoapi-Setup-?.png', 'releass/eoapi-?.png', 'releass/eoapi-?-arm64.png'].map((it) =>
// it.replace(/\?/, `${version}`)
// );
2022-07-05 19:08:05 +08:00
const app = async () => {
const uploadResult = await Promise.all(
fileList.map(async (it) => {
2022-07-11 00:02:27 +08:00
let isOK;
2022-07-05 19:08:05 +08:00
// * 生成上传 Token
2022-07-11 00:02:27 +08:00
try {
2022-07-11 10:49:34 +08:00
const token = uptoken(bucket, `${version}/${it.replace(/release\//, '')}`);
isOK = await uploadFile(token, `${version}/${it.replace(/release\//, '')}`, it);
2022-07-11 00:02:27 +08:00
} catch (error) {
console.log('error', error);
}
2022-07-05 19:08:05 +08:00
return Promise.resolve(isOK || false);
})
);
console.log('上传结果:', uploadResult);
const deleteResult = await Promise.all(
fileList.map(async (it) => {
const isOK = await removeFile(bucket, `latest/${toLatest(onlyName(it))}`);
Promise.resolve(isOK || false);
})
);
console.log('删除结果:', deleteResult);
const copyResult = await Promise.all(
fileList.map(async (it) => {
const isOK = await cpFile(`${version}/${onlyName(it)}`, `latest/${toLatest(onlyName(it))}`);
2022-07-07 15:22:08 +08:00
Promise.resolve(isOK || false);
2022-07-05 19:08:05 +08:00
})
);
console.log('拷贝结果', copyResult);
};
app();