This commit is contained in:
@@ -0,0 +1,83 @@
|
||||
import COS from "cos-nodejs-sdk-v5";
|
||||
import { readFile } from "node:fs/promises";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import path from "node:path";
|
||||
import { buildReleaseUploadTargets } from "../release-assets.mjs";
|
||||
|
||||
export async function uploadReleaseAssets(options = {}) {
|
||||
const env = options.env ?? process.env;
|
||||
const projectRoot = options.projectRoot ?? resolveProjectRoot();
|
||||
const releaseVersion = options.releaseVersion ?? env.EXTENSION_VERSION ?? env.DRONE_TAG;
|
||||
|
||||
if (!releaseVersion) {
|
||||
throw new Error("release version is required for COS upload");
|
||||
}
|
||||
|
||||
const cos = options.cosClient ?? createCosClient(env);
|
||||
const targets =
|
||||
options.targets ??
|
||||
buildReleaseUploadTargets({
|
||||
projectRoot,
|
||||
releaseVersion
|
||||
});
|
||||
|
||||
for (const target of targets) {
|
||||
const body = await readFile(target.localPath);
|
||||
await putObjectAsync(cos, {
|
||||
Bucket: getRequiredEnv(env, "COS_BUCKET"),
|
||||
Body: body,
|
||||
ContentType: getContentType(target.cosKey),
|
||||
Key: target.cosKey,
|
||||
Region: getRequiredEnv(env, "COS_REGION")
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
async function putObjectAsync(client, params) {
|
||||
return await new Promise((resolve, reject) => {
|
||||
client.putObject(params, (error, data) => {
|
||||
if (error) {
|
||||
reject(error);
|
||||
return;
|
||||
}
|
||||
|
||||
resolve(data);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function createCosClient(env) {
|
||||
return new COS({
|
||||
SecretId: getRequiredEnv(env, "COS_SECRET_ID"),
|
||||
SecretKey: getRequiredEnv(env, "COS_SECRET_KEY")
|
||||
});
|
||||
}
|
||||
|
||||
function getContentType(key) {
|
||||
if (key.endsWith(".json")) {
|
||||
return "application/json";
|
||||
}
|
||||
|
||||
if (key.endsWith(".pdf")) {
|
||||
return "application/pdf";
|
||||
}
|
||||
|
||||
if (key.endsWith(".zip")) {
|
||||
return "application/zip";
|
||||
}
|
||||
|
||||
return "application/octet-stream";
|
||||
}
|
||||
|
||||
function getRequiredEnv(env, name) {
|
||||
const value = env[name];
|
||||
if (!value) {
|
||||
throw new Error(`${name} is required`);
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
function resolveProjectRoot() {
|
||||
return path.resolve(path.dirname(fileURLToPath(import.meta.url)), "../..");
|
||||
}
|
||||
Reference in New Issue
Block a user