Skip to content

Commit

Permalink
图片上传七牛云
Browse files Browse the repository at this point in the history
1. 增加图片上传七牛云
2. 删除七牛云指定图片
  • Loading branch information
warriorBrian committed Jan 18, 2019
1 parent 4459aa3 commit de51c76
Show file tree
Hide file tree
Showing 26 changed files with 179 additions and 2,372 deletions.
3 changes: 3 additions & 0 deletions nuxt/assets/css/Markdown.css
Expand Up @@ -111,3 +111,6 @@ blockquote p {
.hljs-strong {
font-weight: bold;
}
.markdown-body img {
width: 100%
}
94 changes: 94 additions & 0 deletions server/controller/articleImg.js
@@ -0,0 +1,94 @@
const qiniu = require('qiniu');
const formidable = require('formidable')

/**
* private API
* @method token
* @param {object} 获取七牛云上传图片token
* @return {token|null} token
*/

let getToken = async (ctx) => {
let accessKey = '' // 源码删除:七牛云获取 ak,必须配置
let secretKey = '' // 源码删除:七牛云获取 sk, 必须配置
let mac = new qiniu.auth.digest.Mac(accessKey, secretKey)
let options = {
scope: 'blog', // 对应七牛云存储空间名称
};
let putPolicy = new qiniu.rs.PutPolicy(options);
let uploadToken = putPolicy.uploadToken(mac);
ctx.body = uploadToken
}

/**
* private API
* @method upload Images
* @param {object} 七牛云上传图片
* @return {object|null} img hash filename
*/

let articleImgUpload = async (ctx) => {
let form = formidable.IncomingForm();
let {respErr, respBody, respInfo, filename} = await new Promise((resolve) => {
form.parse(ctx.req, function (err, fields, file) {
let localFile = file.file.path
let config = new qiniu.conf.Config();
let formUploader = new qiniu.form_up.FormUploader(config);
let putExtra = new qiniu.form_up.PutExtra();
let key= file.file.name;
formUploader.putFile(fields.token, key, localFile, putExtra, function(respErr, respBody, respInfo) {
resolve({
respErr,
respBody,
respInfo,
filename: key
})
});
})
})
ctx.status = respInfo.statusCode
ctx.body = {
respErr,
img: `http://images.brianlee.cn/${respBody.key}`,
hash: respBody.hash,
status: respInfo.statusCode,
filename
}
}

/**
* private API
* @method delete Images
* @param {object} filename 接受文件名称
* @return {object|null} status result
*/

let delArticleImg = async (ctx) => {
try {
let bucket = "blog"; // 对应储存空间名称
let request = ctx.request.body
let accessKey = '' // // 源码删除:七牛云获取 ak,必须配置
let secretKey = '' // // 源码删除:七牛云获取 sk,必须配置
let mac = new qiniu.auth.digest.Mac(accessKey, secretKey);
let config = new qiniu.conf.Config();
let bucketManager = new qiniu.rs.BucketManager(mac, config);
let {error, respInfo} = await new Promise((resolve) => {
bucketManager.delete(bucket, request.filename, function(err, respBody, respInfo) {
resolve({error: err,respBody, respInfo})
});
})
ctx.body = {
error,
status: respInfo.statusCode,
result: respInfo.data
}
} catch (error) {
ctx.body = error
}
}

module.exports = {
getToken,
articleImgUpload,
delArticleImg
}
4 changes: 3 additions & 1 deletion server/package.json
Expand Up @@ -12,6 +12,7 @@
"chalk": "^2.4.1",
"debug": "^2.6.3",
"ejs": "~2.3.3",
"formidable": "^1.2.1",
"koa": "^2.2.0",
"koa-bodyparser": "^3.2.0",
"koa-convert": "^1.2.0",
Expand All @@ -24,7 +25,8 @@
"koa-session": "^5.8.1",
"koa-static": "^3.0.0",
"koa-views": "^5.2.1",
"mongoose": "^5.1.0"
"mongoose": "^5.1.0",
"qiniu": "^7.2.1"
},
"devDependencies": {
"nodemon": "^1.8.1"
Expand Down
1 change: 0 additions & 1 deletion server/public/index.html

This file was deleted.

This file was deleted.

This file was deleted.

Binary file removed server/public/static/favicon.ico
Binary file not shown.
Binary file removed server/public/static/fonts/fontello.068ca2b.ttf
Binary file not shown.
Binary file removed server/public/static/fonts/fontello.e73a064.eot
Binary file not shown.
Binary file removed server/public/static/fonts/ionicons.05acfdb.woff
Binary file not shown.
Binary file removed server/public/static/fonts/ionicons.24712f6.ttf
Binary file not shown.
Binary file removed server/public/static/fonts/ionicons.2c2ae06.eot
Binary file not shown.
Binary file removed server/public/static/images/iview.png
Binary file not shown.
Binary file removed server/public/static/images/login.jpeg
Binary file not shown.
Binary file removed server/public/static/images/stop.png
Binary file not shown.
72 changes: 0 additions & 72 deletions server/public/static/img/fontello.9354499.svg

This file was deleted.

2,230 changes: 0 additions & 2,230 deletions server/public/static/img/ionicons.621bd38.svg

This file was deleted.

2 changes: 0 additions & 2 deletions server/public/static/js/app.66fba9359361ad508036.js

This file was deleted.

1 change: 0 additions & 1 deletion server/public/static/js/app.66fba9359361ad508036.js.map

This file was deleted.

2 changes: 0 additions & 2 deletions server/public/static/js/manifest.2ae2e69a05c33dfc65f8.js

This file was deleted.

This file was deleted.

0 comments on commit de51c76

Please sign in to comment.