Skip to content

Commit

Permalink
feat: 使用代理上传
Browse files Browse the repository at this point in the history
  • Loading branch information
lisonge committed Oct 8, 2023
1 parent c43e78e commit 62b8236
Showing 1 changed file with 30 additions and 4 deletions.
34 changes: 30 additions & 4 deletions src/utils/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,39 @@ type UploadPoliciesAssetsRsonpse = {

export const uploadPoliciesAssets = async (
bf: ArrayBuffer,
fileName?: string,
) => {
): Promise<GithubPoliciesAsset> => {
const [name, content_type] = (() => {
if (isPngBf(bf)) {
return [fileName || `file.png`, `image/png`];
return [`file.png`, `image/png`];
} else if (isZipBf(bf)) {
return [fileName || `file.zip`, `application/x-zip-compressed`];
return [`file.zip`, `application/x-zip-compressed`];
}
throw new Error(`invalid buffer, it must be png or zip`);
})();
const file = new File([bf], name, { type: content_type });
const resp = await fetch(
'https://github-upload-assets.lisonge.workers.dev/',
{
method: 'POST',
body: obj2form({ file }),
},
);
const xRpcOk = resp.headers.get('X_RPC_OK');
if (xRpcOk === 'true') {
return resp.json();
} else if (xRpcOk === 'false') {
throw new Error((await resp.json()).message);
} else {
throw new Error(await resp.text());
}
};

export const uploadPoliciesAssetsByExtension = async (bf: ArrayBuffer) => {
const [name, content_type] = (() => {
if (isPngBf(bf)) {
return [`file.png`, `image/png`];
} else if (isZipBf(bf)) {
return [`file.zip`, `application/x-zip-compressed`];
}
throw new Error(`invalid buffer, it must be png or zip`);
})();
Expand Down

0 comments on commit 62b8236

Please sign in to comment.