62 lines
1.6 KiB
JavaScript
62 lines
1.6 KiB
JavaScript
const crypto = require("crypto");
|
|
|
|
async function dogecloudApi(apiPath, data = {}) {
|
|
const accessKey = process.env.DOGECLOUD_ACCESS_KEY;
|
|
const secretKey = process.env.DOGECLOUD_SECRET_KEY;
|
|
|
|
if (!accessKey || !secretKey) {
|
|
console.error("Error: DOGECLOUD_ACCESS_KEY or DOGECLOUD_SECRET_KEY not set.");
|
|
process.exit(1);
|
|
}
|
|
|
|
const body = JSON.stringify(data);
|
|
const signStr = apiPath + "\n" + body;
|
|
const sign = crypto.createHmac("sha1", secretKey).update(signStr).digest("hex");
|
|
const authorization = `TOKEN ${accessKey}:${sign}`;
|
|
|
|
const res = await fetch(`https://api.dogecloud.com${apiPath}`, {
|
|
method: "POST",
|
|
headers: {
|
|
Authorization: authorization,
|
|
"Content-Type": "application/json",
|
|
},
|
|
body,
|
|
});
|
|
return res.json();
|
|
}
|
|
|
|
async function main() {
|
|
const cdnUrlListStr = process.env.CDN_URL_LIST;
|
|
if (!cdnUrlListStr) {
|
|
console.log("Warning: CDN_URL_LIST not found or empty.");
|
|
return;
|
|
}
|
|
|
|
const urlList = cdnUrlListStr
|
|
.replace(/\n/g, ",")
|
|
.split(",")
|
|
.map((u) => u.trim())
|
|
.filter(Boolean);
|
|
|
|
if (urlList.length === 0) {
|
|
console.log("No URLs to refresh.");
|
|
return;
|
|
}
|
|
|
|
console.log(`Refreshing ${urlList.length} URLs...`);
|
|
|
|
const result = await dogecloudApi("/cdn/refresh/add.json", {
|
|
rtype: "path",
|
|
urls: JSON.stringify(urlList),
|
|
});
|
|
|
|
if (result && result.code === 200) {
|
|
console.log(`Refresh task submitted. Task ID: ${result.data.task_id}`);
|
|
} else {
|
|
console.error(`Failed to submit refresh task. Response: ${JSON.stringify(result)}`);
|
|
process.exit(1);
|
|
}
|
|
}
|
|
|
|
main();
|