mirror of
https://github.com/zfile-dev/zfile.git
synced 2025-04-19 05:34:52 +00:00
Compare commits
86 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3249266cd1 | ||
|
|
1a6235df73 | ||
|
|
430aee2b7f | ||
|
|
de2f7e4b80 | ||
|
|
6dfcc409ac | ||
|
|
78f795e1cb | ||
|
|
c69ee0f356 | ||
|
|
39475de789 | ||
|
|
bd71712765 | ||
|
|
8698686a47 | ||
|
|
762c67ee37 | ||
|
|
7bf3a29c17 | ||
|
|
6ee5002f0c | ||
|
|
fadc64add4 | ||
|
|
234f43846f | ||
|
|
67e42d9753 | ||
|
|
04f94b4bf5 | ||
|
|
d29c498457 | ||
|
|
5aa45b44b2 | ||
|
|
8273a645f2 | ||
|
|
46f292cc9b | ||
|
|
261d48059e | ||
|
|
79f931c51b | ||
|
|
399e961a65 | ||
|
|
3e61d7d146 | ||
|
|
ace95d9071 | ||
|
|
60d2a2b986 | ||
|
|
69d5661e06 | ||
|
|
01d11dfc23 | ||
|
|
d35e3ecd93 | ||
|
|
9e5a3e5385 | ||
|
|
b62163b4e8 | ||
|
|
595a00f067 | ||
|
|
a759d9fe44 | ||
|
|
7a24fd10e0 | ||
|
|
7e04a817d7 | ||
|
|
791967f45e | ||
|
|
d789436a16 | ||
|
|
96ab8ff7dd | ||
|
|
8809aca170 | ||
|
|
97106383b6 | ||
|
|
208da95234 | ||
|
|
d4c843f5f5 | ||
|
|
d273fc9f12 | ||
|
|
0b4a38218c | ||
|
|
2f57c5b5cc | ||
|
|
610f68295f | ||
|
|
39ced8eb84 | ||
|
|
368b7a1df2 | ||
|
|
8e2107cd46 | ||
|
|
fa32a33371 | ||
|
|
59116a9414 | ||
|
|
b2c732a389 | ||
|
|
0e1ffef92b | ||
|
|
a5b19d3577 | ||
|
|
185c84dd79 | ||
|
|
d554dd298c | ||
|
|
aa6ecf0aaa | ||
|
|
83692718e3 | ||
|
|
030bd95941 | ||
|
|
8722d11ac3 | ||
|
|
0131ff02c0 | ||
|
|
2d115bf1c6 | ||
|
|
946113216d | ||
|
|
77b05c6dac | ||
|
|
07c9fca210 | ||
|
|
27cf61693a | ||
|
|
37e1aa1fec | ||
|
|
31b54a3c05 | ||
|
|
589f07c103 | ||
|
|
fe8b60d873 | ||
|
|
1734619eac | ||
|
|
f5724dc9ab | ||
|
|
f7bb147b71 | ||
|
|
47fc1bc2df | ||
|
|
45172f69ba | ||
|
|
9566b138ff | ||
|
|
e15b6c2242 | ||
|
|
acc41511e0 | ||
|
|
b882b87405 | ||
|
|
518b5170ae | ||
|
|
8bfac6d9ac | ||
|
|
3ffdb4f1b2 | ||
|
|
47509450a0 | ||
|
|
812fd18aac | ||
|
|
77a13cf8ad |
48
.github/ISSUE_TEMPLATE/bug_report.md
vendored
Normal file
48
.github/ISSUE_TEMPLATE/bug_report.md
vendored
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
---
|
||||||
|
name: BUG 反馈
|
||||||
|
about: 事情不像预期的那样工作吗?
|
||||||
|
title: ''
|
||||||
|
labels: 'bug'
|
||||||
|
assignees: ''
|
||||||
|
---
|
||||||
|
|
||||||
|
<!--
|
||||||
|
你好!感谢你正在考虑为 ZFile 提交一个 bug。请花一点点时间尽量详细地回答以下基础问题。
|
||||||
|
|
||||||
|
谢谢!
|
||||||
|
-->
|
||||||
|
|
||||||
|
<!--
|
||||||
|
请确认你已经做了下面这些事情,若 bug 还是未解决,那么请尽可详细地描述你的问题。
|
||||||
|
|
||||||
|
- 我已经安装了最新版的 ZFile
|
||||||
|
- 我已经阅读了 ZFile 的文档:http://docs.zhaojun.im/zfile
|
||||||
|
- 我已经搜索了已有的 Issues 列表中有关的信息
|
||||||
|
- 我已经清理过浏览器缓存并重试
|
||||||
|
-->
|
||||||
|
|
||||||
|
## 我的环境
|
||||||
|
|
||||||
|
<!--
|
||||||
|
请登录到管理后台,点击左侧系统监控, 复制或截图此页内容.
|
||||||
|
-->
|
||||||
|
|
||||||
|
## 错误日志
|
||||||
|
|
||||||
|
<!--
|
||||||
|
请登录到管理后台,点击左侧系统监控, 点击右上角诊断日志下载, 然后上传到此 Issue 中.
|
||||||
|
-->
|
||||||
|
|
||||||
|
## 期望行为
|
||||||
|
|
||||||
|
<!--
|
||||||
|
你期望会发生什么?
|
||||||
|
-->
|
||||||
|
|
||||||
|
## 当前行为
|
||||||
|
|
||||||
|
<!--
|
||||||
|
描述 bug 细节,确认出现此问题的复现步骤,例如点击了哪里,发生了什么情况?
|
||||||
|
|
||||||
|
你可以粘贴截图或附件。
|
||||||
|
-->
|
||||||
34
.github/ISSUE_TEMPLATE/feature_request.md
vendored
Normal file
34
.github/ISSUE_TEMPLATE/feature_request.md
vendored
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
---
|
||||||
|
name: 功能建议
|
||||||
|
about: 想让我们为 ZFile 增加什么功能吗?
|
||||||
|
title: 'feat: '
|
||||||
|
labels: 'Feature Request'
|
||||||
|
assignees: ''
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
<!--
|
||||||
|
你好!感谢你愿意考虑希望 ZFile 增加某个新功能。请花一点点时间尽量详细地回答以下基础问题。
|
||||||
|
|
||||||
|
谢谢!
|
||||||
|
-->
|
||||||
|
|
||||||
|
## 概述
|
||||||
|
|
||||||
|
<!--
|
||||||
|
对这个新功能的一段描述
|
||||||
|
-->
|
||||||
|
|
||||||
|
## 动机
|
||||||
|
|
||||||
|
<!--
|
||||||
|
为什么你希望在 ZFile 中使用这个功能?
|
||||||
|
-->
|
||||||
|
|
||||||
|
## 详细解释
|
||||||
|
|
||||||
|
<!--
|
||||||
|
详细描述这个新功能。
|
||||||
|
|
||||||
|
如果这是一个小功能,你可以忽略这部分。
|
||||||
|
-->
|
||||||
14
.github/ISSUE_TEMPLATE/question.md
vendored
Normal file
14
.github/ISSUE_TEMPLATE/question.md
vendored
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
---
|
||||||
|
name: Question
|
||||||
|
about: 对 ZFile 有任何问题吗?
|
||||||
|
title: ''
|
||||||
|
labels: 'question'
|
||||||
|
assignees: ''
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
<!--
|
||||||
|
如果你有任何问题也可以通过此渠道来向我们反馈。
|
||||||
|
|
||||||
|
谢谢!
|
||||||
|
-->
|
||||||
164
API.md
Normal file
164
API.md
Normal file
@@ -0,0 +1,164 @@
|
|||||||
|
## API 标准
|
||||||
|
|
||||||
|
所有 API 均返回 `msg`, `code`, `data` 三个属性.
|
||||||
|
|
||||||
|
| code | 描述 |
|
||||||
|
| :---: | :------------: |
|
||||||
|
| 0 | 请求成功 |
|
||||||
|
| -1 | 请求失败 |
|
||||||
|
| -2 | 文件夹需要密码 |
|
||||||
|
|
||||||
|
当 `code == 0` 时, `data` 中为请求所需数据.
|
||||||
|
|
||||||
|
当 `code != 0` 时, 应当将 `msg` 中的属性作为参考值.
|
||||||
|
|
||||||
|
|
||||||
|
## 获取文件列表
|
||||||
|
|
||||||
|
### 请求 URL
|
||||||
|
|
||||||
|
`/api/list` `GET`
|
||||||
|
|
||||||
|
### 参数
|
||||||
|
|
||||||
|
| 参数名 | 描述 | 是否必填 | 参考值 |
|
||||||
|
| :------: | :--------: | :------: | :--------------------------: |
|
||||||
|
| path | 路径 | 是 | `/`, `/文件夹名称` |
|
||||||
|
| password | 文件夹密码 | 否 | 当文件夹需要密码时, |
|
||||||
|
| page | 页数 | 否 | 默认取第一页, 每页固定 30 条 |
|
||||||
|
|
||||||
|
### 响应
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"msg": "操作成功",
|
||||||
|
"code": 0,
|
||||||
|
"data": [
|
||||||
|
{
|
||||||
|
"name": "密码文件夹",
|
||||||
|
"time": "2020-01-28 13:17",
|
||||||
|
"size": 4096,
|
||||||
|
"type": "FOLDER",
|
||||||
|
"path": "/",
|
||||||
|
"url": null
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "新建 文本文档.txt",
|
||||||
|
"time": "2020-01-28 13:16",
|
||||||
|
"size": 3,
|
||||||
|
"type": "FILE",
|
||||||
|
"path": "/",
|
||||||
|
"url": "http://127.0.0.1:8080/file/新建 文本文档.txt"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## 搜索
|
||||||
|
|
||||||
|
|
||||||
|
### 请求 URL
|
||||||
|
|
||||||
|
`/api/search` `GET`
|
||||||
|
|
||||||
|
### 参数
|
||||||
|
|
||||||
|
| 参数名 | 描述 | 是否必填 | 参考值 |
|
||||||
|
| :----: | :----: | :------: | :--------------------------: |
|
||||||
|
| name | 搜索值 | 是 | 模糊匹配 |
|
||||||
|
| page | 页数 | 否 | 默认取第一页, 每页固定 30 条 |
|
||||||
|
|
||||||
|
### 响应
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"msg": "操作成功",
|
||||||
|
"code": 0,
|
||||||
|
"data": [
|
||||||
|
{
|
||||||
|
"name": "密码文件夹",
|
||||||
|
"time": "2020-01-28 13:17",
|
||||||
|
"size": 4096,
|
||||||
|
"type": "FOLDER",
|
||||||
|
"path": "/",
|
||||||
|
"url": null
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "新建 文本文档.txt",
|
||||||
|
"time": "2020-01-28 13:16",
|
||||||
|
"size": 3,
|
||||||
|
"type": "FILE",
|
||||||
|
"path": "/",
|
||||||
|
"url": "http://127.0.0.1:8080/file/新建 文本文档.txt"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
## 获取单个文件信息
|
||||||
|
|
||||||
|
### 请求 URL
|
||||||
|
|
||||||
|
`/api/directlink` `GET`
|
||||||
|
|
||||||
|
### 参数
|
||||||
|
|
||||||
|
| 参数名 | 描述 | 是否必填 | 参考值 |
|
||||||
|
| :----: | :--------: | :------: | :------------------: |
|
||||||
|
| path | 文件全路径 | 是 | `/新建 文本文档.txt` |
|
||||||
|
|
||||||
|
### 响应
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"msg": "操作成功",
|
||||||
|
"code": 0,
|
||||||
|
"data": {
|
||||||
|
"name": "新建 文本文档.txt",
|
||||||
|
"time": "2020-01-28 13:16",
|
||||||
|
"size": 3,
|
||||||
|
"type": "FILE",
|
||||||
|
"path": "d:/test",
|
||||||
|
"url": "http://127.0.0.1:8080/file/新建 文本文档.txt"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## 获取系统配置
|
||||||
|
|
||||||
|
|
||||||
|
### 请求 URL
|
||||||
|
|
||||||
|
`/api/config` `GET`
|
||||||
|
|
||||||
|
### 参数
|
||||||
|
|
||||||
|
| 参数名 | 描述 | 是否必填 | 参考值 |
|
||||||
|
| :----: | :--------: | :------: | :-----------: |
|
||||||
|
| path | 文件夹名称 | 是 | `/文件夹名称` |
|
||||||
|
|
||||||
|
### 响应
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"msg": "操作成功",
|
||||||
|
"code": 0,
|
||||||
|
"data": {
|
||||||
|
"readme": null, # 文档文件名称
|
||||||
|
"viewConfig": {
|
||||||
|
"siteName": "站点名称", # 站点名称
|
||||||
|
"infoEnable": false, # 是否开启右侧信息框
|
||||||
|
"searchEnable": false, # 是否开启搜索
|
||||||
|
"searchIgnoreCase": true, # 搜索是否忽略大小写
|
||||||
|
"storageStrategy": "local", # 当前启用存储引擎
|
||||||
|
"username": "2", # 用户名
|
||||||
|
"domain": "http://127.0.0.1:8080", # 域名
|
||||||
|
"enableCache": false, # 是否开启缓存
|
||||||
|
"searchContainEncryptedFile": false, # 搜索是否包含加密文件夹
|
||||||
|
"customJs": "", # 自定义 js 片段
|
||||||
|
"customCss": "" # 自定义 css 片段
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
72
README.md
72
README.md
@@ -11,12 +11,14 @@
|
|||||||
|
|
||||||
预览地址: [https://zfile.jun6.net](https://zfile.jun6.net)
|
预览地址: [https://zfile.jun6.net](https://zfile.jun6.net)
|
||||||
|
|
||||||
|
文档地址: [http://docs.zhaojun.im/zfile](http://docs.zhaojun.im/zfile)
|
||||||
|
|
||||||
## 系统特色
|
## 系统特色
|
||||||
|
|
||||||
* 内存缓存 (免安装)
|
* 内存缓存 (免安装)
|
||||||
* 内存数据库 (免安装)
|
* 内存数据库 (免安装)
|
||||||
* 个性化配置
|
* 个性化配置
|
||||||
* 自定义目录的 header 说明文件
|
* 自定义目录的 readme 说明文件
|
||||||
* 自定义 JS, CSS
|
* 自定义 JS, CSS
|
||||||
* 文件夹密码
|
* 文件夹密码
|
||||||
* 支持在线浏览文本文件, 视频, 图片, 音乐. (支持 FLV 和 HLS)
|
* 支持在线浏览文本文件, 视频, 图片, 音乐. (支持 FLV 和 HLS)
|
||||||
@@ -35,16 +37,18 @@ yum install -y java-1.8.0-openjdk unzip
|
|||||||
|
|
||||||
# Debian/Ubuntu系统
|
# Debian/Ubuntu系统
|
||||||
apt update
|
apt update
|
||||||
apt install -y default-jdk unzip
|
apt install -y openjdk-8-jre-headless unzip
|
||||||
```
|
```
|
||||||
|
|
||||||
|
> 如为更新程序, 则请先执行 `~/zfile/bin/stop.sh && rm -rf ~/zfile` 清理旧程序. 首次安装请忽略此选项.
|
||||||
|
|
||||||
下载项目:
|
下载项目:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
wget -P ~ https://c.jun6.net/ZFILE/zfile-0.9.war
|
|
||||||
cd ~
|
cd ~
|
||||||
mkdir zfile && unzip zfile-0.9.war -d zfile && rm -rf zfile-0.9.war
|
wget https://c.jun6.net/ZFILE/zfile-release.war
|
||||||
chmod +x ~/zfile/bin/*.sh
|
mkdir zfile && unzip zfile-release.war -d zfile && rm -rf zfile-release.war
|
||||||
|
chmod +x zfile/bin/*.sh
|
||||||
```
|
```
|
||||||
|
|
||||||
程序的目录结构为:
|
程序的目录结构为:
|
||||||
@@ -64,26 +68,7 @@ chmod +x ~/zfile/bin/*.sh
|
|||||||
~/zfile/bin/start.sh
|
~/zfile/bin/start.sh
|
||||||
```
|
```
|
||||||
|
|
||||||
停止项目:
|
篇幅有限, 更详细的安装教程请参考: [安装文档](http://zhaojun.im/zfile-install)
|
||||||
|
|
||||||
```bash
|
|
||||||
~/zfile/bin/stop.sh
|
|
||||||
```
|
|
||||||
|
|
||||||
重启项目:
|
|
||||||
|
|
||||||
```bash
|
|
||||||
~/zfile/bin/restart.sh
|
|
||||||
```
|
|
||||||
|
|
||||||
|
|
||||||
修改配置文件:
|
|
||||||
|
|
||||||
```bash
|
|
||||||
vim ~/zfile/WEB-INF/classes/application.yml
|
|
||||||
```
|
|
||||||
|
|
||||||
> 默认启动端口为 8080, 如需请配置文件请编辑上述文件, 修改后重启程序生效.
|
|
||||||
|
|
||||||
访问地址:
|
访问地址:
|
||||||
|
|
||||||
@@ -101,12 +86,12 @@ vim ~/zfile/WEB-INF/classes/application.yml
|
|||||||
|
|
||||||
国际/家庭/个人版:
|
国际/家庭/个人版:
|
||||||
|
|
||||||
https://login.microsoftonline.com/common/oauth2/v2.0/authorize?client_id=09939809-c617-43c8-a220-a93c1513c5d4&response_type=code&redirect_uri=https://zfile.jun6.net/onedirve/callback&scope=offline_access%20User.Read%20Files.ReadWrite.All
|
https://login.microsoftonline.com/common/oauth2/v2.0/authorize?client_id=09939809-c617-43c8-a220-a93c1513c5d4&response_type=code&redirect_uri=https://zfile.jun6.net/onedrive/callback&scope=offline_access%20User.Read%20Files.ReadWrite.All
|
||||||
|
|
||||||
|
|
||||||
世纪互联版:
|
世纪互联版:
|
||||||
|
|
||||||
https://login.chinacloudapi.cn/common/oauth2/v2.0/authorize?client_id=4a72d927-1907-488d-9eb2-1b465c53c1c5&response_type=code&redirect_uri=https://zfile.jun6.net/onedirve/china-callback&scope=offline_access%20User.Read%20Files.ReadWrite.All
|
https://login.chinacloudapi.cn/common/oauth2/v2.0/authorize?client_id=4a72d927-1907-488d-9eb2-1b465c53c1c5&response_type=code&redirect_uri=https://zfile.jun6.net/onedrive/china-callback&scope=offline_access%20User.Read%20Files.ReadWrite.All
|
||||||
|
|
||||||
|
|
||||||
然后分别填写至访问令牌和刷新令牌即可:
|
然后分别填写至访问令牌和刷新令牌即可:
|
||||||
@@ -116,9 +101,15 @@ https://login.chinacloudapi.cn/common/oauth2/v2.0/authorize?client_id=4a72d927-1
|
|||||||
## 运行环境
|
## 运行环境
|
||||||
|
|
||||||
* JDK: `1.8`
|
* JDK: `1.8`
|
||||||
* 缓存: `caffeine`
|
|
||||||
* 数据库: `h2/mysql`
|
* 数据库: `h2/mysql`
|
||||||
|
|
||||||
|
## 预览
|
||||||
|
|
||||||
|

|
||||||
|

|
||||||
|

|
||||||
|

|
||||||
|
|
||||||
## 常见问题
|
## 常见问题
|
||||||
|
|
||||||
### 数据库
|
### 数据库
|
||||||
@@ -129,18 +120,27 @@ https://login.chinacloudapi.cn/common/oauth2/v2.0/authorize?client_id=4a72d927-1
|
|||||||
|
|
||||||
默认 H2 数据库文件地址: `~/.zfile/db/`, `~` 表示用户目录, windows 为 `C:/Users/用户名/`, linux 为 `/home/用户名/`, root 用户为 `/root/`
|
默认 H2 数据库文件地址: `~/.zfile/db/`, `~` 表示用户目录, windows 为 `C:/Users/用户名/`, linux 为 `/home/用户名/`, root 用户为 `/root/`
|
||||||
|
|
||||||
### 头尾文件和加密文件
|
### 文档文件和加密文件
|
||||||
|
|
||||||
- 目录头部显示文件名为 `header.md`
|
- 目录文档显示文件名为 `readme.md`
|
||||||
- 目录需要密码访问, 添加文件 `password.txt` (无法拦截此文件被下载, 但可以改名文件)
|
- 目录需要密码访问, 添加文件 `password.txt` (无法拦截此文件被下载, 但可以改名文件)
|
||||||
|
|
||||||
## TODO
|
## 开发计划
|
||||||
|
|
||||||
- 文本预览更换更好用的编辑器
|
|
||||||
- 后台支持上传、编辑、删除等操作
|
|
||||||
- API 支持
|
|
||||||
- 更方便的部署方式
|
|
||||||
|
|
||||||
|
- [x] API 支持 [点击查看文档](https://github.com/zhaojun1998/zfile/blob/master/API.md)
|
||||||
|
- [x] 更方便的部署方式
|
||||||
|
- [x] 布局优化 - 自定义操作按钮 (现为右键实现)
|
||||||
|
- [x] 后台优化 - 设置按照其功能进行分离
|
||||||
|
- [x] 体验优化 - 支持前后端分离部署
|
||||||
|
- [x] 体验优化 - 文本预览更换 vscode 同款编辑器 monaco editor
|
||||||
|
- [ ] 新功能 - 后台支持上传、编辑、删除等操作
|
||||||
|
- [ ] 新功能 - WebDav 支持
|
||||||
|
- [ ] 新功能 - Docker 支持
|
||||||
|
- [ ] 新功能 - 离线下载 (aria2)
|
||||||
|
- [ ] 体验优化 - 忽略文件列表 (正则表达式)
|
||||||
|
- [ ] 体验优化 - 自定义支持预览的文件后缀 (正则表达式)
|
||||||
|
- [ ] 架构调整 - 支持多存储策略
|
||||||
|
- [ ] 体验优化 - 一键安装脚本
|
||||||
|
|
||||||
## 支持作者
|
## 支持作者
|
||||||
|
|
||||||
|
|||||||
31
pom.xml
31
pom.xml
@@ -12,7 +12,7 @@
|
|||||||
|
|
||||||
<groupId>im.zhaojun</groupId>
|
<groupId>im.zhaojun</groupId>
|
||||||
<artifactId>zfile</artifactId>
|
<artifactId>zfile</artifactId>
|
||||||
<version>0.9</version>
|
<version>2.2</version>
|
||||||
<name>zfile</name>
|
<name>zfile</name>
|
||||||
<packaging>war</packaging>
|
<packaging>war</packaging>
|
||||||
<description>一个在线的文件浏览系统</description>
|
<description>一个在线的文件浏览系统</description>
|
||||||
@@ -45,6 +45,10 @@
|
|||||||
<artifactId>spring-boot-configuration-processor</artifactId>
|
<artifactId>spring-boot-configuration-processor</artifactId>
|
||||||
<optional>true</optional>
|
<optional>true</optional>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-cache</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
@@ -67,7 +71,7 @@
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>cn.hutool</groupId>
|
<groupId>cn.hutool</groupId>
|
||||||
<artifactId>hutool-all</artifactId>
|
<artifactId>hutool-all</artifactId>
|
||||||
<version>4.5.11</version>
|
<version>5.1.3</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<!-- 存储策略相关 API, 对象存储、FTP、 Rest API-->
|
<!-- 存储策略相关 API, 对象存储、FTP、 Rest API-->
|
||||||
@@ -106,23 +110,9 @@
|
|||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.alicp.jetcache</groupId>
|
<groupId>com.alibaba</groupId>
|
||||||
<artifactId>jetcache-starter-redis</artifactId>
|
<artifactId>fastjson</artifactId>
|
||||||
<version>2.5.14</version>
|
<version>1.2.61</version>
|
||||||
<exclusions>
|
|
||||||
<exclusion>
|
|
||||||
<groupId>com.alicp.jetcache</groupId>
|
|
||||||
<artifactId>jetcache-redis</artifactId>
|
|
||||||
</exclusion>
|
|
||||||
<exclusion>
|
|
||||||
<groupId>redis.clients</groupId>
|
|
||||||
<artifactId>jedis</artifactId>
|
|
||||||
</exclusion>
|
|
||||||
<exclusion>
|
|
||||||
<groupId>com.esotericsoftware</groupId>
|
|
||||||
<artifactId>kryo</artifactId>
|
|
||||||
</exclusion>
|
|
||||||
</exclusions>
|
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
@@ -147,9 +137,8 @@
|
|||||||
</executions>
|
</executions>
|
||||||
<configuration>
|
<configuration>
|
||||||
<jvms>
|
<jvms>
|
||||||
<jvm>-server</jvm>
|
|
||||||
<jvm>-Xmx512m</jvm>
|
|
||||||
<jvm>-Djava.security.egd=file:/dev/./urandom</jvm>
|
<jvm>-Djava.security.egd=file:/dev/./urandom</jvm>
|
||||||
|
<jvm>-Dfile.encoding=utf-8</jvm>
|
||||||
</jvms>
|
</jvms>
|
||||||
</configuration>
|
</configuration>
|
||||||
</plugin>
|
</plugin>
|
||||||
|
|||||||
@@ -1,103 +0,0 @@
|
|||||||
package im.zhaojun.common.config;
|
|
||||||
|
|
||||||
import im.zhaojun.common.model.StorageConfig;
|
|
||||||
import im.zhaojun.common.model.constant.StorageConfigConstant;
|
|
||||||
import im.zhaojun.common.model.enums.StorageTypeEnum;
|
|
||||||
import im.zhaojun.common.service.AbstractFileService;
|
|
||||||
import im.zhaojun.common.service.StorageConfigService;
|
|
||||||
import im.zhaojun.common.service.SystemConfigService;
|
|
||||||
import im.zhaojun.onedrive.china.service.OneDriveChinaService;
|
|
||||||
import im.zhaojun.onedrive.china.service.OneDriveServiceChinaImpl;
|
|
||||||
import im.zhaojun.onedrive.common.model.OneDriveToken;
|
|
||||||
import im.zhaojun.onedrive.international.service.OneDriveService;
|
|
||||||
import im.zhaojun.onedrive.international.service.OneDriveServiceImpl;
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
|
||||||
import org.springframework.context.annotation.Configuration;
|
|
||||||
import org.springframework.scheduling.annotation.EnableScheduling;
|
|
||||||
import org.springframework.scheduling.annotation.Scheduled;
|
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
|
||||||
import java.time.LocalDateTime;
|
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.Objects;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author zhaojun
|
|
||||||
*/
|
|
||||||
@Configuration
|
|
||||||
@EnableScheduling
|
|
||||||
@Slf4j
|
|
||||||
public class GlobalScheduleTask {
|
|
||||||
|
|
||||||
@Resource
|
|
||||||
private StorageConfigService storageConfigService;
|
|
||||||
|
|
||||||
@Resource
|
|
||||||
private OneDriveService oneDriveService;
|
|
||||||
|
|
||||||
@Resource
|
|
||||||
private OneDriveChinaService oneDriveChinaService;
|
|
||||||
|
|
||||||
@Resource
|
|
||||||
private SystemConfigService systemConfigService;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 项目启动 30 秒后, 每半小时执行一次刷新 OneDrive Token 的定时任务.
|
|
||||||
*/
|
|
||||||
@Scheduled(fixedRate = 1000 * 60 * 30, initialDelay = 1000 * 30)
|
|
||||||
public void autoRefreshOneDriveToken() {
|
|
||||||
|
|
||||||
AbstractFileService currentFileService = systemConfigService.getCurrentFileService();
|
|
||||||
|
|
||||||
if (!(currentFileService instanceof OneDriveServiceImpl
|
|
||||||
|| currentFileService instanceof OneDriveServiceChinaImpl)) {
|
|
||||||
log.debug("当前启用存储类型, 不是 OneDrive, 跳过自动刷新 AccessToken");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (currentFileService.getIsUnInitialized()) {
|
|
||||||
log.debug("当前启用 OneDrive 未初始化成功, 跳过自动刷新 AccessToken");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
refreshOneDriveToken(StorageTypeEnum.ONE_DRIVE);
|
|
||||||
} catch (Exception e) {
|
|
||||||
log.debug("刷新 OneDrive Token 失败.", e);
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
refreshOneDriveToken(StorageTypeEnum.ONE_DRIVE_CHINA);
|
|
||||||
} catch (Exception e) {
|
|
||||||
log.debug("刷新 OneDrive 世纪互联 Token 失败.", e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 调用刷新 OneDrive Token
|
|
||||||
*/
|
|
||||||
public void refreshOneDriveToken(StorageTypeEnum storageType) {
|
|
||||||
|
|
||||||
OneDriveToken refreshToken;
|
|
||||||
if (Objects.equals(storageType, StorageTypeEnum.ONE_DRIVE_CHINA)) {
|
|
||||||
refreshToken = oneDriveChinaService.getRefreshToken();
|
|
||||||
} else {
|
|
||||||
refreshToken = oneDriveService.getRefreshToken();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
if (refreshToken.getAccessToken() == null || refreshToken.getRefreshToken() == null) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
StorageConfig accessTokenConfig =
|
|
||||||
storageConfigService.selectByTypeAndKey(storageType, StorageConfigConstant.ACCESS_TOKEN_KEY);
|
|
||||||
StorageConfig refreshTokenConfig =
|
|
||||||
storageConfigService.selectByTypeAndKey(storageType, StorageConfigConstant.REFRESH_TOKEN_KEY);
|
|
||||||
accessTokenConfig.setValue(refreshToken.getAccessToken());
|
|
||||||
refreshTokenConfig.setValue(refreshToken.getRefreshToken());
|
|
||||||
|
|
||||||
storageConfigService.updateStorageConfig(Arrays.asList(accessTokenConfig, refreshTokenConfig));
|
|
||||||
log.info("刷新 {} key 时间: {}", storageType.getDescription(), LocalDateTime.now());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,18 +0,0 @@
|
|||||||
package im.zhaojun.common.config;
|
|
||||||
|
|
||||||
import im.zhaojun.common.model.enums.StorageTypeEnumDeSerializerConvert;
|
|
||||||
import org.springframework.context.annotation.Configuration;
|
|
||||||
import org.springframework.format.FormatterRegistry;
|
|
||||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author zhaojun
|
|
||||||
*/
|
|
||||||
@Configuration
|
|
||||||
public class WebMvcConfig implements WebMvcConfigurer {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void addFormatters(FormatterRegistry registry) {
|
|
||||||
registry.addConverter(new StorageTypeEnumDeSerializerConvert());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,244 +0,0 @@
|
|||||||
package im.zhaojun.common.service;
|
|
||||||
|
|
||||||
import cn.hutool.core.util.BooleanUtil;
|
|
||||||
import cn.hutool.core.util.StrUtil;
|
|
||||||
import com.alicp.jetcache.Cache;
|
|
||||||
import com.alicp.jetcache.RefreshPolicy;
|
|
||||||
import com.alicp.jetcache.anno.CacheRefresh;
|
|
||||||
import com.alicp.jetcache.anno.CacheType;
|
|
||||||
import com.alicp.jetcache.anno.Cached;
|
|
||||||
import com.alicp.jetcache.anno.CreateCache;
|
|
||||||
import im.zhaojun.common.model.constant.ZFileConstant;
|
|
||||||
import im.zhaojun.common.model.dto.FileItemDTO;
|
|
||||||
import im.zhaojun.common.model.dto.SystemConfigDTO;
|
|
||||||
import im.zhaojun.common.model.enums.FileTypeEnum;
|
|
||||||
import im.zhaojun.common.model.enums.StorageTypeEnum;
|
|
||||||
import im.zhaojun.common.util.StringUtils;
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
|
||||||
import org.springframework.aop.framework.AopContext;
|
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
|
||||||
|
|
||||||
import javax.annotation.PostConstruct;
|
|
||||||
import javax.annotation.Resource;
|
|
||||||
import java.util.ArrayDeque;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Objects;
|
|
||||||
import java.util.Set;
|
|
||||||
import java.util.concurrent.TimeUnit;
|
|
||||||
import java.util.stream.Collectors;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author zhaojun
|
|
||||||
* @date 2019/12/28 19:27
|
|
||||||
*/
|
|
||||||
@Slf4j
|
|
||||||
public abstract class AbstractFileService extends FileCacheService implements FileService {
|
|
||||||
|
|
||||||
private static final String SYSTEM_CONFIG_CACHE_PREFIX = "zfile-cache:";
|
|
||||||
|
|
||||||
@Value("${zfile.cache.timeout}")
|
|
||||||
protected Long timeout;
|
|
||||||
|
|
||||||
protected boolean isInitialized = false;
|
|
||||||
|
|
||||||
@Resource
|
|
||||||
private SystemConfigService systemConfigService;
|
|
||||||
|
|
||||||
@Resource
|
|
||||||
private FileAsyncCacheService fileAsyncCacheService;
|
|
||||||
|
|
||||||
@CreateCache(name = SYSTEM_CONFIG_CACHE_PREFIX, cacheType = CacheType.LOCAL)
|
|
||||||
private Cache<String, List<FileItemDTO>> cache;
|
|
||||||
|
|
||||||
/***
|
|
||||||
* 获取指定路径下的文件及文件夹, 默认缓存 60 分钟,每隔 30 分钟刷新一次.
|
|
||||||
* @param path 文件路径
|
|
||||||
* @return 文件及文件夹列表
|
|
||||||
* @throws Exception 获取文件列表中出现的异常
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
@Cached(name = SYSTEM_CONFIG_CACHE_PREFIX,
|
|
||||||
key = "args[0]",
|
|
||||||
cacheType = CacheType.LOCAL, localLimit = 100000, condition = "mvel{bean('systemConfigService').enableCache}")
|
|
||||||
@CacheRefresh(refresh = 30, timeUnit = TimeUnit.MINUTES)
|
|
||||||
public abstract List<FileItemDTO> fileList(String path) throws Exception;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 清理当前存储策略的缓存
|
|
||||||
* 1. 删除全部缓存
|
|
||||||
* 2. 关闭自动刷新
|
|
||||||
* 3. 重置缓存个数
|
|
||||||
* 4. 标记为当前处于未完成缓存状态
|
|
||||||
*/
|
|
||||||
public void clearFileCache() throws Exception {
|
|
||||||
Set<String> cacheKeys = getCacheKeys();
|
|
||||||
cache.removeAll(cacheKeys);
|
|
||||||
closeCacheAutoRefresh();
|
|
||||||
fileAsyncCacheService.resetCacheCount();
|
|
||||||
fileAsyncCacheService.setCacheFinish(false);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 初始化方法, 启动时自动调用实现类的此方法进行初始化.
|
|
||||||
*/
|
|
||||||
@PostConstruct
|
|
||||||
public abstract void init();
|
|
||||||
|
|
||||||
protected boolean testConnection() {
|
|
||||||
boolean flag = true;
|
|
||||||
try {
|
|
||||||
fileList("/");
|
|
||||||
} catch (Exception e) {
|
|
||||||
log.debug(getStorageTypeEnum().getDescription() + " 初始化异常", e);
|
|
||||||
flag = false;
|
|
||||||
}
|
|
||||||
return flag;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 获取是否初始化成功
|
|
||||||
* @return 初始化成功与否
|
|
||||||
*/
|
|
||||||
public boolean getIsUnInitialized() {
|
|
||||||
return !isInitialized;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 获取是否初始化成功
|
|
||||||
* @return 初始化成功与否
|
|
||||||
*/
|
|
||||||
public boolean getIsInitialized() {
|
|
||||||
return isInitialized;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 获取存储策略类型
|
|
||||||
* @return 存储策略类型枚举
|
|
||||||
*/
|
|
||||||
public abstract StorageTypeEnum getStorageTypeEnum();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 搜索文件
|
|
||||||
* @param name 文件名
|
|
||||||
* @return 包含该文件名的所有文件或文件夹
|
|
||||||
*/
|
|
||||||
public List<FileItemDTO> search(String name) {
|
|
||||||
List<FileItemDTO> result = new ArrayList<>();
|
|
||||||
|
|
||||||
boolean searchIgnoreCase = systemConfigService.getSearchIgnoreCase();
|
|
||||||
|
|
||||||
List<FileItemDTO> fileItemList = selectAllFileList();
|
|
||||||
for (FileItemDTO fileItemDTO : fileItemList) {
|
|
||||||
|
|
||||||
boolean testResult;
|
|
||||||
|
|
||||||
if (searchIgnoreCase) {
|
|
||||||
testResult = StrUtil.containsIgnoreCase(fileItemDTO.getName(), name);
|
|
||||||
} else {
|
|
||||||
testResult = fileItemDTO.getName().contains(name);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (testResult) {
|
|
||||||
result.add(fileItemDTO);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 查询所有文件, 仅去缓存中查询.
|
|
||||||
* @return 所有文件
|
|
||||||
*/
|
|
||||||
public List<FileItemDTO> selectAllFileList() {
|
|
||||||
List<FileItemDTO> result = new ArrayList<>();
|
|
||||||
boolean enableCache = systemConfigService.getEnableCache();
|
|
||||||
if (!enableCache) {
|
|
||||||
log.debug("未开启缓存, 不支持查询所有文件.");
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
String path = "/";
|
|
||||||
|
|
||||||
List<FileItemDTO> fileItemList = cache.get(path);
|
|
||||||
fileItemList = fileItemList == null ? new ArrayList<>() : fileItemList;
|
|
||||||
ArrayDeque<FileItemDTO> queue = new ArrayDeque<>(fileItemList);
|
|
||||||
|
|
||||||
while (!queue.isEmpty()) {
|
|
||||||
FileItemDTO fileItemDTO = queue.pop();
|
|
||||||
result.add(fileItemDTO);
|
|
||||||
if (fileItemDTO.getType() == FileTypeEnum.FOLDER) {
|
|
||||||
String filePath = StringUtils.removeDuplicateSeparator("/" + fileItemDTO.getPath() + "/" + fileItemDTO.getName() + "/");
|
|
||||||
List<FileItemDTO> cacheList = cache.get(filePath);
|
|
||||||
if (cacheList != null && isNotEncryptedFolder(cacheList)) {
|
|
||||||
queue.addAll(cacheList);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 不是加密文件夹
|
|
||||||
* @param list 文件夹中的内容
|
|
||||||
* @return 返回此文件夹是否加密.
|
|
||||||
*/
|
|
||||||
private boolean isNotEncryptedFolder(List<FileItemDTO> list) {
|
|
||||||
// 如果开启了 "搜索包含加密文件" 选项, 则直接返回 true.
|
|
||||||
SystemConfigDTO systemConfig = systemConfigService.getSystemConfig();
|
|
||||||
if (BooleanUtil.isFalse(systemConfig.getSearchContainEncryptedFile())) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 遍历文件判断是否包含
|
|
||||||
for (FileItemDTO fileItemDTO : list) {
|
|
||||||
if (Objects.equals(ZFileConstant.PASSWORD_FILE_NAME, fileItemDTO.getName())) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 获取所有缓存的 Key, 仅当开启缓存, 且缓存完成时, 可获取.
|
|
||||||
* @return 所有缓存的 Key
|
|
||||||
*/
|
|
||||||
public Set<String> getCacheKeys() {
|
|
||||||
if (systemConfigService.getEnableCache() && fileAsyncCacheService.isCacheFinish()) {
|
|
||||||
Set<String> collect = selectAllFileList().stream().map(fileItemDTO -> {
|
|
||||||
if (fileItemDTO.getType() == FileTypeEnum.FOLDER) {
|
|
||||||
return StringUtils.removeDuplicateSeparator("/" + fileItemDTO.getPath() + "/" + fileItemDTO.getName() + "/");
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}).collect(Collectors.toSet());
|
|
||||||
collect.remove(null);
|
|
||||||
collect.add("/");
|
|
||||||
return collect;
|
|
||||||
} else {
|
|
||||||
return Collections.emptySet();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 刷新缓存
|
|
||||||
*/
|
|
||||||
public void refreshCache(String key) throws Exception {
|
|
||||||
cache.remove(key);
|
|
||||||
FileService currentFileService = (FileService) AopContext.currentProxy();
|
|
||||||
currentFileService.fileList(key);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void closeCacheAutoRefresh() {
|
|
||||||
cache.config().setRefreshPolicy(null);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void openCacheAutoRefresh() {
|
|
||||||
RefreshPolicy refreshPolicy = RefreshPolicy.newPolicy(30, TimeUnit.MINUTES);
|
|
||||||
cache.config().setRefreshPolicy(refreshPolicy);
|
|
||||||
}
|
|
||||||
|
|
||||||
public abstract FileItemDTO getFileItem(String path);
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,140 +0,0 @@
|
|||||||
package im.zhaojun.common.service;
|
|
||||||
|
|
||||||
import cn.hutool.core.util.ObjectUtil;
|
|
||||||
import com.alicp.jetcache.Cache;
|
|
||||||
import com.alicp.jetcache.anno.CacheType;
|
|
||||||
import com.alicp.jetcache.anno.CreateCache;
|
|
||||||
import im.zhaojun.common.config.StorageTypeFactory;
|
|
||||||
import im.zhaojun.common.model.dto.FileItemDTO;
|
|
||||||
import im.zhaojun.common.model.enums.FileTypeEnum;
|
|
||||||
import im.zhaojun.common.model.enums.StorageTypeEnum;
|
|
||||||
import im.zhaojun.common.util.StringUtils;
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
|
||||||
import org.springframework.scheduling.annotation.Async;
|
|
||||||
import org.springframework.stereotype.Service;
|
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
|
||||||
import java.util.ArrayDeque;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author zhaojun
|
|
||||||
*/
|
|
||||||
@Slf4j
|
|
||||||
@Service
|
|
||||||
public class FileAsyncCacheService {
|
|
||||||
|
|
||||||
public static final String CACHE_PROCESS_PREFIX = "zfile-process-cache:";
|
|
||||||
|
|
||||||
public static final String CACHE_FILE_COUNT_KEY = "file-count";
|
|
||||||
|
|
||||||
public static final String CACHE_DIRECTORY_COUNT_KEY = "directory-count";
|
|
||||||
|
|
||||||
@CreateCache(name = "SYSTEM_CONFIG_CACHE_PREFIX", cacheType = CacheType.LOCAL)
|
|
||||||
private Cache<String, Integer> cache;
|
|
||||||
|
|
||||||
private boolean cacheFinish;
|
|
||||||
|
|
||||||
@Resource
|
|
||||||
private SystemConfigService systemConfigService;
|
|
||||||
|
|
||||||
@Async
|
|
||||||
public void cacheGlobalFile() {
|
|
||||||
StorageTypeEnum storageStrategy = systemConfigService.getCurrentStorageStrategy();
|
|
||||||
|
|
||||||
if (storageStrategy == null) {
|
|
||||||
log.debug("尚未配置存储策略. 跳过启动缓存.");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
boolean enableCache = systemConfigService.getEnableCache();
|
|
||||||
if (!enableCache) {
|
|
||||||
log.debug("存储策略 {} 未启用缓存, 跳过缓存.", storageStrategy.getDescription());
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
AbstractFileService fileService = StorageTypeFactory.getStorageTypeService(storageStrategy);
|
|
||||||
|
|
||||||
if (fileService.getIsUnInitialized()) {
|
|
||||||
log.debug("存储策略 {} 未初始化成功, 跳过缓存.", storageStrategy.getDescription());
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
Integer cacheDirectoryCount = cache.get(CACHE_DIRECTORY_COUNT_KEY);
|
|
||||||
if (cacheDirectoryCount == null) {
|
|
||||||
cacheDirectoryCount = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
Integer cacheFileCount = cache.get(CACHE_FILE_COUNT_KEY);
|
|
||||||
if (cacheFileCount == null) {
|
|
||||||
cacheFileCount = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
log.info("缓存 {} 所有文件开始", storageStrategy.getDescription());
|
|
||||||
long startTime = System.currentTimeMillis();
|
|
||||||
try {
|
|
||||||
FileService currentFileService = systemConfigService.getCurrentFileService();
|
|
||||||
List<FileItemDTO> rootFileItems = currentFileService.fileList("/");
|
|
||||||
ArrayDeque<FileItemDTO> queue = new ArrayDeque<>(rootFileItems);
|
|
||||||
|
|
||||||
while (!queue.isEmpty()) {
|
|
||||||
FileItemDTO fileItemDTO = queue.pop();
|
|
||||||
|
|
||||||
if (FileTypeEnum.FOLDER.equals(fileItemDTO.getType())) {
|
|
||||||
cacheDirectoryCount++;
|
|
||||||
}
|
|
||||||
if (FileTypeEnum.FILE.equals(fileItemDTO.getType())) {
|
|
||||||
cacheFileCount++;
|
|
||||||
}
|
|
||||||
|
|
||||||
log.debug("已缓存 {} 个文件夹", cacheDirectoryCount);
|
|
||||||
cache.put(CACHE_DIRECTORY_COUNT_KEY, cacheDirectoryCount);
|
|
||||||
|
|
||||||
log.debug("已缓存 {} 个文件", cacheFileCount);
|
|
||||||
cache.put(CACHE_FILE_COUNT_KEY, cacheFileCount);
|
|
||||||
|
|
||||||
if (fileItemDTO.getType() == FileTypeEnum.FOLDER) {
|
|
||||||
String filePath = StringUtils.removeDuplicateSeparator("/" + fileItemDTO.getPath() + "/" + fileItemDTO.getName() + "/");
|
|
||||||
|
|
||||||
List<FileItemDTO> fileItems = currentFileService.fileList(filePath);
|
|
||||||
queue.addAll(fileItems);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
cache.put(CACHE_DIRECTORY_COUNT_KEY, cacheDirectoryCount);
|
|
||||||
cache.put(CACHE_FILE_COUNT_KEY, cacheFileCount);
|
|
||||||
} catch (Exception e) {
|
|
||||||
log.error("缓存所有文件失败", e);
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
long endTime = System.currentTimeMillis();
|
|
||||||
log.info("缓存 {} 所有文件结束, 用时: {} 秒, 文件夹共 {} 个, 文件共 {} 个",
|
|
||||||
storageStrategy.getDescription(),
|
|
||||||
( (endTime - startTime) / 1000 ), cacheDirectoryCount, cacheFileCount);
|
|
||||||
cacheFinish = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 清理缓存的文件/文件夹数量统计
|
|
||||||
*/
|
|
||||||
public void resetCacheCount() {
|
|
||||||
cache.remove(CACHE_DIRECTORY_COUNT_KEY);
|
|
||||||
cache.remove(CACHE_FILE_COUNT_KEY);
|
|
||||||
}
|
|
||||||
|
|
||||||
public Integer getCacheDirectoryCount() {
|
|
||||||
return ObjectUtil.defaultIfNull(cache.get(CACHE_DIRECTORY_COUNT_KEY), 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
public Integer getCacheFileCount() {
|
|
||||||
return ObjectUtil.defaultIfNull(cache.get(CACHE_FILE_COUNT_KEY), 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean isCacheFinish() {
|
|
||||||
return cacheFinish;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setCacheFinish(boolean cacheFinish) {
|
|
||||||
this.cacheFinish = cacheFinish;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,40 +0,0 @@
|
|||||||
package im.zhaojun.common.service;
|
|
||||||
|
|
||||||
import im.zhaojun.common.model.constant.ZFileConstant;
|
|
||||||
import im.zhaojun.common.model.dto.FileItemDTO;
|
|
||||||
import im.zhaojun.common.model.dto.SiteConfigDTO;
|
|
||||||
import im.zhaojun.common.util.HttpUtil;
|
|
||||||
import org.springframework.stereotype.Service;
|
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author zhaojun
|
|
||||||
*/
|
|
||||||
@Service
|
|
||||||
public class SystemService {
|
|
||||||
|
|
||||||
@Resource
|
|
||||||
private SystemConfigService systemConfigService;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 构建指定路径下标题, 页头, 页尾
|
|
||||||
* @param path 路径
|
|
||||||
*/
|
|
||||||
public synchronized SiteConfigDTO getConfig(String path) throws Exception {
|
|
||||||
|
|
||||||
SiteConfigDTO siteConfigDTO = new SiteConfigDTO();
|
|
||||||
AbstractFileService fileService = systemConfigService.getCurrentFileService();
|
|
||||||
|
|
||||||
List<FileItemDTO> fileItemList = new ArrayList<>(fileService.fileList(path));
|
|
||||||
for (FileItemDTO fileItemDTO : fileItemList) {
|
|
||||||
if (ZFileConstant.HEADER_FILE_NAME.equalsIgnoreCase(fileItemDTO.getName())) {
|
|
||||||
siteConfigDTO.setHeader(HttpUtil.getTextContent(fileItemDTO.getUrl()));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return siteConfigDTO;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,68 +0,0 @@
|
|||||||
package im.zhaojun.local.controller;
|
|
||||||
|
|
||||||
import cn.hutool.core.util.URLUtil;
|
|
||||||
import im.zhaojun.common.exception.NotExistFileException;
|
|
||||||
import im.zhaojun.common.util.StringUtils;
|
|
||||||
import im.zhaojun.local.service.LocalServiceImpl;
|
|
||||||
import org.springframework.core.io.FileSystemResource;
|
|
||||||
import org.springframework.http.HttpHeaders;
|
|
||||||
import org.springframework.http.MediaType;
|
|
||||||
import org.springframework.http.ResponseEntity;
|
|
||||||
import org.springframework.stereotype.Controller;
|
|
||||||
import org.springframework.util.AntPathMatcher;
|
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
|
||||||
import org.springframework.web.bind.annotation.ResponseBody;
|
|
||||||
import org.springframework.web.servlet.HandlerMapping;
|
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
|
||||||
import javax.servlet.http.HttpServletRequest;
|
|
||||||
import java.io.File;
|
|
||||||
import java.util.Date;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author zhaojun
|
|
||||||
*/
|
|
||||||
@Controller
|
|
||||||
public class LocalController {
|
|
||||||
|
|
||||||
@Resource
|
|
||||||
private LocalServiceImpl localServiceImpl;
|
|
||||||
|
|
||||||
@GetMapping("/file/**")
|
|
||||||
@ResponseBody
|
|
||||||
public ResponseEntity<FileSystemResource> downAttachment(final HttpServletRequest request) {
|
|
||||||
String path = (String) request.getAttribute(
|
|
||||||
HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE);
|
|
||||||
String bestMatchPattern = (String) request.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE);
|
|
||||||
AntPathMatcher apm = new AntPathMatcher();
|
|
||||||
String filePath = apm.extractPathWithinPattern(bestMatchPattern, path);
|
|
||||||
|
|
||||||
return export(new File(StringUtils.concatPath(localServiceImpl.getFilePath(), URLUtil.decode(filePath))));
|
|
||||||
}
|
|
||||||
|
|
||||||
private ResponseEntity<FileSystemResource> export(File file) {
|
|
||||||
|
|
||||||
if (!file.exists()) {
|
|
||||||
throw new NotExistFileException();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
MediaType mediaType = MediaType.APPLICATION_OCTET_STREAM;
|
|
||||||
|
|
||||||
HttpHeaders headers = new HttpHeaders();
|
|
||||||
headers.add("Cache-Control", "no-cache, no-store, must-revalidate");
|
|
||||||
headers.setContentDispositionFormData("attachment", URLUtil.encode(file.getName()));
|
|
||||||
|
|
||||||
headers.add("Pragma", "no-cache");
|
|
||||||
headers.add("Expires", "0");
|
|
||||||
headers.add("Last-Modified", new Date().toString());
|
|
||||||
headers.add("ETag", String.valueOf(System.currentTimeMillis()));
|
|
||||||
return ResponseEntity
|
|
||||||
.ok()
|
|
||||||
.headers(headers)
|
|
||||||
.contentLength(file.length())
|
|
||||||
.contentType(mediaType)
|
|
||||||
.body(new FileSystemResource(file));
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,54 +0,0 @@
|
|||||||
package im.zhaojun.onedrive.china.service;
|
|
||||||
|
|
||||||
import im.zhaojun.onedrive.common.service.AbstractOneDriveService;
|
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
|
||||||
import org.springframework.stereotype.Service;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author zhaojun
|
|
||||||
*/
|
|
||||||
@Service
|
|
||||||
public class OneDriveChinaService extends AbstractOneDriveService {
|
|
||||||
|
|
||||||
@Value("${zfile.onedirve-china.clientId}")
|
|
||||||
private String clientId;
|
|
||||||
|
|
||||||
@Value("${zfile.onedirve-china.redirectUri}")
|
|
||||||
private String redirectUri;
|
|
||||||
|
|
||||||
@Value("${zfile.onedirve-china.clientSecret}")
|
|
||||||
private String clientSecret;
|
|
||||||
|
|
||||||
@Value("${zfile.onedirve-china.scope}")
|
|
||||||
private String scope;
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String getGraphEndPoint() {
|
|
||||||
return "microsoftgraph.chinacloudapi.cn";
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String getAuthenticateEndPoint() {
|
|
||||||
return "login.partner.microsoftonline.cn";
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String getClientId() {
|
|
||||||
return clientId;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String getRedirectUri() {
|
|
||||||
return redirectUri;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String getClientSecret() {
|
|
||||||
return clientSecret;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String getScope() {
|
|
||||||
return scope;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,83 +0,0 @@
|
|||||||
package im.zhaojun.onedrive.china.service;
|
|
||||||
|
|
||||||
import im.zhaojun.common.config.GlobalScheduleTask;
|
|
||||||
import im.zhaojun.common.exception.NotExistFileException;
|
|
||||||
import im.zhaojun.common.model.StorageConfig;
|
|
||||||
import im.zhaojun.common.model.constant.StorageConfigConstant;
|
|
||||||
import im.zhaojun.common.model.dto.FileItemDTO;
|
|
||||||
import im.zhaojun.common.model.enums.StorageTypeEnum;
|
|
||||||
import im.zhaojun.common.service.AbstractFileService;
|
|
||||||
import im.zhaojun.common.service.FileService;
|
|
||||||
import im.zhaojun.common.service.StorageConfigService;
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
|
||||||
import org.springframework.stereotype.Service;
|
|
||||||
import org.springframework.util.StringUtils;
|
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author zhaojun
|
|
||||||
* @date 2020/1/12 13:53
|
|
||||||
*/
|
|
||||||
@Service
|
|
||||||
@Slf4j
|
|
||||||
public class OneDriveServiceChinaImpl extends AbstractFileService implements FileService {
|
|
||||||
|
|
||||||
@Resource
|
|
||||||
private GlobalScheduleTask globalScheduleTask;
|
|
||||||
|
|
||||||
@Resource
|
|
||||||
private StorageConfigService storageConfigService;
|
|
||||||
|
|
||||||
@Resource
|
|
||||||
private OneDriveChinaService oneDriveChinaService;
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void init() {
|
|
||||||
try {
|
|
||||||
Map<String, StorageConfig> stringStorageConfigMap =
|
|
||||||
storageConfigService.selectStorageConfigMapByKey(getStorageTypeEnum());
|
|
||||||
String accessToken = stringStorageConfigMap.get(StorageConfigConstant.ACCESS_TOKEN_KEY).getValue();
|
|
||||||
String refreshToken = stringStorageConfigMap.get(StorageConfigConstant.REFRESH_TOKEN_KEY).getValue();
|
|
||||||
|
|
||||||
if (StringUtils.isEmpty(accessToken) || StringUtils.isEmpty(refreshToken)) {
|
|
||||||
log.debug("初始化存储策略 [{}] 失败: 参数不完整", getStorageTypeEnum().getDescription());
|
|
||||||
isInitialized = false;
|
|
||||||
} else {
|
|
||||||
globalScheduleTask.refreshOneDriveToken(getStorageTypeEnum());
|
|
||||||
isInitialized = testConnection();
|
|
||||||
}
|
|
||||||
} catch (Exception e) {
|
|
||||||
log.debug(getStorageTypeEnum().getDescription() + " 初始化异常, 已跳过");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public List<FileItemDTO> fileList(String path) {
|
|
||||||
return oneDriveChinaService.list(path);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String getDownloadUrl(String path) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public StorageTypeEnum getStorageTypeEnum() {
|
|
||||||
return StorageTypeEnum.ONE_DRIVE_CHINA;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public FileItemDTO getFileItem(String path) {
|
|
||||||
FileItemDTO fileItemDTO ;
|
|
||||||
|
|
||||||
try {
|
|
||||||
fileItemDTO = oneDriveChinaService.getItem(path);
|
|
||||||
} catch (Exception e) {
|
|
||||||
throw new NotExistFileException();
|
|
||||||
}
|
|
||||||
return fileItemDTO;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,54 +0,0 @@
|
|||||||
package im.zhaojun.onedrive.international.service;
|
|
||||||
|
|
||||||
import im.zhaojun.onedrive.common.service.AbstractOneDriveService;
|
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
|
||||||
import org.springframework.stereotype.Service;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author zhaojun
|
|
||||||
*/
|
|
||||||
@Service
|
|
||||||
public class OneDriveService extends AbstractOneDriveService {
|
|
||||||
|
|
||||||
@Value("${zfile.onedirve.clientId}")
|
|
||||||
protected String clientId;
|
|
||||||
|
|
||||||
@Value("${zfile.onedirve.redirectUri}")
|
|
||||||
protected String redirectUri;
|
|
||||||
|
|
||||||
@Value("${zfile.onedirve.clientSecret}")
|
|
||||||
protected String clientSecret;
|
|
||||||
|
|
||||||
@Value("${zfile.onedirve.scope}")
|
|
||||||
protected String scope;
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String getGraphEndPoint() {
|
|
||||||
return "graph.microsoft.com";
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String getAuthenticateEndPoint() {
|
|
||||||
return "login.microsoftonline.com";
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String getClientId() {
|
|
||||||
return clientId;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String getRedirectUri() {
|
|
||||||
return redirectUri;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String getClientSecret() {
|
|
||||||
return clientSecret;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String getScope() {
|
|
||||||
return scope;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,83 +0,0 @@
|
|||||||
package im.zhaojun.onedrive.international.service;
|
|
||||||
|
|
||||||
import im.zhaojun.common.config.GlobalScheduleTask;
|
|
||||||
import im.zhaojun.common.exception.NotExistFileException;
|
|
||||||
import im.zhaojun.common.model.StorageConfig;
|
|
||||||
import im.zhaojun.common.model.constant.StorageConfigConstant;
|
|
||||||
import im.zhaojun.common.model.dto.FileItemDTO;
|
|
||||||
import im.zhaojun.common.model.enums.StorageTypeEnum;
|
|
||||||
import im.zhaojun.common.service.AbstractFileService;
|
|
||||||
import im.zhaojun.common.service.FileService;
|
|
||||||
import im.zhaojun.common.service.StorageConfigService;
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
|
||||||
import org.springframework.stereotype.Service;
|
|
||||||
import org.springframework.util.StringUtils;
|
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author zhaojun
|
|
||||||
* @date 2020/1/12 13:53
|
|
||||||
*/
|
|
||||||
@Service
|
|
||||||
@Slf4j
|
|
||||||
public class OneDriveServiceImpl extends AbstractFileService implements FileService {
|
|
||||||
|
|
||||||
@Resource
|
|
||||||
private GlobalScheduleTask globalScheduleTask;
|
|
||||||
|
|
||||||
@Resource
|
|
||||||
private StorageConfigService storageConfigService;
|
|
||||||
|
|
||||||
@Resource
|
|
||||||
private OneDriveService oneDriveService;
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void init() {
|
|
||||||
try {
|
|
||||||
Map<String, StorageConfig> stringStorageConfigMap =
|
|
||||||
storageConfigService.selectStorageConfigMapByKey(getStorageTypeEnum());
|
|
||||||
String accessToken = stringStorageConfigMap.get(StorageConfigConstant.ACCESS_TOKEN_KEY).getValue();
|
|
||||||
String refreshToken = stringStorageConfigMap.get(StorageConfigConstant.REFRESH_TOKEN_KEY).getValue();
|
|
||||||
|
|
||||||
if (StringUtils.isEmpty(accessToken) || StringUtils.isEmpty(refreshToken)) {
|
|
||||||
log.debug("初始化存储策略 [{}] 失败: 参数不完整", getStorageTypeEnum().getDescription());
|
|
||||||
isInitialized = false;
|
|
||||||
} else {
|
|
||||||
globalScheduleTask.refreshOneDriveToken(getStorageTypeEnum());
|
|
||||||
isInitialized = testConnection();
|
|
||||||
}
|
|
||||||
} catch (Exception e) {
|
|
||||||
log.debug(getStorageTypeEnum().getDescription() + " 初始化异常, 已跳过");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public List<FileItemDTO> fileList(String path) {
|
|
||||||
return oneDriveService.list(path);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String getDownloadUrl(String path) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public StorageTypeEnum getStorageTypeEnum() {
|
|
||||||
return StorageTypeEnum.ONE_DRIVE;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public FileItemDTO getFileItem(String path) {
|
|
||||||
FileItemDTO fileItemDTO ;
|
|
||||||
|
|
||||||
try {
|
|
||||||
fileItemDTO = oneDriveService.getItem(path);
|
|
||||||
} catch (Exception e) {
|
|
||||||
throw new NotExistFileException();
|
|
||||||
}
|
|
||||||
return fileItemDTO;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,7 +1,5 @@
|
|||||||
package im.zhaojun;
|
package im.zhaojun.zfile;
|
||||||
|
|
||||||
import com.alicp.jetcache.anno.config.EnableCreateCacheAnnotation;
|
|
||||||
import com.alicp.jetcache.anno.config.EnableMethodCache;
|
|
||||||
import org.springframework.boot.SpringApplication;
|
import org.springframework.boot.SpringApplication;
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
import org.springframework.context.annotation.EnableAspectJAutoProxy;
|
import org.springframework.context.annotation.EnableAspectJAutoProxy;
|
||||||
@@ -12,8 +10,6 @@ import org.springframework.scheduling.annotation.EnableAsync;
|
|||||||
*/
|
*/
|
||||||
@EnableAsync
|
@EnableAsync
|
||||||
@SpringBootApplication
|
@SpringBootApplication
|
||||||
@EnableMethodCache(basePackages = "im.zhaojun", proxyTargetClass = true)
|
|
||||||
@EnableCreateCacheAnnotation
|
|
||||||
@EnableAspectJAutoProxy(exposeProxy = true)
|
@EnableAspectJAutoProxy(exposeProxy = true)
|
||||||
public class ZfileApplication {
|
public class ZfileApplication {
|
||||||
|
|
||||||
112
src/main/java/im/zhaojun/zfile/cache/ZFileCache.java
vendored
Normal file
112
src/main/java/im/zhaojun/zfile/cache/ZFileCache.java
vendored
Normal file
@@ -0,0 +1,112 @@
|
|||||||
|
package im.zhaojun.zfile.cache;
|
||||||
|
|
||||||
|
import cn.hutool.core.util.StrUtil;
|
||||||
|
import im.zhaojun.zfile.model.constant.ZFileConstant;
|
||||||
|
import im.zhaojun.zfile.model.dto.FileItemDTO;
|
||||||
|
import im.zhaojun.zfile.model.dto.SystemConfigDTO;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import java.util.*;
|
||||||
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
import java.util.concurrent.ConcurrentMap;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author zhaojun
|
||||||
|
*/
|
||||||
|
@Component
|
||||||
|
public class ZFileCache {
|
||||||
|
|
||||||
|
private ConcurrentMap<String, List<FileItemDTO>> fileCache = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
|
private SystemConfigDTO systemConfigCache;
|
||||||
|
|
||||||
|
public Date lastCacheAutoRefreshDate;
|
||||||
|
|
||||||
|
public synchronized void put(String key, List<FileItemDTO> value) {
|
||||||
|
fileCache.put(key, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<FileItemDTO> get(String key) {
|
||||||
|
return fileCache.get(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void clear() {
|
||||||
|
fileCache.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
public int cacheCount() {
|
||||||
|
return fileCache.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<FileItemDTO> find(String key, boolean ignoreCase, boolean searchContainEncryptedFile) {
|
||||||
|
List<FileItemDTO> result = new ArrayList<>();
|
||||||
|
|
||||||
|
Collection<List<FileItemDTO>> values = fileCache.values();
|
||||||
|
for (List<FileItemDTO> fileItemList : values) {
|
||||||
|
|
||||||
|
// 如果开启了 "搜索包含加密文件" 选项, 则直接返回 true.
|
||||||
|
if (!searchContainEncryptedFile && isEncryptedFolder(fileItemList)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (FileItemDTO fileItemDTO : fileItemList) {
|
||||||
|
boolean testResult;
|
||||||
|
|
||||||
|
if (ignoreCase) {
|
||||||
|
testResult = StrUtil.containsIgnoreCase(fileItemDTO.getName(), key);
|
||||||
|
} else {
|
||||||
|
testResult = fileItemDTO.getName().contains(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (testResult) {
|
||||||
|
result.add(fileItemDTO);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Set<String> keySet() {
|
||||||
|
return fileCache.keySet();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void remove(String key) {
|
||||||
|
fileCache.remove(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void updateConfig(SystemConfigDTO systemConfigCache) {
|
||||||
|
this.systemConfigCache = systemConfigCache;
|
||||||
|
}
|
||||||
|
|
||||||
|
public SystemConfigDTO getConfig() {
|
||||||
|
return this.systemConfigCache;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void removeConfig() {
|
||||||
|
this.systemConfigCache = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Date getLastCacheAutoRefreshDate() {
|
||||||
|
return lastCacheAutoRefreshDate;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setLastCacheAutoRefreshDate(Date lastCacheAutoRefreshDate) {
|
||||||
|
this.lastCacheAutoRefreshDate = lastCacheAutoRefreshDate;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 不是加密文件夹
|
||||||
|
* @param list 文件夹中的内容
|
||||||
|
* @return 返回此文件夹是否加密.
|
||||||
|
*/
|
||||||
|
private boolean isEncryptedFolder(List<FileItemDTO> list) {
|
||||||
|
// 遍历文件判断是否包含
|
||||||
|
for (FileItemDTO fileItemDTO : list) {
|
||||||
|
if (Objects.equals(ZFileConstant.PASSWORD_FILE_NAME, fileItemDTO.getName())) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
21
src/main/java/im/zhaojun/zfile/config/CacheConfig.java
Normal file
21
src/main/java/im/zhaojun/zfile/config/CacheConfig.java
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
package im.zhaojun.zfile.config;
|
||||||
|
|
||||||
|
import im.zhaojun.zfile.model.dto.FileItemDTO;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
import java.util.concurrent.ConcurrentMap;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author zhaojun
|
||||||
|
*/
|
||||||
|
@Configuration
|
||||||
|
public class CacheConfig {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public ConcurrentMap<String, List<FileItemDTO>> concurrentMapCache() {
|
||||||
|
return new ConcurrentHashMap<>();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
package im.zhaojun.zfile.config;
|
||||||
|
|
||||||
|
import org.springframework.http.HttpHeaders;
|
||||||
|
import org.springframework.http.HttpRequest;
|
||||||
|
import org.springframework.http.client.ClientHttpRequestExecution;
|
||||||
|
import org.springframework.http.client.ClientHttpRequestInterceptor;
|
||||||
|
import org.springframework.http.client.ClientHttpResponse;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.Collections;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author zhaojun
|
||||||
|
*/
|
||||||
|
public class ContentTypeTextToTextJson implements ClientHttpRequestInterceptor {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution)
|
||||||
|
throws IOException {
|
||||||
|
ClientHttpResponse response = execution.execute(request, body);
|
||||||
|
HttpHeaders headers = response.getHeaders();
|
||||||
|
headers.put("Content-Type", Collections.singletonList("application/text"));
|
||||||
|
return response;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package im.zhaojun.common.config;
|
package im.zhaojun.zfile.config;
|
||||||
|
|
||||||
import org.springframework.http.HttpHeaders;
|
import org.springframework.http.HttpHeaders;
|
||||||
import org.springframework.web.cors.CorsUtils;
|
import org.springframework.web.cors.CorsUtils;
|
||||||
@@ -0,0 +1,81 @@
|
|||||||
|
package im.zhaojun.zfile.config;
|
||||||
|
|
||||||
|
import im.zhaojun.zfile.model.enums.StorageTypeEnum;
|
||||||
|
import im.zhaojun.zfile.service.SystemConfigService;
|
||||||
|
import im.zhaojun.zfile.service.base.AbstractBaseFileService;
|
||||||
|
import im.zhaojun.zfile.service.impl.OneDriveChinaServiceImpl;
|
||||||
|
import im.zhaojun.zfile.service.impl.OneDriveServiceImpl;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.scheduling.annotation.EnableScheduling;
|
||||||
|
import org.springframework.scheduling.annotation.Scheduled;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author zhaojun
|
||||||
|
*/
|
||||||
|
@Configuration
|
||||||
|
@EnableScheduling
|
||||||
|
@Slf4j
|
||||||
|
public class GlobalScheduleTask {
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private OneDriveServiceImpl oneDriveServiceImpl;
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private OneDriveChinaServiceImpl oneDriveChinaServiceImpl;
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private SystemConfigService systemConfigService;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 项目启动 30 秒后, 每 15 分钟执行一次刷新 OneDrive Token 的定时任务.
|
||||||
|
*/
|
||||||
|
@Scheduled(fixedRate = 1000 * 60 * 10, initialDelay = 1000 * 30)
|
||||||
|
public void autoRefreshOneDriveToken() {
|
||||||
|
|
||||||
|
try {
|
||||||
|
log.debug("尝试调用 OneDrive 自动刷新 AccessToken 定时任务");
|
||||||
|
|
||||||
|
AbstractBaseFileService currentFileService = systemConfigService.getCurrentFileService();
|
||||||
|
|
||||||
|
if (!(currentFileService instanceof OneDriveServiceImpl
|
||||||
|
|| currentFileService instanceof OneDriveChinaServiceImpl)) {
|
||||||
|
log.debug("当前启用存储类型, 不是 OneDrive, 跳过自动刷新 AccessToken");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (currentFileService.getIsUnInitialized()) {
|
||||||
|
log.debug("当前启用 OneDrive 未初始化成功, 跳过自动刷新 AccessToken");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
StorageTypeEnum currentStorageTypeEnum = currentFileService.getStorageTypeEnum();
|
||||||
|
|
||||||
|
try {
|
||||||
|
refreshOneDriveToken(currentStorageTypeEnum);
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.debug("刷新 " + currentStorageTypeEnum.getDescription() + " Token 失败.", e);
|
||||||
|
}
|
||||||
|
} catch (Throwable e) {
|
||||||
|
log.debug("尝试调用 OneDrive 自动刷新 AccessToken 定时任务出现未知异常", e);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 调用刷新 OneDrive Token
|
||||||
|
*/
|
||||||
|
public void refreshOneDriveToken(StorageTypeEnum storageType) {
|
||||||
|
if (Objects.equals(storageType, StorageTypeEnum.ONE_DRIVE_CHINA)) {
|
||||||
|
oneDriveChinaServiceImpl.refreshOneDriveToken();
|
||||||
|
} else {
|
||||||
|
oneDriveServiceImpl.refreshOneDriveToken();
|
||||||
|
}
|
||||||
|
log.info("刷新 {} key 时间: {}", storageType.getDescription(), LocalDateTime.now());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,11 +1,11 @@
|
|||||||
package im.zhaojun.onedrive.common.config;
|
package im.zhaojun.zfile.config;
|
||||||
|
|
||||||
import im.zhaojun.common.model.StorageConfig;
|
import im.zhaojun.zfile.model.entity.StorageConfig;
|
||||||
import im.zhaojun.common.model.constant.StorageConfigConstant;
|
import im.zhaojun.zfile.model.constant.StorageConfigConstant;
|
||||||
import im.zhaojun.common.model.enums.StorageTypeEnum;
|
import im.zhaojun.zfile.model.enums.StorageTypeEnum;
|
||||||
import im.zhaojun.common.service.StorageConfigService;
|
import im.zhaojun.zfile.service.StorageConfigService;
|
||||||
import im.zhaojun.onedrive.china.service.OneDriveChinaService;
|
import im.zhaojun.zfile.service.impl.OneDriveChinaServiceImpl;
|
||||||
import im.zhaojun.onedrive.international.service.OneDriveService;
|
import im.zhaojun.zfile.service.impl.OneDriveServiceImpl;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.context.annotation.Lazy;
|
import org.springframework.context.annotation.Lazy;
|
||||||
@@ -17,7 +17,6 @@ import java.util.Collections;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @author zhaojun
|
* @author zhaojun
|
||||||
* @date 2020/1/18 17:13
|
|
||||||
*/
|
*/
|
||||||
@Configuration
|
@Configuration
|
||||||
public class OneDriveConfig {
|
public class OneDriveConfig {
|
||||||
@@ -27,11 +26,11 @@ public class OneDriveConfig {
|
|||||||
|
|
||||||
@Resource
|
@Resource
|
||||||
@Lazy
|
@Lazy
|
||||||
private OneDriveService oneDriveService;
|
private OneDriveServiceImpl oneDriveServiceImpl;
|
||||||
|
|
||||||
@Resource
|
@Resource
|
||||||
@Lazy
|
@Lazy
|
||||||
private OneDriveChinaService oneDriveChinaService;
|
private OneDriveChinaServiceImpl oneDriveChinaServiceImpl;
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public RestTemplate oneDriveRestTemplate() {
|
public RestTemplate oneDriveRestTemplate() {
|
||||||
@@ -40,9 +39,9 @@ public class OneDriveConfig {
|
|||||||
ClientHttpRequestInterceptor interceptor = (httpRequest, bytes, clientHttpRequestExecution) -> {
|
ClientHttpRequestInterceptor interceptor = (httpRequest, bytes, clientHttpRequestExecution) -> {
|
||||||
String host = httpRequest.getURI().getHost();
|
String host = httpRequest.getURI().getHost();
|
||||||
StorageTypeEnum type;
|
StorageTypeEnum type;
|
||||||
if (oneDriveChinaService.getGraphEndPoint().contains(host)) {
|
if (oneDriveChinaServiceImpl.getGraphEndPoint().contains(host)) {
|
||||||
type = StorageTypeEnum.ONE_DRIVE_CHINA;
|
type = StorageTypeEnum.ONE_DRIVE_CHINA;
|
||||||
} else if (oneDriveService.getGraphEndPoint().contains(host)) {
|
} else if (oneDriveServiceImpl.getGraphEndPoint().contains(host)) {
|
||||||
type = StorageTypeEnum.ONE_DRIVE;
|
type = StorageTypeEnum.ONE_DRIVE;
|
||||||
} else {
|
} else {
|
||||||
return clientHttpRequestExecution.execute(httpRequest, bytes);
|
return clientHttpRequestExecution.execute(httpRequest, bytes);
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
package im.zhaojun.common.config;
|
package im.zhaojun.zfile.config;
|
||||||
|
|
||||||
import im.zhaojun.common.model.enums.StorageTypeEnum;
|
import im.zhaojun.zfile.model.enums.StorageTypeEnum;
|
||||||
import im.zhaojun.common.service.AbstractFileService;
|
import im.zhaojun.zfile.service.base.AbstractBaseFileService;
|
||||||
import org.springframework.beans.BeansException;
|
import org.springframework.beans.BeansException;
|
||||||
import org.springframework.context.ApplicationContext;
|
import org.springframework.context.ApplicationContext;
|
||||||
import org.springframework.context.ApplicationContextAware;
|
import org.springframework.context.ApplicationContextAware;
|
||||||
@@ -15,7 +15,7 @@ import java.util.Map;
|
|||||||
@Component
|
@Component
|
||||||
public class StorageTypeFactory implements ApplicationContextAware {
|
public class StorageTypeFactory implements ApplicationContextAware {
|
||||||
|
|
||||||
private static Map<String, AbstractFileService> storageTypeEnumFileServiceMap;
|
private static Map<String, AbstractBaseFileService> storageTypeEnumFileServiceMap;
|
||||||
|
|
||||||
private static ApplicationContext applicationContext;
|
private static ApplicationContext applicationContext;
|
||||||
|
|
||||||
@@ -27,15 +27,15 @@ public class StorageTypeFactory implements ApplicationContextAware {
|
|||||||
applicationContext = act;
|
applicationContext = act;
|
||||||
|
|
||||||
// 获取 Spring 容器中所有 FileService 类型的类
|
// 获取 Spring 容器中所有 FileService 类型的类
|
||||||
storageTypeEnumFileServiceMap = act.getBeansOfType(AbstractFileService.class);
|
storageTypeEnumFileServiceMap = act.getBeansOfType(AbstractBaseFileService.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取指定存储类型 Service
|
* 获取指定存储类型 Service
|
||||||
*/
|
*/
|
||||||
public static AbstractFileService getStorageTypeService(StorageTypeEnum type) {
|
public static AbstractBaseFileService getStorageTypeService(StorageTypeEnum type) {
|
||||||
AbstractFileService result = null;
|
AbstractBaseFileService result = null;
|
||||||
for (AbstractFileService fileService : storageTypeEnumFileServiceMap.values()) {
|
for (AbstractBaseFileService fileService : storageTypeEnumFileServiceMap.values()) {
|
||||||
if (fileService.getStorageTypeEnum() == type) {
|
if (fileService.getStorageTypeEnum() == type) {
|
||||||
result = fileService;
|
result = fileService;
|
||||||
break;
|
break;
|
||||||
31
src/main/java/im/zhaojun/zfile/config/WebMvcConfig.java
Normal file
31
src/main/java/im/zhaojun/zfile/config/WebMvcConfig.java
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
package im.zhaojun.zfile.config;
|
||||||
|
|
||||||
|
import im.zhaojun.zfile.model.enums.StorageTypeEnumDeSerializerConvert;
|
||||||
|
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
|
||||||
|
import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.format.FormatterRegistry;
|
||||||
|
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author zhaojun
|
||||||
|
*/
|
||||||
|
@Configuration
|
||||||
|
public class WebMvcConfig implements WebMvcConfigurer {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void addFormatters(FormatterRegistry registry) {
|
||||||
|
registry.addConverter(new StorageTypeEnumDeSerializerConvert());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public ServletWebServerFactory webServerFactory() {
|
||||||
|
TomcatServletWebServerFactory webServerFactory = new TomcatServletWebServerFactory();
|
||||||
|
webServerFactory.addConnectorCustomizers(connector -> {
|
||||||
|
connector.setAttribute("relaxedPathChars", "<>[\\]^`{|}");
|
||||||
|
connector.setAttribute("relaxedQueryChars", "<>[\\]^`{|}");
|
||||||
|
});
|
||||||
|
return webServerFactory;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package im.zhaojun.common.config;
|
package im.zhaojun.zfile.config;
|
||||||
|
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
@@ -6,6 +6,7 @@ import org.springframework.http.converter.StringHttpMessageConverter;
|
|||||||
import org.springframework.web.client.RestTemplate;
|
import org.springframework.web.client.RestTemplate;
|
||||||
|
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.util.Collections;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author zhaojun
|
* @author zhaojun
|
||||||
@@ -17,6 +18,7 @@ public class ZFileConfiguration {
|
|||||||
public RestTemplate restTemplate(){
|
public RestTemplate restTemplate(){
|
||||||
RestTemplate restTemplate = new RestTemplate();
|
RestTemplate restTemplate = new RestTemplate();
|
||||||
restTemplate.getMessageConverters().set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8));
|
restTemplate.getMessageConverters().set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8));
|
||||||
|
restTemplate.setInterceptors(Collections.singletonList(new ContentTypeTextToTextJson()));
|
||||||
return restTemplate;
|
return restTemplate;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1,17 +1,22 @@
|
|||||||
package im.zhaojun.common.controller;
|
package im.zhaojun.zfile.controller;
|
||||||
|
|
||||||
import im.zhaojun.common.config.StorageTypeFactory;
|
import cn.hutool.core.date.DateUtil;
|
||||||
import im.zhaojun.common.model.StorageConfig;
|
import cn.hutool.core.util.ZipUtil;
|
||||||
import im.zhaojun.common.model.dto.ResultBean;
|
import im.zhaojun.zfile.config.StorageTypeFactory;
|
||||||
import im.zhaojun.common.model.dto.StorageStrategyDTO;
|
import im.zhaojun.zfile.model.dto.ResultBean;
|
||||||
import im.zhaojun.common.model.dto.SystemConfigDTO;
|
import im.zhaojun.zfile.model.dto.StorageStrategyDTO;
|
||||||
import im.zhaojun.common.model.enums.StorageTypeEnum;
|
import im.zhaojun.zfile.model.dto.SystemConfigDTO;
|
||||||
import im.zhaojun.common.service.AbstractFileService;
|
import im.zhaojun.zfile.model.entity.StorageConfig;
|
||||||
import im.zhaojun.common.service.FileAsyncCacheService;
|
import im.zhaojun.zfile.model.enums.StorageTypeEnum;
|
||||||
import im.zhaojun.common.service.StorageConfigService;
|
import im.zhaojun.zfile.model.support.SystemMonitorInfo;
|
||||||
import im.zhaojun.common.service.SystemConfigService;
|
import im.zhaojun.zfile.service.StorageConfigService;
|
||||||
|
import im.zhaojun.zfile.service.SystemConfigService;
|
||||||
|
import im.zhaojun.zfile.service.base.AbstractBaseFileService;
|
||||||
|
import im.zhaojun.zfile.service.support.FileAsyncCacheService;
|
||||||
|
import im.zhaojun.zfile.util.FileUtil;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
@@ -19,10 +24,9 @@ import org.springframework.web.bind.annotation.RequestParam;
|
|||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
import java.util.ArrayList;
|
import javax.servlet.http.HttpServletResponse;
|
||||||
import java.util.List;
|
import java.io.File;
|
||||||
import java.util.Map;
|
import java.util.*;
|
||||||
import java.util.Objects;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 后台管理
|
* 后台管理
|
||||||
@@ -57,18 +61,20 @@ public class AdminController {
|
|||||||
*/
|
*/
|
||||||
@PostMapping("/config")
|
@PostMapping("/config")
|
||||||
public ResultBean updateConfig(SystemConfigDTO systemConfigDTO) throws Exception {
|
public ResultBean updateConfig(SystemConfigDTO systemConfigDTO) throws Exception {
|
||||||
AbstractFileService currentFileService = systemConfigService.getCurrentFileService();
|
StorageTypeEnum currentStorageStrategy = systemConfigService.getCurrentStorageStrategy();
|
||||||
systemConfigDTO.setId(1);
|
|
||||||
systemConfigService.updateSystemConfig(systemConfigDTO);
|
|
||||||
|
|
||||||
StorageTypeEnum currentStorageStrategy = currentFileService.getStorageTypeEnum();
|
|
||||||
if (!Objects.equals(currentStorageStrategy, systemConfigDTO.getStorageStrategy())) {
|
if (!Objects.equals(currentStorageStrategy, systemConfigDTO.getStorageStrategy())) {
|
||||||
|
if (systemConfigService.getEnableCache()) {
|
||||||
|
return ResultBean.error("不支持缓存开启状态下, 切换存储策略, 请先手动关闭缓存");
|
||||||
|
}
|
||||||
log.info("已将存储策略由 {} 切换为 {}",
|
log.info("已将存储策略由 {} 切换为 {}",
|
||||||
currentStorageStrategy.getDescription(),
|
currentStorageStrategy.getDescription(),
|
||||||
systemConfigDTO.getStorageStrategy().getDescription());
|
systemConfigDTO.getStorageStrategy().getDescription());
|
||||||
refreshStorageStrategy();
|
refreshStorageStrategy();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
systemConfigDTO.setId(1);
|
||||||
|
systemConfigService.updateSystemConfig(systemConfigDTO);
|
||||||
|
|
||||||
return ResultBean.success();
|
return ResultBean.success();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -92,12 +98,15 @@ public class AdminController {
|
|||||||
return ResultBean.success(storageConfigList);
|
return ResultBean.success(storageConfigList);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 返回支持的存储引擎.
|
||||||
|
*/
|
||||||
@GetMapping("/support-strategy")
|
@GetMapping("/support-strategy")
|
||||||
public ResultBean supportStrategy() {
|
public ResultBean supportStrategy() {
|
||||||
List<StorageStrategyDTO> result = new ArrayList<>();
|
List<StorageStrategyDTO> result = new ArrayList<>();
|
||||||
StorageTypeEnum[] values = StorageTypeEnum.values();
|
StorageTypeEnum[] values = StorageTypeEnum.values();
|
||||||
for (StorageTypeEnum value : values) {
|
for (StorageTypeEnum value : values) {
|
||||||
AbstractFileService storageTypeService = StorageTypeFactory.getStorageTypeService(value);
|
AbstractBaseFileService storageTypeService = StorageTypeFactory.getStorageTypeService(value);
|
||||||
result.add(new StorageStrategyDTO(value.getKey(),
|
result.add(new StorageStrategyDTO(value.getKey(),
|
||||||
value.getDescription(),
|
value.getDescription(),
|
||||||
storageTypeService.getIsInitialized()));
|
storageTypeService.getIsInitialized()));
|
||||||
@@ -110,10 +119,9 @@ public class AdminController {
|
|||||||
* @param storageStrategyConfig 保存表单值
|
* @param storageStrategyConfig 保存表单值
|
||||||
* @param storageStrategy 所属策略
|
* @param storageStrategy 所属策略
|
||||||
* @return 操作结果
|
* @return 操作结果
|
||||||
* @throws Exception 表单解析出错异常
|
|
||||||
*/
|
*/
|
||||||
@PostMapping("/storage-strategy")
|
@PostMapping("/storage-strategy")
|
||||||
public ResultBean save(@RequestParam Map<String, String> storageStrategyConfig, StorageTypeEnum storageStrategy) throws Exception {
|
public ResultBean save(@RequestParam Map<String, String> storageStrategyConfig, StorageTypeEnum storageStrategy) {
|
||||||
// 保存设置.
|
// 保存设置.
|
||||||
List<StorageConfig> storageConfigList = storageConfigService.selectStorageConfigByType(storageStrategy);
|
List<StorageConfig> storageConfigList = storageConfigService.selectStorageConfigByType(storageStrategy);
|
||||||
for (StorageConfig storageConfig : storageConfigList) {
|
for (StorageConfig storageConfig : storageConfigList) {
|
||||||
@@ -124,7 +132,7 @@ public class AdminController {
|
|||||||
storageConfigService.updateStorageConfig(storageConfigList);
|
storageConfigService.updateStorageConfig(storageConfigList);
|
||||||
|
|
||||||
// 获取当前修改的存储策略 Service, 尝试调用初始化.
|
// 获取当前修改的存储策略 Service, 尝试调用初始化.
|
||||||
AbstractFileService updateStorageStrategyService = StorageTypeFactory.getStorageTypeService(storageStrategy);
|
AbstractBaseFileService updateStorageStrategyService = StorageTypeFactory.getStorageTypeService(storageStrategy);
|
||||||
updateStorageStrategyService.init();
|
updateStorageStrategyService.init();
|
||||||
|
|
||||||
// 如果修改的为当前启用的缓存, 则重新进行缓存.
|
// 如果修改的为当前启用的缓存, 则重新进行缓存.
|
||||||
@@ -134,7 +142,7 @@ public class AdminController {
|
|||||||
log.debug("检测到更新了当前启用的存储策略 {}, 已清理缓存.", currentStorageStrategy);
|
log.debug("检测到更新了当前启用的存储策略 {}, 已清理缓存.", currentStorageStrategy);
|
||||||
}
|
}
|
||||||
|
|
||||||
AbstractFileService fileService = systemConfigService.getCurrentFileService();
|
AbstractBaseFileService fileService = systemConfigService.getCurrentFileService();
|
||||||
fileService.clearFileCache();
|
fileService.clearFileCache();
|
||||||
fileAsyncCacheService.cacheGlobalFile();
|
fileAsyncCacheService.cacheGlobalFile();
|
||||||
}
|
}
|
||||||
@@ -150,7 +158,7 @@ public class AdminController {
|
|||||||
/**
|
/**
|
||||||
* 更新存储策略
|
* 更新存储策略
|
||||||
*/
|
*/
|
||||||
public void refreshStorageStrategy() throws Exception {
|
public void refreshStorageStrategy() {
|
||||||
StorageTypeEnum storageStrategy = systemConfigService.getCurrentStorageStrategy();
|
StorageTypeEnum storageStrategy = systemConfigService.getCurrentStorageStrategy();
|
||||||
refreshStorageStrategy(storageStrategy);
|
refreshStorageStrategy(storageStrategy);
|
||||||
}
|
}
|
||||||
@@ -158,11 +166,11 @@ public class AdminController {
|
|||||||
/**
|
/**
|
||||||
* 更新存储策略
|
* 更新存储策略
|
||||||
*/
|
*/
|
||||||
public void refreshStorageStrategy(StorageTypeEnum storageStrategy) throws Exception {
|
private void refreshStorageStrategy(StorageTypeEnum storageStrategy) {
|
||||||
if (storageStrategy == null) {
|
if (storageStrategy == null) {
|
||||||
log.info("尚未配置存储策略.");
|
log.info("尚未配置存储策略.");
|
||||||
} else {
|
} else {
|
||||||
AbstractFileService fileService = systemConfigService.getCurrentFileService();
|
AbstractBaseFileService fileService = systemConfigService.getCurrentFileService();
|
||||||
fileService.init();
|
fileService.init();
|
||||||
fileService.clearFileCache();
|
fileService.clearFileCache();
|
||||||
log.info("切换至存储类型: {}", storageStrategy.getDescription());
|
log.info("切换至存储类型: {}", storageStrategy.getDescription());
|
||||||
@@ -170,4 +178,23 @@ public class AdminController {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 系统日志下载
|
||||||
|
*/
|
||||||
|
@GetMapping("/log")
|
||||||
|
public ResponseEntity<Object> downloadLog(HttpServletResponse response) {
|
||||||
|
String userHome = System.getProperty("user.home");
|
||||||
|
File fileZip = ZipUtil.zip(userHome + "/.zfile/logs");
|
||||||
|
String currentDate = DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss");
|
||||||
|
return FileUtil.export(fileZip, "ZFile 诊断日志 - " + currentDate + ".zip");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取系统监控信息
|
||||||
|
*/
|
||||||
|
@GetMapping("monitor")
|
||||||
|
public ResultBean monitor() {
|
||||||
|
return ResultBean.success(new SystemMonitorInfo());
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1,22 +1,21 @@
|
|||||||
package im.zhaojun.common.controller;
|
package im.zhaojun.zfile.controller;
|
||||||
|
|
||||||
import im.zhaojun.common.model.dto.CacheConfigDTO;
|
import im.zhaojun.zfile.cache.ZFileCache;
|
||||||
import im.zhaojun.common.model.dto.ResultBean;
|
import im.zhaojun.zfile.model.dto.CacheConfigDTO;
|
||||||
import im.zhaojun.common.service.AbstractFileService;
|
import im.zhaojun.zfile.model.dto.ResultBean;
|
||||||
import im.zhaojun.common.service.FileAsyncCacheService;
|
import im.zhaojun.zfile.service.SystemConfigService;
|
||||||
import im.zhaojun.common.service.FileCacheService;
|
import im.zhaojun.zfile.service.base.AbstractBaseFileService;
|
||||||
import im.zhaojun.common.service.SystemConfigService;
|
import im.zhaojun.zfile.service.support.FileAsyncCacheService;
|
||||||
|
import im.zhaojun.zfile.service.support.FileCacheService;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
import java.util.Set;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author zhaojun
|
* @author zhaojun
|
||||||
* @date 2020/1/11 14:03
|
|
||||||
*/
|
*/
|
||||||
@RestController
|
@RestController
|
||||||
@RequestMapping("/admin/cache")
|
@RequestMapping("/admin/cache")
|
||||||
@@ -31,51 +30,55 @@ public class CacheController {
|
|||||||
@Resource
|
@Resource
|
||||||
private FileCacheService fileCacheService;
|
private FileCacheService fileCacheService;
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private ZFileCache zFileCache;
|
||||||
|
|
||||||
@PostMapping("/enable")
|
@PostMapping("/enable")
|
||||||
public ResultBean enableCache() throws Exception {
|
public ResultBean enableCache() {
|
||||||
fileCacheService.enableCache();
|
fileCacheService.enableCache();
|
||||||
return ResultBean.success();
|
return ResultBean.success();
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping("/disable")
|
@PostMapping("/disable")
|
||||||
public ResultBean disableCache() throws Exception {
|
public ResultBean disableCache() {
|
||||||
fileCacheService.disableCache();
|
fileCacheService.disableCache();
|
||||||
return ResultBean.success();
|
return ResultBean.success();
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/config")
|
@GetMapping("/config")
|
||||||
public ResultBean cacheConfig() throws Exception {
|
public ResultBean cacheConfig() {
|
||||||
AbstractFileService fileService = systemConfigService.getCurrentFileService();
|
|
||||||
Set<String> cacheKeys = fileService.getCacheKeys();
|
|
||||||
|
|
||||||
CacheConfigDTO cacheConfigDTO = new CacheConfigDTO();
|
CacheConfigDTO cacheConfigDTO = new CacheConfigDTO();
|
||||||
cacheConfigDTO.setEnableCache(systemConfigService.getEnableCache());
|
cacheConfigDTO.setEnableCache(systemConfigService.getEnableCache());
|
||||||
cacheConfigDTO.setCacheFinish(fileAsyncCacheService.isCacheFinish());
|
cacheConfigDTO.setCacheFinish(fileAsyncCacheService.isCacheFinish());
|
||||||
cacheConfigDTO.setCacheKeys(cacheKeys);
|
cacheConfigDTO.setCacheKeys(zFileCache.keySet());
|
||||||
cacheConfigDTO.setCacheDirectoryCount(fileAsyncCacheService.getCacheDirectoryCount());
|
cacheConfigDTO.setCacheDirectoryCount(zFileCache.cacheCount());
|
||||||
cacheConfigDTO.setCacheFileCount(fileAsyncCacheService.getCacheFileCount());
|
cacheConfigDTO.setLastCacheAutoRefreshDate(zFileCache.getLastCacheAutoRefreshDate());
|
||||||
return ResultBean.success(cacheConfigDTO);
|
return ResultBean.success(cacheConfigDTO);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@PostMapping("/refresh")
|
@PostMapping("/refresh")
|
||||||
public ResultBean refreshCache(String key) throws Exception {
|
public ResultBean refreshCache(String key) throws Exception {
|
||||||
AbstractFileService fileService = systemConfigService.getCurrentFileService();
|
AbstractBaseFileService fileService = systemConfigService.getCurrentFileService();
|
||||||
fileService.refreshCache(key);
|
fileService.refreshCache(key);
|
||||||
return ResultBean.success();
|
return ResultBean.success();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
@PostMapping("/clear")
|
@PostMapping("/clear")
|
||||||
public ResultBean clearCache(String key) throws Exception {
|
public ResultBean clearCache(String key) {
|
||||||
AbstractFileService fileService = systemConfigService.getCurrentFileService();
|
AbstractFileService fileService = systemConfigService.getCurrentFileService();
|
||||||
fileService.clearFileCache();
|
fileService.clearFileCache();
|
||||||
return ResultBean.success();
|
return ResultBean.success();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@PostMapping("/all")
|
@PostMapping("/all")
|
||||||
public ResultBean cacheAll() throws Exception {
|
public ResultBean cacheAll() {
|
||||||
AbstractFileService fileService = systemConfigService.getCurrentFileService();
|
AbstractFileService fileService = systemConfigService.getCurrentFileService();
|
||||||
fileService.clearFileCache();
|
fileService.clearFileCache();
|
||||||
fileAsyncCacheService.cacheGlobalFile();
|
fileAsyncCacheService.cacheGlobalFile();
|
||||||
return ResultBean.success();
|
return ResultBean.success();
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
}
|
}
|
||||||
@@ -1,16 +1,15 @@
|
|||||||
package im.zhaojun.common.controller;
|
package im.zhaojun.zfile.controller;
|
||||||
|
|
||||||
import im.zhaojun.common.model.dto.ResultBean;
|
import im.zhaojun.zfile.model.dto.ResultBean;
|
||||||
import im.zhaojun.common.model.enums.StorageTypeEnum;
|
import im.zhaojun.zfile.model.enums.StorageTypeEnum;
|
||||||
import im.zhaojun.common.util.AudioHelper;
|
import im.zhaojun.zfile.util.AudioHelper;
|
||||||
import im.zhaojun.common.util.HttpUtil;
|
import im.zhaojun.zfile.util.HttpUtil;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author zhaojun
|
* @author zhaojun
|
||||||
* @date 2020/1/13 21:40
|
|
||||||
*/
|
*/
|
||||||
@RestController
|
@RestController
|
||||||
@RequestMapping("/common")
|
@RequestMapping("/common")
|
||||||
@@ -1,34 +1,37 @@
|
|||||||
package im.zhaojun.common.controller;
|
package im.zhaojun.zfile.controller;
|
||||||
|
|
||||||
import cn.hutool.core.util.BooleanUtil;
|
import cn.hutool.core.util.BooleanUtil;
|
||||||
import cn.hutool.core.util.URLUtil;
|
import cn.hutool.core.util.URLUtil;
|
||||||
import im.zhaojun.common.annotation.CheckStorageStrategyInit;
|
import im.zhaojun.zfile.model.annotation.CheckStorageStrategyInit;
|
||||||
import im.zhaojun.common.exception.SearchDisableException;
|
import im.zhaojun.zfile.exception.SearchDisableException;
|
||||||
import im.zhaojun.common.model.constant.ZFileConstant;
|
import im.zhaojun.zfile.model.support.FilePageModel;
|
||||||
import im.zhaojun.common.model.dto.FileItemDTO;
|
import im.zhaojun.zfile.model.constant.ZFileConstant;
|
||||||
import im.zhaojun.common.model.dto.ResultBean;
|
import im.zhaojun.zfile.model.dto.FileItemDTO;
|
||||||
import im.zhaojun.common.model.dto.SiteConfigDTO;
|
import im.zhaojun.zfile.model.dto.ResultBean;
|
||||||
import im.zhaojun.common.model.dto.SystemConfigDTO;
|
import im.zhaojun.zfile.model.dto.SiteConfigDTO;
|
||||||
import im.zhaojun.common.service.AbstractFileService;
|
import im.zhaojun.zfile.model.dto.SystemConfigDTO;
|
||||||
import im.zhaojun.common.service.FileAsyncCacheService;
|
import im.zhaojun.zfile.service.base.AbstractBaseFileService;
|
||||||
import im.zhaojun.common.service.SystemConfigService;
|
import im.zhaojun.zfile.service.support.FileAsyncCacheService;
|
||||||
import im.zhaojun.common.service.SystemService;
|
import im.zhaojun.zfile.service.SystemConfigService;
|
||||||
import im.zhaojun.common.util.FileComparator;
|
import im.zhaojun.zfile.service.SystemService;
|
||||||
import im.zhaojun.common.util.HttpUtil;
|
import im.zhaojun.zfile.util.FileComparator;
|
||||||
import im.zhaojun.common.util.StringUtils;
|
import im.zhaojun.zfile.util.HttpUtil;
|
||||||
|
import im.zhaojun.zfile.util.StringUtils;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestParam;
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
import org.springframework.web.client.HttpClientErrorException;
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
import java.util.ArrayList;
|
import java.util.*;
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 前台文件管理
|
* 前台文件管理
|
||||||
* @author zhaojun
|
* @author zhaojun
|
||||||
*/
|
*/
|
||||||
|
@Slf4j
|
||||||
@RequestMapping("/api")
|
@RequestMapping("/api")
|
||||||
@RestController
|
@RestController
|
||||||
public class FileController {
|
public class FileController {
|
||||||
@@ -54,25 +57,42 @@ public class FileController {
|
|||||||
@RequestParam(defaultValue = "asc") String order,
|
@RequestParam(defaultValue = "asc") String order,
|
||||||
@RequestParam(required = false) String password,
|
@RequestParam(required = false) String password,
|
||||||
@RequestParam(defaultValue = "1") Integer page) throws Exception {
|
@RequestParam(defaultValue = "1") Integer page) throws Exception {
|
||||||
AbstractFileService fileService = systemConfigService.getCurrentFileService();
|
AbstractBaseFileService fileService = systemConfigService.getCurrentFileService();
|
||||||
List<FileItemDTO> fileItemList = fileService.fileList(StringUtils.removeDuplicateSeparator("/" + path + "/"));
|
List<FileItemDTO> fileItemList = fileService.fileList(StringUtils.removeDuplicateSeparator("/" + path + "/"));
|
||||||
for (FileItemDTO fileItemDTO : fileItemList) {
|
for (FileItemDTO fileItemDTO : fileItemList) {
|
||||||
if (ZFileConstant.PASSWORD_FILE_NAME.equals(fileItemDTO.getName())
|
if (ZFileConstant.PASSWORD_FILE_NAME.equals(fileItemDTO.getName())) {
|
||||||
&& !HttpUtil.getTextContent(fileItemDTO.getUrl()).equals(password)) {
|
String expectedPasswordContent;
|
||||||
|
try {
|
||||||
|
expectedPasswordContent = HttpUtil.getTextContent(fileItemDTO.getUrl() + '1');
|
||||||
|
} catch (HttpClientErrorException httpClientErrorException) {
|
||||||
|
log.debug("尝试重新获取密码文件缓存中链接后仍失败", httpClientErrorException);
|
||||||
|
try {
|
||||||
|
String fullPath = StringUtils.removeDuplicateSeparator(fileItemDTO.getPath() + "/" + fileItemDTO.getName());
|
||||||
|
FileItemDTO fileItem = fileService.getFileItem(fullPath);
|
||||||
|
expectedPasswordContent = HttpUtil.getTextContent(fileItem.getUrl());
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.debug("尝试重新获取密码文件链接后仍失败, 已暂时取消密码", e);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Objects.equals(expectedPasswordContent, password)) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
if (password != null && !"".equals(password)) {
|
if (password != null && !"".equals(password)) {
|
||||||
return ResultBean.error("密码错误.");
|
return ResultBean.error("密码错误.", ResultBean.INVALID_PASSWORD);
|
||||||
}
|
}
|
||||||
return ResultBean.error("此文件夹需要密码.", ResultBean.REQUIRED_PASSWORD);
|
return ResultBean.error("此文件夹需要密码.", ResultBean.REQUIRED_PASSWORD);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
List<FileItemDTO> sortedPagingData = getSortedPagingData(fileItemList, page);
|
return ResultBean.successData(getSortedPagingData(fileItemList, page));
|
||||||
return ResultBean.successData(sortedPagingData);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取系统配置信息和当前页的标题, 文件头, 文件尾信息
|
* 获取系统配置信息和当前页的标题, 页面文档信息
|
||||||
* @param path 路径
|
* @param path 路径
|
||||||
*/
|
*/
|
||||||
@CheckStorageStrategyInit
|
@CheckStorageStrategyInit
|
||||||
@@ -90,7 +110,7 @@ public class FileController {
|
|||||||
@RequestParam(defaultValue = "name") String sortBy,
|
@RequestParam(defaultValue = "name") String sortBy,
|
||||||
@RequestParam(defaultValue = "asc") String order,
|
@RequestParam(defaultValue = "asc") String order,
|
||||||
@RequestParam(defaultValue = "1") Integer page) {
|
@RequestParam(defaultValue = "1") Integer page) {
|
||||||
AbstractFileService fileService = systemConfigService.getCurrentFileService();
|
AbstractBaseFileService fileService = systemConfigService.getCurrentFileService();
|
||||||
SystemConfigDTO systemConfigDTO = systemConfigService.getSystemConfig();
|
SystemConfigDTO systemConfigDTO = systemConfigService.getSystemConfig();
|
||||||
if (BooleanUtil.isFalse(systemConfigDTO.getSearchEnable())) {
|
if (BooleanUtil.isFalse(systemConfigDTO.getSearchEnable())) {
|
||||||
throw new SearchDisableException("搜索功能未开启");
|
throw new SearchDisableException("搜索功能未开启");
|
||||||
@@ -99,13 +119,12 @@ public class FileController {
|
|||||||
throw new SearchDisableException("搜索功能缓存预热中, 请稍后再试");
|
throw new SearchDisableException("搜索功能缓存预热中, 请稍后再试");
|
||||||
}
|
}
|
||||||
List<FileItemDTO> fileItemList = fileService.search(URLUtil.decode(name));
|
List<FileItemDTO> fileItemList = fileService.search(URLUtil.decode(name));
|
||||||
List<FileItemDTO> sortedPagingData = getSortedPagingData(fileItemList, page);
|
return ResultBean.successData(getSortedPagingData(fileItemList, page));
|
||||||
return ResultBean.successData(sortedPagingData);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 过滤文件列表, 不显示密码, 头部和尾部文件.
|
* 过滤文件列表, 不显示密码, 文档文件.
|
||||||
*/
|
*/
|
||||||
private void filterFileList(List<FileItemDTO> fileItemList) {
|
private void filterFileList(List<FileItemDTO> fileItemList) {
|
||||||
if (fileItemList == null) {
|
if (fileItemList == null) {
|
||||||
@@ -113,26 +132,29 @@ public class FileController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fileItemList.removeIf(fileItem -> ZFileConstant.PASSWORD_FILE_NAME.equals(fileItem.getName())
|
fileItemList.removeIf(fileItem -> ZFileConstant.PASSWORD_FILE_NAME.equals(fileItem.getName())
|
||||||
|| ZFileConstant.HEADER_FILE_NAME.equals(fileItem.getName()));
|
|| ZFileConstant.README_FILE_NAME.equals(fileItem.getName()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private List<FileItemDTO> getSortedPagingData(List<FileItemDTO> fileItemList, Integer page) {
|
private FilePageModel getSortedPagingData(List<FileItemDTO> fileItemList, Integer page) {
|
||||||
// 排序, 先按照文件类型比较, 文件夹在前, 文件在后, 然后根据 sortBy 字段排序, 默认为升序;
|
ArrayList<FileItemDTO> copy = new ArrayList<>(Arrays.asList(new FileItemDTO[fileItemList.size()]));
|
||||||
fileItemList.sort(new FileComparator());
|
Collections.copy(copy, fileItemList);
|
||||||
filterFileList(fileItemList);
|
|
||||||
|
|
||||||
int total = fileItemList.size();
|
// 排序, 先按照文件类型比较, 文件夹在前, 文件在后, 然后根据 sortBy 字段排序, 默认为升序;
|
||||||
|
copy.sort(new FileComparator());
|
||||||
|
filterFileList(copy);
|
||||||
|
|
||||||
|
int total = copy.size();
|
||||||
int totalPage = (total + PAGE_SIZE - 1) / PAGE_SIZE;
|
int totalPage = (total + PAGE_SIZE - 1) / PAGE_SIZE;
|
||||||
|
|
||||||
if (page > totalPage) {
|
if (page > totalPage) {
|
||||||
return new ArrayList<>();
|
return new FilePageModel(total, totalPage, Collections.emptyList());
|
||||||
}
|
}
|
||||||
|
|
||||||
int start = (page - 1) * PAGE_SIZE;
|
int start = (page - 1) * PAGE_SIZE;
|
||||||
int end = page * PAGE_SIZE;
|
int end = page * PAGE_SIZE;
|
||||||
end = Math.min(end, total);
|
end = Math.min(end, total);
|
||||||
return new ArrayList<>(fileItemList.subList(start, end));
|
return new FilePageModel(total, totalPage, copy.subList(start, end));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -144,7 +166,7 @@ public class FileController {
|
|||||||
@CheckStorageStrategyInit
|
@CheckStorageStrategyInit
|
||||||
@GetMapping("/directlink")
|
@GetMapping("/directlink")
|
||||||
public ResultBean directlink(String path) {
|
public ResultBean directlink(String path) {
|
||||||
AbstractFileService fileService = systemConfigService.getCurrentFileService();
|
AbstractBaseFileService fileService = systemConfigService.getCurrentFileService();
|
||||||
return ResultBean.successData(fileService.getFileItem(path));
|
return ResultBean.successData(fileService.getFileItem(path));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,13 +1,13 @@
|
|||||||
package im.zhaojun.common.controller;
|
package im.zhaojun.zfile.controller;
|
||||||
|
|
||||||
import cn.hutool.crypto.SecureUtil;
|
import cn.hutool.crypto.SecureUtil;
|
||||||
import im.zhaojun.common.model.StorageConfig;
|
import im.zhaojun.zfile.model.entity.StorageConfig;
|
||||||
import im.zhaojun.common.model.dto.InstallModelDTO;
|
import im.zhaojun.zfile.model.dto.InstallModelDTO;
|
||||||
import im.zhaojun.common.model.dto.ResultBean;
|
import im.zhaojun.zfile.model.dto.ResultBean;
|
||||||
import im.zhaojun.common.model.dto.SystemConfigDTO;
|
import im.zhaojun.zfile.model.dto.SystemConfigDTO;
|
||||||
import im.zhaojun.common.model.enums.StorageTypeEnum;
|
import im.zhaojun.zfile.model.enums.StorageTypeEnum;
|
||||||
import im.zhaojun.common.service.StorageConfigService;
|
import im.zhaojun.zfile.service.StorageConfigService;
|
||||||
import im.zhaojun.common.service.SystemConfigService;
|
import im.zhaojun.zfile.service.SystemConfigService;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
@@ -0,0 +1,37 @@
|
|||||||
|
package im.zhaojun.zfile.controller;
|
||||||
|
|
||||||
|
import im.zhaojun.zfile.util.FileUtil;
|
||||||
|
import im.zhaojun.zfile.util.StringUtils;
|
||||||
|
import im.zhaojun.zfile.service.impl.LocalServiceImpl;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.stereotype.Controller;
|
||||||
|
import org.springframework.util.AntPathMatcher;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.ResponseBody;
|
||||||
|
import org.springframework.web.servlet.HandlerMapping;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
import java.io.File;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author zhaojun
|
||||||
|
*/
|
||||||
|
@Controller
|
||||||
|
public class LocalController {
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private LocalServiceImpl localServiceImpl;
|
||||||
|
|
||||||
|
@GetMapping("/file/**")
|
||||||
|
@ResponseBody
|
||||||
|
public ResponseEntity<Object> downAttachment(final HttpServletRequest request) {
|
||||||
|
String path = (String) request.getAttribute(
|
||||||
|
HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE);
|
||||||
|
String bestMatchPattern = (String) request.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE);
|
||||||
|
AntPathMatcher apm = new AntPathMatcher();
|
||||||
|
String filePath = apm.extractPathWithinPattern(bestMatchPattern, path);
|
||||||
|
|
||||||
|
return FileUtil.export(new File(StringUtils.concatPath(localServiceImpl.getFilePath(), filePath)));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,8 +1,8 @@
|
|||||||
package im.zhaojun.onedrive.common.controller;
|
package im.zhaojun.zfile.controller;
|
||||||
|
|
||||||
import im.zhaojun.onedrive.china.service.OneDriveChinaService;
|
import im.zhaojun.zfile.service.impl.OneDriveChinaServiceImpl;
|
||||||
import im.zhaojun.onedrive.common.model.OneDriveToken;
|
import im.zhaojun.zfile.model.support.OneDriveToken;
|
||||||
import im.zhaojun.onedrive.international.service.OneDriveService;
|
import im.zhaojun.zfile.service.impl.OneDriveServiceImpl;
|
||||||
import org.springframework.stereotype.Controller;
|
import org.springframework.stereotype.Controller;
|
||||||
import org.springframework.ui.Model;
|
import org.springframework.ui.Model;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
@@ -14,18 +14,18 @@ import javax.annotation.Resource;
|
|||||||
* @author zhaojun
|
* @author zhaojun
|
||||||
*/
|
*/
|
||||||
@Controller
|
@Controller
|
||||||
@RequestMapping("/onedirve")
|
@RequestMapping("/onedrive")
|
||||||
public class OneDriveController {
|
public class OneDriveController {
|
||||||
|
|
||||||
@Resource
|
@Resource
|
||||||
private OneDriveService oneDriveService;
|
private OneDriveServiceImpl oneDriveServiceImpl;
|
||||||
|
|
||||||
@Resource
|
@Resource
|
||||||
private OneDriveChinaService oneDriveChinaService;
|
private OneDriveChinaServiceImpl oneDriveChinaServiceImpl;
|
||||||
|
|
||||||
@GetMapping("/callback")
|
@GetMapping("/callback")
|
||||||
public String onedriveCallback(String code, Model model) {
|
public String onedriveCallback(String code, Model model) {
|
||||||
OneDriveToken oneDriveToken = oneDriveService.getToken(code);
|
OneDriveToken oneDriveToken = oneDriveServiceImpl.getToken(code);
|
||||||
model.addAttribute("accessToken", oneDriveToken.getAccessToken());
|
model.addAttribute("accessToken", oneDriveToken.getAccessToken());
|
||||||
model.addAttribute("refreshToken", oneDriveToken.getRefreshToken());
|
model.addAttribute("refreshToken", oneDriveToken.getRefreshToken());
|
||||||
return "callback";
|
return "callback";
|
||||||
@@ -34,7 +34,7 @@ public class OneDriveController {
|
|||||||
|
|
||||||
@GetMapping("/china-callback")
|
@GetMapping("/china-callback")
|
||||||
public String onedriveChinaCallback(String code, Model model) {
|
public String onedriveChinaCallback(String code, Model model) {
|
||||||
OneDriveToken oneDriveToken = oneDriveChinaService.getToken(code);
|
OneDriveToken oneDriveToken = oneDriveChinaServiceImpl.getToken(code);
|
||||||
model.addAttribute("accessToken", oneDriveToken.getAccessToken());
|
model.addAttribute("accessToken", oneDriveToken.getAccessToken());
|
||||||
model.addAttribute("refreshToken", oneDriveToken.getRefreshToken());
|
model.addAttribute("refreshToken", oneDriveToken.getRefreshToken());
|
||||||
return "callback";
|
return "callback";
|
||||||
@@ -0,0 +1,63 @@
|
|||||||
|
package im.zhaojun.zfile.controller;
|
||||||
|
|
||||||
|
import cn.hutool.core.util.URLUtil;
|
||||||
|
import im.zhaojun.zfile.model.constant.ZFileConstant;
|
||||||
|
import im.zhaojun.zfile.model.dto.FileItemDTO;
|
||||||
|
import im.zhaojun.zfile.model.enums.FileTypeEnum;
|
||||||
|
import im.zhaojun.zfile.service.SystemConfigService;
|
||||||
|
import im.zhaojun.zfile.service.base.AbstractBaseFileService;
|
||||||
|
import org.springframework.stereotype.Controller;
|
||||||
|
import org.springframework.util.AntPathMatcher;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.servlet.HandlerMapping;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Zhao Jun
|
||||||
|
* 2020/2/9 11:17
|
||||||
|
*/
|
||||||
|
@Controller
|
||||||
|
public class PageController {
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private SystemConfigService systemConfigService;
|
||||||
|
|
||||||
|
@GetMapping("/directlink/**")
|
||||||
|
public String directlink(final HttpServletRequest request) {
|
||||||
|
String path = (String) request.getAttribute(
|
||||||
|
HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE);
|
||||||
|
String bestMatchPattern = (String) request.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE);
|
||||||
|
AntPathMatcher apm = new AntPathMatcher();
|
||||||
|
String filePath = apm.extractPathWithinPattern(bestMatchPattern, path);
|
||||||
|
|
||||||
|
if (filePath.length() > 0 && filePath.charAt(0) != ZFileConstant.PATH_SEPARATOR_CHAR) {
|
||||||
|
filePath = "/" + filePath;
|
||||||
|
}
|
||||||
|
|
||||||
|
AbstractBaseFileService fileService = systemConfigService.getCurrentFileService();
|
||||||
|
FileItemDTO fileItem = fileService.getFileItem(filePath);
|
||||||
|
|
||||||
|
String url = fileItem.getUrl();
|
||||||
|
|
||||||
|
int queryIndex = url.indexOf('?');
|
||||||
|
|
||||||
|
if (queryIndex != -1) {
|
||||||
|
String origin = url.substring(0, queryIndex);
|
||||||
|
String queryString = url.substring(queryIndex + 1);
|
||||||
|
|
||||||
|
url = URLUtil.encode(origin) + "?" + URLUtil.encode(queryString);
|
||||||
|
} else {
|
||||||
|
url = URLUtil.encode(url);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
if (Objects.equals(fileItem.getType(), FileTypeEnum.FOLDER)) {
|
||||||
|
return "redirect:" + fileItem.getUrl();
|
||||||
|
} else {
|
||||||
|
return "redirect:" + url;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
51
src/main/java/im/zhaojun/zfile/core/FileListCacheAop.java
Normal file
51
src/main/java/im/zhaojun/zfile/core/FileListCacheAop.java
Normal file
@@ -0,0 +1,51 @@
|
|||||||
|
package im.zhaojun.zfile.core;
|
||||||
|
|
||||||
|
import im.zhaojun.zfile.cache.ZFileCache;
|
||||||
|
import im.zhaojun.zfile.model.dto.FileItemDTO;
|
||||||
|
import im.zhaojun.zfile.service.SystemConfigService;
|
||||||
|
import org.aspectj.lang.ProceedingJoinPoint;
|
||||||
|
import org.aspectj.lang.annotation.Around;
|
||||||
|
import org.aspectj.lang.annotation.Aspect;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
import org.springframework.util.CollectionUtils;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author zhaojun
|
||||||
|
* 缓存切面类, 用于访问文件夹时, 缓存文件列表内容.
|
||||||
|
*/
|
||||||
|
@Aspect
|
||||||
|
@Component
|
||||||
|
public class FileListCacheAop {
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private ZFileCache zFileCache;
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private SystemConfigService systemConfigService;
|
||||||
|
|
||||||
|
@Around(value = "execution(public * im.zhaojun.zfile.service.base.AbstractBaseFileService.fileList(..))")
|
||||||
|
public Object around(ProceedingJoinPoint point) throws Throwable {
|
||||||
|
List<FileItemDTO> result;
|
||||||
|
|
||||||
|
Object[] args = point.getArgs();
|
||||||
|
String path = String.valueOf(args[0]);
|
||||||
|
|
||||||
|
boolean enableCache = systemConfigService.getEnableCache();
|
||||||
|
|
||||||
|
if (enableCache) {
|
||||||
|
List<FileItemDTO> cacheFileList = zFileCache.get(path);
|
||||||
|
if (CollectionUtils.isEmpty(cacheFileList)) {
|
||||||
|
result = (List<FileItemDTO>) point.proceed();
|
||||||
|
zFileCache.put(path, result);
|
||||||
|
} else {
|
||||||
|
result = cacheFileList;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
result = (List<FileItemDTO>) point.proceed();
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,9 +1,9 @@
|
|||||||
package im.zhaojun.common.aspect;
|
package im.zhaojun.zfile.core;
|
||||||
|
|
||||||
import im.zhaojun.common.exception.StorageStrategyUninitializedException;
|
import im.zhaojun.zfile.exception.StorageStrategyUninitializedException;
|
||||||
import im.zhaojun.common.service.AbstractFileService;
|
import im.zhaojun.zfile.service.base.AbstractBaseFileService;
|
||||||
import im.zhaojun.common.service.SystemConfigService;
|
import im.zhaojun.zfile.service.SystemConfigService;
|
||||||
import im.zhaojun.common.util.SpringContextHolder;
|
import im.zhaojun.zfile.util.SpringContextHolder;
|
||||||
import org.aspectj.lang.annotation.Aspect;
|
import org.aspectj.lang.annotation.Aspect;
|
||||||
import org.aspectj.lang.annotation.Before;
|
import org.aspectj.lang.annotation.Before;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
@@ -13,12 +13,12 @@ import org.springframework.stereotype.Component;
|
|||||||
*/
|
*/
|
||||||
@Aspect
|
@Aspect
|
||||||
@Component
|
@Component
|
||||||
public class StorageStrategyInitCheckAspect {
|
public class StorageStrategyInitCheckAop {
|
||||||
|
|
||||||
@Before("@annotation(im.zhaojun.common.annotation.CheckStorageStrategyInit)")
|
@Before("@annotation(im.zhaojun.zfile.model.annotation.CheckStorageStrategyInit)")
|
||||||
public void logStart() {
|
public void logStart() {
|
||||||
SystemConfigService systemConfigService = SpringContextHolder.getBean(SystemConfigService.class);
|
SystemConfigService systemConfigService = SpringContextHolder.getBean(SystemConfigService.class);
|
||||||
AbstractFileService currentFileService = systemConfigService.getCurrentFileService();
|
AbstractBaseFileService currentFileService = systemConfigService.getCurrentFileService();
|
||||||
if (currentFileService == null) {
|
if (currentFileService == null) {
|
||||||
throw new StorageStrategyUninitializedException("存储策略尚未初始化, 请联系管理员!");
|
throw new StorageStrategyUninitializedException("存储策略尚未初始化, 请联系管理员!");
|
||||||
}
|
}
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
package im.zhaojun.common.exception;
|
package im.zhaojun.zfile.exception;
|
||||||
|
|
||||||
import im.zhaojun.common.model.dto.ResultBean;
|
import im.zhaojun.zfile.model.dto.ResultBean;
|
||||||
import org.apache.catalina.connector.ClientAbortException;
|
import org.apache.catalina.connector.ClientAbortException;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package im.zhaojun.common.exception;
|
package im.zhaojun.zfile.exception;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 对象存储初始化异常
|
* 对象存储初始化异常
|
||||||
@@ -1,8 +1,7 @@
|
|||||||
package im.zhaojun.common.exception;
|
package im.zhaojun.zfile.exception;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author zhaojun
|
* @author zhaojun
|
||||||
* @date 2020/1/20 22:15
|
|
||||||
*/
|
*/
|
||||||
public class NotExistFileException extends RuntimeException {
|
public class NotExistFileException extends RuntimeException {
|
||||||
|
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package im.zhaojun.common.exception;
|
package im.zhaojun.zfile.exception;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author zhaojun
|
* @author zhaojun
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package im.zhaojun.common.exception;
|
package im.zhaojun.zfile.exception;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 存储策略未初始化异常
|
* 存储策略未初始化异常
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package im.zhaojun.common.exception;
|
package im.zhaojun.zfile.exception;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 未知的存储类型异常
|
* 未知的存储类型异常
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package im.zhaojun.common.security;
|
package im.zhaojun.zfile.filter;
|
||||||
|
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package im.zhaojun.common.annotation;
|
package im.zhaojun.zfile.model.annotation;
|
||||||
|
|
||||||
import java.lang.annotation.ElementType;
|
import java.lang.annotation.ElementType;
|
||||||
import java.lang.annotation.Retention;
|
import java.lang.annotation.Retention;
|
||||||
@@ -1,8 +1,7 @@
|
|||||||
package im.zhaojun.common.model.constant;
|
package im.zhaojun.zfile.model.constant;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author zhaojun
|
* @author zhaojun
|
||||||
* @date 2019/12/28 18:47
|
|
||||||
*/
|
*/
|
||||||
public class StorageConfigConstant {
|
public class StorageConfigConstant {
|
||||||
|
|
||||||
@@ -34,4 +33,8 @@ public class StorageConfigConstant {
|
|||||||
|
|
||||||
public static final String REFRESH_TOKEN_KEY = "refreshToken";
|
public static final String REFRESH_TOKEN_KEY = "refreshToken";
|
||||||
|
|
||||||
|
public static final String PATH_STYLE = "pathStyle";
|
||||||
|
|
||||||
|
public static final String IS_PRIVATE = "isPrivate";
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package im.zhaojun.common.model.constant;
|
package im.zhaojun.zfile.model.constant;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author zhaojun
|
* @author zhaojun
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package im.zhaojun.common.model.constant;
|
package im.zhaojun.zfile.model.constant;
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
@@ -14,10 +14,14 @@ public class ZFileConstant {
|
|||||||
|
|
||||||
public static final String AUDIO_TMP_PATH = "/.zfile/tmp/audio/";
|
public static final String AUDIO_TMP_PATH = "/.zfile/tmp/audio/";
|
||||||
|
|
||||||
|
public static final Character PATH_SEPARATOR_CHAR = '/';
|
||||||
|
|
||||||
|
public static final String PATH_SEPARATOR = "/";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 页面头部文件
|
* 页面文档文件
|
||||||
*/
|
*/
|
||||||
public static String HEADER_FILE_NAME = "header.md";
|
public static String README_FILE_NAME = "readme.md";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 密码文件
|
* 密码文件
|
||||||
@@ -25,8 +29,8 @@ public class ZFileConstant {
|
|||||||
public static String PASSWORD_FILE_NAME = "password.txt";
|
public static String PASSWORD_FILE_NAME = "password.txt";
|
||||||
|
|
||||||
@Autowired(required = false)
|
@Autowired(required = false)
|
||||||
public void setHeaderFileName(@Value("${zfile.constant.header}") String headerFileName) {
|
public void setHeaderFileName(@Value("${zfile.constant.readme}") String headerFileName) {
|
||||||
ZFileConstant.HEADER_FILE_NAME = headerFileName;
|
ZFileConstant.README_FILE_NAME = headerFileName;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Autowired(required = false)
|
@Autowired(required = false)
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package im.zhaojun.common.model.dto;
|
package im.zhaojun.zfile.model.dto;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author zhaojun
|
* @author zhaojun
|
||||||
@@ -1,12 +1,12 @@
|
|||||||
package im.zhaojun.common.model.dto;
|
package im.zhaojun.zfile.model.dto;
|
||||||
|
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
|
||||||
|
import java.util.Date;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author zhaojun
|
* @author zhaojun
|
||||||
* @date 2020/1/3 12:39
|
|
||||||
*/
|
*/
|
||||||
@Data
|
@Data
|
||||||
public class CacheConfigDTO {
|
public class CacheConfigDTO {
|
||||||
@@ -15,4 +15,5 @@ public class CacheConfigDTO {
|
|||||||
private Set<String> cacheKeys;
|
private Set<String> cacheKeys;
|
||||||
private Integer cacheDirectoryCount;
|
private Integer cacheDirectoryCount;
|
||||||
private Integer cacheFileCount;
|
private Integer cacheFileCount;
|
||||||
|
private Date lastCacheAutoRefreshDate;
|
||||||
}
|
}
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
package im.zhaojun.common.model.dto;
|
package im.zhaojun.zfile.model.dto;
|
||||||
|
|
||||||
import im.zhaojun.common.model.enums.FileTypeEnum;
|
import im.zhaojun.zfile.model.enums.FileTypeEnum;
|
||||||
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
package im.zhaojun.common.model.dto;
|
package im.zhaojun.zfile.model.dto;
|
||||||
|
|
||||||
import im.zhaojun.common.model.enums.StorageTypeEnum;
|
import im.zhaojun.zfile.model.enums.StorageTypeEnum;
|
||||||
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package im.zhaojun.common.model.dto;
|
package im.zhaojun.zfile.model.dto;
|
||||||
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
|
|
||||||
@@ -15,6 +15,8 @@ public class ResultBean implements Serializable {
|
|||||||
|
|
||||||
public static final int REQUIRED_PASSWORD = -2;
|
public static final int REQUIRED_PASSWORD = -2;
|
||||||
|
|
||||||
|
public static final int INVALID_PASSWORD = -3;
|
||||||
|
|
||||||
private String msg = "操作成功";
|
private String msg = "操作成功";
|
||||||
|
|
||||||
private int code = SUCCESS;
|
private int code = SUCCESS;
|
||||||
@@ -43,6 +45,10 @@ public class ResultBean implements Serializable {
|
|||||||
return success("操作成功", data);
|
return success("操作成功", data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static ResultBean successPage(Object data, Long total) {
|
||||||
|
return success("操作成功", data);
|
||||||
|
}
|
||||||
|
|
||||||
public static ResultBean success(Object data) {
|
public static ResultBean success(Object data) {
|
||||||
return success("操作成功", data);
|
return success("操作成功", data);
|
||||||
}
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package im.zhaojun.common.model.dto;
|
package im.zhaojun.zfile.model.dto;
|
||||||
|
|
||||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
@@ -15,7 +15,7 @@ public class SiteConfigDTO implements Serializable {
|
|||||||
|
|
||||||
private static final long serialVersionUID = 8811196207046121740L;
|
private static final long serialVersionUID = 8811196207046121740L;
|
||||||
|
|
||||||
private String header;
|
private String readme;
|
||||||
|
|
||||||
@JsonProperty("viewConfig")
|
@JsonProperty("viewConfig")
|
||||||
private SystemConfigDTO systemConfigDTO;
|
private SystemConfigDTO systemConfigDTO;
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
package im.zhaojun.common.model.dto;
|
package im.zhaojun.zfile.model.dto;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import lombok.NoArgsConstructor;
|
import lombok.NoArgsConstructor;
|
||||||
@@ -16,6 +17,7 @@ public class StorageStrategyDTO {
|
|||||||
|
|
||||||
private String description;
|
private String description;
|
||||||
|
|
||||||
private boolean available;
|
@JsonProperty(defaultValue = "false")
|
||||||
|
private Boolean available;
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1,9 +1,9 @@
|
|||||||
package im.zhaojun.common.model.dto;
|
package im.zhaojun.zfile.model.dto;
|
||||||
|
|
||||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||||
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
|
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
|
||||||
import im.zhaojun.common.model.enums.StorageTypeEnum;
|
import im.zhaojun.zfile.model.enums.StorageTypeEnum;
|
||||||
import im.zhaojun.common.model.enums.StorageTypeEnumSerializerConvert;
|
import im.zhaojun.zfile.model.enums.StorageTypeEnumSerializerConvert;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import lombok.ToString;
|
import lombok.ToString;
|
||||||
|
|
||||||
@@ -44,4 +44,15 @@ public class SystemConfigDTO {
|
|||||||
|
|
||||||
private String customCss;
|
private String customCss;
|
||||||
|
|
||||||
|
private String tableSize;
|
||||||
|
|
||||||
|
private Boolean showOperator;
|
||||||
|
|
||||||
|
private Boolean showDocument;
|
||||||
|
|
||||||
|
private String announcement;
|
||||||
|
|
||||||
|
private Boolean showAnnouncement;
|
||||||
|
|
||||||
|
private String layout;
|
||||||
}
|
}
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
package im.zhaojun.common.model;
|
package im.zhaojun.zfile.model.entity;
|
||||||
|
|
||||||
import im.zhaojun.common.model.enums.StorageTypeEnum;
|
import im.zhaojun.zfile.model.enums.StorageTypeEnum;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
|
||||||
import javax.persistence.Column;
|
import javax.persistence.Column;
|
||||||
@@ -27,7 +27,7 @@ public class StorageConfig {
|
|||||||
|
|
||||||
private String title;
|
private String title;
|
||||||
|
|
||||||
@Column(length = 2048)
|
@Column(length = 4000)
|
||||||
private String value;
|
private String value;
|
||||||
|
|
||||||
public Integer getId() {
|
public Integer getId() {
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package im.zhaojun.common.model;
|
package im.zhaojun.zfile.model.entity;
|
||||||
|
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package im.zhaojun.common.model.enums;
|
package im.zhaojun.zfile.model.enums;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author zhaojun
|
* @author zhaojun
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package im.zhaojun.common.model.enums;
|
package im.zhaojun.zfile.model.enums;
|
||||||
|
|
||||||
import com.fasterxml.jackson.annotation.JsonFormat;
|
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||||
|
|
||||||
@@ -14,17 +14,18 @@ public enum StorageTypeEnum {
|
|||||||
/**
|
/**
|
||||||
* 当前系统支持的所有存储策略
|
* 当前系统支持的所有存储策略
|
||||||
*/
|
*/
|
||||||
ALIYUN("aliyun", "阿里云 OSS"),
|
|
||||||
FTP("ftp", "FTP"),
|
|
||||||
HUAWEI("huawei", "华为云 OBS"),
|
|
||||||
LOCAL("local", "本地存储"),
|
LOCAL("local", "本地存储"),
|
||||||
|
ALIYUN("aliyun", "阿里云 OSS"),
|
||||||
|
TENCENT("tencent", "腾讯云 COS"),
|
||||||
|
UPYUN("upyun", "又拍云 USS"),
|
||||||
|
FTP("ftp", "FTP"),
|
||||||
|
UFILE("ufile", "UFile"),
|
||||||
|
HUAWEI("huawei", "华为云 OBS"),
|
||||||
MINIO("minio", "MINIO"),
|
MINIO("minio", "MINIO"),
|
||||||
|
S3("s3", "S3通用协议"),
|
||||||
ONE_DRIVE("onedrive", "OneDrive"),
|
ONE_DRIVE("onedrive", "OneDrive"),
|
||||||
ONE_DRIVE_CHINA("onedrive-china", "OneDrive 世纪互联"),
|
ONE_DRIVE_CHINA("onedrive-china", "OneDrive 世纪互联"),
|
||||||
QINIU("qiniu", "七牛云 KODO"),
|
QINIU("qiniu", "七牛云 KODO");
|
||||||
TENCENT("tencent", "腾讯云 COS"),
|
|
||||||
UPYUN("upyun", "又拍云 USS");
|
|
||||||
|
|
||||||
|
|
||||||
private String key;
|
private String key;
|
||||||
private String description;
|
private String description;
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package im.zhaojun.common.model.enums;
|
package im.zhaojun.zfile.model.enums;
|
||||||
|
|
||||||
import javax.persistence.AttributeConverter;
|
import javax.persistence.AttributeConverter;
|
||||||
import javax.persistence.Converter;
|
import javax.persistence.Converter;
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package im.zhaojun.common.model.enums;
|
package im.zhaojun.zfile.model.enums;
|
||||||
|
|
||||||
import org.springframework.core.convert.converter.Converter;
|
import org.springframework.core.convert.converter.Converter;
|
||||||
import org.springframework.lang.NonNull;
|
import org.springframework.lang.NonNull;
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package im.zhaojun.common.model.enums;
|
package im.zhaojun.zfile.model.enums;
|
||||||
|
|
||||||
import com.fasterxml.jackson.core.JsonGenerator;
|
import com.fasterxml.jackson.core.JsonGenerator;
|
||||||
import com.fasterxml.jackson.databind.JsonSerializer;
|
import com.fasterxml.jackson.databind.JsonSerializer;
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
package im.zhaojun.zfile.model.support;
|
||||||
|
|
||||||
|
import im.zhaojun.zfile.model.dto.FileItemDTO;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author zhaojun
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
@AllArgsConstructor
|
||||||
|
public class FilePageModel {
|
||||||
|
|
||||||
|
private int total;
|
||||||
|
|
||||||
|
private int totalPage;
|
||||||
|
|
||||||
|
private List<FileItemDTO> fileList;
|
||||||
|
|
||||||
|
}
|
||||||
39
src/main/java/im/zhaojun/zfile/model/support/Jvm.java
Normal file
39
src/main/java/im/zhaojun/zfile/model/support/Jvm.java
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
package im.zhaojun.zfile.model.support;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author zhaojun
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
public class Jvm {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 当前 JVM 占用的内存总数 (M)
|
||||||
|
*/
|
||||||
|
private double total;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* JVM 最大可用内存总数 (M)
|
||||||
|
*/
|
||||||
|
private double max;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* JVM 空闲内存 (M)
|
||||||
|
*/
|
||||||
|
private double free;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* JDK 版本
|
||||||
|
*/
|
||||||
|
private String version;
|
||||||
|
|
||||||
|
public Jvm() {
|
||||||
|
Runtime runtime = Runtime.getRuntime();
|
||||||
|
this.total = runtime.totalMemory();
|
||||||
|
this.free = runtime.freeMemory();
|
||||||
|
this.max = runtime.maxMemory();
|
||||||
|
this.version = System.getProperty("java.version");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
41
src/main/java/im/zhaojun/zfile/model/support/Mem.java
Normal file
41
src/main/java/im/zhaojun/zfile/model/support/Mem.java
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
package im.zhaojun.zfile.model.support;
|
||||||
|
|
||||||
|
import com.sun.management.OperatingSystemMXBean;
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
import java.lang.management.ManagementFactory;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author zhaojun
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
public class Mem {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 内存总量
|
||||||
|
*/
|
||||||
|
private double total;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 已用内存
|
||||||
|
*/
|
||||||
|
private double used;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 剩余内存
|
||||||
|
*/
|
||||||
|
private double free;
|
||||||
|
|
||||||
|
public Mem() {
|
||||||
|
OperatingSystemMXBean osb = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
|
||||||
|
// 总的物理内存+虚拟内存
|
||||||
|
long totalVirtualMemory = osb.getTotalSwapSpaceSize();
|
||||||
|
// 剩余的物理内存
|
||||||
|
long freePhysicalMemorySize = osb.getFreePhysicalMemorySize();
|
||||||
|
this.total = totalVirtualMemory;
|
||||||
|
this.free = freePhysicalMemorySize;
|
||||||
|
this.used = totalVirtualMemory - freePhysicalMemorySize;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,11 +1,10 @@
|
|||||||
package im.zhaojun.onedrive.common.model;
|
package im.zhaojun.zfile.model.support;
|
||||||
|
|
||||||
import com.alibaba.fastjson.annotation.JSONField;
|
import com.alibaba.fastjson.annotation.JSONField;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author zhaojun
|
* @author zhaojun
|
||||||
* @date 2020/1/18 17:28
|
|
||||||
*/
|
*/
|
||||||
@Data
|
@Data
|
||||||
public class OneDriveToken {
|
public class OneDriveToken {
|
||||||
58
src/main/java/im/zhaojun/zfile/model/support/Sys.java
Normal file
58
src/main/java/im/zhaojun/zfile/model/support/Sys.java
Normal file
@@ -0,0 +1,58 @@
|
|||||||
|
package im.zhaojun.zfile.model.support;
|
||||||
|
|
||||||
|
import cn.hutool.core.date.BetweenFormater;
|
||||||
|
import cn.hutool.core.date.DateUtil;
|
||||||
|
import lombok.Data;
|
||||||
|
import org.springframework.core.io.ClassPathResource;
|
||||||
|
import org.springframework.core.io.Resource;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.lang.management.ManagementFactory;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author zhaojun
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
public class Sys {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 项目路径
|
||||||
|
*/
|
||||||
|
private String projectDir;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 操作系统
|
||||||
|
*/
|
||||||
|
private String osName;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 系统架构
|
||||||
|
*/
|
||||||
|
private String osArch;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 系统版本
|
||||||
|
*/
|
||||||
|
private String osVersion;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 启动时间
|
||||||
|
*/
|
||||||
|
private String upTime;
|
||||||
|
|
||||||
|
public Sys() {
|
||||||
|
this.osName = System.getProperty("os.name");
|
||||||
|
this.osArch = System.getProperty("os.arch");
|
||||||
|
this.osVersion = System.getProperty("os.version");
|
||||||
|
Resource resource = new ClassPathResource("");
|
||||||
|
try {
|
||||||
|
this.projectDir = resource.getFile().getAbsolutePath();
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
long uptime = ManagementFactory.getRuntimeMXBean().getUptime();
|
||||||
|
this.upTime = DateUtil.formatBetween(uptime, BetweenFormater.Level.SECOND);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,37 @@
|
|||||||
|
package im.zhaojun.zfile.model.support;
|
||||||
|
|
||||||
|
import im.zhaojun.zfile.model.support.Jvm;
|
||||||
|
import im.zhaojun.zfile.model.support.Mem;
|
||||||
|
import im.zhaojun.zfile.model.support.Sys;
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author zhaojun
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
public class SystemMonitorInfo implements Serializable {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 服务器基本信息
|
||||||
|
*/
|
||||||
|
private Sys sys;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* JVM 信息
|
||||||
|
*/
|
||||||
|
private Jvm jvm;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 系统内存
|
||||||
|
*/
|
||||||
|
private Mem mem;
|
||||||
|
|
||||||
|
public SystemMonitorInfo() {
|
||||||
|
this.jvm = new Jvm();
|
||||||
|
this.sys = new Sys();
|
||||||
|
this.mem = new Mem();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
package im.zhaojun.common.repository;
|
package im.zhaojun.zfile.repository;
|
||||||
|
|
||||||
import im.zhaojun.common.model.StorageConfig;
|
import im.zhaojun.zfile.model.entity.StorageConfig;
|
||||||
import im.zhaojun.common.model.enums.StorageTypeEnum;
|
import im.zhaojun.zfile.model.enums.StorageTypeEnum;
|
||||||
import org.springframework.data.jpa.repository.JpaRepository;
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
import org.springframework.stereotype.Repository;
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
package im.zhaojun.common.repository;
|
package im.zhaojun.zfile.repository;
|
||||||
|
|
||||||
import im.zhaojun.common.model.SystemConfig;
|
import im.zhaojun.zfile.model.entity.SystemConfig;
|
||||||
import org.springframework.data.jpa.repository.JpaRepository;
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
import org.springframework.stereotype.Repository;
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package im.zhaojun.common.security;
|
package im.zhaojun.zfile.security;
|
||||||
|
|
||||||
import cn.hutool.crypto.SecureUtil;
|
import cn.hutool.crypto.SecureUtil;
|
||||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
package im.zhaojun.common.security;
|
package im.zhaojun.zfile.security;
|
||||||
|
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
import im.zhaojun.common.model.dto.ResultBean;
|
import im.zhaojun.zfile.model.dto.ResultBean;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.http.HttpMethod;
|
import org.springframework.http.HttpMethod;
|
||||||
import org.springframework.security.authentication.BadCredentialsException;
|
import org.springframework.security.authentication.BadCredentialsException;
|
||||||
@@ -21,6 +21,7 @@ import java.io.PrintWriter;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 自定义 Security 配置类
|
* 自定义 Security 配置类
|
||||||
|
*
|
||||||
* @author zhaojun
|
* @author zhaojun
|
||||||
*/
|
*/
|
||||||
@EnableWebSecurity
|
@EnableWebSecurity
|
||||||
@@ -34,7 +35,7 @@ public class MySecurityConfig extends WebSecurityConfigurerAdapter {
|
|||||||
http
|
http
|
||||||
// .authenticationProvider(authenticationProvider())
|
// .authenticationProvider(authenticationProvider())
|
||||||
.exceptionHandling()
|
.exceptionHandling()
|
||||||
//未登录时,进行json格式的提示,很喜欢这种写法,不用单独写一个又一个的类
|
// 未登录时,进行 json 格式的提示.
|
||||||
.authenticationEntryPoint((request, response, authException) -> {
|
.authenticationEntryPoint((request, response, authException) -> {
|
||||||
response.setContentType("application/json;charset=utf-8");
|
response.setContentType("application/json;charset=utf-8");
|
||||||
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
|
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
|
||||||
@@ -48,8 +49,8 @@ public class MySecurityConfig extends WebSecurityConfigurerAdapter {
|
|||||||
.antMatchers("/").permitAll()
|
.antMatchers("/").permitAll()
|
||||||
.antMatchers("/admin/**").authenticated()
|
.antMatchers("/admin/**").authenticated()
|
||||||
.and()
|
.and()
|
||||||
.formLogin() //使用自带的登录
|
.formLogin() // 使用自带的登录
|
||||||
//登录失败,返回json
|
// 登录失败,返回json
|
||||||
.failureHandler((request, response, ex) -> {
|
.failureHandler((request, response, ex) -> {
|
||||||
response.setContentType("application/json;charset=utf-8");
|
response.setContentType("application/json;charset=utf-8");
|
||||||
response.setStatus(HttpServletResponse.SC_FORBIDDEN);
|
response.setStatus(HttpServletResponse.SC_FORBIDDEN);
|
||||||
@@ -64,7 +65,7 @@ public class MySecurityConfig extends WebSecurityConfigurerAdapter {
|
|||||||
out.flush();
|
out.flush();
|
||||||
out.close();
|
out.close();
|
||||||
})
|
})
|
||||||
//登录成功,返回json
|
// 登录成功,返回json
|
||||||
.successHandler((request, response, authentication) -> {
|
.successHandler((request, response, authentication) -> {
|
||||||
response.setContentType("application/json;charset=utf-8");
|
response.setContentType("application/json;charset=utf-8");
|
||||||
PrintWriter out = response.getWriter();
|
PrintWriter out = response.getWriter();
|
||||||
@@ -74,7 +75,7 @@ public class MySecurityConfig extends WebSecurityConfigurerAdapter {
|
|||||||
})
|
})
|
||||||
.and()
|
.and()
|
||||||
.exceptionHandling()
|
.exceptionHandling()
|
||||||
//没有权限,返回json
|
// 没有权限,返回json
|
||||||
.accessDeniedHandler((request, response, ex) -> {
|
.accessDeniedHandler((request, response, ex) -> {
|
||||||
response.setContentType("application/json;charset=utf-8");
|
response.setContentType("application/json;charset=utf-8");
|
||||||
response.setStatus(HttpServletResponse.SC_FORBIDDEN);
|
response.setStatus(HttpServletResponse.SC_FORBIDDEN);
|
||||||
@@ -85,7 +86,7 @@ public class MySecurityConfig extends WebSecurityConfigurerAdapter {
|
|||||||
})
|
})
|
||||||
.and()
|
.and()
|
||||||
.logout()
|
.logout()
|
||||||
//退出成功,返回json
|
// 退出成功,返回 json
|
||||||
.logoutSuccessHandler((request, response, authentication) -> {
|
.logoutSuccessHandler((request, response, authentication) -> {
|
||||||
response.setContentType("application/json;charset=utf-8");
|
response.setContentType("application/json;charset=utf-8");
|
||||||
PrintWriter out = response.getWriter();
|
PrintWriter out = response.getWriter();
|
||||||
@@ -98,6 +99,7 @@ public class MySecurityConfig extends WebSecurityConfigurerAdapter {
|
|||||||
|
|
||||||
http.cors();
|
http.cors();
|
||||||
http.csrf().disable();
|
http.csrf().disable();
|
||||||
|
http.headers().frameOptions().sameOrigin();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
@@ -119,7 +121,7 @@ public class MySecurityConfig extends WebSecurityConfigurerAdapter {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void configure(WebSecurity web) {
|
public void configure(WebSecurity web) {
|
||||||
//对于在header里面增加token等类似情况,放行所有OPTIONS请求。
|
// 对于在 header 里面增加 token 等类似情况,放行所有 OPTIONS 请求。
|
||||||
web.ignoring().antMatchers(HttpMethod.OPTIONS, "/**");
|
web.ignoring().antMatchers(HttpMethod.OPTIONS, "/**");
|
||||||
web.httpFirewall(allowUrlEncodedSlashHttpFirewall());
|
web.httpFirewall(allowUrlEncodedSlashHttpFirewall());
|
||||||
}
|
}
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
package im.zhaojun.common.security;
|
package im.zhaojun.zfile.security;
|
||||||
|
|
||||||
import im.zhaojun.common.model.dto.SystemConfigDTO;
|
import im.zhaojun.zfile.model.dto.SystemConfigDTO;
|
||||||
import im.zhaojun.common.service.SystemConfigService;
|
import im.zhaojun.zfile.service.SystemConfigService;
|
||||||
import org.springframework.security.core.userdetails.User;
|
import org.springframework.security.core.userdetails.User;
|
||||||
import org.springframework.security.core.userdetails.UserDetails;
|
import org.springframework.security.core.userdetails.UserDetails;
|
||||||
import org.springframework.security.core.userdetails.UserDetailsService;
|
import org.springframework.security.core.userdetails.UserDetailsService;
|
||||||
@@ -9,6 +9,7 @@ import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
|||||||
|
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author zhaojun
|
* @author zhaojun
|
||||||
@@ -24,6 +25,9 @@ public class MyUserDetailsServiceImpl implements UserDetailsService {
|
|||||||
@Override
|
@Override
|
||||||
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
|
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
|
||||||
SystemConfigDTO systemConfig = systemConfigService.getSystemConfig();
|
SystemConfigDTO systemConfig = systemConfigService.getSystemConfig();
|
||||||
|
if (!Objects.equals(systemConfig.getUsername(), username)) {
|
||||||
|
throw new UsernameNotFoundException("用户名不存在");
|
||||||
|
}
|
||||||
return new User(systemConfig.getUsername(), systemConfig.getPassword(), Collections.emptyList());
|
return new User(systemConfig.getUsername(), systemConfig.getPassword(), Collections.emptyList());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,8 +1,8 @@
|
|||||||
package im.zhaojun.common.service;
|
package im.zhaojun.zfile.service;
|
||||||
|
|
||||||
import im.zhaojun.common.model.StorageConfig;
|
import im.zhaojun.zfile.model.entity.StorageConfig;
|
||||||
import im.zhaojun.common.model.enums.StorageTypeEnum;
|
import im.zhaojun.zfile.model.enums.StorageTypeEnum;
|
||||||
import im.zhaojun.common.repository.StorageConfigRepository;
|
import im.zhaojun.zfile.repository.StorageConfigRepository;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
@@ -1,17 +1,16 @@
|
|||||||
package im.zhaojun.common.service;
|
package im.zhaojun.zfile.service;
|
||||||
|
|
||||||
import cn.hutool.core.convert.Convert;
|
import cn.hutool.core.convert.Convert;
|
||||||
import cn.hutool.core.util.BooleanUtil;
|
import cn.hutool.core.util.BooleanUtil;
|
||||||
import cn.hutool.crypto.SecureUtil;
|
import cn.hutool.crypto.SecureUtil;
|
||||||
import com.alicp.jetcache.Cache;
|
import im.zhaojun.zfile.cache.ZFileCache;
|
||||||
import com.alicp.jetcache.anno.CacheType;
|
import im.zhaojun.zfile.config.StorageTypeFactory;
|
||||||
import com.alicp.jetcache.anno.CreateCache;
|
import im.zhaojun.zfile.model.constant.SystemConfigConstant;
|
||||||
import im.zhaojun.common.config.StorageTypeFactory;
|
import im.zhaojun.zfile.model.dto.SystemConfigDTO;
|
||||||
import im.zhaojun.common.model.SystemConfig;
|
import im.zhaojun.zfile.model.entity.SystemConfig;
|
||||||
import im.zhaojun.common.model.constant.SystemConfigConstant;
|
import im.zhaojun.zfile.model.enums.StorageTypeEnum;
|
||||||
import im.zhaojun.common.model.dto.SystemConfigDTO;
|
import im.zhaojun.zfile.repository.SystemConfigRepository;
|
||||||
import im.zhaojun.common.model.enums.StorageTypeEnum;
|
import im.zhaojun.zfile.service.base.AbstractBaseFileService;
|
||||||
import im.zhaojun.common.repository.SystemConfigRepository;
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
@@ -27,25 +26,18 @@ import java.util.List;
|
|||||||
@Service
|
@Service
|
||||||
public class SystemConfigService {
|
public class SystemConfigService {
|
||||||
|
|
||||||
public static final String SYSTEM_CONFIG_CACHE_PREFIX = "zfile-config-cache:";
|
@Resource
|
||||||
|
private ZFileCache zFileCache;
|
||||||
public static final String SYSTEM_CONFIG_CACHE_KEY = "1";
|
|
||||||
|
|
||||||
@CreateCache(name = SYSTEM_CONFIG_CACHE_PREFIX, cacheType = CacheType.LOCAL)
|
|
||||||
private Cache<String, Object> configCache;
|
|
||||||
|
|
||||||
@Resource
|
@Resource
|
||||||
private SystemConfigRepository systemConfigRepository;
|
private SystemConfigRepository systemConfigRepository;
|
||||||
|
|
||||||
@Resource
|
private Class<SystemConfigDTO> systemConfigClazz = SystemConfigDTO.class;
|
||||||
private FileCacheService fileCacheService;
|
|
||||||
|
|
||||||
private Class<SystemConfigDTO> systemConfigDTOClass = SystemConfigDTO.class;
|
|
||||||
|
|
||||||
public SystemConfigDTO getSystemConfig() {
|
public SystemConfigDTO getSystemConfig() {
|
||||||
Object cache = configCache.get(SYSTEM_CONFIG_CACHE_KEY);
|
SystemConfigDTO cacheConfig = zFileCache.getConfig();
|
||||||
if (configCache.get(SYSTEM_CONFIG_CACHE_KEY) != null) {
|
if (cacheConfig != null) {
|
||||||
return (SystemConfigDTO) cache;
|
return cacheConfig;
|
||||||
}
|
}
|
||||||
|
|
||||||
SystemConfigDTO systemConfigDTO = new SystemConfigDTO();
|
SystemConfigDTO systemConfigDTO = new SystemConfigDTO();
|
||||||
@@ -55,7 +47,7 @@ public class SystemConfigService {
|
|||||||
String key = systemConfig.getKey();
|
String key = systemConfig.getKey();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
Field field = systemConfigDTOClass.getDeclaredField(key);
|
Field field = systemConfigClazz.getDeclaredField(key);
|
||||||
if (field != null) {
|
if (field != null) {
|
||||||
field.setAccessible(true);
|
field.setAccessible(true);
|
||||||
String strVal = systemConfig.getValue();
|
String strVal = systemConfig.getValue();
|
||||||
@@ -69,7 +61,7 @@ public class SystemConfigService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
configCache.put(SYSTEM_CONFIG_CACHE_KEY, systemConfigDTO);
|
zFileCache.updateConfig(systemConfigDTO);
|
||||||
return systemConfigDTO;
|
return systemConfigDTO;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -77,7 +69,7 @@ public class SystemConfigService {
|
|||||||
public void updateSystemConfig(SystemConfigDTO systemConfigDTO) throws Exception {
|
public void updateSystemConfig(SystemConfigDTO systemConfigDTO) throws Exception {
|
||||||
List<SystemConfig> systemConfigList = new ArrayList<>();
|
List<SystemConfig> systemConfigList = new ArrayList<>();
|
||||||
|
|
||||||
Field[] fields = systemConfigDTOClass.getDeclaredFields();
|
Field[] fields = systemConfigClazz.getDeclaredFields();
|
||||||
for (Field field : fields) {
|
for (Field field : fields) {
|
||||||
String key = field.getName();
|
String key = field.getName();
|
||||||
SystemConfig systemConfig = systemConfigRepository.findByKey(key);
|
SystemConfig systemConfig = systemConfigRepository.findByKey(key);
|
||||||
@@ -91,22 +83,8 @@ public class SystemConfigService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
boolean oldEnableCache = getEnableCache();
|
zFileCache.removeConfig();
|
||||||
boolean curEnableCache = BooleanUtil.isTrue(systemConfigDTO.getEnableCache());
|
|
||||||
|
|
||||||
configCache.remove(SYSTEM_CONFIG_CACHE_KEY);
|
|
||||||
|
|
||||||
systemConfigRepository.saveAll(systemConfigList);
|
systemConfigRepository.saveAll(systemConfigList);
|
||||||
|
|
||||||
if (!oldEnableCache && curEnableCache) {
|
|
||||||
log.debug("检测到开启了缓存, 开启预热缓存");
|
|
||||||
fileCacheService.enableCache();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (oldEnableCache && !curEnableCache) {
|
|
||||||
log.debug("检测到关闭了缓存, 正在清理缓存数据及关闭自动刷新");
|
|
||||||
fileCacheService.disableCache();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -119,7 +97,7 @@ public class SystemConfigService {
|
|||||||
SystemConfig systemConfig = systemConfigRepository.findByKey(SystemConfigConstant.PASSWORD);
|
SystemConfig systemConfig = systemConfigRepository.findByKey(SystemConfigConstant.PASSWORD);
|
||||||
systemConfig.setValue(encryptionPassword);
|
systemConfig.setValue(encryptionPassword);
|
||||||
|
|
||||||
configCache.remove(SYSTEM_CONFIG_CACHE_KEY);
|
zFileCache.removeConfig();
|
||||||
|
|
||||||
systemConfigRepository.save(systemConfig);
|
systemConfigRepository.save(systemConfig);
|
||||||
}
|
}
|
||||||
@@ -129,11 +107,11 @@ public class SystemConfigService {
|
|||||||
SystemConfig enableConfig = systemConfigRepository.findByKey(SystemConfigConstant.ENABLE_CACHE);
|
SystemConfig enableConfig = systemConfigRepository.findByKey(SystemConfigConstant.ENABLE_CACHE);
|
||||||
enableConfig.setValue(isEnable.toString());
|
enableConfig.setValue(isEnable.toString());
|
||||||
systemConfigRepository.save(enableConfig);
|
systemConfigRepository.save(enableConfig);
|
||||||
configCache.remove(SYSTEM_CONFIG_CACHE_KEY);
|
zFileCache.removeConfig();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public AbstractFileService getCurrentFileService() {
|
public AbstractBaseFileService getCurrentFileService() {
|
||||||
StorageTypeEnum storageStrategy = getCurrentStorageStrategy();
|
StorageTypeEnum storageStrategy = getCurrentStorageStrategy();
|
||||||
return StorageTypeFactory.getStorageTypeService(storageStrategy);
|
return StorageTypeFactory.getStorageTypeService(storageStrategy);
|
||||||
}
|
}
|
||||||
67
src/main/java/im/zhaojun/zfile/service/SystemService.java
Normal file
67
src/main/java/im/zhaojun/zfile/service/SystemService.java
Normal file
@@ -0,0 +1,67 @@
|
|||||||
|
package im.zhaojun.zfile.service;
|
||||||
|
|
||||||
|
import im.zhaojun.zfile.model.constant.ZFileConstant;
|
||||||
|
import im.zhaojun.zfile.model.dto.FileItemDTO;
|
||||||
|
import im.zhaojun.zfile.model.dto.SiteConfigDTO;
|
||||||
|
import im.zhaojun.zfile.model.enums.StorageTypeEnum;
|
||||||
|
import im.zhaojun.zfile.service.base.AbstractBaseFileService;
|
||||||
|
import im.zhaojun.zfile.util.HttpUtil;
|
||||||
|
import im.zhaojun.zfile.util.StringUtils;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.web.client.HttpClientErrorException;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author zhaojun
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
@Service
|
||||||
|
public class SystemService {
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private SystemConfigService systemConfigService;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 构建指定路径下标题, 页面文档信息
|
||||||
|
* @param path 路径
|
||||||
|
*/
|
||||||
|
public SiteConfigDTO getConfig(String path) throws Exception {
|
||||||
|
|
||||||
|
SiteConfigDTO siteConfigDTO = new SiteConfigDTO();
|
||||||
|
AbstractBaseFileService fileService = systemConfigService.getCurrentFileService();
|
||||||
|
|
||||||
|
List<FileItemDTO> fileItemList;
|
||||||
|
|
||||||
|
if (Objects.equals(systemConfigService.getSystemConfig().getStorageStrategy(), StorageTypeEnum.FTP)) {
|
||||||
|
fileItemList = new ArrayList<>();
|
||||||
|
} else {
|
||||||
|
fileItemList = fileService.fileList(path);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (FileItemDTO fileItemDTO : fileItemList) {
|
||||||
|
if (ZFileConstant.README_FILE_NAME.equalsIgnoreCase(fileItemDTO.getName())) {
|
||||||
|
String textContent = null;
|
||||||
|
try {
|
||||||
|
textContent = HttpUtil.getTextContent(fileItemDTO.getUrl());
|
||||||
|
} catch (HttpClientErrorException httpClientErrorException) {
|
||||||
|
log.debug("尝试重新获取文档区缓存中链接后仍失败", httpClientErrorException);
|
||||||
|
try {
|
||||||
|
String fullPath = StringUtils.removeDuplicateSeparator(fileItemDTO.getPath() + "/" + fileItemDTO.getName());
|
||||||
|
FileItemDTO fileItem = fileService.getFileItem(fullPath);
|
||||||
|
textContent = HttpUtil.getTextContent(fileItem.getUrl());
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.debug("尝试重新获取文档区链接后仍失败, 已置为空", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
siteConfigDTO.setReadme(textContent);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return siteConfigDTO;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,128 @@
|
|||||||
|
package im.zhaojun.zfile.service.base;
|
||||||
|
|
||||||
|
import cn.hutool.core.util.BooleanUtil;
|
||||||
|
import im.zhaojun.zfile.cache.ZFileCache;
|
||||||
|
import im.zhaojun.zfile.model.dto.FileItemDTO;
|
||||||
|
import im.zhaojun.zfile.model.dto.SystemConfigDTO;
|
||||||
|
import im.zhaojun.zfile.model.enums.StorageTypeEnum;
|
||||||
|
import im.zhaojun.zfile.service.SystemConfigService;
|
||||||
|
import im.zhaojun.zfile.service.support.FileAsyncCacheService;
|
||||||
|
import im.zhaojun.zfile.service.support.FileCacheService;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.aop.framework.AopContext;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
|
||||||
|
import javax.annotation.PostConstruct;
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author zhaojun
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
public abstract class AbstractBaseFileService extends FileCacheService implements BaseFileService {
|
||||||
|
|
||||||
|
@Value("${zfile.cache.timeout}")
|
||||||
|
protected Long timeout;
|
||||||
|
|
||||||
|
protected boolean isInitialized = false;
|
||||||
|
|
||||||
|
protected String basePath;
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private SystemConfigService systemConfigService;
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private FileAsyncCacheService fileAsyncCacheService;
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private ZFileCache zFileCache;
|
||||||
|
|
||||||
|
/***
|
||||||
|
* 获取指定路径下的文件及文件夹, 默认缓存 60 分钟,每隔 30 分钟刷新一次.
|
||||||
|
* @param path 文件路径
|
||||||
|
* @return 文件及文件夹列表
|
||||||
|
* @throws Exception 获取文件列表中出现的异常
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public abstract List<FileItemDTO> fileList(String path) throws Exception;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 清理当前存储策略的缓存
|
||||||
|
* 1. 删除全部缓存
|
||||||
|
* 2. 标记为当前处于未完成缓存状态
|
||||||
|
*/
|
||||||
|
public void clearFileCache() {
|
||||||
|
zFileCache.clear();
|
||||||
|
fileAsyncCacheService.setCacheFinish(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 初始化方法, 启动时自动调用实现类的此方法进行初始化.
|
||||||
|
*/
|
||||||
|
@PostConstruct
|
||||||
|
public abstract void init();
|
||||||
|
|
||||||
|
protected boolean testConnection() {
|
||||||
|
boolean flag = true;
|
||||||
|
try {
|
||||||
|
fileList("/");
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.debug(getStorageTypeEnum().getDescription() + " 初始化异常", e);
|
||||||
|
flag = false;
|
||||||
|
}
|
||||||
|
return flag;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取是否初始化成功
|
||||||
|
* @return 初始化成功与否
|
||||||
|
*/
|
||||||
|
public boolean getIsUnInitialized() {
|
||||||
|
return !isInitialized;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取是否初始化成功
|
||||||
|
* @return 初始化成功与否
|
||||||
|
*/
|
||||||
|
public boolean getIsInitialized() {
|
||||||
|
return isInitialized;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取存储策略类型
|
||||||
|
* @return 存储策略类型枚举
|
||||||
|
*/
|
||||||
|
public abstract StorageTypeEnum getStorageTypeEnum();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 搜索文件
|
||||||
|
* @param name 文件名
|
||||||
|
* @return 包含该文件名的所有文件或文件夹
|
||||||
|
*/
|
||||||
|
public List<FileItemDTO> search(String name) {
|
||||||
|
SystemConfigDTO systemConfig = systemConfigService.getSystemConfig();
|
||||||
|
|
||||||
|
boolean searchIgnoreCase = BooleanUtil.isTrue(systemConfig.getSearchIgnoreCase());
|
||||||
|
boolean searchContainEncryptedFile = BooleanUtil.isTrue(systemConfig.getSearchContainEncryptedFile());
|
||||||
|
return zFileCache.find(name, searchIgnoreCase, searchContainEncryptedFile);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 刷新缓存
|
||||||
|
*/
|
||||||
|
public void refreshCache(String key) throws Exception {
|
||||||
|
zFileCache.remove(key);
|
||||||
|
BaseFileService currentFileService = (BaseFileService) AopContext.currentProxy();
|
||||||
|
currentFileService.fileList(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取单个文件信息
|
||||||
|
* @param path 文件路径
|
||||||
|
* @return 单个文件的内容.
|
||||||
|
*/
|
||||||
|
public abstract FileItemDTO getFileItem(String path);
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,31 +1,37 @@
|
|||||||
package im.zhaojun.onedrive.common.service;
|
package im.zhaojun.zfile.service.base;
|
||||||
|
|
||||||
|
import cn.hutool.core.util.URLUtil;
|
||||||
import cn.hutool.http.HttpRequest;
|
import cn.hutool.http.HttpRequest;
|
||||||
import cn.hutool.http.HttpResponse;
|
import cn.hutool.http.HttpResponse;
|
||||||
import cn.hutool.http.HttpUtil;
|
import cn.hutool.http.HttpUtil;
|
||||||
import com.alibaba.fastjson.JSON;
|
import com.alibaba.fastjson.JSON;
|
||||||
import com.alibaba.fastjson.JSONArray;
|
import com.alibaba.fastjson.JSONArray;
|
||||||
import com.alibaba.fastjson.JSONObject;
|
import com.alibaba.fastjson.JSONObject;
|
||||||
import im.zhaojun.common.model.StorageConfig;
|
import im.zhaojun.zfile.model.constant.ZFileConstant;
|
||||||
import im.zhaojun.common.model.constant.StorageConfigConstant;
|
import im.zhaojun.zfile.model.entity.StorageConfig;
|
||||||
import im.zhaojun.common.model.dto.FileItemDTO;
|
import im.zhaojun.zfile.model.constant.StorageConfigConstant;
|
||||||
import im.zhaojun.common.model.enums.FileTypeEnum;
|
import im.zhaojun.zfile.model.dto.FileItemDTO;
|
||||||
import im.zhaojun.common.model.enums.StorageTypeEnum;
|
import im.zhaojun.zfile.model.enums.FileTypeEnum;
|
||||||
import im.zhaojun.common.repository.StorageConfigRepository;
|
import im.zhaojun.zfile.repository.StorageConfigRepository;
|
||||||
import im.zhaojun.common.util.StringUtils;
|
import im.zhaojun.zfile.service.StorageConfigService;
|
||||||
import im.zhaojun.onedrive.common.model.OneDriveToken;
|
import im.zhaojun.zfile.util.StringUtils;
|
||||||
|
import im.zhaojun.zfile.model.support.OneDriveToken;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.web.client.HttpClientErrorException;
|
||||||
import org.springframework.web.client.RestTemplate;
|
import org.springframework.web.client.RestTemplate;
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Zhao Jun
|
* @author Zhao Jun
|
||||||
* 2020/1/29 11:54
|
* 2020/1/29 11:54
|
||||||
*/
|
*/
|
||||||
public abstract class AbstractOneDriveService {
|
@Slf4j
|
||||||
|
public abstract class AbstractOneDriveServiceBase extends AbstractBaseFileService {
|
||||||
|
|
||||||
protected static final String DRIVER_INFO_URL = "https://{graphEndPoint}/v1.0/me/drives";
|
protected static final String DRIVER_INFO_URL = "https://{graphEndPoint}/v1.0/me/drives";
|
||||||
|
|
||||||
@@ -37,15 +43,20 @@ public abstract class AbstractOneDriveService {
|
|||||||
|
|
||||||
protected static final String AUTHENTICATE_URL = "https://{authenticateEndPoint}/common/oauth2/v2.0/token";
|
protected static final String AUTHENTICATE_URL = "https://{authenticateEndPoint}/common/oauth2/v2.0/token";
|
||||||
|
|
||||||
|
private static final String ONE_DRIVE_FILE_FLAG = "file";
|
||||||
|
|
||||||
@Resource
|
@Resource
|
||||||
private RestTemplate oneDriveRestTemplate;
|
private RestTemplate oneDriveRestTemplate;
|
||||||
|
|
||||||
@Resource
|
@Resource
|
||||||
private StorageConfigRepository storageConfigRepository;
|
private StorageConfigRepository storageConfigRepository;
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private StorageConfigService storageConfigService;
|
||||||
|
|
||||||
public OneDriveToken getRefreshToken() {
|
public OneDriveToken getRefreshToken() {
|
||||||
StorageConfig refreshStorageConfig =
|
StorageConfig refreshStorageConfig =
|
||||||
storageConfigRepository.findByTypeAndKey(StorageTypeEnum.ONE_DRIVE, StorageConfigConstant.REFRESH_TOKEN_KEY);
|
storageConfigRepository.findByTypeAndKey(this.getStorageTypeEnum(), StorageConfigConstant.REFRESH_TOKEN_KEY);
|
||||||
|
|
||||||
String param = "client_id=" + getClientId() +
|
String param = "client_id=" + getClientId() +
|
||||||
"&redirect_uri=" + getRedirectUri() +
|
"&redirect_uri=" + getRedirectUri() +
|
||||||
@@ -77,13 +88,15 @@ public abstract class AbstractOneDriveService {
|
|||||||
return JSONObject.parseObject(response.body(), OneDriveToken.class);
|
return JSONObject.parseObject(response.body(), OneDriveToken.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public String getUserInfo() {
|
public String getUserInfo() {
|
||||||
return oneDriveRestTemplate.getForObject(DRIVER_INFO_URL, String.class);
|
return oneDriveRestTemplate.getForObject(DRIVER_INFO_URL, String.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<FileItemDTO> list(String path) {
|
@Override
|
||||||
|
public List<FileItemDTO> fileList(String path) {
|
||||||
|
path = StringUtils.removeFirstSeparator(path);
|
||||||
|
String fullPath = StringUtils.getFullPath(basePath, path);
|
||||||
|
|
||||||
List<FileItemDTO> result = new ArrayList<>();
|
List<FileItemDTO> result = new ArrayList<>();
|
||||||
String nextLink = null;
|
String nextLink = null;
|
||||||
|
|
||||||
@@ -92,15 +105,24 @@ public abstract class AbstractOneDriveService {
|
|||||||
String requestUrl;
|
String requestUrl;
|
||||||
|
|
||||||
if (nextLink != null) {
|
if (nextLink != null) {
|
||||||
requestUrl = nextLink;
|
nextLink = nextLink.replace("+", "%2B");
|
||||||
}else if ("/".equalsIgnoreCase(path)) {
|
requestUrl = URLUtil.decode(nextLink);
|
||||||
|
}else if (ZFileConstant.PATH_SEPARATOR.equalsIgnoreCase(fullPath) || "".equalsIgnoreCase(fullPath)) {
|
||||||
requestUrl = DRIVER_ROOT_URL;
|
requestUrl = DRIVER_ROOT_URL;
|
||||||
} else {
|
} else {
|
||||||
requestUrl = DRIVER_ITEMS_URL;
|
requestUrl = DRIVER_ITEMS_URL;
|
||||||
}
|
}
|
||||||
path = StringUtils.removeLastSeparator(path);
|
fullPath = StringUtils.removeLastSeparator(fullPath);
|
||||||
|
|
||||||
|
ResponseEntity<String> responseEntity;
|
||||||
|
try {
|
||||||
|
responseEntity = oneDriveRestTemplate.getForEntity(requestUrl, String.class, getGraphEndPoint(), fullPath);
|
||||||
|
} catch (HttpClientErrorException e) {
|
||||||
|
log.debug("调用 OneDrive 时出现了网络异常: {} , 已尝试重新刷新 token 后再试.", e.getMessage());
|
||||||
|
refreshOneDriveToken();
|
||||||
|
responseEntity = oneDriveRestTemplate.getForEntity(requestUrl, String.class, getGraphEndPoint(), fullPath);
|
||||||
|
}
|
||||||
|
|
||||||
ResponseEntity<String> responseEntity = oneDriveRestTemplate.getForEntity(requestUrl, String.class, getGraphEndPoint(), path);
|
|
||||||
String body = responseEntity.getBody();
|
String body = responseEntity.getBody();
|
||||||
|
|
||||||
JSONObject root = JSON.parseObject(body);
|
JSONObject root = JSON.parseObject(body);
|
||||||
@@ -132,11 +154,14 @@ public abstract class AbstractOneDriveService {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public FileItemDTO getFileItem(String path) {
|
||||||
|
|
||||||
|
String fullPath = StringUtils.getFullPath(basePath, path);
|
||||||
|
|
||||||
public FileItemDTO getItem(String path) {
|
|
||||||
String requestUrl;
|
String requestUrl;
|
||||||
|
|
||||||
ResponseEntity<String> responseEntity = oneDriveRestTemplate.getForEntity(DRIVER_ITEM_URL, String.class, path);
|
ResponseEntity<String> responseEntity = oneDriveRestTemplate.getForEntity(DRIVER_ITEM_URL, String.class, getGraphEndPoint(), fullPath);
|
||||||
String body = responseEntity.getBody();
|
String body = responseEntity.getBody();
|
||||||
|
|
||||||
JSONObject fileItem = JSON.parseObject(body);
|
JSONObject fileItem = JSON.parseObject(body);
|
||||||
@@ -146,7 +171,7 @@ public abstract class AbstractOneDriveService {
|
|||||||
fileItemDTO.setSize(fileItem.getLong("size"));
|
fileItemDTO.setSize(fileItem.getLong("size"));
|
||||||
fileItemDTO.setTime(fileItem.getDate("lastModifiedDateTime"));
|
fileItemDTO.setTime(fileItem.getDate("lastModifiedDateTime"));
|
||||||
|
|
||||||
if (fileItem.containsKey("file")) {
|
if (fileItem.containsKey(ONE_DRIVE_FILE_FLAG)) {
|
||||||
fileItemDTO.setUrl(fileItem.getString("@microsoft.graph.downloadUrl"));
|
fileItemDTO.setUrl(fileItem.getString("@microsoft.graph.downloadUrl"));
|
||||||
fileItemDTO.setType(FileTypeEnum.FILE);
|
fileItemDTO.setType(FileTypeEnum.FILE);
|
||||||
} else {
|
} else {
|
||||||
@@ -169,4 +194,21 @@ public abstract class AbstractOneDriveService {
|
|||||||
public abstract String getClientSecret();
|
public abstract String getClientSecret();
|
||||||
|
|
||||||
public abstract String getScope();
|
public abstract String getScope();
|
||||||
|
|
||||||
|
public void refreshOneDriveToken() {
|
||||||
|
OneDriveToken refreshToken = getRefreshToken();
|
||||||
|
|
||||||
|
if (refreshToken.getAccessToken() == null || refreshToken.getRefreshToken() == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
StorageConfig accessTokenConfig =
|
||||||
|
storageConfigService.selectByTypeAndKey(this.getStorageTypeEnum(), StorageConfigConstant.ACCESS_TOKEN_KEY);
|
||||||
|
StorageConfig refreshTokenConfig =
|
||||||
|
storageConfigService.selectByTypeAndKey(this.getStorageTypeEnum(), StorageConfigConstant.REFRESH_TOKEN_KEY);
|
||||||
|
accessTokenConfig.setValue(refreshToken.getAccessToken());
|
||||||
|
refreshTokenConfig.setValue(refreshToken.getRefreshToken());
|
||||||
|
|
||||||
|
storageConfigService.updateStorageConfig(Arrays.asList(accessTokenConfig, refreshTokenConfig));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -1,15 +1,16 @@
|
|||||||
package im.zhaojun.common.service;
|
package im.zhaojun.zfile.service.base;
|
||||||
|
|
||||||
import cn.hutool.core.util.ObjectUtil;
|
import cn.hutool.core.util.BooleanUtil;
|
||||||
import cn.hutool.core.util.URLUtil;
|
import cn.hutool.core.util.URLUtil;
|
||||||
import com.amazonaws.services.s3.AmazonS3;
|
import com.amazonaws.services.s3.AmazonS3;
|
||||||
import com.amazonaws.services.s3.model.ListObjectsRequest;
|
import com.amazonaws.services.s3.model.ListObjectsRequest;
|
||||||
import com.amazonaws.services.s3.model.ObjectListing;
|
import com.amazonaws.services.s3.model.ObjectListing;
|
||||||
import com.amazonaws.services.s3.model.S3ObjectSummary;
|
import com.amazonaws.services.s3.model.S3ObjectSummary;
|
||||||
import im.zhaojun.common.exception.NotExistFileException;
|
import im.zhaojun.zfile.service.StorageConfigService;
|
||||||
import im.zhaojun.common.model.dto.FileItemDTO;
|
import im.zhaojun.zfile.exception.NotExistFileException;
|
||||||
import im.zhaojun.common.model.enums.FileTypeEnum;
|
import im.zhaojun.zfile.model.dto.FileItemDTO;
|
||||||
import im.zhaojun.common.util.StringUtils;
|
import im.zhaojun.zfile.model.enums.FileTypeEnum;
|
||||||
|
import im.zhaojun.zfile.util.StringUtils;
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
@@ -20,23 +21,22 @@ import java.util.Objects;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @author zhaojun
|
* @author zhaojun
|
||||||
* @date 2019/12/26 22:26
|
|
||||||
*/
|
*/
|
||||||
public abstract class AbstractS3FileService extends AbstractFileService {
|
public abstract class AbstractS3BaseFileService extends AbstractBaseFileService {
|
||||||
|
|
||||||
@Resource
|
@Resource
|
||||||
protected StorageConfigService storageConfigService;
|
protected StorageConfigService storageConfigService;
|
||||||
|
|
||||||
protected String path;
|
protected String path;
|
||||||
|
|
||||||
protected String basePath;
|
|
||||||
|
|
||||||
protected String bucketName;
|
protected String bucketName;
|
||||||
|
|
||||||
protected String domain;
|
protected String domain;
|
||||||
|
|
||||||
protected AmazonS3 s3Client;
|
protected AmazonS3 s3Client;
|
||||||
|
|
||||||
|
protected boolean isPrivate;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<FileItemDTO> fileList(String path) {
|
public List<FileItemDTO> fileList(String path) {
|
||||||
this.path = path;
|
this.path = path;
|
||||||
@@ -56,7 +56,7 @@ public abstract class AbstractS3FileService extends AbstractFileService {
|
|||||||
*/
|
*/
|
||||||
public List<FileItemDTO> s3FileList(String path) {
|
public List<FileItemDTO> s3FileList(String path) {
|
||||||
path = StringUtils.removeFirstSeparator(path);
|
path = StringUtils.removeFirstSeparator(path);
|
||||||
String fullPath = StringUtils.removeFirstSeparator(getFullPath());
|
String fullPath = StringUtils.removeFirstSeparator(StringUtils.getFullPath(basePath, path));
|
||||||
List<FileItemDTO> fileItemList = new ArrayList<>();
|
List<FileItemDTO> fileItemList = new ArrayList<>();
|
||||||
ObjectListing objectListing = s3Client.listObjects(new ListObjectsRequest(bucketName, fullPath, "", "/", 1000));
|
ObjectListing objectListing = s3Client.listObjects(new ListObjectsRequest(bucketName, fullPath, "", "/", 1000));
|
||||||
|
|
||||||
@@ -95,6 +95,11 @@ public abstract class AbstractS3FileService extends AbstractFileService {
|
|||||||
public String s3ObjectUrl(String path) {
|
public String s3ObjectUrl(String path) {
|
||||||
String fullPath = StringUtils.removeFirstSeparator(StringUtils.removeDuplicateSeparator(basePath + "/" + path));
|
String fullPath = StringUtils.removeFirstSeparator(StringUtils.removeDuplicateSeparator(basePath + "/" + path));
|
||||||
|
|
||||||
|
// 如果不是私有空间, 且指定了加速域名, 则直接返回下载地址.
|
||||||
|
if (BooleanUtil.isFalse(isPrivate) && StringUtils.isNotNullOrEmpty(domain)) {
|
||||||
|
return StringUtils.concatPath(domain, fullPath);
|
||||||
|
}
|
||||||
|
|
||||||
Date expirationDate = new Date(System.currentTimeMillis() + timeout * 1000);
|
Date expirationDate = new Date(System.currentTimeMillis() + timeout * 1000);
|
||||||
URL url = s3Client.generatePresignedUrl(bucketName, fullPath, expirationDate);
|
URL url = s3Client.generatePresignedUrl(bucketName, fullPath, expirationDate);
|
||||||
|
|
||||||
@@ -105,23 +110,23 @@ public abstract class AbstractS3FileService extends AbstractFileService {
|
|||||||
return URLUtil.decode(defaultUrl);
|
return URLUtil.decode(defaultUrl);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 获取 basePath + path 的全路径地址.
|
|
||||||
* @return basePath + path 的全路径地址.
|
|
||||||
*/
|
|
||||||
public String getFullPath() {
|
|
||||||
String basePath = ObjectUtil.defaultIfNull(this.basePath, "");
|
|
||||||
String path = ObjectUtil.defaultIfNull(this.path, "");
|
|
||||||
return StringUtils.removeDuplicateSeparator(basePath + "/" + path);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public FileItemDTO getFileItem(String path) {
|
public FileItemDTO getFileItem(String path) {
|
||||||
List<FileItemDTO> list = fileList(path);
|
List<FileItemDTO> list;
|
||||||
|
try {
|
||||||
if (list == null || list.size() == 0) {
|
int end = path.lastIndexOf("/");
|
||||||
|
list = fileList(path.substring(0, end + 1));
|
||||||
|
} catch (Exception e) {
|
||||||
throw new NotExistFileException();
|
throw new NotExistFileException();
|
||||||
}
|
}
|
||||||
return list.get(0);
|
|
||||||
|
for (FileItemDTO fileItemDTO : list) {
|
||||||
|
String fullPath = StringUtils.concatUrl(fileItemDTO.getPath(), fileItemDTO.getName());
|
||||||
|
if (Objects.equals(fullPath, path)) {
|
||||||
|
return fileItemDTO;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new NotExistFileException();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,13 +1,13 @@
|
|||||||
package im.zhaojun.common.service;
|
package im.zhaojun.zfile.service.base;
|
||||||
|
|
||||||
import im.zhaojun.common.model.dto.FileItemDTO;
|
import im.zhaojun.zfile.model.dto.FileItemDTO;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author zhaojun
|
* @author zhaojun
|
||||||
*/
|
*/
|
||||||
public interface FileService {
|
public interface BaseFileService {
|
||||||
|
|
||||||
/***
|
/***
|
||||||
* 获取指定路径下的文件及文件夹
|
* 获取指定路径下的文件及文件夹
|
||||||
@@ -1,14 +1,15 @@
|
|||||||
package im.zhaojun.aliyun.service;
|
package im.zhaojun.zfile.service.impl;
|
||||||
|
|
||||||
|
import cn.hutool.core.convert.Convert;
|
||||||
import com.amazonaws.auth.AWSStaticCredentialsProvider;
|
import com.amazonaws.auth.AWSStaticCredentialsProvider;
|
||||||
import com.amazonaws.auth.BasicAWSCredentials;
|
import com.amazonaws.auth.BasicAWSCredentials;
|
||||||
import com.amazonaws.client.builder.AwsClientBuilder;
|
import com.amazonaws.client.builder.AwsClientBuilder;
|
||||||
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
|
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
|
||||||
import im.zhaojun.common.model.StorageConfig;
|
import im.zhaojun.zfile.model.entity.StorageConfig;
|
||||||
import im.zhaojun.common.model.constant.StorageConfigConstant;
|
import im.zhaojun.zfile.model.constant.StorageConfigConstant;
|
||||||
import im.zhaojun.common.model.enums.StorageTypeEnum;
|
import im.zhaojun.zfile.model.enums.StorageTypeEnum;
|
||||||
import im.zhaojun.common.service.AbstractS3FileService;
|
import im.zhaojun.zfile.service.base.AbstractS3BaseFileService;
|
||||||
import im.zhaojun.common.service.FileService;
|
import im.zhaojun.zfile.service.base.BaseFileService;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
@@ -20,21 +21,23 @@ import java.util.Objects;
|
|||||||
* @author zhaojun
|
* @author zhaojun
|
||||||
*/
|
*/
|
||||||
@Service
|
@Service
|
||||||
public class AliyunServiceImpl extends AbstractS3FileService implements FileService {
|
public class AliyunServiceImpl extends AbstractS3BaseFileService implements BaseFileService {
|
||||||
|
|
||||||
private static final Logger log = LoggerFactory.getLogger(AliyunServiceImpl.class);
|
private static final Logger log = LoggerFactory.getLogger(AliyunServiceImpl.class);
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void init() {
|
public void init() {
|
||||||
try {
|
try {
|
||||||
Map<String, StorageConfig> stringStorageConfigMap = storageConfigService.selectStorageConfigMapByKey(StorageTypeEnum.ALIYUN);
|
Map<String, StorageConfig> stringStorageConfigMap =
|
||||||
|
storageConfigService.selectStorageConfigMapByKey(getStorageTypeEnum());
|
||||||
String accessKey = stringStorageConfigMap.get(StorageConfigConstant.ACCESS_KEY).getValue();
|
String accessKey = stringStorageConfigMap.get(StorageConfigConstant.ACCESS_KEY).getValue();
|
||||||
String secretKey = stringStorageConfigMap.get(StorageConfigConstant.SECRET_KEY).getValue();
|
String secretKey = stringStorageConfigMap.get(StorageConfigConstant.SECRET_KEY).getValue();
|
||||||
String endPoint = stringStorageConfigMap.get(StorageConfigConstant.ENDPOINT_KEY).getValue();
|
String endPoint = stringStorageConfigMap.get(StorageConfigConstant.ENDPOINT_KEY).getValue();
|
||||||
|
|
||||||
super.bucketName = stringStorageConfigMap.get(StorageConfigConstant.BUCKET_NAME_KEY).getValue();
|
|
||||||
super.domain = stringStorageConfigMap.get(StorageConfigConstant.DOMAIN_KEY).getValue();
|
super.domain = stringStorageConfigMap.get(StorageConfigConstant.DOMAIN_KEY).getValue();
|
||||||
super.basePath = stringStorageConfigMap.get(StorageConfigConstant.BASE_PATH).getValue();
|
super.basePath = stringStorageConfigMap.get(StorageConfigConstant.BASE_PATH).getValue();
|
||||||
|
super.bucketName = stringStorageConfigMap.get(StorageConfigConstant.BUCKET_NAME_KEY).getValue();
|
||||||
|
super.isPrivate = Convert.toBool(stringStorageConfigMap.get(StorageConfigConstant.IS_PRIVATE).getValue(), true);
|
||||||
|
|
||||||
if (Objects.isNull(accessKey) || Objects.isNull(secretKey) || Objects.isNull(endPoint) || Objects.isNull(bucketName)) {
|
if (Objects.isNull(accessKey) || Objects.isNull(secretKey) || Objects.isNull(endPoint) || Objects.isNull(bucketName)) {
|
||||||
log.debug("初始化存储策略 [{}] 失败: 参数不完整", getStorageTypeEnum().getDescription());
|
log.debug("初始化存储策略 [{}] 失败: 参数不完整", getStorageTypeEnum().getDescription());
|
||||||
@@ -1,22 +1,25 @@
|
|||||||
package im.zhaojun.ftp.service;
|
package im.zhaojun.zfile.service.impl;
|
||||||
|
|
||||||
import cn.hutool.core.util.URLUtil;
|
import cn.hutool.core.util.URLUtil;
|
||||||
import cn.hutool.extra.ftp.Ftp;
|
import cn.hutool.extra.ftp.Ftp;
|
||||||
import im.zhaojun.common.model.StorageConfig;
|
import im.zhaojun.zfile.model.constant.StorageConfigConstant;
|
||||||
import im.zhaojun.common.model.constant.StorageConfigConstant;
|
import im.zhaojun.zfile.model.dto.FileItemDTO;
|
||||||
import im.zhaojun.common.model.dto.FileItemDTO;
|
import im.zhaojun.zfile.model.entity.StorageConfig;
|
||||||
import im.zhaojun.common.model.enums.FileTypeEnum;
|
import im.zhaojun.zfile.model.enums.FileTypeEnum;
|
||||||
import im.zhaojun.common.model.enums.StorageTypeEnum;
|
import im.zhaojun.zfile.model.enums.StorageTypeEnum;
|
||||||
import im.zhaojun.common.service.AbstractFileService;
|
import im.zhaojun.zfile.service.StorageConfigService;
|
||||||
import im.zhaojun.common.service.FileService;
|
import im.zhaojun.zfile.service.base.AbstractBaseFileService;
|
||||||
import im.zhaojun.common.service.StorageConfigService;
|
import im.zhaojun.zfile.service.base.BaseFileService;
|
||||||
import im.zhaojun.common.util.StringUtils;
|
import im.zhaojun.zfile.util.StringUtils;
|
||||||
|
import org.apache.commons.net.ftp.FTP;
|
||||||
|
import org.apache.commons.net.ftp.FTPClientConfig;
|
||||||
import org.apache.commons.net.ftp.FTPFile;
|
import org.apache.commons.net.ftp.FTPFile;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
@@ -26,7 +29,7 @@ import java.util.Objects;
|
|||||||
* @author zhaojun
|
* @author zhaojun
|
||||||
*/
|
*/
|
||||||
@Service
|
@Service
|
||||||
public class FtpServiceImpl extends AbstractFileService implements FileService {
|
public class FtpServiceImpl extends AbstractBaseFileService implements BaseFileService {
|
||||||
|
|
||||||
private static final Logger log = LoggerFactory.getLogger(FtpServiceImpl.class);
|
private static final Logger log = LoggerFactory.getLogger(FtpServiceImpl.class);
|
||||||
|
|
||||||
@@ -37,24 +40,33 @@ public class FtpServiceImpl extends AbstractFileService implements FileService {
|
|||||||
|
|
||||||
private String domain;
|
private String domain;
|
||||||
|
|
||||||
|
private String host;
|
||||||
|
|
||||||
|
private String port;
|
||||||
|
|
||||||
|
private String username;
|
||||||
|
|
||||||
|
private String password;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void init() {
|
public void init() {
|
||||||
try {
|
try {
|
||||||
Map<String, StorageConfig> stringStorageConfigMap =
|
Map<String, StorageConfig> stringStorageConfigMap =
|
||||||
storageConfigService.selectStorageConfigMapByKey(StorageTypeEnum.FTP);
|
storageConfigService.selectStorageConfigMapByKey(getStorageTypeEnum());
|
||||||
String host = stringStorageConfigMap.get(StorageConfigConstant.HOST_KEY).getValue();
|
host = stringStorageConfigMap.get(StorageConfigConstant.HOST_KEY).getValue();
|
||||||
String port = stringStorageConfigMap.get(StorageConfigConstant.PORT_KEY).getValue();
|
port = stringStorageConfigMap.get(StorageConfigConstant.PORT_KEY).getValue();
|
||||||
String username = stringStorageConfigMap.get(StorageConfigConstant.USERNAME_KEY).getValue();
|
username = stringStorageConfigMap.get(StorageConfigConstant.USERNAME_KEY).getValue();
|
||||||
String password = stringStorageConfigMap.get(StorageConfigConstant.PASSWORD_KEY).getValue();
|
password = stringStorageConfigMap.get(StorageConfigConstant.PASSWORD_KEY).getValue();
|
||||||
domain = stringStorageConfigMap.get(StorageConfigConstant.DOMAIN_KEY).getValue();
|
domain = stringStorageConfigMap.get(StorageConfigConstant.DOMAIN_KEY).getValue();
|
||||||
|
super.basePath = stringStorageConfigMap.get(StorageConfigConstant.BASE_PATH).getValue();
|
||||||
if (Objects.isNull(host) || Objects.isNull(port) || Objects.isNull(username) || Objects.isNull(password)) {
|
if (Objects.isNull(host) || Objects.isNull(port) || Objects.isNull(username) || Objects.isNull(password)) {
|
||||||
isInitialized = false;
|
isInitialized = false;
|
||||||
} else {
|
} else {
|
||||||
ftp = new Ftp(host, Integer.parseInt(port), username, password);
|
ftp = new Ftp(host, Integer.parseInt(port), username, password, StandardCharsets.UTF_8);
|
||||||
|
ftp.getClient().configure(new FTPClientConfig(FTPClientConfig.SYST_UNIX));
|
||||||
|
ftp.getClient().type(FTP.BINARY_FILE_TYPE);
|
||||||
isInitialized = testConnection();
|
isInitialized = testConnection();
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.debug(getStorageTypeEnum().getDescription() + " 初始化异常, 已跳过");
|
log.debug(getStorageTypeEnum().getDescription() + " 初始化异常, 已跳过");
|
||||||
}
|
}
|
||||||
@@ -62,7 +74,17 @@ public class FtpServiceImpl extends AbstractFileService implements FileService {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<FileItemDTO> fileList(String path) {
|
public List<FileItemDTO> fileList(String path) {
|
||||||
FTPFile[] ftpFiles = ftp.lsFiles(path);
|
ftp.reconnectIfTimeout();
|
||||||
|
String fullPath = StringUtils.getFullPath(basePath, path);
|
||||||
|
ftp.cd(fullPath);
|
||||||
|
FTPFile[] ftpFiles = new FTPFile[]{};
|
||||||
|
try {
|
||||||
|
ftp.getClient().changeWorkingDirectory("/");
|
||||||
|
ftpFiles = ftp.getClient().listFiles(fullPath);
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
// ignore
|
||||||
|
}
|
||||||
|
|
||||||
List<FileItemDTO> fileItemList = new ArrayList<>();
|
List<FileItemDTO> fileItemList = new ArrayList<>();
|
||||||
|
|
||||||
@@ -83,7 +105,17 @@ public class FtpServiceImpl extends AbstractFileService implements FileService {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getDownloadUrl(String path) {
|
public String getDownloadUrl(String path) {
|
||||||
return URLUtil.complateUrl(domain, path);
|
String fullPath = StringUtils.getFullPath(basePath, path);
|
||||||
|
if (StringUtils.isNullOrEmpty(domain)) {
|
||||||
|
return "ftp://"
|
||||||
|
+ URLUtil.encodeQuery(username)
|
||||||
|
+ ":"
|
||||||
|
+ URLUtil.encodeQuery(password)
|
||||||
|
+ "@"
|
||||||
|
+ host + ":" + port + fullPath;
|
||||||
|
}
|
||||||
|
|
||||||
|
return URLUtil.complateUrl(domain, fullPath);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -97,5 +129,4 @@ public class FtpServiceImpl extends AbstractFileService implements FileService {
|
|||||||
fileItemDTO.setUrl(getDownloadUrl(path));
|
fileItemDTO.setUrl(getDownloadUrl(path));
|
||||||
return fileItemDTO;
|
return fileItemDTO;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
|
||||||
@@ -1,14 +1,15 @@
|
|||||||
package im.zhaojun.huawei.service;
|
package im.zhaojun.zfile.service.impl;
|
||||||
|
|
||||||
|
import cn.hutool.core.convert.Convert;
|
||||||
import com.amazonaws.auth.AWSStaticCredentialsProvider;
|
import com.amazonaws.auth.AWSStaticCredentialsProvider;
|
||||||
import com.amazonaws.auth.BasicAWSCredentials;
|
import com.amazonaws.auth.BasicAWSCredentials;
|
||||||
import com.amazonaws.client.builder.AwsClientBuilder;
|
import com.amazonaws.client.builder.AwsClientBuilder;
|
||||||
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
|
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
|
||||||
import im.zhaojun.common.model.StorageConfig;
|
import im.zhaojun.zfile.model.entity.StorageConfig;
|
||||||
import im.zhaojun.common.model.constant.StorageConfigConstant;
|
import im.zhaojun.zfile.model.constant.StorageConfigConstant;
|
||||||
import im.zhaojun.common.model.enums.StorageTypeEnum;
|
import im.zhaojun.zfile.model.enums.StorageTypeEnum;
|
||||||
import im.zhaojun.common.service.AbstractS3FileService;
|
import im.zhaojun.zfile.service.base.AbstractS3BaseFileService;
|
||||||
import im.zhaojun.common.service.FileService;
|
import im.zhaojun.zfile.service.base.BaseFileService;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
@@ -20,14 +21,15 @@ import java.util.Objects;
|
|||||||
* @author zhaojun
|
* @author zhaojun
|
||||||
*/
|
*/
|
||||||
@Service
|
@Service
|
||||||
public class HuaweiServiceImpl extends AbstractS3FileService implements FileService {
|
public class HuaweiServiceImpl extends AbstractS3BaseFileService implements BaseFileService {
|
||||||
|
|
||||||
private static final Logger log = LoggerFactory.getLogger(HuaweiServiceImpl.class);
|
private static final Logger log = LoggerFactory.getLogger(HuaweiServiceImpl.class);
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void init() {
|
public void init() {
|
||||||
try {
|
try {
|
||||||
Map<String, StorageConfig> stringStorageConfigMap = storageConfigService.selectStorageConfigMapByKey(StorageTypeEnum.HUAWEI);
|
Map<String, StorageConfig> stringStorageConfigMap =
|
||||||
|
storageConfigService.selectStorageConfigMapByKey(getStorageTypeEnum());
|
||||||
String accessKey = stringStorageConfigMap.get(StorageConfigConstant.ACCESS_KEY).getValue();
|
String accessKey = stringStorageConfigMap.get(StorageConfigConstant.ACCESS_KEY).getValue();
|
||||||
String secretKey = stringStorageConfigMap.get(StorageConfigConstant.SECRET_KEY).getValue();
|
String secretKey = stringStorageConfigMap.get(StorageConfigConstant.SECRET_KEY).getValue();
|
||||||
String endPoint = stringStorageConfigMap.get(StorageConfigConstant.ENDPOINT_KEY).getValue();
|
String endPoint = stringStorageConfigMap.get(StorageConfigConstant.ENDPOINT_KEY).getValue();
|
||||||
@@ -35,6 +37,7 @@ public class HuaweiServiceImpl extends AbstractS3FileService implements FileServ
|
|||||||
bucketName = stringStorageConfigMap.get(StorageConfigConstant.BUCKET_NAME_KEY).getValue();
|
bucketName = stringStorageConfigMap.get(StorageConfigConstant.BUCKET_NAME_KEY).getValue();
|
||||||
domain = stringStorageConfigMap.get(StorageConfigConstant.DOMAIN_KEY).getValue();
|
domain = stringStorageConfigMap.get(StorageConfigConstant.DOMAIN_KEY).getValue();
|
||||||
basePath = stringStorageConfigMap.get(StorageConfigConstant.BASE_PATH).getValue();
|
basePath = stringStorageConfigMap.get(StorageConfigConstant.BASE_PATH).getValue();
|
||||||
|
isPrivate = Convert.toBool(stringStorageConfigMap.get(StorageConfigConstant.IS_PRIVATE).getValue(), true);
|
||||||
|
|
||||||
if (Objects.isNull(accessKey) || Objects.isNull(secretKey) || Objects.isNull(endPoint) || Objects.isNull(bucketName)) {
|
if (Objects.isNull(accessKey) || Objects.isNull(secretKey) || Objects.isNull(endPoint) || Objects.isNull(bucketName)) {
|
||||||
log.debug("初始化存储策略 [{}] 失败: 参数不完整", getStorageTypeEnum().getDescription());
|
log.debug("初始化存储策略 [{}] 失败: 参数不完整", getStorageTypeEnum().getDescription());
|
||||||
@@ -1,19 +1,18 @@
|
|||||||
package im.zhaojun.local.service;
|
package im.zhaojun.zfile.service.impl;
|
||||||
|
|
||||||
import cn.hutool.core.util.URLUtil;
|
import im.zhaojun.zfile.exception.NotExistFileException;
|
||||||
import im.zhaojun.common.exception.NotExistFileException;
|
import im.zhaojun.zfile.model.entity.StorageConfig;
|
||||||
import im.zhaojun.common.model.StorageConfig;
|
import im.zhaojun.zfile.model.entity.SystemConfig;
|
||||||
import im.zhaojun.common.model.SystemConfig;
|
import im.zhaojun.zfile.model.constant.StorageConfigConstant;
|
||||||
import im.zhaojun.common.model.constant.StorageConfigConstant;
|
import im.zhaojun.zfile.model.constant.SystemConfigConstant;
|
||||||
import im.zhaojun.common.model.constant.SystemConfigConstant;
|
import im.zhaojun.zfile.model.dto.FileItemDTO;
|
||||||
import im.zhaojun.common.model.dto.FileItemDTO;
|
import im.zhaojun.zfile.model.enums.FileTypeEnum;
|
||||||
import im.zhaojun.common.model.enums.FileTypeEnum;
|
import im.zhaojun.zfile.model.enums.StorageTypeEnum;
|
||||||
import im.zhaojun.common.model.enums.StorageTypeEnum;
|
import im.zhaojun.zfile.repository.SystemConfigRepository;
|
||||||
import im.zhaojun.common.repository.SystemConfigRepository;
|
import im.zhaojun.zfile.service.base.AbstractBaseFileService;
|
||||||
import im.zhaojun.common.service.AbstractFileService;
|
import im.zhaojun.zfile.service.base.BaseFileService;
|
||||||
import im.zhaojun.common.service.FileService;
|
import im.zhaojun.zfile.service.StorageConfigService;
|
||||||
import im.zhaojun.common.service.StorageConfigService;
|
import im.zhaojun.zfile.util.StringUtils;
|
||||||
import im.zhaojun.common.util.StringUtils;
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
@@ -30,7 +29,7 @@ import java.util.Objects;
|
|||||||
* @author zhaojun
|
* @author zhaojun
|
||||||
*/
|
*/
|
||||||
@Service
|
@Service
|
||||||
public class LocalServiceImpl extends AbstractFileService implements FileService {
|
public class LocalServiceImpl extends AbstractBaseFileService implements BaseFileService {
|
||||||
|
|
||||||
private static final Logger log = LoggerFactory.getLogger(LocalServiceImpl.class);
|
private static final Logger log = LoggerFactory.getLogger(LocalServiceImpl.class);
|
||||||
|
|
||||||
@@ -46,7 +45,7 @@ public class LocalServiceImpl extends AbstractFileService implements FileService
|
|||||||
public void init() {
|
public void init() {
|
||||||
try {
|
try {
|
||||||
Map<String, StorageConfig> stringStorageConfigMap =
|
Map<String, StorageConfig> stringStorageConfigMap =
|
||||||
storageConfigService.selectStorageConfigMapByKey(StorageTypeEnum.LOCAL);
|
storageConfigService.selectStorageConfigMapByKey(getStorageTypeEnum());
|
||||||
filePath = stringStorageConfigMap.get(StorageConfigConstant.FILE_PATH_KEY).getValue();
|
filePath = stringStorageConfigMap.get(StorageConfigConstant.FILE_PATH_KEY).getValue();
|
||||||
if (Objects.isNull(filePath)) {
|
if (Objects.isNull(filePath)) {
|
||||||
log.debug("初始化存储策略 [{}] 失败: 参数不完整", getStorageTypeEnum().getDescription());
|
log.debug("初始化存储策略 [{}] 失败: 参数不完整", getStorageTypeEnum().getDescription());
|
||||||
@@ -90,7 +89,7 @@ public class LocalServiceImpl extends AbstractFileService implements FileService
|
|||||||
@Override
|
@Override
|
||||||
public String getDownloadUrl(String path) {
|
public String getDownloadUrl(String path) {
|
||||||
SystemConfig usernameConfig = systemConfigRepository.findByKey(SystemConfigConstant.DOMAIN);
|
SystemConfig usernameConfig = systemConfigRepository.findByKey(SystemConfigConstant.DOMAIN);
|
||||||
return URLUtil.encode(StringUtils.removeDuplicateSeparator(usernameConfig.getValue() + "/file/" + path));
|
return StringUtils.removeDuplicateSeparator(usernameConfig.getValue() + "/file/" + path);
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getFilePath() {
|
public String getFilePath() {
|
||||||
@@ -1,14 +1,15 @@
|
|||||||
package im.zhaojun.minio;
|
package im.zhaojun.zfile.service.impl;
|
||||||
|
|
||||||
|
import cn.hutool.core.convert.Convert;
|
||||||
import com.amazonaws.auth.AWSStaticCredentialsProvider;
|
import com.amazonaws.auth.AWSStaticCredentialsProvider;
|
||||||
import com.amazonaws.auth.BasicAWSCredentials;
|
import com.amazonaws.auth.BasicAWSCredentials;
|
||||||
import com.amazonaws.client.builder.AwsClientBuilder;
|
import com.amazonaws.client.builder.AwsClientBuilder;
|
||||||
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
|
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
|
||||||
import im.zhaojun.common.model.StorageConfig;
|
import im.zhaojun.zfile.model.entity.StorageConfig;
|
||||||
import im.zhaojun.common.model.constant.StorageConfigConstant;
|
import im.zhaojun.zfile.model.constant.StorageConfigConstant;
|
||||||
import im.zhaojun.common.model.enums.StorageTypeEnum;
|
import im.zhaojun.zfile.model.enums.StorageTypeEnum;
|
||||||
import im.zhaojun.common.service.AbstractS3FileService;
|
import im.zhaojun.zfile.service.base.AbstractS3BaseFileService;
|
||||||
import im.zhaojun.common.service.FileService;
|
import im.zhaojun.zfile.service.base.BaseFileService;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
@@ -20,19 +21,21 @@ import java.util.Objects;
|
|||||||
* @author zhaojun
|
* @author zhaojun
|
||||||
*/
|
*/
|
||||||
@Service
|
@Service
|
||||||
public class MinIOServiceImpl extends AbstractS3FileService implements FileService {
|
public class MinIOServiceImpl extends AbstractS3BaseFileService implements BaseFileService {
|
||||||
|
|
||||||
private static final Logger log = LoggerFactory.getLogger(MinIOServiceImpl.class);
|
private static final Logger log = LoggerFactory.getLogger(MinIOServiceImpl.class);
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void init() {
|
public void init() {
|
||||||
try {
|
try {
|
||||||
Map<String, StorageConfig> stringStorageConfigMap = storageConfigService.selectStorageConfigMapByKey(StorageTypeEnum.MINIO);
|
Map<String, StorageConfig> stringStorageConfigMap =
|
||||||
|
storageConfigService.selectStorageConfigMapByKey(getStorageTypeEnum());
|
||||||
String accessKey = stringStorageConfigMap.get(StorageConfigConstant.ACCESS_KEY).getValue();
|
String accessKey = stringStorageConfigMap.get(StorageConfigConstant.ACCESS_KEY).getValue();
|
||||||
String secretKey = stringStorageConfigMap.get(StorageConfigConstant.SECRET_KEY).getValue();
|
String secretKey = stringStorageConfigMap.get(StorageConfigConstant.SECRET_KEY).getValue();
|
||||||
String endPoint = stringStorageConfigMap.get(StorageConfigConstant.ENDPOINT_KEY).getValue();
|
String endPoint = stringStorageConfigMap.get(StorageConfigConstant.ENDPOINT_KEY).getValue();
|
||||||
bucketName = stringStorageConfigMap.get(StorageConfigConstant.BUCKET_NAME_KEY).getValue();
|
bucketName = stringStorageConfigMap.get(StorageConfigConstant.BUCKET_NAME_KEY).getValue();
|
||||||
basePath = stringStorageConfigMap.get(StorageConfigConstant.BASE_PATH).getValue();
|
basePath = stringStorageConfigMap.get(StorageConfigConstant.BASE_PATH).getValue();
|
||||||
|
isPrivate = Convert.toBool(stringStorageConfigMap.get(StorageConfigConstant.IS_PRIVATE).getValue(), true);
|
||||||
|
|
||||||
if (Objects.isNull(accessKey) || Objects.isNull(secretKey) || Objects.isNull(endPoint) || Objects.isNull(bucketName)) {
|
if (Objects.isNull(accessKey) || Objects.isNull(secretKey) || Objects.isNull(endPoint) || Objects.isNull(bucketName)) {
|
||||||
log.debug("初始化存储策略 [{}] 失败: 参数不完整", getStorageTypeEnum().getDescription());
|
log.debug("初始化存储策略 [{}] 失败: 参数不完整", getStorageTypeEnum().getDescription());
|
||||||
@@ -0,0 +1,99 @@
|
|||||||
|
package im.zhaojun.zfile.service.impl;
|
||||||
|
|
||||||
|
import im.zhaojun.zfile.model.constant.StorageConfigConstant;
|
||||||
|
import im.zhaojun.zfile.model.entity.StorageConfig;
|
||||||
|
import im.zhaojun.zfile.model.enums.StorageTypeEnum;
|
||||||
|
import im.zhaojun.zfile.service.StorageConfigService;
|
||||||
|
import im.zhaojun.zfile.service.base.AbstractOneDriveServiceBase;
|
||||||
|
import im.zhaojun.zfile.service.base.BaseFileService;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author zhaojun
|
||||||
|
*/
|
||||||
|
@Service
|
||||||
|
@Slf4j
|
||||||
|
public class OneDriveChinaServiceImpl extends AbstractOneDriveServiceBase implements BaseFileService {
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private StorageConfigService storageConfigService;
|
||||||
|
|
||||||
|
@Value("${zfile.onedrive-china.clientId}")
|
||||||
|
private String clientId;
|
||||||
|
|
||||||
|
@Value("${zfile.onedrive-china.redirectUri}")
|
||||||
|
private String redirectUri;
|
||||||
|
|
||||||
|
@Value("${zfile.onedrive-china.clientSecret}")
|
||||||
|
private String clientSecret;
|
||||||
|
|
||||||
|
@Value("${zfile.onedrive-china.scope}")
|
||||||
|
private String scope;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void init() {
|
||||||
|
try {
|
||||||
|
Map<String, StorageConfig> stringStorageConfigMap =
|
||||||
|
storageConfigService.selectStorageConfigMapByKey(getStorageTypeEnum());
|
||||||
|
String accessToken = stringStorageConfigMap.get(StorageConfigConstant.ACCESS_TOKEN_KEY).getValue();
|
||||||
|
String refreshToken = stringStorageConfigMap.get(StorageConfigConstant.REFRESH_TOKEN_KEY).getValue();
|
||||||
|
super.basePath = stringStorageConfigMap.get(StorageConfigConstant.BASE_PATH).getValue();
|
||||||
|
|
||||||
|
if (StringUtils.isEmpty(accessToken) || StringUtils.isEmpty(refreshToken)) {
|
||||||
|
log.debug("初始化存储策略 [{}] 失败: 参数不完整", getStorageTypeEnum().getDescription());
|
||||||
|
isInitialized = false;
|
||||||
|
} else {
|
||||||
|
refreshOneDriveToken();
|
||||||
|
isInitialized = testConnection();
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.debug(getStorageTypeEnum().getDescription() + " 初始化异常, 已跳过");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getDownloadUrl(String path) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public StorageTypeEnum getStorageTypeEnum() {
|
||||||
|
return StorageTypeEnum.ONE_DRIVE_CHINA;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getGraphEndPoint() {
|
||||||
|
return "microsoftgraph.chinacloudapi.cn";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getAuthenticateEndPoint() {
|
||||||
|
return "login.partner.microsoftonline.cn";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getClientId() {
|
||||||
|
return clientId;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getRedirectUri() {
|
||||||
|
return redirectUri;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getClientSecret() {
|
||||||
|
return clientSecret;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getScope() {
|
||||||
|
return scope;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,99 @@
|
|||||||
|
package im.zhaojun.zfile.service.impl;
|
||||||
|
|
||||||
|
import im.zhaojun.zfile.model.constant.StorageConfigConstant;
|
||||||
|
import im.zhaojun.zfile.model.entity.StorageConfig;
|
||||||
|
import im.zhaojun.zfile.model.enums.StorageTypeEnum;
|
||||||
|
import im.zhaojun.zfile.service.StorageConfigService;
|
||||||
|
import im.zhaojun.zfile.service.base.AbstractOneDriveServiceBase;
|
||||||
|
import im.zhaojun.zfile.service.base.BaseFileService;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author zhaojun
|
||||||
|
*/
|
||||||
|
@Service
|
||||||
|
@Slf4j
|
||||||
|
public class OneDriveServiceImpl extends AbstractOneDriveServiceBase implements BaseFileService {
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private StorageConfigService storageConfigService;
|
||||||
|
|
||||||
|
@Value("${zfile.onedrive.clientId}")
|
||||||
|
protected String clientId;
|
||||||
|
|
||||||
|
@Value("${zfile.onedrive.redirectUri}")
|
||||||
|
protected String redirectUri;
|
||||||
|
|
||||||
|
@Value("${zfile.onedrive.clientSecret}")
|
||||||
|
protected String clientSecret;
|
||||||
|
|
||||||
|
@Value("${zfile.onedrive.scope}")
|
||||||
|
protected String scope;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void init() {
|
||||||
|
try {
|
||||||
|
Map<String, StorageConfig> stringStorageConfigMap =
|
||||||
|
storageConfigService.selectStorageConfigMapByKey(getStorageTypeEnum());
|
||||||
|
String accessToken = stringStorageConfigMap.get(StorageConfigConstant.ACCESS_TOKEN_KEY).getValue();
|
||||||
|
String refreshToken = stringStorageConfigMap.get(StorageConfigConstant.REFRESH_TOKEN_KEY).getValue();
|
||||||
|
super.basePath = stringStorageConfigMap.get(StorageConfigConstant.BASE_PATH).getValue();
|
||||||
|
|
||||||
|
if (StringUtils.isEmpty(accessToken) || StringUtils.isEmpty(refreshToken)) {
|
||||||
|
log.debug("初始化存储策略 [{}] 失败: 参数不完整", getStorageTypeEnum().getDescription());
|
||||||
|
isInitialized = false;
|
||||||
|
} else {
|
||||||
|
refreshOneDriveToken();
|
||||||
|
isInitialized = testConnection();
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.debug(getStorageTypeEnum().getDescription() + " 初始化异常, 已跳过");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getDownloadUrl(String path) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public StorageTypeEnum getStorageTypeEnum() {
|
||||||
|
return StorageTypeEnum.ONE_DRIVE;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getGraphEndPoint() {
|
||||||
|
return "graph.microsoft.com";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getAuthenticateEndPoint() {
|
||||||
|
return "login.microsoftonline.com";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getClientId() {
|
||||||
|
return clientId;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getRedirectUri() {
|
||||||
|
return redirectUri;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getClientSecret() {
|
||||||
|
return clientSecret;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getScope() {
|
||||||
|
return scope;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,14 +1,15 @@
|
|||||||
package im.zhaojun.qiniu.service;
|
package im.zhaojun.zfile.service.impl;
|
||||||
|
|
||||||
|
import cn.hutool.core.convert.Convert;
|
||||||
import com.amazonaws.auth.AWSStaticCredentialsProvider;
|
import com.amazonaws.auth.AWSStaticCredentialsProvider;
|
||||||
import com.amazonaws.auth.BasicAWSCredentials;
|
import com.amazonaws.auth.BasicAWSCredentials;
|
||||||
import com.amazonaws.client.builder.AwsClientBuilder;
|
import com.amazonaws.client.builder.AwsClientBuilder;
|
||||||
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
|
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
|
||||||
import im.zhaojun.common.model.StorageConfig;
|
import im.zhaojun.zfile.model.entity.StorageConfig;
|
||||||
import im.zhaojun.common.model.constant.StorageConfigConstant;
|
import im.zhaojun.zfile.model.constant.StorageConfigConstant;
|
||||||
import im.zhaojun.common.model.enums.StorageTypeEnum;
|
import im.zhaojun.zfile.model.enums.StorageTypeEnum;
|
||||||
import im.zhaojun.common.service.AbstractS3FileService;
|
import im.zhaojun.zfile.service.base.AbstractS3BaseFileService;
|
||||||
import im.zhaojun.common.service.FileService;
|
import im.zhaojun.zfile.service.base.BaseFileService;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
@@ -20,14 +21,15 @@ import java.util.Objects;
|
|||||||
* @author zhaojun
|
* @author zhaojun
|
||||||
*/
|
*/
|
||||||
@Service
|
@Service
|
||||||
public class QiniuServiceImpl extends AbstractS3FileService implements FileService {
|
public class QiniuServiceImpl extends AbstractS3BaseFileService implements BaseFileService {
|
||||||
|
|
||||||
private static final Logger log = LoggerFactory.getLogger(QiniuServiceImpl.class);
|
private static final Logger log = LoggerFactory.getLogger(QiniuServiceImpl.class);
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void init() {
|
public void init() {
|
||||||
try {
|
try {
|
||||||
Map<String, StorageConfig> stringStorageConfigMap = storageConfigService.selectStorageConfigMapByKey(StorageTypeEnum.QINIU);
|
Map<String, StorageConfig> stringStorageConfigMap =
|
||||||
|
storageConfigService.selectStorageConfigMapByKey(getStorageTypeEnum());
|
||||||
String accessKey = stringStorageConfigMap.get(StorageConfigConstant.ACCESS_KEY).getValue();
|
String accessKey = stringStorageConfigMap.get(StorageConfigConstant.ACCESS_KEY).getValue();
|
||||||
String secretKey = stringStorageConfigMap.get(StorageConfigConstant.SECRET_KEY).getValue();
|
String secretKey = stringStorageConfigMap.get(StorageConfigConstant.SECRET_KEY).getValue();
|
||||||
String endPoint = stringStorageConfigMap.get(StorageConfigConstant.ENDPOINT_KEY).getValue();
|
String endPoint = stringStorageConfigMap.get(StorageConfigConstant.ENDPOINT_KEY).getValue();
|
||||||
@@ -35,6 +37,7 @@ public class QiniuServiceImpl extends AbstractS3FileService implements FileServi
|
|||||||
bucketName = stringStorageConfigMap.get(StorageConfigConstant.BUCKET_NAME_KEY).getValue();
|
bucketName = stringStorageConfigMap.get(StorageConfigConstant.BUCKET_NAME_KEY).getValue();
|
||||||
domain = stringStorageConfigMap.get(StorageConfigConstant.DOMAIN_KEY).getValue();
|
domain = stringStorageConfigMap.get(StorageConfigConstant.DOMAIN_KEY).getValue();
|
||||||
basePath = stringStorageConfigMap.get(StorageConfigConstant.BASE_PATH).getValue();
|
basePath = stringStorageConfigMap.get(StorageConfigConstant.BASE_PATH).getValue();
|
||||||
|
isPrivate = Convert.toBool(stringStorageConfigMap.get(StorageConfigConstant.IS_PRIVATE).getValue(), true);
|
||||||
|
|
||||||
if (Objects.isNull(accessKey) || Objects.isNull(secretKey) || Objects.isNull(endPoint) || Objects.isNull(bucketName)) {
|
if (Objects.isNull(accessKey) || Objects.isNull(secretKey) || Objects.isNull(endPoint) || Objects.isNull(bucketName)) {
|
||||||
log.debug("初始化存储策略 [{}] 失败: 参数不完整", getStorageTypeEnum().getDescription());
|
log.debug("初始化存储策略 [{}] 失败: 参数不完整", getStorageTypeEnum().getDescription());
|
||||||
@@ -0,0 +1,68 @@
|
|||||||
|
package im.zhaojun.zfile.service.impl;
|
||||||
|
|
||||||
|
import cn.hutool.core.convert.Convert;
|
||||||
|
import com.amazonaws.auth.AWSStaticCredentialsProvider;
|
||||||
|
import com.amazonaws.auth.BasicAWSCredentials;
|
||||||
|
import com.amazonaws.client.builder.AwsClientBuilder;
|
||||||
|
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
|
||||||
|
import im.zhaojun.zfile.model.entity.StorageConfig;
|
||||||
|
import im.zhaojun.zfile.model.constant.StorageConfigConstant;
|
||||||
|
import im.zhaojun.zfile.model.enums.StorageTypeEnum;
|
||||||
|
import im.zhaojun.zfile.service.base.AbstractS3BaseFileService;
|
||||||
|
import im.zhaojun.zfile.service.base.BaseFileService;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author zhaojun
|
||||||
|
*/
|
||||||
|
@Service
|
||||||
|
public class S3ServiceImpl extends AbstractS3BaseFileService implements BaseFileService {
|
||||||
|
|
||||||
|
private static final Logger log = LoggerFactory.getLogger(S3ServiceImpl.class);
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void init() {
|
||||||
|
try {
|
||||||
|
Map<String, StorageConfig> stringStorageConfigMap =
|
||||||
|
storageConfigService.selectStorageConfigMapByKey(getStorageTypeEnum());
|
||||||
|
String accessKey = stringStorageConfigMap.get(StorageConfigConstant.ACCESS_KEY).getValue();
|
||||||
|
String secretKey = stringStorageConfigMap.get(StorageConfigConstant.SECRET_KEY).getValue();
|
||||||
|
String endPoint = stringStorageConfigMap.get(StorageConfigConstant.ENDPOINT_KEY).getValue();
|
||||||
|
|
||||||
|
super.domain = stringStorageConfigMap.get(StorageConfigConstant.DOMAIN_KEY).getValue();
|
||||||
|
super.basePath = stringStorageConfigMap.get(StorageConfigConstant.BASE_PATH).getValue();
|
||||||
|
super.bucketName = stringStorageConfigMap.get(StorageConfigConstant.BUCKET_NAME_KEY).getValue();
|
||||||
|
super.isPrivate = Convert.toBool(stringStorageConfigMap.get(StorageConfigConstant.IS_PRIVATE).getValue(), true);
|
||||||
|
|
||||||
|
String pathStyle = stringStorageConfigMap.get(StorageConfigConstant.PATH_STYLE).getValue();
|
||||||
|
|
||||||
|
boolean isPathStyle = "path-style".equals(pathStyle);
|
||||||
|
|
||||||
|
if (Objects.isNull(accessKey) || Objects.isNull(secretKey) || Objects.isNull(endPoint) || Objects.isNull(bucketName)) {
|
||||||
|
log.debug("初始化存储策略 [{}] 失败: 参数不完整", getStorageTypeEnum().getDescription());
|
||||||
|
isInitialized = false;
|
||||||
|
} else {
|
||||||
|
BasicAWSCredentials credentials = new BasicAWSCredentials(accessKey, secretKey);
|
||||||
|
s3Client = AmazonS3ClientBuilder.standard()
|
||||||
|
.withPathStyleAccessEnabled(isPathStyle)
|
||||||
|
.withCredentials(new AWSStaticCredentialsProvider(credentials))
|
||||||
|
.withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(endPoint, "")).build();
|
||||||
|
|
||||||
|
isInitialized = testConnection();
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.debug(getStorageTypeEnum().getDescription() + " 初始化异常, 已跳过");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public StorageTypeEnum getStorageTypeEnum() {
|
||||||
|
return StorageTypeEnum.S3;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,14 +1,15 @@
|
|||||||
package im.zhaojun.tencent;
|
package im.zhaojun.zfile.service.impl;
|
||||||
|
|
||||||
|
import cn.hutool.core.convert.Convert;
|
||||||
import com.amazonaws.auth.AWSStaticCredentialsProvider;
|
import com.amazonaws.auth.AWSStaticCredentialsProvider;
|
||||||
import com.amazonaws.auth.BasicAWSCredentials;
|
import com.amazonaws.auth.BasicAWSCredentials;
|
||||||
import com.amazonaws.client.builder.AwsClientBuilder;
|
import com.amazonaws.client.builder.AwsClientBuilder;
|
||||||
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
|
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
|
||||||
import im.zhaojun.common.model.StorageConfig;
|
import im.zhaojun.zfile.model.entity.StorageConfig;
|
||||||
import im.zhaojun.common.model.constant.StorageConfigConstant;
|
import im.zhaojun.zfile.model.constant.StorageConfigConstant;
|
||||||
import im.zhaojun.common.model.enums.StorageTypeEnum;
|
import im.zhaojun.zfile.model.enums.StorageTypeEnum;
|
||||||
import im.zhaojun.common.service.AbstractS3FileService;
|
import im.zhaojun.zfile.service.base.AbstractS3BaseFileService;
|
||||||
import im.zhaojun.common.service.FileService;
|
import im.zhaojun.zfile.service.base.BaseFileService;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
@@ -20,20 +21,22 @@ import java.util.Objects;
|
|||||||
* @author zhaojun
|
* @author zhaojun
|
||||||
*/
|
*/
|
||||||
@Service
|
@Service
|
||||||
public class TencentServiceImpl extends AbstractS3FileService implements FileService {
|
public class TencentServiceImpl extends AbstractS3BaseFileService implements BaseFileService {
|
||||||
|
|
||||||
private static final Logger log = LoggerFactory.getLogger(TencentServiceImpl.class);
|
private static final Logger log = LoggerFactory.getLogger(TencentServiceImpl.class);
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void init() {
|
public void init() {
|
||||||
try {
|
try {
|
||||||
Map<String, StorageConfig> stringStorageConfigMap = storageConfigService.selectStorageConfigMapByKey(StorageTypeEnum.TENCENT);
|
Map<String, StorageConfig> stringStorageConfigMap =
|
||||||
|
storageConfigService.selectStorageConfigMapByKey(getStorageTypeEnum());
|
||||||
String secretId = stringStorageConfigMap.get(StorageConfigConstant.SECRET_ID_KEY).getValue();
|
String secretId = stringStorageConfigMap.get(StorageConfigConstant.SECRET_ID_KEY).getValue();
|
||||||
String secretKey = stringStorageConfigMap.get(StorageConfigConstant.SECRET_KEY).getValue();
|
String secretKey = stringStorageConfigMap.get(StorageConfigConstant.SECRET_KEY).getValue();
|
||||||
String endPoint = stringStorageConfigMap.get(StorageConfigConstant.ENDPOINT_KEY).getValue();
|
String endPoint = stringStorageConfigMap.get(StorageConfigConstant.ENDPOINT_KEY).getValue();
|
||||||
bucketName = stringStorageConfigMap.get(StorageConfigConstant.BUCKET_NAME_KEY).getValue();
|
bucketName = stringStorageConfigMap.get(StorageConfigConstant.BUCKET_NAME_KEY).getValue();
|
||||||
domain = stringStorageConfigMap.get(StorageConfigConstant.DOMAIN_KEY).getValue();
|
domain = stringStorageConfigMap.get(StorageConfigConstant.DOMAIN_KEY).getValue();
|
||||||
basePath = stringStorageConfigMap.get(StorageConfigConstant.BASE_PATH).getValue();
|
basePath = stringStorageConfigMap.get(StorageConfigConstant.BASE_PATH).getValue();
|
||||||
|
isPrivate = Convert.toBool(stringStorageConfigMap.get(StorageConfigConstant.IS_PRIVATE).getValue(), true);
|
||||||
|
|
||||||
if (Objects.isNull(secretId) || Objects.isNull(secretKey) || Objects.isNull(endPoint) || Objects.isNull(bucketName)) {
|
if (Objects.isNull(secretId) || Objects.isNull(secretKey) || Objects.isNull(endPoint) || Objects.isNull(bucketName)) {
|
||||||
log.debug("初始化存储策略 [{}] 失败: 参数不完整", getStorageTypeEnum().getDescription());
|
log.debug("初始化存储策略 [{}] 失败: 参数不完整", getStorageTypeEnum().getDescription());
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
package im.zhaojun.zfile.service.impl;
|
||||||
|
|
||||||
|
import im.zhaojun.zfile.model.enums.StorageTypeEnum;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author zhaojun
|
||||||
|
*/
|
||||||
|
@Service
|
||||||
|
public class UFileServiceImpl extends UpYunServiceImpl {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public StorageTypeEnum getStorageTypeEnum() {
|
||||||
|
return StorageTypeEnum.UFILE;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,18 +1,18 @@
|
|||||||
package im.zhaojun.upyun.service;
|
package im.zhaojun.zfile.service.impl;
|
||||||
|
|
||||||
import cn.hutool.core.util.ObjectUtil;
|
import cn.hutool.core.util.ObjectUtil;
|
||||||
import cn.hutool.core.util.URLUtil;
|
import cn.hutool.core.util.URLUtil;
|
||||||
import com.UpYun;
|
import com.UpYun;
|
||||||
import im.zhaojun.common.exception.NotExistFileException;
|
import im.zhaojun.zfile.exception.NotExistFileException;
|
||||||
import im.zhaojun.common.model.StorageConfig;
|
import im.zhaojun.zfile.model.entity.StorageConfig;
|
||||||
import im.zhaojun.common.model.constant.StorageConfigConstant;
|
import im.zhaojun.zfile.model.constant.StorageConfigConstant;
|
||||||
import im.zhaojun.common.model.dto.FileItemDTO;
|
import im.zhaojun.zfile.model.dto.FileItemDTO;
|
||||||
import im.zhaojun.common.model.enums.FileTypeEnum;
|
import im.zhaojun.zfile.model.enums.FileTypeEnum;
|
||||||
import im.zhaojun.common.model.enums.StorageTypeEnum;
|
import im.zhaojun.zfile.model.enums.StorageTypeEnum;
|
||||||
import im.zhaojun.common.service.AbstractFileService;
|
import im.zhaojun.zfile.service.base.AbstractBaseFileService;
|
||||||
import im.zhaojun.common.service.FileService;
|
import im.zhaojun.zfile.service.base.BaseFileService;
|
||||||
import im.zhaojun.common.service.StorageConfigService;
|
import im.zhaojun.zfile.service.StorageConfigService;
|
||||||
import im.zhaojun.common.util.StringUtils;
|
import im.zhaojun.zfile.util.StringUtils;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
@@ -28,7 +28,7 @@ import java.util.Objects;
|
|||||||
*/
|
*/
|
||||||
@Service
|
@Service
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class UpYunServiceImpl extends AbstractFileService implements FileService {
|
public class UpYunServiceImpl extends AbstractBaseFileService implements BaseFileService {
|
||||||
|
|
||||||
private static final String END_MARK = "g2gCZAAEbmV4dGQAA2VvZg";
|
private static final String END_MARK = "g2gCZAAEbmV4dGQAA2VvZg";
|
||||||
|
|
||||||
@@ -45,7 +45,7 @@ public class UpYunServiceImpl extends AbstractFileService implements FileService
|
|||||||
public void init() {
|
public void init() {
|
||||||
try {
|
try {
|
||||||
Map<String, StorageConfig> stringStorageConfigMap =
|
Map<String, StorageConfig> stringStorageConfigMap =
|
||||||
storageConfigService.selectStorageConfigMapByKey(StorageTypeEnum.UPYUN);
|
storageConfigService.selectStorageConfigMapByKey(getStorageTypeEnum());
|
||||||
String bucketName = stringStorageConfigMap.get(StorageConfigConstant.BUCKET_NAME_KEY).getValue();
|
String bucketName = stringStorageConfigMap.get(StorageConfigConstant.BUCKET_NAME_KEY).getValue();
|
||||||
String username = stringStorageConfigMap.get(StorageConfigConstant.USERNAME_KEY).getValue();
|
String username = stringStorageConfigMap.get(StorageConfigConstant.USERNAME_KEY).getValue();
|
||||||
String password = stringStorageConfigMap.get(StorageConfigConstant.PASSWORD_KEY).getValue();
|
String password = stringStorageConfigMap.get(StorageConfigConstant.PASSWORD_KEY).getValue();
|
||||||
@@ -0,0 +1,191 @@
|
|||||||
|
package im.zhaojun.zfile.service.support;
|
||||||
|
|
||||||
|
import im.zhaojun.zfile.cache.ZFileCache;
|
||||||
|
import im.zhaojun.zfile.config.StorageTypeFactory;
|
||||||
|
import im.zhaojun.zfile.model.dto.FileItemDTO;
|
||||||
|
import im.zhaojun.zfile.model.enums.FileTypeEnum;
|
||||||
|
import im.zhaojun.zfile.model.enums.StorageTypeEnum;
|
||||||
|
import im.zhaojun.zfile.service.SystemConfigService;
|
||||||
|
import im.zhaojun.zfile.service.base.AbstractBaseFileService;
|
||||||
|
import im.zhaojun.zfile.service.base.BaseFileService;
|
||||||
|
import im.zhaojun.zfile.util.StringUtils;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.scheduling.annotation.Async;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
import java.util.ArrayDeque;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Objects;
|
||||||
|
import java.util.Set;
|
||||||
|
import java.util.concurrent.Executors;
|
||||||
|
import java.util.concurrent.ScheduledExecutorService;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author zhaojun
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
@Service
|
||||||
|
public class FileAsyncCacheService {
|
||||||
|
|
||||||
|
public static final String CACHE_PROCESS_PREFIX = "zfile-process-cache:";
|
||||||
|
|
||||||
|
private boolean cacheFinish;
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private SystemConfigService systemConfigService;
|
||||||
|
|
||||||
|
private volatile boolean stopFlag = false;
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private ZFileCache zFileCache;
|
||||||
|
|
||||||
|
@Value("${zfile.cache.auto-refresh.enable}")
|
||||||
|
protected boolean enableAutoRefreshCache;
|
||||||
|
|
||||||
|
@Value("${zfile.cache.auto-refresh.delay}")
|
||||||
|
protected Long delay;
|
||||||
|
|
||||||
|
@Value("${zfile.cache.auto-refresh.interval}")
|
||||||
|
protected Long interval;
|
||||||
|
|
||||||
|
@Async
|
||||||
|
public void cacheGlobalFile() {
|
||||||
|
stopFlag = false;
|
||||||
|
StorageTypeEnum storageStrategy = systemConfigService.getCurrentStorageStrategy();
|
||||||
|
|
||||||
|
if (storageStrategy == null) {
|
||||||
|
log.debug("尚未配置存储策略. 跳过启动缓存.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
boolean enableCache = systemConfigService.getEnableCache();
|
||||||
|
if (!enableCache) {
|
||||||
|
log.debug("存储策略 {} 未启用缓存, 跳过缓存.", storageStrategy.getDescription());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
AbstractBaseFileService fileService = StorageTypeFactory.getStorageTypeService(storageStrategy);
|
||||||
|
|
||||||
|
if (fileService.getIsUnInitialized()) {
|
||||||
|
log.debug("存储策略 {} 未初始化成功, 跳过缓存.", storageStrategy.getDescription());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
log.info("缓存 {} 所有文件开始", storageStrategy.getDescription());
|
||||||
|
long startTime = System.currentTimeMillis();
|
||||||
|
try {
|
||||||
|
BaseFileService currentFileService = systemConfigService.getCurrentFileService();
|
||||||
|
List<FileItemDTO> rootFileItems = currentFileService.fileList("/");
|
||||||
|
ArrayDeque<FileItemDTO> queue = new ArrayDeque<>(rootFileItems);
|
||||||
|
|
||||||
|
while (!queue.isEmpty()) {
|
||||||
|
FileItemDTO fileItemDTO = queue.pop();
|
||||||
|
|
||||||
|
if (stopFlag) {
|
||||||
|
zFileCache.clear();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (fileItemDTO.getType() == FileTypeEnum.FOLDER) {
|
||||||
|
String filePath = StringUtils.removeDuplicateSeparator("/" + fileItemDTO.getPath() + "/" + fileItemDTO.getName() + "/");
|
||||||
|
|
||||||
|
List<FileItemDTO> fileItems = currentFileService.fileList(filePath);
|
||||||
|
queue.addAll(fileItems);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("缓存所有文件失败", e);
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
long endTime = System.currentTimeMillis();
|
||||||
|
|
||||||
|
if (stopFlag) {
|
||||||
|
log.info("缓存 {} 所有文件被强制结束, 用时: {} 秒", storageStrategy.getDescription(), ((endTime - startTime) / 1000));
|
||||||
|
cacheFinish = false;
|
||||||
|
stopFlag = false;
|
||||||
|
} else {
|
||||||
|
log.info("缓存 {} 所有文件结束, 用时: {} 秒", storageStrategy.getDescription(), ((endTime - startTime) / 1000));
|
||||||
|
enableCacheAutoRefreshTask();
|
||||||
|
cacheFinish = true;
|
||||||
|
stopFlag = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void enableCacheAutoRefreshTask() {
|
||||||
|
StorageTypeEnum currentStorageStrategy = systemConfigService.getCurrentStorageStrategy();
|
||||||
|
|
||||||
|
if (enableAutoRefreshCache) {
|
||||||
|
ScheduledExecutorService scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();
|
||||||
|
scheduledExecutorService.scheduleWithFixedDelay(() -> {
|
||||||
|
zFileCache.setLastCacheAutoRefreshDate(new Date());
|
||||||
|
|
||||||
|
boolean enableCache = systemConfigService.getEnableCache();
|
||||||
|
|
||||||
|
if (!enableCache) {
|
||||||
|
log.debug("当前存储引擎未开启缓存, 跳过自动刷新缓存");
|
||||||
|
zFileCache.clear();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
log.debug("开始调用自动刷新缓存");
|
||||||
|
|
||||||
|
Set<String> keySet = zFileCache.keySet();
|
||||||
|
|
||||||
|
ArrayList<String> keys = new ArrayList<>(keySet);
|
||||||
|
|
||||||
|
|
||||||
|
for (String key : keys) {
|
||||||
|
try {
|
||||||
|
Thread.sleep(300);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (stopFlag) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
zFileCache.remove(key);
|
||||||
|
AbstractBaseFileService currentFileService = systemConfigService.getCurrentFileService();
|
||||||
|
try {
|
||||||
|
if (Objects.equals(currentStorageStrategy, systemConfigService.getCurrentStorageStrategy())) {
|
||||||
|
currentFileService.fileList(key);
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("刷新过程中出错 : [" + key + "]", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (stopFlag) {
|
||||||
|
log.debug("检测到停止 [{}] 缓存指令, 已停止自动刷新任务", currentStorageStrategy);
|
||||||
|
scheduledExecutorService.shutdownNow();
|
||||||
|
stopFlag = false;
|
||||||
|
} else {
|
||||||
|
log.debug("自动刷新缓存完成");
|
||||||
|
}
|
||||||
|
}, delay, interval, TimeUnit.SECONDS);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void stopScheduled() {
|
||||||
|
this.stopFlag = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void enableScheduled() {
|
||||||
|
this.stopFlag = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isCacheFinish() {
|
||||||
|
return cacheFinish;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCacheFinish(boolean cacheFinish) {
|
||||||
|
this.cacheFinish = cacheFinish;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,5 +1,7 @@
|
|||||||
package im.zhaojun.common.service;
|
package im.zhaojun.zfile.service.support;
|
||||||
|
|
||||||
|
import im.zhaojun.zfile.cache.ZFileCache;
|
||||||
|
import im.zhaojun.zfile.service.SystemConfigService;
|
||||||
import org.springframework.context.annotation.Lazy;
|
import org.springframework.context.annotation.Lazy;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
@@ -7,7 +9,6 @@ import javax.annotation.Resource;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @author zhaojun
|
* @author zhaojun
|
||||||
* @date 2020/1/11 14:08
|
|
||||||
*/
|
*/
|
||||||
@Service
|
@Service
|
||||||
public class FileCacheService {
|
public class FileCacheService {
|
||||||
@@ -19,21 +20,19 @@ public class FileCacheService {
|
|||||||
@Lazy
|
@Lazy
|
||||||
private FileAsyncCacheService fileAsyncCacheService;
|
private FileAsyncCacheService fileAsyncCacheService;
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private ZFileCache zFileCache;
|
||||||
|
|
||||||
public void enableCache() {
|
public void enableCache() {
|
||||||
systemConfigService.updateCacheEnableConfig(true);
|
systemConfigService.updateCacheEnableConfig(true);
|
||||||
|
|
||||||
AbstractFileService currentFileService = systemConfigService.getCurrentFileService();
|
|
||||||
currentFileService.openCacheAutoRefresh();
|
|
||||||
fileAsyncCacheService.cacheGlobalFile();
|
fileAsyncCacheService.cacheGlobalFile();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void disableCache() {
|
||||||
public void disableCache() throws Exception {
|
|
||||||
systemConfigService.updateCacheEnableConfig(false);
|
systemConfigService.updateCacheEnableConfig(false);
|
||||||
|
zFileCache.clear();
|
||||||
AbstractFileService currentFileService = systemConfigService.getCurrentFileService();
|
fileAsyncCacheService.setCacheFinish(false);
|
||||||
currentFileService.clearFileCache();
|
fileAsyncCacheService.stopScheduled();
|
||||||
fileAsyncCacheService.resetCacheCount();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
package im.zhaojun.zfile.service.support;
|
||||||
|
|
||||||
|
import im.zhaojun.zfile.model.support.SystemMonitorInfo;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author zhaojun
|
||||||
|
*/
|
||||||
|
@Service
|
||||||
|
public class SystemMonitorService {
|
||||||
|
|
||||||
|
public SystemMonitorInfo systemMonitorInfo() {
|
||||||
|
return new SystemMonitorInfo();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package im.zhaojun.common.util;
|
package im.zhaojun.zfile.util;
|
||||||
|
|
||||||
import cn.hutool.core.codec.Base64;
|
import cn.hutool.core.codec.Base64;
|
||||||
import cn.hutool.core.io.FileUtil;
|
import cn.hutool.core.io.FileUtil;
|
||||||
@@ -10,8 +10,8 @@ import com.mpatric.mp3agic.ID3v2;
|
|||||||
import com.mpatric.mp3agic.InvalidDataException;
|
import com.mpatric.mp3agic.InvalidDataException;
|
||||||
import com.mpatric.mp3agic.Mp3File;
|
import com.mpatric.mp3agic.Mp3File;
|
||||||
import com.mpatric.mp3agic.UnsupportedTagException;
|
import com.mpatric.mp3agic.UnsupportedTagException;
|
||||||
import im.zhaojun.common.model.constant.ZFileConstant;
|
import im.zhaojun.zfile.model.constant.ZFileConstant;
|
||||||
import im.zhaojun.common.model.dto.AudioInfoDTO;
|
import im.zhaojun.zfile.model.dto.AudioInfoDTO;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
package im.zhaojun.common.util;
|
package im.zhaojun.zfile.util;
|
||||||
|
|
||||||
import im.zhaojun.common.model.dto.FileItemDTO;
|
import im.zhaojun.zfile.model.dto.FileItemDTO;
|
||||||
import im.zhaojun.common.model.enums.FileTypeEnum;
|
import im.zhaojun.zfile.model.enums.FileTypeEnum;
|
||||||
|
|
||||||
import java.util.Comparator;
|
import java.util.Comparator;
|
||||||
|
|
||||||
@@ -37,16 +37,15 @@ public class FileComparator implements Comparator<FileItemDTO> {
|
|||||||
if (order == null) {
|
if (order == null) {
|
||||||
order = "asc";
|
order = "asc";
|
||||||
}
|
}
|
||||||
|
|
||||||
FileTypeEnum o1Type = o1.getType();
|
FileTypeEnum o1Type = o1.getType();
|
||||||
FileTypeEnum o2Type = o2.getType();
|
FileTypeEnum o2Type = o2.getType();
|
||||||
|
NaturalOrderComparator naturalOrderComparator = new NaturalOrderComparator();
|
||||||
if (o1Type.equals(o2Type)) {
|
if (o1Type.equals(o2Type)) {
|
||||||
int result;
|
int result;
|
||||||
switch (sortBy) {
|
switch (sortBy) {
|
||||||
case "time": result = o1.getTime().compareTo(o2.getTime()); break;
|
case "time": result = o1.getTime().compareTo(o2.getTime()); break;
|
||||||
case "size": result = o1.getSize().compareTo(o2.getSize()); break;
|
case "size": result = o1.getSize().compareTo(o2.getSize()); break;
|
||||||
default: result = o1.getName().compareToIgnoreCase(o2.getName()); break;
|
default: result = naturalOrderComparator.compare(o1.getName(), o2.getName()); break;
|
||||||
}
|
}
|
||||||
return "asc".equals(order) ? result : -result;
|
return "asc".equals(order) ? result : -result;
|
||||||
}
|
}
|
||||||
@@ -57,4 +56,4 @@ public class FileComparator implements Comparator<FileItemDTO> {
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
68
src/main/java/im/zhaojun/zfile/util/FileUtil.java
Normal file
68
src/main/java/im/zhaojun/zfile/util/FileUtil.java
Normal file
@@ -0,0 +1,68 @@
|
|||||||
|
package im.zhaojun.zfile.util;
|
||||||
|
|
||||||
|
import cn.hutool.core.util.URLUtil;
|
||||||
|
import org.springframework.core.io.FileSystemResource;
|
||||||
|
import org.springframework.http.HttpHeaders;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.http.MediaType;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author zhaojun
|
||||||
|
*/
|
||||||
|
public class FileUtil {
|
||||||
|
|
||||||
|
public static ResponseEntity<Object> export(File file, String fileName) {
|
||||||
|
if (!file.exists()) {
|
||||||
|
return ResponseEntity.status(HttpStatus.NOT_FOUND).body("404 FILE NOT FOUND");
|
||||||
|
}
|
||||||
|
|
||||||
|
MediaType mediaType = MediaType.APPLICATION_OCTET_STREAM;
|
||||||
|
|
||||||
|
HttpHeaders headers = new HttpHeaders();
|
||||||
|
headers.add("Cache-Control", "no-cache, no-store, must-revalidate");
|
||||||
|
|
||||||
|
if (StringUtils.isNullOrEmpty(fileName)) {
|
||||||
|
fileName = file.getName();
|
||||||
|
}
|
||||||
|
|
||||||
|
headers.setContentDispositionFormData("attachment", URLUtil.encode(fileName));
|
||||||
|
|
||||||
|
headers.add("Pragma", "no-cache");
|
||||||
|
headers.add("Expires", "0");
|
||||||
|
headers.add("Last-Modified", new Date().toString());
|
||||||
|
headers.add("ETag", String.valueOf(System.currentTimeMillis()));
|
||||||
|
return ResponseEntity
|
||||||
|
.ok()
|
||||||
|
.headers(headers)
|
||||||
|
.contentLength(file.length())
|
||||||
|
.contentType(mediaType)
|
||||||
|
.body(new FileSystemResource(file));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static ResponseEntity<Object> export(File file) {
|
||||||
|
if (!file.exists()) {
|
||||||
|
return ResponseEntity.status(HttpStatus.NOT_FOUND).body("404 FILE NOT FOUND");
|
||||||
|
}
|
||||||
|
|
||||||
|
MediaType mediaType = MediaType.APPLICATION_OCTET_STREAM;
|
||||||
|
|
||||||
|
HttpHeaders headers = new HttpHeaders();
|
||||||
|
headers.add("Cache-Control", "no-cache, no-store, must-revalidate");
|
||||||
|
headers.setContentDispositionFormData("attachment", URLUtil.encode(file.getName()));
|
||||||
|
|
||||||
|
headers.add("Pragma", "no-cache");
|
||||||
|
headers.add("Expires", "0");
|
||||||
|
headers.add("Last-Modified", new Date().toString());
|
||||||
|
headers.add("ETag", String.valueOf(System.currentTimeMillis()));
|
||||||
|
return ResponseEntity
|
||||||
|
.ok()
|
||||||
|
.headers(headers)
|
||||||
|
.contentLength(file.length())
|
||||||
|
.contentType(mediaType)
|
||||||
|
.body(new FileSystemResource(file));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package im.zhaojun.common.util;
|
package im.zhaojun.zfile.util;
|
||||||
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.web.client.RestClientException;
|
import org.springframework.web.client.RestClientException;
|
||||||
150
src/main/java/im/zhaojun/zfile/util/NaturalOrderComparator.java
Normal file
150
src/main/java/im/zhaojun/zfile/util/NaturalOrderComparator.java
Normal file
@@ -0,0 +1,150 @@
|
|||||||
|
package im.zhaojun.zfile.util;
|
||||||
|
/*
|
||||||
|
NaturalOrderComparator.java -- Perform 'natural order' comparisons of strings in Java.
|
||||||
|
Copyright (C) 2003 by Pierre-Luc Paour <natorder@paour.com>
|
||||||
|
|
||||||
|
Based on the C version by Martin Pool, of which this is more or less a straight conversion.
|
||||||
|
Copyright (C) 2000 by Martin Pool <mbp@humbug.org.au>
|
||||||
|
|
||||||
|
This software is provided 'as-is', without any express or implied
|
||||||
|
warranty. In no event will the authors be held liable for any damages
|
||||||
|
arising from the use of this software.
|
||||||
|
|
||||||
|
Permission is granted to anyone to use this software for any purpose,
|
||||||
|
including commercial applications, and to alter it and redistribute it
|
||||||
|
freely, subject to the following restrictions:
|
||||||
|
|
||||||
|
1. The origin of this software must not be misrepresented; you must not
|
||||||
|
claim that you wrote the original software. If you use this software
|
||||||
|
in a product, an acknowledgment in the product documentation would be
|
||||||
|
appreciated but is not required.
|
||||||
|
2. Altered source versions must be plainly marked as such, and must not be
|
||||||
|
misrepresented as being the original software.
|
||||||
|
3. This notice may not be removed or altered from any source distribution.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import java.util.Comparator;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author zhaojun
|
||||||
|
*/
|
||||||
|
public class NaturalOrderComparator implements Comparator<String> {
|
||||||
|
|
||||||
|
private static final char ZERO_CHAR = '0';
|
||||||
|
|
||||||
|
private int compareRight(String a, String b) {
|
||||||
|
int bias = 0, ia = 0, ib = 0;
|
||||||
|
|
||||||
|
// The longest run of digits wins. That aside, the greatest
|
||||||
|
// value wins, but we can't know that it will until we've scanned
|
||||||
|
// both numbers to know that they have the same magnitude, so we
|
||||||
|
// remember it in BIAS.
|
||||||
|
for (; ; ia++, ib++) {
|
||||||
|
char ca = charAt(a, ia);
|
||||||
|
char cb = charAt(b, ib);
|
||||||
|
|
||||||
|
if (!isDigit(ca) && !isDigit(cb)) {
|
||||||
|
return bias;
|
||||||
|
}
|
||||||
|
if (!isDigit(ca)) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
if (!isDigit(cb)) {
|
||||||
|
return +1;
|
||||||
|
}
|
||||||
|
if (ca == 0 && cb == 0) {
|
||||||
|
return bias;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (bias == 0) {
|
||||||
|
if (ca < cb) {
|
||||||
|
bias = -1;
|
||||||
|
} else if (ca > cb) {
|
||||||
|
bias = +1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int compare(String a, String b) {
|
||||||
|
int ia = 0, ib = 0;
|
||||||
|
int nza, nzb;
|
||||||
|
char ca, cb;
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
// Only count the number of zeroes leading the last number compared
|
||||||
|
nza = nzb = 0;
|
||||||
|
|
||||||
|
ca = charAt(a, ia);
|
||||||
|
cb = charAt(b, ib);
|
||||||
|
|
||||||
|
// skip over leading spaces or zeros
|
||||||
|
while (Character.isSpaceChar(ca) || ca == ZERO_CHAR) {
|
||||||
|
if (ca == ZERO_CHAR) {
|
||||||
|
nza++;
|
||||||
|
} else {
|
||||||
|
// Only count consecutive zeroes
|
||||||
|
nza = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
ca = charAt(a, ++ia);
|
||||||
|
}
|
||||||
|
|
||||||
|
while (Character.isSpaceChar(cb) || cb == '0') {
|
||||||
|
if (cb == '0') {
|
||||||
|
nzb++;
|
||||||
|
} else {
|
||||||
|
// Only count consecutive zeroes
|
||||||
|
nzb = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
cb = charAt(b, ++ib);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Process run of digits
|
||||||
|
if (Character.isDigit(ca) && Character.isDigit(cb)) {
|
||||||
|
int bias = compareRight(a.substring(ia), b.substring(ib));
|
||||||
|
if (bias != 0) {
|
||||||
|
return bias;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ca == 0 && cb == 0) {
|
||||||
|
// The strings compare the same. Perhaps the caller
|
||||||
|
// will want to call strcmp to break the tie.
|
||||||
|
return compareEqual(a, b, nza, nzb);
|
||||||
|
}
|
||||||
|
if (ca < cb) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
if (ca > cb) {
|
||||||
|
return +1;
|
||||||
|
}
|
||||||
|
|
||||||
|
++ia;
|
||||||
|
++ib;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static boolean isDigit(char c) {
|
||||||
|
return Character.isDigit(c) || c == '.' || c == ',';
|
||||||
|
}
|
||||||
|
|
||||||
|
private static char charAt(String s, int i) {
|
||||||
|
return i >= s.length() ? 0 : s.charAt(i);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static int compareEqual(String a, String b, int nza, int nzb) {
|
||||||
|
if (nza - nzb != 0) {
|
||||||
|
return nza - nzb;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (a.length() == b.length()) {
|
||||||
|
return a.compareTo(b);
|
||||||
|
}
|
||||||
|
|
||||||
|
return a.length() - b.length();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user