-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #107 from LetTTGACO/img2cos-master
feat(图床): 图片替换支持腾讯云图床、阿里云图床、七牛云图床
- Loading branch information
Showing
10 changed files
with
351 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
'use strict'; | ||
|
||
// 腾讯云图床 | ||
const COS = require('cos-nodejs-sdk-v5'); | ||
const out = require('../../lib/out'); | ||
|
||
const secretId = process.env.SECRET_ID; | ||
const secretKey = process.env.SECRET_KEY; | ||
|
||
|
||
class CosClient { | ||
constructor(config) { | ||
this.config = config; | ||
this.imageBedInstance = new COS({ | ||
SecretId: secretId, // 身份识别ID | ||
SecretKey: secretKey, // 身份秘钥 | ||
}); | ||
} | ||
|
||
static getInstance(config) { | ||
if (!this.instance) { | ||
this.instance = new CosClient(config); | ||
} | ||
return this.instance; | ||
} | ||
|
||
/** | ||
* 检查图床是否已经存在图片,存在则返回url,不存在返回空 | ||
* | ||
* @param {string} fileName 文件名 | ||
* @return {Promise<string>} 图片url | ||
*/ | ||
async hasImage(fileName) { | ||
try { | ||
await this.imageBedInstance.headObject({ | ||
Bucket: this.config.bucket, // 存储桶名字(必须) | ||
Region: this.config.region, // 存储桶所在地域,必须字段 | ||
Key: `${this.config.prefixKey}/${fileName}`, // 文件名 必须 | ||
}); | ||
return `https://${this.config.bucket}.cos.${this.config.region}.myqcloud.com/${this.config.prefixKey}/${fileName}`; | ||
} catch (e) { | ||
return ''; | ||
} | ||
} | ||
|
||
/** | ||
* 上传图片到图床 | ||
* | ||
* @param {Buffer} imgBuffer 文件buffer | ||
* @param {string} fileName 文件名 | ||
* @return {Promise<string>} 图床的图片url | ||
*/ | ||
async uploadImg(imgBuffer, fileName) { | ||
try { | ||
const res = await this.imageBedInstance.putObject({ | ||
Bucket: this.config.bucket, // 存储桶名字(必须) | ||
Region: this.config.region, // 存储桶所在地域,必须字段 | ||
Key: `${this.config.prefixKey}/${fileName}`, // 文件名 必须 | ||
StorageClass: 'STANDARD', // 上传模式(标准模式) | ||
Body: imgBuffer, // 上传文件对象 | ||
}); | ||
return `https://${res.Location}`; | ||
} catch (e) { | ||
out.error(`上传图片失败,请检查: ${e}`); | ||
process.exit(-1); | ||
} | ||
} | ||
} | ||
|
||
module.exports = CosClient; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
'use strict'; | ||
|
||
const CosClient = require('./cos'); | ||
const OssClient = require('./oss'); | ||
const QiniuClient = require('./qiniu'); | ||
const out = require('../../lib/out'); | ||
|
||
// 目前已适配图床列表 | ||
const imageBedList = [ 'qiniu', 'cos', 'oss' ]; | ||
|
||
class ImageBeds { | ||
constructor(config) { | ||
this.config = config; | ||
this.imageBedInstance = this.getImageBedInstance(config.imageBed); | ||
} | ||
|
||
static getInstance(config) { | ||
if (!this.instance) { | ||
this.instance = new ImageBeds(config); | ||
} | ||
return this.instance; | ||
} | ||
|
||
/** | ||
* 获取图床对象的实例 | ||
* | ||
* @param {string} imageBed 图床类型: cos | oss | ||
* @return {any} 图床实例 | ||
*/ | ||
getImageBedInstance(imageBed) { | ||
if (!imageBedList.includes(imageBed)) { | ||
out.error(`imageBed配置错误,目前只支持${imageBedList.toString()}`); | ||
process.exit(-1); | ||
} | ||
switch (imageBed) { | ||
case 'cos': | ||
return CosClient.getInstance(this.config); | ||
case 'oss': | ||
return OssClient.getInstance(this.config); | ||
case 'qiniu': | ||
return QiniuClient.getInstance(this.config); | ||
default: | ||
return QiniuClient.getInstance(this.config); | ||
} | ||
} | ||
|
||
/** | ||
* 检查图床是否已经存在图片,存在则返回url | ||
* | ||
* @param {string} fileName 文件名 | ||
* @return {Promise<string>} 图片url | ||
*/ | ||
async hasImage(fileName) { | ||
return await this.imageBedInstance.hasImage(fileName); | ||
} | ||
|
||
/** | ||
* 上传图片到图床 | ||
* | ||
* @param {Buffer} imgBuffer 文件buffer | ||
* @param {string} fileName 文件名 | ||
* @return {Promise<string>} 图床的图片url | ||
*/ | ||
async uploadImg(imgBuffer, fileName) { | ||
return await this.imageBedInstance.uploadImg(imgBuffer, fileName); | ||
} | ||
|
||
} | ||
|
||
module.exports = ImageBeds; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
'use strict'; | ||
|
||
// 阿里云图床 | ||
const OSS = require('ali-oss'); | ||
const out = require('../../lib/out'); | ||
|
||
const secretId = process.env.SECRET_ID; | ||
const secretKey = process.env.SECRET_KEY; | ||
|
||
|
||
class OssClient { | ||
constructor(config) { | ||
this.config = config; | ||
this.imageBedInstance = new OSS({ | ||
bucket: config.bucket, | ||
// yourRegion填写Bucket所在地域。以华东1(杭州)为例,Region填写为oss-cn-hangzhou。 | ||
region: config.region, | ||
// 阿里云账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维,请登录RAM控制台创建RAM用户。 | ||
accessKeyId: secretId, | ||
accessKeySecret: secretKey, | ||
}); | ||
} | ||
|
||
static getInstance(config) { | ||
if (!this.instance) { | ||
this.instance = new OssClient(config); | ||
} | ||
return this.instance; | ||
} | ||
|
||
/** | ||
* 检查图床是否已经存在图片,存在则返回url,不存在返回空 | ||
* | ||
* @param {string} fileName 文件名 | ||
* @return {Promise<string>} 图片url | ||
*/ | ||
async hasImage(fileName) { | ||
try { | ||
await this.imageBedInstance.head(`${this.config.prefixKey}/${fileName}`); | ||
return `https://${this.config.bucket}.${this.config.region}.aliyuncs.com/${this.config.prefixKey}/${fileName}`; | ||
} catch (e) { | ||
return ''; | ||
} | ||
} | ||
|
||
/** | ||
* 上传图片到图床 | ||
* | ||
* @param {Buffer} imgBuffer 文件buffer | ||
* @param {string} fileName 文件名 | ||
* @return {Promise<string>} 图床的图片url | ||
*/ | ||
async uploadImg(imgBuffer, fileName) { | ||
try { | ||
const res = await this.imageBedInstance.put(`${this.config.prefixKey}/${fileName}`, imgBuffer); | ||
return res.url; | ||
} catch (e) { | ||
out.error(`上传图片失败,请检查: ${e}`); | ||
process.exit(-1); | ||
} | ||
} | ||
} | ||
|
||
module.exports = OssClient; |
Oops, something went wrong.