Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

更改七牛上传库为qn库 #13

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion extension.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { window, commands, workspace } = require('vscode')
const path = require('path')
const qnUpload = require('./lib/upload')
const qnUpload = require('./lib/uploadqn')

const upload = (config, fsPath) => {
if(!fsPath) return
Expand Down
74 changes: 74 additions & 0 deletions lib/uploadqn.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
const qiniu = require('qn')
const path = require('path')
const url = require('url')

// 默认参数
const formatParam = (file, mdFileName) => {
const dt = new Date()
const y = dt.getFullYear()
const m = dt.getMonth() + 1
const d = dt.getDate()
const h = dt.getHours()
const mm = dt.getMinutes()
const s = dt.getSeconds()

const date = `${y}${m}${d}`
var ext = path.extname(file)

return {
date,
dateTime: `${date}${h}${mm}${s}`,
fileName: path.basename(file, ext),
ext,
mdFileName
}
}

const formatString = (tplString, data) => {
const keys = Object.keys(data)
const values = keys.map(k => data[k])

return new Function(keys.join(','), 'return `' + tplString + '`').apply(null, values)
}

module.exports = ({
access_key,
secret_key,
bucket,
domain,
remotePath }, file, mdFile) => {

var client = qiniu.create({
accessKey: access_key,
secretKey: secret_key,
bucket: bucket,
origin: domain,
// timeout: 3600000, // default rpc timeout: one hour, optional
// if your app outside of China, please set `uploadURL` to `http://up.qiniug.com/`
// uploadURL: 'http://up.qiniu.com/',
});

// 预设参数值
const param = formatParam(file, mdFile)
//上传到七牛后保存的文件名
const saveFile = formatString(remotePath + '${ext}', param)

return new Promise((resolve, reject) => {

// upload a file with custom key
client.uploadFile(file, {key: saveFile}, function (err, res) {
if (!err) {
// 上传成功, 处理返回值
resolve({
name: path.basename(res.key, param.ext),
url: url.resolve(domain, saveFile)
})
} else {
// 上传失败, 处理返回代码
reject(err)
}
});
})
}


Loading