diff --git a/README.md b/README.md index d16758c..0a89eb2 100644 --- a/README.md +++ b/README.md @@ -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" } diff --git a/util/imageBeds/github.js b/util/imageBeds/github.js index 9669d3b..78b1d9c 100644 --- a/util/imageBeds/github.js +++ b/util/imageBeds/github.js @@ -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) { @@ -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)}`); @@ -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)}`); diff --git a/util/imageBeds/qiniu.js b/util/imageBeds/qiniu.js index 7c7ce5c..d3d7df7 100644 --- a/util/imageBeds/qiniu.js +++ b/util/imageBeds/qiniu.js @@ -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); // 获取上传凭证 diff --git a/util/imageBeds/upyun.js b/util/imageBeds/upyun.js index 4f1b436..a6779ee 100644 --- a/util/imageBeds/upyun.js +++ b/util/imageBeds/upyun.js @@ -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)); } diff --git a/util/img2cdn.js b/util/img2cdn.js index 32d778f..b929b5a 100644 --- a/util/img2cdn.js +++ b/util/img2cdn.js @@ -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}`); @@ -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;