Skip to content

Commit

Permalink
feta:统一七牛操作
Browse files Browse the repository at this point in the history
  • Loading branch information
willnewii committed Jul 8, 2017
1 parent 31b25b4 commit e3a1032
Show file tree
Hide file tree
Showing 7 changed files with 149 additions and 65 deletions.
40 changes: 20 additions & 20 deletions app/src/renderer/api/API.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,28 @@

import axios from 'axios'
import config from './config'
import qiniu from 'qiniu'
import * as cloudStorage from '../util/cloudStorage'

import Qs from 'qs'

class API {

constructor(view){
constructor(view) {
this.that = view;
}

method = {
getBuckets: qiniu.conf.RS_HOST + '/buckets',
getDomains: qiniu.conf.API_HOST + '/v6/domain/list',
getResources: qiniu.conf.RSF_HOST + '/list',
}
getBuckets: 'http://rs.qbox.me/buckets',
getDomains: 'http://api.qiniu.com/v6/domain/list',
getResources: 'http://rsf.qbox.me/list',
};

post(url, param) {
//config.data = param;
// return axios.post("", null, config);
let _url = url;
if (param)
_url = _url + '?' + Qs.stringify(param)
_url = _url + '?' + Qs.stringify(param);
return this._request(_url, 'post')
}

Expand All @@ -34,35 +35,34 @@ class API {

let _url = url;
if (param)
_url = _url + '?' + Qs.stringify(param)
_url = _url + '?' + Qs.stringify(param);

return this._request(_url, 'get')
}

_request(url, type, param) {
this.that.$Loading.start();
/* if (type === 'get') {
config.params = param;
} else {
config.data = param;
}*/

config.headers.Authorization = qiniu.util.generateAccessToken(url, null);
/* if (type === 'get') {
config.params = param;
} else {
config.data = param;
}*/
config.headers.Authorization = cloudStorage.httpAuthorization(url);
config.method = type;

let request ;
let request;
if (type === 'get') {
request = axios.get(url, config)
} else {
request = axios[type](url, null, config)
}

request.then((response)=>{
request.then((response) => {
this.that.$Loading.finish();
}).catch((error)=>{
}).catch((error) => {
this.that.$Loading.error();
})
return request ;
});
return request;
}

}
Expand Down
6 changes: 2 additions & 4 deletions app/src/renderer/components/Main.vue
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
</template>
<script>
import {mapGetters, mapActions} from 'vuex'
import qiniu from 'qiniu'
import * as cloudStorage from '../util/cloudStorage'
import * as types from '../vuex/mutation-types'
import storage from 'electron-json-storage'
Expand Down Expand Up @@ -127,9 +127,7 @@
if (!error) {
if (data.access_key && data.secret_key) {
if (callback) {
qiniu.conf.ACCESS_KEY = data.access_key;
qiniu.conf.SECRET_KEY = data.secret_key;
cloudStorage.init({access_key: data.access_key, secret_key: data.secret_key});
callback(data);
}
} else {
Expand Down
34 changes: 22 additions & 12 deletions app/src/renderer/components/Main/ClientHeader.vue
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@
<i-button type="text" @click="actionBtn(1)" v-if="bucketname">
<Icon type="arrow-swap" size="32"/>
</i-button>
<Input class="input-search" v-model="search" placeholder="搜索" @on-enter="doSearch(search)" v-if="bucketname"></Input>
<Input class="input-search" v-model="search" placeholder="搜索" @on-enter="doSearch(search)"
v-if="bucketname"></Input>
<Modal v-model="uploadModal.isShow" class-name='vertical-center-modal' title="上传对话框" @on-ok="doQiniuUploadFile">

<Input class='modal-url' v-if="uploadModal.type == 'fetch'" v-model="uploadModal.path"
Expand All @@ -56,7 +57,7 @@
</div>
</template>
<script>
import qiniu from 'qiniu'
import * as cloudStorage from '../../util/cloudStorage'
let ipc;
export default {
Expand Down Expand Up @@ -147,20 +148,29 @@
let that = this;
let key = this.uploadModal.prepend + this.uploadModal.input + this.uploadModal.fileName;
this.$Notice.info({
title: '文件上传中...',
desc: this.uploadModal.path,
});
let param = {
bucket: this.bucketname,
key: key,
path: this.uploadModal.path
};
let callback = (err, ret) => {
that.uploadResult(err, ret);
};
if (this.uploadModal.type === 'fetch') {
new qiniu.rs.Client().fetch(this.uploadModal.path, this.bucketname, key, (err, ret, res) => {
that.uploadResult(err, ret, res);
});
cloudStorage.fetch(param, callback)
} else {
let uptoken = new qiniu.rs.PutPolicy(this.bucketname + ":" + key).token();
let extra = new qiniu.io.PutExtra();
qiniu.io.putFile(uptoken, key, this.uploadModal.path, extra, function (err, ret, res) {
that.uploadResult(err, ret, res);
});
cloudStorage.upload(param, callback);
}
},
uploadResult(err, ret, res){
let key = this.uploadModal.prepend + this.uploadModal.input + this.uploadModal.fileName;
uploadResult(err, ret){
//let key = this.uploadModal.prepend + this.uploadModal.input + this.uploadModal.fileName;
this.uploadModal.input = '';
this.uploadModal.prepend = '';
Expand Down
26 changes: 13 additions & 13 deletions app/src/renderer/components/Main/ResourceTable.vue
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@
import {mapGetters} from 'vuex'
import * as types from '../../vuex/mutation-types'
import * as util from '../../util/util'
import qiniu from 'qiniu'
import * as cloudStorage from '../../util/cloudStorage'
import moment from 'moment'
export default {
Expand Down Expand Up @@ -162,7 +163,7 @@
this.$Message.info('文件路径以复制到剪贴板');
},
remove(index) {
this.deleteKey = this.files[index].key
this.deleteKey = this.files[index].key;
if (this.setup_deleteNoAsk) {
this.deleteNoAskModel = true;
console.log(this.setup_deleteNoAsk);
Expand All @@ -171,19 +172,18 @@
}
},
doRemove(){
new qiniu.rs.Client().remove(this.bucketname, this.deleteKey, (err, ret) => {
if (!err) {
this.$Message.info('移除成功');
if (!ret) {
ret = {
key: this.deleteKey
}
cloudStorage.remove({
bucket: this.bucketname,
key: this.deleteKey
}, (ret) => {
this.$Message.info('移除成功');
if (!ret) {
ret = {
key: this.deleteKey
}
this.$emit('on-update', ret, 'remove', event);
} else {
console.log(err);
}
});
this.$emit('on-update', ret, 'remove', event);
})
},
setTableSize(){
if (this.$parent) {
Expand Down
9 changes: 4 additions & 5 deletions app/src/renderer/components/SetToken.vue
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@
</div>
</template>
<script>
import qiniu from 'qiniu'
import * as cloudStorage from '../util/cloudStorage'
const storage = require('electron-json-storage');
import api from '../api/API'
let API ;
let API;
export default {
data () {
Expand Down Expand Up @@ -71,16 +71,15 @@
this.$refs[name].resetFields();
},
validateToken(access_key, secret_key){
qiniu.conf.ACCESS_KEY = access_key;
qiniu.conf.SECRET_KEY = secret_key;
cloudStorage.init({access_key: access_key, secret_key: secret_key});
API.get(API.method.getBuckets).then((response) => {
storage.set('qiniu_key', {
access_key: access_key,
secret_key: secret_key
}, (error) => {
console.log(error);
if (!error){
if (!error) {
this.$router.push({path: '/'});
}
});
Expand Down
22 changes: 11 additions & 11 deletions app/src/renderer/components/Tray.vue
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@
<div>
<div>{{item.key}}</div>
<div>
<i-button class='btn' type="primary" size="small" @click="open(item.key)">查看</i-button>
<i-button class='btn' type="primary" size="small" @click="show(item.key)">查看</i-button>
<i-button class='btn' type="primary" size="small" @click="copy(item.key)">复制路径</i-button>
<i-button class='btn' type="primary" size="small" @click="openInFolder(item.path)">源文件</i-button>
<i-button class='btn' type="primary" size="small" @click="openInFolder(item.path)">源文件
</i-button>
</div>
</div>

Expand All @@ -54,7 +55,7 @@
import {mapGetters, mapActions} from 'vuex'
import * as types from '../vuex/mutation-types'
import * as util from '../util/util'
import qiniu from 'qiniu'
import * as cloudStorage from '../util/cloudStorage'
import storage from 'electron-json-storage'
import api from '../api/API'
let API, ipc;
Expand Down Expand Up @@ -87,8 +88,7 @@
storage.get(types.APP.qiniu_key, (error, data) => {
if (!error) {
if (data.access_key && data.secret_key) {
qiniu.conf.ACCESS_KEY = data.access_key;
qiniu.conf.SECRET_KEY = data.secret_key;
cloudStorage.init({access_key: data.access_key, secret_key: data.secret_key});
API.get(API.method.getDomains, {tbl: this.bucket_name}).then((response) => {
that.domains = response.data;
Expand All @@ -104,7 +104,6 @@
storage.get('app_setup', (error, app) => {
if (!error) {
console.log(app);
this.config = app;
this.doUploadFile();
}
Expand Down Expand Up @@ -141,16 +140,17 @@
},
uploadFile(filePath){
let key = this.config.bucket_dir + '/' + util.getName(filePath);
let uptoken = new qiniu.rs.PutPolicy(this.config.bucket_name + ":" + key).token();
let extra = new qiniu.io.PutExtra();
let log = {
path: filePath,
key: key
};
try {
qiniu.io.putFile(uptoken, key, filePath, extra, (err, ret, res) => {
console.log(err, ret, res);
cloudStorage.upload({
bucket: this.config.bucket_name,
key: key,
path: filePath
}, (err, ret) => {
this.handleResult(log, err);
});
} catch (err) {
Expand All @@ -175,7 +175,7 @@
},
copy(key){
let url = util.getQiniuUrl(this.domains[0], key);
util.setClipboardText(this, this.config.setup_copyType, url);
util.setClipboardText(this, this.config.copyType, url);
},
openInFolder(path){
this.$electron.shell.showItemInFolder(path);
Expand Down
77 changes: 77 additions & 0 deletions app/src/renderer/util/cloudStorage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import qiniu from 'qiniu';

function init(param) {
qiniu.conf.ACCESS_KEY = param.access_key;
qiniu.conf.SECRET_KEY = param.secret_key;
}

function getToken() {
return new qiniu.auth.digest.Mac(qiniu.conf.ACCESS_KEY, qiniu.conf.SECRET_KEY);
}

/**
* http请求鉴权
* @param url
* @returns {*}
*/
function httpAuthorization(url) {
return qiniu.util.generateAccessToken(getToken(), url, null)
}

/**
* 通过url抓取文件
*/
function fetch(params, callback) {
let config = new qiniu.conf.Config();
let bucketManager = new qiniu.rs.BucketManager(getToken(), config);

bucketManager.fetch(params.path, params.bucket, params.key, function (respErr, respBody, respInfo) {
if (respErr) {
throw respErr;
}
callback(respErr, respBody);
});
}

/**
* 上传文件
* @param params
* @param callback
*/
function upload(params, callback) {
let options = {
scope: params.bucket,
};
let putPolicy = new qiniu.rs.PutPolicy(options);
let uploadToken = putPolicy.uploadToken(getToken());

let config = new qiniu.conf.Config();
let formUploader = new qiniu.form_up.FormUploader(config);
let putExtra = new qiniu.form_up.PutExtra();

formUploader.putFile(uploadToken, params.key, params.path, putExtra, function (respErr, respBody, respInfo) {
if (respErr) {
throw respErr;
}
callback(respErr, respBody);
});
}

/**
* 删除文件操作
*/
function remove(params, callback) {
let config = new qiniu.conf.Config();
let bucketManager = new qiniu.rs.BucketManager(getToken(), config);

bucketManager.delete(params.bucket, params.key, function (err, respBody, respInfo) {
console.log(respBody, respInfo);
if (!err) {
callback(respBody);
} else {
console.log(err);
}
});
}

export {init, httpAuthorization, remove, upload, fetch}

0 comments on commit e3a1032

Please sign in to comment.