Skip to content
This repository was archived by the owner on Mar 6, 2024. It is now read-only.

Commit ffee588

Browse files
committed
feat: support http upload file, add git commit info
1 parent ff3230a commit ffee588

File tree

8 files changed

+150
-4
lines changed

8 files changed

+150
-4
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
/wetool/
33
wetool-*.jar
44
conf/
5+
upload
6+
src/main/resources/gitinfo
57

68
# Compiled class file
79
*.class

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ java -jar ./wetool.jar &
7373
7474
### 使用小技巧
7575
76+
- HTTP文件上传服务,首先在配置文件中设置上传文件的保存目录,然后浏览器打开 [文件上传页面](http://localhost:8189/upload.html)
7677
- 使用全局快捷键 `Ctrl+Alt+Shift+Enter` 显示或隐藏主界面
7778
- 应用内使用快捷键 `Ctrl+数字` 选中指定位置的选项卡,数字 `9` 表示选中最后一个
7879
- 关闭指定位置的选项卡:`Alt+数字`,数字 `9` 表示关闭最后一个

bin/package.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import os
44
import re
5-
import shutil
65

76
os.chdir('..')
87
print(os.popen('git pull').read())
@@ -22,6 +21,11 @@ def package(os_name):
2221
os.rename('./target/wetool-%s.jar' % version, filename)
2322

2423

24+
with open('./src/main/resources/gitinfo', 'w+', encoding='utf-8') as fw:
25+
branch = os.popen('git symbolic-ref --short -q HEAD').read().replace('\n', '')
26+
last_commit = os.popen('git rev-parse --short HEAD').read().replace('\n', '')
27+
fw.write("%s:%s" % (branch, last_commit))
28+
2529
package('win')
2630
package('mac')
2731
package('linux')

history.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
- 支持HutoolCli命令
99
- 支持暴露模式匹配的HTTP接口
1010
- 支持HTTP文件浏览服务
11-
- 支持暴露HTTP文件上传接口
11+
- 支持HTTP文件上传
1212
- 仅支持单实例运行
1313
- 支持添加网页工具
1414

src/main/java/org/code4everything/wetool/WeApplication.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import cn.hutool.core.date.DateUtil;
55
import cn.hutool.core.exceptions.ExceptionUtil;
66
import cn.hutool.core.io.FileUtil;
7+
import cn.hutool.core.io.resource.ResourceUtil;
78
import cn.hutool.core.thread.ThreadFactoryBuilder;
89
import cn.hutool.core.thread.ThreadUtil;
910
import cn.hutool.core.util.ArrayUtil;
@@ -19,6 +20,7 @@
1920
import com.alibaba.fastjson.JSONObject;
2021
import com.alibaba.fastjson.parser.Feature;
2122
import com.dustinredmond.fxtrayicon.FXTrayIcon;
23+
import io.netty.handler.codec.http.HttpResponseStatus;
2224
import javafx.application.Application;
2325
import javafx.application.Platform;
2426
import javafx.event.EventType;
@@ -48,9 +50,11 @@
4850
import org.code4everything.wetool.plugin.support.event.EventPublisher;
4951
import org.code4everything.wetool.plugin.support.event.message.KeyboardListenerEventMessage;
5052
import org.code4everything.wetool.plugin.support.event.message.QuickStartEventMessage;
53+
import org.code4everything.wetool.plugin.support.exception.HttpException;
5154
import org.code4everything.wetool.plugin.support.exception.ToDialogException;
5255
import org.code4everything.wetool.plugin.support.factory.BeanFactory;
5356
import org.code4everything.wetool.plugin.support.http.HttpService;
57+
import org.code4everything.wetool.plugin.support.http.Https;
5458
import org.code4everything.wetool.plugin.support.http.ObjectResp;
5559
import org.code4everything.wetool.plugin.support.listener.WeKeyboardListener;
5660
import org.code4everything.wetool.plugin.support.listener.WeMouseListener;
@@ -257,6 +261,15 @@ private static void exportHttpService() {
257261
FxUtils.showStage();
258262
return ObjectResp.of("status", "success");
259263
});
264+
HttpService.exportHttp("post/wetool/file/upload", (req, resp, param, body) -> {
265+
String fileDir = BeanFactory.get(WeConfig.class).getFileUploadDir();
266+
if (StrUtil.isBlank(fileDir)) {
267+
throw new HttpException().setStatus(HttpResponseStatus.METHOD_NOT_ALLOWED).setMsg("文件上传未开启");
268+
}
269+
Https.writeMultipartFiles(req, fileDir);
270+
return null;
271+
});
272+
HttpService.exportHttp("get/upload.html", (req, resp, param, body) -> Https.responseHtml(resp, ResourceUtil.readUtf8Str("static/upload.html")));
260273
WeUtils.getConfig().getHttpFiles().forEach(e -> HttpFileBrowserService.getInstance().handleExportCmd(e, false));
261274
} catch (Exception e) {
262275
log.error(e.getMessage());

src/main/java/org/code4everything/wetool/controller/MainController.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import cn.hutool.core.collection.CollUtil;
66
import cn.hutool.core.date.DateUtil;
77
import cn.hutool.core.io.FileUtil;
8+
import cn.hutool.core.io.resource.ResourceUtil;
89
import cn.hutool.core.lang.Pair;
910
import cn.hutool.core.swing.clipboard.ClipboardUtil;
1011
import cn.hutool.core.util.ArrayUtil;
@@ -621,7 +622,17 @@ public void quit() {
621622
}
622623

623624
public void about() {
624-
FxDialogs.showInformation(TitleConsts.ABOUT_APP, TipConsts.ABOUT_APP);
625+
String gitInfo = StrUtil.trim(ResourceUtil.readUtf8Str("gitinfo"));
626+
String aboutApp = TipConsts.ABOUT_APP;
627+
if (StrUtil.isNotEmpty(gitInfo) && !StrUtil.startWith(gitInfo, "master")) {
628+
List<String> infos = StrUtil.splitTrim(gitInfo, ":");
629+
aboutApp += "\r\n开发版分支:" + infos.get(0);
630+
if (infos.size() > 1) {
631+
aboutApp += ",提交:" + infos.get(1);
632+
}
633+
aboutApp += "\r\n";
634+
}
635+
FxDialogs.showInformation(TitleConsts.ABOUT_APP, aboutApp);
625636
}
626637

627638
public void closeAllTab() {

src/main/resources/static/upload.html

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<title>Layui</title>
6+
<meta name="renderer" content="webkit">
7+
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
8+
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
9+
<link rel="stylesheet" href="https://code4everything.gitee.io/repository/frontend/layui/layui/css/layui.css" media="all">
10+
</head>
11+
<body>
12+
13+
<br/>
14+
<fieldset class="layui-elem-field layui-field-title" style="margin-top: 30px;">
15+
<legend>批量上传文件</legend>
16+
</fieldset>
17+
18+
<div class="layui-upload">
19+
<button type="button" class="layui-btn layui-btn-normal" id="testList">选择多文件</button>
20+
<div class="layui-upload-list" style="max-width: 1000px;">
21+
<table class="layui-table">
22+
<colgroup>
23+
<col>
24+
<col width="150">
25+
<col width="260">
26+
<col width="150">
27+
</colgroup>
28+
<thead>
29+
<tr><th>文件名</th>
30+
<th>大小</th>
31+
<th>上传进度</th>
32+
<th>操作</th>
33+
</tr></thead>
34+
<tbody id="demoList"></tbody>
35+
</table>
36+
</div>
37+
<button type="button" class="layui-btn" id="testListAction">开始上传</button>
38+
</div>
39+
40+
<script src="http://code4everything.gitee.io/repository/frontend/layui/layui/layui.js" charset="utf-8"></script>
41+
<script>
42+
layui.use(['upload', 'element', 'layer'], function(){
43+
var $ = layui.jquery
44+
,upload = layui.upload
45+
,element = layui.element
46+
,layer = layui.layer;
47+
48+
var uploadListIns = upload.render({
49+
elem: '#testList'
50+
,elemList: $('#demoList')
51+
,url: '/wetool/file/upload'
52+
,accept: 'file'
53+
,multiple: true
54+
,number: 9999999
55+
,auto: false
56+
,bindAction: '#testListAction'
57+
,choose: function(obj){
58+
var that = this;
59+
var files = this.files = obj.pushFile();
60+
obj.preview(function(index, file, result){
61+
var tr = $(['<tr id="upload-'+ index +'">'
62+
,'<td>'+ file.name +'</td>'
63+
,'<td>'+ (file.size/1014).toFixed(1) +'kb</td>'
64+
,'<td><div class="layui-progress" lay-filter="progress-demo-'+ index +'"><div class="layui-progress-bar" lay-percent=""></div></div></td>'
65+
,'<td>'
66+
,'<button class="layui-btn layui-btn-xs demo-reload layui-hide">重传</button>'
67+
,'<button class="layui-btn layui-btn-xs layui-btn-danger demo-delete">删除</button>'
68+
,'</td>'
69+
,'</tr>'].join(''));
70+
71+
tr.find('.demo-reload').on('click', function(){
72+
obj.upload(index, file);
73+
});
74+
75+
tr.find('.demo-delete').on('click', function(){
76+
delete files[index];
77+
tr.remove();
78+
uploadListIns.config.elem.next()[0].value = '';
79+
});
80+
81+
that.elemList.append(tr);
82+
element.render('progress');
83+
});
84+
}
85+
,done: function(res, index, upload){
86+
var that = this;
87+
//if(res.code == 0){ //上传成功
88+
var tr = that.elemList.find('tr#upload-'+ index)
89+
,tds = tr.children();
90+
tds.eq(3).html(''); //清空操作
91+
delete this.files[index]; //删除文件队列已经上传成功的文件
92+
return;
93+
//}
94+
this.error(index, upload);
95+
}
96+
,allDone: function(obj){ //多文件上传完毕后的状态回调
97+
console.log(obj)
98+
}
99+
,error: function(index, upload){ //错误回调
100+
var that = this;
101+
var tr = that.elemList.find('tr#upload-'+ index)
102+
,tds = tr.children();
103+
tds.eq(3).find('.demo-reload').removeClass('layui-hide'); //显示重传
104+
}
105+
,progress: function(n, elem, e, index){ //注意:index 参数为 layui 2.6.6 新增
106+
element.progress('progress-demo-'+ index, n + '%'); //执行进度条。n 即为返回的进度百分比
107+
}
108+
});
109+
});
110+
</script>
111+
</body>
112+
</html>

we-config.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,5 +83,8 @@
8383
},
8484
/*代理*/
8585
"proxy": "127.0.0.1:6789",
86-
"httpFiles": ["get/file/* C:\\"]
86+
/*HTTP文件浏览服务接口,包括接口名及本地文件绝对路径*/
87+
"httpFiles": ["get/file/* C:\\"],
88+
/*上传文件保存路径,为空不允许上传文件,支持相对路径*/
89+
"fileUploadDir": "upload"
8790
}

0 commit comments

Comments
 (0)