Skip to content

Commit 6cc36a3

Browse files
committed
feat:文件下载功能
1 parent 340c480 commit 6cc36a3

File tree

14 files changed

+167
-61
lines changed

14 files changed

+167
-61
lines changed

README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,15 @@ npm run pack
4444
More information can be found [electron-vue](https://simulatedgreg.gitbooks.io/electron-vue/content/docs/npm_scripts.html).
4545

4646
## issue
47+
- 小文件下载状态更新异常
4748
- 上传进度不灵敏.七牛上传文件块设置的是4M.调小以后,进度条反应比较正常,但是会提示上传失败 😂,所以会感觉明明上传了,但是半天没响应.
48-
- 小文件上传失败.没有详细测试,大概范围是小于100KB的文件上传会失败.
49+
- ~~小文件上传失败.没有详细测试,大概范围是小于100KB的文件上传会失败.~~ 解决啦~ 🤓
50+
51+
## tips
52+
### npm 安装git分支
53+
```shell
54+
npm i --save git://github.com/willnewii/nodejs-sdk.git#patch-1
55+
```
4956

5057
## 参考资料
5158
- [electron 中文文档](https://github.com/electron/electron/tree/master/docs-translations/zh-CN)

app/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"electron-json-storage": "^3.0.6",
99
"iview": "^2.0.0",
1010
"moment": "^2.18.1",
11-
"qiniu": "^7.0.6",
11+
"qiniu": "git://github.com/willnewii/nodejs-sdk.git#patch-1",
1212
"qs": "^6.4.0",
1313
"vue": "^2.4.1",
1414
"vue-electron": "^1.0.6",

app/src/main/index.js

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
'use strict'
22

33
import {app, BrowserWindow, Menu, ipcMain, dialog} from 'electron'
4+
5+
const {download} = require('electron-dl');
6+
47
import * as util from './util'
58
import * as trayUtil from './trayUtil';
69

@@ -55,19 +58,46 @@ app.on('activate', () => {
5558
* 注册IPC事件
5659
*/
5760
const registerIPC = function () {
58-
ipcMain.on('open-file-dialog', function (event) {
61+
//选择文件
62+
ipcMain.on('open-file-dialog', function (event, option) {
5963
dialog.showOpenDialog({
6064
properties: ['openFile', 'multiSelections']
6165
}, function (files) {
6266
if (files) event.sender.send('selected-directory', files)
6367
})
6468
});
6569

66-
ipcMain.on('previewFile', function (event, filePath) {
67-
if (mainWindow) {
68-
mainWindow.previewFile(filePath);
69-
}
70+
//选择目录
71+
ipcMain.on('choiceFolder', function (event, option) {
72+
dialog.showOpenDialog(option, function (files) {
73+
if (files) event.sender.send('choiceFolder', files)
74+
})
7075
});
76+
77+
//下载文件
78+
ipcMain.on('downloadFile', function (event, file, option) {
79+
option.onProgress = function (num) {
80+
if (num !== 1) {
81+
event.sender.send('updateDownloadProgress', num);
82+
}
83+
};
84+
85+
download(BrowserWindow.getFocusedWindow(), file, option)
86+
.then(dl => {
87+
console.log('getSavePath:' + dl.getSavePath());
88+
event.sender.send('updateDownloadProgress', 1);
89+
})
90+
.catch(error => {
91+
console.error(error);
92+
event.sender.send('updateDownloadProgress', 1);
93+
});
94+
});
95+
96+
/* ipcMain.on('previewFile', function (event, filePath) {
97+
if (mainWindow) {
98+
mainWindow.previewFile(filePath);
99+
}
100+
});*/
71101
};
72102

73103
/**

app/src/renderer/components/Main/ClientHeader.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@
156156
switch (index) {
157157
case 0://调用文件选取对话框
158158
this.filePaths = [];
159-
ipc.send('open-file-dialog', {properties: ['openFile']});
159+
ipc.send('open-file-dialog', {properties: ['openFile', 'multiSelections']});
160160
break;
161161
case 1://抓取文件
162162
this.filePaths = [];

app/src/renderer/components/Main/DirTag.vue

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
<style lang="scss" scoped>
22
.layout-tag {
33
flex-grow: 1;
4+
overflow-x: scroll;
5+
white-space: nowrap;
6+
margin-right: 10px;
47
.blue-tag {
58
color: #39f !important;
69
border: 1px solid #39f !important;

app/src/renderer/components/Main/ResourceTable.vue

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
import mixin_resource from '../../mixins/mixin-resource'
2626
import moment from 'moment'
2727
28+
let tempNum;
29+
2830
export default {
2931
name: 'ResourceTable',
3032
mixins: [mixin_resource],
@@ -105,9 +107,25 @@
105107
}
106108
},
107109
created() {
110+
EventBus.$off(Constants.Event.removes);
108111
EventBus.$on(Constants.Event.removes, () => {
109112
this.removes();
110113
});
114+
115+
EventBus.$off(Constants.Event.download);
116+
EventBus.$on(Constants.Event.download, () => {
117+
this.downloadFiles();
118+
});
119+
120+
this.$electron.ipcRenderer.removeAllListeners('updateDownloadProgress');
121+
this.$electron.ipcRenderer.on('updateDownloadProgress', (event, num) => {
122+
this.$Loading.update(num * 100);
123+
if (num === 1) {
124+
this.$Loading.finish();
125+
console.log(this.bucket.selection.length + ':' + this.bucket.name);
126+
this.downloadFiles();
127+
}
128+
});
111129
},
112130
mounted() {
113131
this.setTableSize();

app/src/renderer/mixins/mixin-resource.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export default {
1010
setup_copyType: types.APP.setup_copyType,
1111
setup_deleteNoAsk: types.APP.setup_deleteNoAsk,
1212
setup_imagestyle: types.APP.setup_imagestyle,
13+
setup_downloaddir: types.APP.setup_downloaddir,
1314
})
1415
},
1516
props: {
@@ -27,7 +28,6 @@ export default {
2728
},
2829
methods: {
2930
show(index) {
30-
//this.$electron.ipcRenderer.send('previewFile', util.getQiniuUrl(this.bucket.domains[0], this.bucket.files[index].key));
3131
this.$electron.shell.openExternal(util.getQiniuUrl(this.bucket.domains[0], this.bucket.files[index].key))
3232
},
3333
copy(index) {
@@ -38,7 +38,6 @@ export default {
3838
event.stopPropagation();
3939
},
4040
removes() {
41-
console.log(this.bucket.selection);
4241
this.deleteKey = this.bucket.selection[0].key;
4342
if (this.bucket.selection.length === 1) {
4443
this.doRemove();
@@ -50,6 +49,20 @@ export default {
5049
});
5150
}
5251
},
52+
downloadFiles() {
53+
console.log('selection.length:' + this.bucket.selection.length + ':' + this.bucket.name);
54+
if (this.bucket.selection.length > 0) {
55+
this.$Loading.start();
56+
let option = {};
57+
if (this.setup_downloaddir) {
58+
option.directory = this.setup_downloaddir;
59+
}
60+
this.$electron.ipcRenderer.send('downloadFile', util.getQiniuUrl(this.bucket.domains[0], this.bucket.selection[0].key), option);
61+
this.bucket.selection.shift();
62+
} else {
63+
this.$Message.info('文件下载完成');
64+
}
65+
},
5366
remove(index, event) {
5467
this.deleteKey = this.bucket.files[index].key;
5568

app/src/renderer/pages/Setup.vue

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
<style scoped>
2+
.title {
3+
padding: 10px 0;
4+
font-size: 16px;
5+
}
6+
27
.item {
38
padding: 30px 0 0 30px;
49
}
@@ -22,17 +27,25 @@
2227
</Radio-group>
2328
</div>
2429
<div class="item">
25-
默认托盘上传位置:
26-
<Select v-model="bucketname" size="small" style="width:100px">
30+
默认托盘上传位置:<br>
31+
<Select v-model="bucketname" size="small" style="width:100px" placeholder="空间名称">
2732
<Option v-for="item in buckets" :value="item" :key="item">{{ item }}</Option>
2833
</Select>
2934
/
3035
<Input class='bucketdir' v-model="bucketdir" size="small" placeholder="路径"></Input>
3136
<Button @click="saveDir" size="small">保存</Button>
3237
<br>提示:默认文件将会被上传到 {{setup_bucket_name}}/{{setup_bucket_dir}}/ 目录下
3338
</div>
39+
40+
<div class="item">
41+
下载目录:<br>
42+
<Input class='bucketdir' v-model="downloaddir" size="small" placeholder="默认下载目录" style="width: 40%;"
43+
disabled></Input>
44+
<Button @click="choiceDownloadolder" size="small" icon="ios-folder-outline"></Button>
45+
</div>
46+
3447
<div class="item">
35-
预览图片样式:
48+
预览图片样式:<br>
3649
<Input class='bucketdir' v-model="imagestyle" size="small" placeholder="七牛图片样式" style="width: 40%;"></Input>
3750
<Button @click="saveImagestyle" size="small">保存</Button>
3851
<Button @click="openBrowser" size="small">关于图片样式</Button>
@@ -45,12 +58,16 @@
4558
import {mapGetters, mapActions} from 'vuex'
4659
import * as types from '../vuex/mutation-types'
4760
61+
let ipc;
62+
4863
export default {
4964
name: 'setup-page',
5065
data() {
5166
return {
5267
bucketname: '',
5368
bucketdir: '',
69+
imagestyle: '',
70+
downloaddir: ''
5471
}
5572
},
5673
computed: {
@@ -61,26 +78,35 @@
6178
setup_bucket_name: types.APP.setup_bucket_name,
6279
setup_bucket_dir: types.APP.setup_bucket_dir,
6380
setup_imagestyle: types.APP.setup_imagestyle,
81+
setup_downloaddir: types.APP.setup_downloaddir
6482
})
6583
},
6684
components: {ClientHeader},
6785
created: function () {
6886
this.bucketname = this.setup_bucket_name;
6987
this.bucketdir = this.setup_bucket_dir;
7088
this.imagestyle = this.setup_imagestyle;
89+
this.downloaddir = this.setup_downloaddir;
90+
91+
ipc = this.$electron.ipcRenderer;
92+
ipc.on('choiceFolder', (event, path) => {
93+
this.downloaddir = path[0];
94+
95+
this.saveDownloadolder();
96+
});
7197
},
7298
methods: {
7399
...mapActions([
74100
types.APP.setup_a_copyType,
75101
types.APP.setup_a_deleteNoAsk,
76102
types.APP.setup_a_savedir,
77103
types.APP.setup_a_imagestyle,
104+
types.APP.setup_a_downloaddir,
78105
]),
79106
deleteNoAskChange: function (state) {
80107
this[types.APP.setup_a_deleteNoAsk](state);
81108
},
82109
copyTypeChange: function (model) {
83-
console.log(model);
84110
this[types.APP.setup_a_copyType](model);
85111
},
86112
saveDir: function () {
@@ -91,7 +117,13 @@
91117
this[types.APP.setup_a_imagestyle]([this.imagestyle]);
92118
this.$Message.success('图片样式修改成功');
93119
},
94-
openBrowser(){
120+
choiceDownloadolder() {
121+
ipc.send('choiceFolder', {properties: ['openDirectory']});
122+
},
123+
saveDownloadolder() {
124+
this[types.APP.setup_a_downloaddir](this.downloaddir);
125+
},
126+
openBrowser() {
95127
this.$electron.shell.openExternal('https://developer.qiniu.com/dora/manual/1279/basic-processing-images-imageview2');
96128
}
97129
}

app/src/renderer/pages/bucketPage.vue

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,14 @@
1919
<div class="dir-layout">
2020
<DirTag v-if="endable" :bucket="bucket" @on-click="doDirSearch"></DirTag>
2121

22-
<Button-group size="small" style="background: #FFF;margin-right: 10px;display: flex;"
23-
v-if="bucket.selection.length > 0">
24-
<Button type="error" @click="removes()" icon="trash-b">{{bucket.selection.length}}</Button>
25-
</Button-group>
22+
23+
<Button type="ghost" size="small" @click="downloads()" icon="ios-download" style="margin-right: 10px;"
24+
v-if="bucket.selection.length > 0">下载({{bucket.selection.length}})
25+
</Button>
26+
27+
<Button type="error" size="small" @click="removes()" icon="trash-b" style="margin-right: 10px;"
28+
v-if="bucket.selection.length > 0">删除({{bucket.selection.length}})
29+
</Button>
2630

2731
<Button-group size="small" style="background: #FFF;margin-right: 10px;display: flex;">
2832
<Button :type="bucket.showType === 0 ? 'primary' : 'ghost'" @click="showType(0)"
@@ -198,6 +202,9 @@
198202
removes() {
199203
EventBus.$emit(Constants.Event.removes);
200204
},
205+
downloads() {
206+
EventBus.$emit(Constants.Event.download);
207+
},
201208
/**
202209
* 表单模式/图片模式
203210
* @param type

app/src/renderer/service/constants.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@ export const URL = {
1414

1515
export const Event = {
1616
removes: 'removes',
17+
download: 'download'
1718
};

0 commit comments

Comments
 (0)