Skip to content

Commit

Permalink
fix(upload): github图床上传问题
Browse files Browse the repository at this point in the history
  • Loading branch information
LetTTGACO committed Oct 18, 2022
1 parent 2f92bc2 commit d84d2ad
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 4 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,10 @@ imgCdn 语雀图片转图床配置说明
>
> 例如 'host': `upyun.1874.cool``https://upyun.1874.cool `
> Github图床的默认域名是`raw.githubusercontent.com`,如果要使用jsdelivr进行加速,可以配置`host``cdn.jsdelivr.net`
>
> 例如 'host': `cdn.jsdelivr.net`
> bucket和region说明
>
> [获取腾讯云的bucket和region](https://console.cloud.tencent.com/cos/bucket),示例:{ bucket: "blog", region: "ap-guangzhou" }
Expand Down
25 changes: 22 additions & 3 deletions util/imageBeds/github.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,18 @@ const secretKey = process.env.SECRET_KEY;
class GithubClient {
constructor(config) {
this.config = config;
this.init();
}

init() {
if (!this.config.host) {
out.warn('未指定加速域名,将使用默认域名:https://raw.githubusercontent.com');
}
// 如果指定了加速域名
if (this.config.host && this.config.host.includes('cdn.jsdelivr.net')) {
this.config.host = 'https://cdn.jsdelivr.net';
out.info(`图床域名:${this.config.host}`);
}
}

static getInstance(config) {
Expand Down Expand Up @@ -39,10 +51,16 @@ class GithubClient {
Authorization: `token ${secretKey}`,
},
});
if (result.status === 200) {
return result.data.download_url;
if (result.status === 200 || result.status === 201) {
if (this.config.host) {
return `${this.config.host}/gh/${secretId}/${this.config.bucket}/${this.config.prefixKey}/${fileName}`;
}
if (method === 'GET') {
return result.data.download_url;
}
return result.data.content.download_url;
}
out.warn(`请求图片失败,请检查: ${transformRes(result)}`);
method === 'PUT' && out.warn(`请求图片失败,请检查: ${transformRes(result)}`);
return '';
} catch (error) {
out.warn(`请求图片失败,请检查: ${transformRes(error)}`);
Expand Down Expand Up @@ -78,6 +96,7 @@ class GithubClient {
const base64File = imgBuffer.toString('base64');
const imgUrl = await this._fetch('PUT', fileName, base64File);
if (imgUrl) return imgUrl;
out.error('上传图片失败,请检查');
process.exit(-1);
} catch (e) {
out.error(`上传图片失败,请检查: ${transformRes(e)}`);
Expand Down
1 change: 1 addition & 0 deletions util/imageBeds/qiniu.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class QiniuClient {
out.error('使用七牛云时,需要在imgCdn中指定域名host');
process.exit(-1);
}
out.info(`图床域名:${this.config.host}`);
const mac = new qiniu.auth.digest.Mac(secretId, secretKey);
const putPolicy = new qiniu.rs.PutPolicy({ scope: this.config.bucket }); // 配置
this.uploadToken = putPolicy.uploadToken(mac); // 获取上传凭证
Expand Down
1 change: 1 addition & 0 deletions util/imageBeds/upyun.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class UPClient {
// 如果不指定协议,默认使用http
if (!this.config.host.startsWith('http')) {
this.config.host = `http://${this.config.bucket}`;
out.info(`图床域名:${this.config.host}`);
}
this.imageBedInstance = new upyun.Client(new upyun.Service(this.config.bucket, secretId, secretKey));
}
Expand Down
9 changes: 8 additions & 1 deletion util/img2cdn.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,17 @@ async function img2Cdn(article) {
try {
// 4。检查图床是否存在该文件
let url = await imageBed.hasImage(fileName);
let exists = true;
// 5。如果图床已经存在,直接替换;如果图床不存在,则先上传到图床,再将原本的语雀url进行替换
if (!url) {
url = await imageBed.uploadImg(imgBuffer, fileName);
exists = false;
}
return {
originalUrl: matchYuqueImgUrl,
yuqueRealImgUrl: yuqueImgUrl,
url,
exists,
};
} catch (e) {
out.error(`访问图床出错,请检查配置: ${e}`);
Expand All @@ -118,7 +121,11 @@ async function img2Cdn(article) {
urlList.forEach(function(url) {
if (url) {
article.body = article.body.replace(url.originalUrl, `![](${url.url})`);
out.info(`replace ${url.yuqueRealImgUrl} to ${url.url}`);
if (url.exists) {
out.info(`图片已存在 skip: ${url.url}`);
} else {
out.info(`replace ${url.yuqueRealImgUrl} to ${url.url}`);
}
}
});
return article;
Expand Down

0 comments on commit d84d2ad

Please sign in to comment.