Compare commits

...

156 Commits
2.6 ... 3.2.6

Author SHA1 Message Date
zhaojun
082bb07213 fix: 修复 webdav 账号密码错误问题 2022-06-16 20:11:54 +08:00
zhaojun
803b8cdf71 🔖 发布 3.2.5 版本 2022-05-16 21:41:59 +08:00
zhaojun
eca5f7e48b feature: 新增挂载 webdav client pom 依赖 2022-05-16 21:41:36 +08:00
zhaojun
b70c37f3f0 feature: 新增挂载 webdav 功能 2022-05-16 21:40:58 +08:00
zhaojun
5cb2844141 🐛 修复错误的 content_disposition 和 contentType 导致下载文件格式被浏览器识别错误 2022-05-16 20:50:49 +08:00
赵俊
4dd6cdb4b3 合并拉取请求 #348
fix: modify extract driveId regex in WebDavController
2022-05-03 21:16:40 +08:00
quericy
c6127c029f fix: modify extract driveId regex in WebDavController 2022-04-14 10:06:46 +08:00
zhaojun
e149039ecb 🔖 发布 3.2.3 版本 2022-04-13 00:03:49 +08:00
zhaojun
37688d83cf feature: 降低 webdav 日志级别 2022-04-13 00:03:28 +08:00
zhaojun
6692016642 feature: 新增 webdav 开关功能 2022-04-13 00:01:59 +08:00
zhaojun
3f41aeda9a fix(bug): 修复因字段名称修改升级后导致短链异常 bug 2022-04-12 23:38:49 +08:00
赵俊
de86d5c47d 合并拉取请求 #347
feat: support webdav
2022-04-11 09:56:11 +08:00
quericy
c89e072005 feat: support webdav
1, feat:config webDav and milton in configuration;
2, feat:add event handler in WebDavController;
3, feat:check auth by admin in systemConfig;
4, feat:download webDav file by redirect to site DirectLink;
2022-04-10 04:11:34 +08:00
zhaojun
b533b5e959 🔖 发布 3.2.2 版本 2022-04-02 18:50:50 +08:00
zhaojun
d35cf27d47 fix(bug): ftp 默认为被动模式,防止因端口不同,无法读取。 2022-04-02 18:50:26 +08:00
zhaojun
796c4c1fb0 页面更新 2022-04-02 18:49:26 +08:00
zhaojun
1033d6c1a9 fix(bug): 修复某些情况下直链无法正常使用的 BUG 2022-04-02 18:36:44 +08:00
zhaojun
e09c6b4e58 fix(bug): 修复又拍云文件中包含特殊字符时,调用 upyun 接口未进行 url encode 返回 400,影响又拍云单文件直链无法使用。 2022-04-02 18:26:32 +08:00
zhaojun
168b0b08f3 fix(bug): 修复本地存储可以通过特殊路径符获取任意目录的 BUG. 2022-04-02 18:24:49 +08:00
zhaojun
60a6a5348c fix(bug): 修复获取音频信息时,因文件链接 302, 导致无法正常获取音频文件信息的 BUG. 2022-04-02 18:24:00 +08:00
zhaojun
2ec8a5df1f fix(bug): 修复 sharepoint 未自动刷新 accessToken 和 refreshToken 的 bug 2022-02-02 20:50:13 +08:00
zhaojun
47b5f6ac12 🔖 发布 3.2.1 版本 2022-02-02 20:46:45 +08:00
zhaojun
c64c8465f2 fix(bug): 升级相关有漏洞的依赖版本为最新版 2022-02-02 20:46:08 +08:00
zhaojun
f636681dd8 fix(bug): 修复本地存储可通过特殊命令访问到非指定目录的文件 2022-02-02 20:45:05 +08:00
赵俊
eadd2434e0 新增 ZFILE 社区地址 2021-10-30 21:05:11 +08:00
赵俊
b84c0bff42 Merge remote-tracking branch 'origin/master' 2021-10-07 17:53:19 +08:00
赵俊
4cb5b84bfe 🐛 修复 S3 协议无法正常保存的 BUG 2021-10-07 17:52:45 +08:00
赵俊
0351b4401c Merge pull request #286 from pippen57/master
转义SQL关键字, 防止创建表时出现错误
2021-09-25 18:29:42 +08:00
pippen
48cb14be8a 转义SQL关键字, 防止创建表时出现错误 2021-09-25 12:48:31 +08:00
赵俊
eea2ff11f9 ✏️ 修改文档拼写错误 2021-09-21 11:22:00 +08:00
赵俊
325ec1a348 对于因前端 vue history 路由模式下, 直接访问路径时, 出现的 404 响应码, 转化为 200 响应码. (404 下会将请求转发到首页) 2021-09-20 17:06:27 +08:00
赵俊
93205266d3 更新文档 2021-09-20 16:35:30 +08:00
赵俊
6849a4347f 去除自定义 404 错误页面 2021-09-20 16:33:54 +08:00
赵俊
cb5c6a5945 🎨 提交前端页面 2021-09-20 16:33:31 +08:00
赵俊
5ed45c3bb3 🔒 修复因升级 springboot 版本导致的安全和兼容问题 2021-09-20 16:20:25 +08:00
赵俊
4442ec3165 修复因前端 vue history 路由模式下, 直接访问路径时, 请求 /admin/ 下的路由会被权限框架拦截并校验登陆状态, 导致静态页面报错问题. 2021-09-20 16:19:21 +08:00
赵俊
b268a24333 修复因前端 vue history 路由模式下, 直接访问路径时, 接口和路径名冲突问题 2021-09-20 16:17:59 +08:00
赵俊
84f9354d4e 对于因前端 vue history 路由模式下, 直接访问路径时, 出现的 404 响应码, 转化为 200 响应码. (404 下会将请求转发到首页) 2021-09-20 16:17:16 +08:00
赵俊
8dfc4f8004 跨域请求过滤器修改为自注解注册方式 2021-09-20 16:14:45 +08:00
赵俊
1158f5c2b9 🐛 修复 OneDrive 加速域名在直链下未生效的 BUG 2021-09-20 11:18:13 +08:00
赵俊
c5f0e15207 🐛 修复因升级 spring boot 后部分参数过时, 导致的第一次启动无法初始化参数的 BUG 2021-09-19 21:33:07 +08:00
赵俊
68a842ce75 🐛 修复页面路径和接口地址冲突的 BUG 2021-09-19 16:08:19 +08:00
赵俊
3bd4f74dae 🔖 发布 3.2 版本 2021-09-19 15:37:40 +08:00
赵俊
4a0bdc3baf 💬 修改文档和启动日志描述,取消 /#/ 提示 2021-09-19 15:37:10 +08:00
赵俊
603c1b4654 🎨 提交前端页面 2021-09-19 15:36:19 +08:00
赵俊
1136d582df 增加异常处理器,细化异常提示 2021-09-19 14:10:09 +08:00
赵俊
187544fc06 当密码文件无法正常打开时,给予友好提示 2021-09-19 14:09:52 +08:00
赵俊
aa3cde8f59 minio 和 s3 存储引擎支持自定义 region 功能 2021-09-19 11:30:05 +08:00
赵俊
e29a702b6e ✏️ 拼写错误修改 2021-09-19 11:28:40 +08:00
赵俊
e4f663c9f0 🔒 新增自动检测驱动器已存储参数和驱动器支持所有参数比对功能, 防止驱动器新增参数后, 系统已存在的驱动器因 NPE 问题无法正常加载的问题. 2021-09-19 11:27:57 +08:00
赵俊
fb08ef6e78 🐛 如果请求的密码文件链接是 http 的, 且会自动跳转到 https 时, 密码文件无法正常加载的 BUG (原因是 restTemplate http -> https 不会自动重定向) 2021-09-19 10:01:24 +08:00
赵俊
10c465d159 ♻️ 修改 Spring Security 为 Sa-Token 框架. 2021-09-19 09:57:09 +08:00
赵俊
d15d1203b7 跨域配置 2021-09-19 09:56:12 +08:00
赵俊
de48ed3b61 兼容前端 history 模式, 对于 404 请求, 指向到 /index.html 页面 2021-09-19 09:54:23 +08:00
赵俊
d22e2e872a ⬆️ 因旧版本 Spring 的安全问题, 升级 Spring 为 2.5.4 版本, 兼容新版本 2021-09-19 09:53:19 +08:00
赵俊
7da1b454dc 🐛 修复非个人版的 OneDrive CF 加速域名无法正常使用的 BUG 2021-09-18 22:22:50 +08:00
赵俊
463f311dd3 增加本地文件下载/预览行为控制 2021-07-13 21:43:25 +08:00
赵俊
73b42cf654 🔖 发布 3.1 版本 2021-05-30 17:43:37 +08:00
赵俊
1fac59c4cd 🎨 更新静态页面 2021-05-30 17:41:46 +08:00
赵俊
6ed6b4a019 驱动器添加时,默认 ID 修改为 1 2021-05-30 17:40:48 +08:00
赵俊
7bd02437f0 数据库初始化参数增加默认值 2021-05-30 17:40:14 +08:00
赵俊
57aeb5771c 驱动器列表中返回是否已初始化信息 2021-05-30 17:39:40 +08:00
赵俊
695c03a530 Merge remote-tracking branch 'origin/master' 2021-05-30 16:24:58 +08:00
赵俊
6ebc403572 🔧 添加 ZFile Banner 文件 2021-05-30 16:24:28 +08:00
赵俊
7ff6fe43b5 新增自定义直链名称功能功能 2021-05-30 16:23:41 +08:00
赵俊
b34f141181 🔥 移除无用代码 2021-05-30 16:21:11 +08:00
赵俊
87dd7b58d1 🔥 移除无用代码 2021-05-30 16:19:11 +08:00
赵俊
2a949db5d2 m3u8 响应头兼容部分浏览器直接解析 m3u8 文件 2021-05-30 15:53:55 +08:00
赵俊
b889e91fb5 📝 更新文档,更新赞助信息位置 2021-05-14 21:15:54 +08:00
赵俊
b5c757f9f0 📝 更新文档,更新赞助信息位置 2021-04-21 21:04:02 +08:00
赵俊
a465f48b94 📝 更新文档,增加项目 LOGO,修改项目描述信息。 2021-04-21 20:23:54 +08:00
赵俊
797a0a0e4c 新增直链和短链是否显示功能 2021-04-21 20:21:47 +08:00
赵俊
e7ff159b6d 🐛 修复某些下载地址带密钥的存储策略,m3u8 视频无法正常播放的功能 2021-04-11 19:15:50 +08:00
赵俊
a9fbf54bb2 🔊 完善日志输出 2021-04-11 16:02:49 +08:00
赵俊
81f9e262f5 增加获取站点域名方法 2021-04-11 16:00:01 +08:00
赵俊
23bb3960ab 已关闭的驱动器不允许下载 2021-04-11 15:56:08 +08:00
赵俊
c4a17985a4 优化下载地址获取逻辑,直接列表时不直接调用 API 获取下载地址,访问单文件时再调用。 2021-04-11 15:49:33 +08:00
赵俊
75ddcd47f4 :zip: 优化存储器保存逻辑,防止新增加的字段无法正常保存的 BUG。 2021-04-10 20:21:19 +08:00
赵俊
2dd03ae490 🐛 修复 FTP 模式在 Linux 环境下无法正常读取的 BUG 2021-04-10 20:20:03 +08:00
赵俊
5b383c8741 🐛 日志文件无法正常下载功能修复 2021-04-10 20:19:26 +08:00
赵俊
73198d7852 优化本地文件下载功能, 支持断点续传和多线程下载 2021-04-10 18:06:10 +08:00
赵俊
fb0d9721aa OneDrive 和 SharePoint 反代功能 2021-04-10 15:38:16 +08:00
赵俊
b24c663fd6 更新 star 趋势链接 2021-03-27 12:01:37 +08:00
zhaojun1998
6e62cfc84d 📝 更新文档,修改赞助二维码,增加服务器赞助商链接 2021-03-27 11:52:59 +08:00
zhaojun1998
eee22e9dc9 📝 更新文档 2021-03-26 18:24:02 +08:00
zhaojun1998
5109c51ffc 🔖 发布 3.0 版 2021-03-10 21:43:14 +08:00
zhaojun1998
66d6d311ea 🎨 更新静态页面 2021-03-10 21:42:57 +08:00
zhaojun1998
ef7cbdcbd7 🐛 修复旧版本时,直链多次创建后,根据条件查询时数据不唯一的 BUG 2021-03-10 21:42:36 +08:00
zhaojun1998
63bcbebb4b OneDrive 回调地址增加旧版本兼容 2021-03-10 21:41:42 +08:00
zhaojun1998
50ce1bb6db 修改直链 Key 时检测是否重复功能 2021-03-10 21:41:14 +08:00
zhaojun1998
3c88659679 🎨 更新静态页面 2021-03-09 21:14:42 +08:00
zhaojun1998
d79a993eea 新增设置默认打开图片模式功能 2021-03-09 21:14:21 +08:00
zhaojun1998
afafb311b8 合并文件列表和参数信息接口 2021-03-09 21:13:48 +08:00
zhaojun1998
2e280e4931 新增批量删除直链功能,修改直链 Key 功能 2021-03-08 21:12:42 +08:00
zhaojun1998
ed6efac8b7 🐛 修复切换存储策略时,存储参数值匹配错误的 BUG 2021-03-05 23:00:15 +08:00
zhaojun1998
7409df85d7 🐛 修复切换存储器 ID 时,为级联修改或清理缓存、过滤器、短链的问题 2021-03-05 22:59:08 +08:00
zhaojun1998
4d42529c4d 短链生成逻辑优化, 同链接不重复生成 2021-03-05 22:24:28 +08:00
zhaojun1998
65224685c8 短链生成逻辑优化, 同链接不重复生成 2021-03-05 22:23:41 +08:00
zhaojun1998
080a84986e 💩 优化代码, 添加注释, 优化方法命名, 2021-02-24 18:42:17 +08:00
zhaojun1998
3f8beb2f0b debug 模式优化, 增加到系统参数接口中 2021-02-24 18:40:10 +08:00
zhaojun1998
410a87c426 debug 模式优化, 增加到系统参数接口中 2021-02-24 18:39:47 +08:00
zhaojun1998
c14b8343d2 文件解析异常提示信息优化 2021-02-08 17:58:37 +08:00
zhaojun1998
bea440f6c3 文件解析异常提示信息优化 2021-02-08 17:50:25 +08:00
zhaojun1998
6a5fe15121 密码错误提示信息优化 2021-02-08 17:46:29 +08:00
zhaojun1998
e920ab0ec0 🐛 修复因缓存功能导致密码文件失效的 BUG 2021-02-08 17:46:04 +08:00
zhaojun1998
537e3e0563 🐛 修复 S3 协议新增时的 BUG 2021-02-03 22:01:56 +08:00
赵俊
e208dc7c4c Merge pull request #169 from tianyanli/master
修复S3协议没有指定region问题
2021-02-03 21:56:40 +08:00
zhaojun1998
ed64910a53 🔇 无效的驱动器不输出异常信息,仅返回给页面错误消息 2021-02-03 21:06:38 +08:00
zhaojun1998
5b075c3505 完善 SharePoint 支持 2021-02-03 21:05:46 +08:00
zhaojun1998
a8e6d9af6a 🐛 修复某些存储引擎 API 中文件夹不返回时间和大小时,排序的 NPE 问题。 2021-02-03 21:04:28 +08:00
zhaojun1998
2b21d8a73c 完善 SharePoint 自助获取 SiteId 的逻辑 2021-02-03 21:03:28 +08:00
zhaojun1998
e30289d21b 🔖 发布 2.9.0 版 2021-02-01 23:15:29 +08:00
zhaojun1998
3b6e2be7fe 新增 debug 模式, 可访问数据库控制台和重置密码 2021-02-01 23:14:29 +08:00
zhaojun1998
43c12aa8a7 🎨 更新静态页面 2021-02-01 23:13:29 +08:00
zhaojun1998
c03a7710c0 🎨 更新静态页面 2021-02-01 23:13:18 +08:00
zhaojun1998
1833b23d84 新增 sharepoint 及自助获取 siteId 功能 2021-02-01 23:11:49 +08:00
zhaojun1998
f3e393972d Merge remote-tracking branch 'origin/master' 2021-01-23 23:33:21 +08:00
zhaojun1998
4f46c13369 完善异常处理机制,新增异常类 2021-01-23 13:48:04 +08:00
zhaojun1998
f181959218 支持 SharePoint 2021-01-23 13:46:31 +08:00
zhaojun1998
11effc0ae7 支持自定义 client_id 和 client_secret 2021-01-23 13:45:26 +08:00
zhaojun1998
c8397e17bf 💩 优化代码, 抽取公共方法 2021-01-23 13:44:33 +08:00
zhaojun1998
ed32b9f1d4 限制直链不允许下载密码文件 2021-01-23 13:42:53 +08:00
zhaojun1998
4e184936db 💩 优化代码, 更新注释格式, 去除无用 import 2021-01-23 13:42:14 +08:00
zhaojun1998
fe6aebfdee 自带直链功能 2021-01-23 13:40:16 +08:00
zhaojun1998
d65e1a442d 🔥 移除系统检测功能 2021-01-23 13:36:56 +08:00
zhaojun1998
34647793c8 支持修改驱动器 ID 2021-01-23 13:35:52 +08:00
zhaojun1998
e8c249b9ea 🔥 移除系统检测功能 2021-01-23 13:22:41 +08:00
yanli
d1e613dc10 Update S3ServiceImpl.java 2020-12-17 17:26:55 +08:00
赵俊
1adcfee96f Update README.md 2020-10-07 17:19:26 +08:00
zhaojun1998
75f5de6b9a 🔖 发布 2.8.1 版 2020-08-18 20:43:07 +08:00
zhaojun1998
499942ef70 🎨 更新静态页面 2020-08-18 20:43:02 +08:00
zhaojun1998
e11277ce26 🔖 发布 2.8 版 2020-08-17 21:04:40 +08:00
zhaojun1998
5edd9e38a7 🎨 更新静态页面 2020-08-17 21:04:16 +08:00
zhaojun1998
f4ffee706b 驱动器无效时, 访问系统设置, 给予异常提示 2020-08-16 09:23:07 +08:00
zhaojun1998
bb65750278 查询数据库为空判断 2020-08-16 09:20:21 +08:00
zhaojun1998
e09167c0d0 拖动排序功能 2020-08-15 21:18:38 +08:00
zhaojun1998
ee6c04fa11 🐛 修复 ftp 模式, 依旧获取尝试获取 readme 内容的 BUG. 2020-08-15 17:48:51 +08:00
zhaojun1998
b31982b788 增加文件过滤功能, 同一存储器支持多条规则, 支持通配符. 2020-08-15 17:48:07 +08:00
zhaojun1998
544a3d3eb2 更新静态页面 2020-08-07 21:33:36 +08:00
zhaojun1998
1987bc97a9 🎨 改进 README 文件不存在时, 异常处理机制. 2020-06-27 21:13:24 +08:00
zhaojun1998
7e878af06c 驱动器增加 "启用/禁用" 和 "排序" 功能. 2020-06-27 21:12:54 +08:00
zhaojun1998
766a047ee1 🔊 优化日志输出, 完善日志信息, 便于调试. 2020-06-26 21:35:08 +08:00
zhaojun1998
c1d29a46f5 🎨 代码规范调整, 移除无效引用, 增加注释. 2020-06-26 16:26:09 +08:00
zhaojun1998
08e39b3d15 ✏️ 修复拼写错误 2020-06-26 12:36:35 +08:00
zhaojun1998
e7790ac256 🎨 改进异常处理机制, 给予更详细的提示信息, 便于排查问题. 2020-06-26 12:35:34 +08:00
zhaojun1998
499f3e108c 🔧 日志输出添加代码行号显示, 便于调试. 2020-06-26 12:20:26 +08:00
zhaojun1998
19144b653e 📝 更新 API 文档 2020-06-25 17:54:05 +08:00
zhaojun1998
17a87648fa 🐛 修复本地存储, 填写 Windows 盘符, 无法正常识别的 BUG 2020-06-25 17:52:33 +08:00
zhaojun1998
ac4cef0980 配置文件新增配置项, 以自定义 '日志文件', '数据文件', '临时文件' 的路径 2020-06-25 17:51:35 +08:00
zhaojun1998
71978f8003 🎨 移除未用到的代码 2020-06-25 17:49:58 +08:00
zhaojun1998
0b3a67ec6e 🎨 优化代码结构, 拆分代码 2020-06-25 17:48:38 +08:00
zhaojun1998
ee5fb54ebb 🎨 优化代码结构, 调整包名, 方法名. 2020-06-25 17:05:59 +08:00
zhaojun1998
4585f22817 🔖 发布 2.7 版 2020-05-24 15:46:10 +08:00
zhaojun1998
b523453588 💄 更新页面 2020-05-24 15:45:38 +08:00
zhaojun1998
a8cc03c911 🔨 优化代码 2020-05-24 15:45:25 +08:00
zhaojun1998
949c437653 新增缓存管理功能, 支持手动/自动刷新缓存, 查看、清理缓存。 2020-05-24 15:41:33 +08:00
189 changed files with 4891 additions and 2353 deletions

View File

@@ -16,7 +16,7 @@ assignees: ''
请确认你已经做了下面这些事情,若 bug 还是未解决,那么请尽可详细地描述你的问题。
- 我已经安装了最新版的 ZFile
- 我已经阅读了 ZFile 的文档http://docs.zhaojun.im/zfile
- 我已经阅读了 ZFile 的文档https://docs.zfile.vip
- 我已经搜索了已有的 Issues 列表中有关的信息
- 我已经清理过浏览器缓存并重试
-->

115
API.md
View File

@@ -10,22 +10,14 @@
`code == 0` 时, `data` 中为请求所需数据.
`code != 0` 时, 应当将 `msg` 中的属性作为参考值.
`code != 0` 时, 应当将 `msg` 中的内容作为参考值.
## 获取文件列表
## 驱动器列表
### 请求 URL
`/api/list` `GET`
### 参数
| 参数名 | 描述 | 是否必填 | 参考值 |
| :------: | :--------: | :------: | :--------------------------: |
| path | 路径 | 是 | `/`, `/文件夹名称` |
| password | 文件夹密码 | 否 | 当文件夹需要密码时, |
| page | 页数 | 否 | 默认取第一页, 每页固定 30 条 |
`/api/drive/list` `GET`
### 响应
@@ -35,41 +27,52 @@
"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"
"id": 3, --- ID 是驱动器 ID, 用来唯一区分驱动器
"name": "演示 A 盘", --- 驱动器名称
"enableCache": true, --- 是否开启了缓存
"autoRefreshCache": false, --- 是否开启了缓存自动刷新
"type": { --- 存储源类型
"key": "upyun",
"description": "又拍云 USS"
},
"searchEnable": false, --- 是否开启搜索
"searchIgnoreCase": false, --- 搜索是否忽略大小写
"searchContainEncryptedFile": false --- 搜索是否包含加密文件夹
}
]
}
```
## 搜索
## 获取文件列表
### 请求 URL
`/api/search` `GET`
`/api/list/{driveId}` `GET`
### 参数
| 参数名 | 描述 | 是否必填 | 参考值 |
| :----: | :----: | :------: | :--------------------------: |
| name | 搜索值 | 是 | 模糊匹配 |
| page | 页数 | | 默认取第一页, 每页固定 30 条 |
### URL 参数
| 参数名 | 描述 | 是否必填 | 参考值 |
| :-----: | :-------------------: | :------: | :------------------------------------: |
| driveId | 驱动器 ID | 是 | 参考 `获取驱动器列表` 接口返回的 id 值 |
### 请求参数
| 参数名 | 描述 | 是否必填 | 参考值 |
| :------: | :--------: | :------: | :--------------------------: |
| path | 路径 | 是 | `/`, `/文件夹名称` |
| password | 文件夹密码 | 否 | 当文件夹需要密码时, |
| page | 页数 | 否 | 默认取第一页, 每页固定 30 条 |
### 响应
```json
{
"msg": "操作成功",
@@ -100,7 +103,14 @@
### 请求 URL
`/api/directlink` `GET`
`/api/directlink/{driveId}` `GET`
### URL 参数
| 参数名 | 描述 | 是否必填 | 参考值 |
| :-----: | :-------------------: | :------: | :------------------------------------: |
| driveId | 驱动器 ID | 是 | 参考 `获取驱动器列表` 接口返回的 id 值 |
### 参数
@@ -130,7 +140,15 @@
### 请求 URL
`/api/config` `GET`
`/api/config/{driveId}` `GET`
### URL 参数
| 参数名 | 描述 | 是否必填 | 参考值 |
| :-----: | :-------------------: | :------: | :------------------------------------: |
| driveId | 驱动器 ID | 是 | 参考 `获取驱动器列表` 接口返回的 id 值 |
### 参数
@@ -145,20 +163,19 @@
"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 片段
}
"siteName": "ZFile 演示站",
"searchEnable": false,
"username": "zhao",
"domain": "https://zfile.jun6.net",
"customJs": "",
"customCss": "",
"tableSize": "small",
"showOperator": true,
"showDocument": true,
"announcement": "本站是 ZFile 演示站,交流反馈群 180605017",
"showAnnouncement": true,
"layout": "full",
"readme": null
}
}
```
```

125
README.md
View File

@@ -1,32 +1,49 @@
# Z-File
<p align = "center">
<img alt="ZFile" src="https://cdn.jun6.net/2021/04/21/69a89344e2a84.png" height="150px">
<br><br>
基于 Java 的在线网盘程序,支持对接 S3、OneDrive、SharePoint、又拍云、本地存储、FTP 等存储源,支持在线浏览图片、播放音视频,文本文件等文件类型。
<br><br>
<img src="https://img.shields.io/badge/license-MIT-blue.svg?longCache=true&style=flat-square">
<img src="https://api.codacy.com/project/badge/Grade/70b793267f7941d58cbd93f50c9a8e0a">
<img src="https://img.shields.io/github/last-commit/zhaojun1998/zfile.svg?style=flat-square">
<img src="https://img.shields.io/github/downloads/zhaojun1998/zfile/total?style=flat-square">
<img src="https://img.shields.io/github/v/release/zhaojun1998/zfile?style=flat-square">
<img src="https://img.shields.io/github/commit-activity/y/zhaojun1998/zfile?style=flat-square">
<br>
<img src="https://img.shields.io/github/issues/zhaojun1998/zfile?style=flat-square">
<img src="https://img.shields.io/github/issues-closed-raw/zhaojun1998/zfile?style=flat-square">
<img src="https://img.shields.io/github/forks/zhaojun1998/zfile?style=flat-square">
<img src="https://img.shields.io/github/stars/zhaojun1998/zfile?style=flat-square">
<img src="https://img.shields.io/github/watchers/zhaojun1998/zfile?style=flat-square">
</p>
![https://img.shields.io/badge/license-MIT-blue.svg?style=flat-square](https://img.shields.io/badge/license-MIT-blue.svg?longCache=true&style=flat-square)
[![Codacy Badge](https://api.codacy.com/project/badge/Grade/70b793267f7941d58cbd93f50c9a8e0a)](https://www.codacy.com/manual/zhaojun1998/zfile?utm_source=github.com&amp;utm_medium=referral&amp;utm_content=zhaojun1998/zfile&amp;utm_campaign=Badge_Grade)
![https://img.shields.io/badge/springboot-2.0.6-orange.svg?style=flat-square](https://img.shields.io/badge/springboot-2.0.6-yellow.svg?longCache=true&style=popout-square)
![GitHub tag (latest SemVer)](https://img.shields.io/github/tag/zhaojun1998/zfile.svg?style=flat-square)
## 相关地址
此项目是一个在线文件目录的程序, 支持各种对象存储和本地存储, 使用定位是个人放常用工具下载, 或做公共的文件库. 不会向多账户方向开发.
预览地址: [https://zfile.vip](https://zfile.vip)
前端基于 [h5ai](https://larsjung.de/h5ai/) 的原有功能使用 Vue 重新开发了一遍. 后端采用 SpringBoot, 数据库采用内嵌数据库.
文档地址: [https://docs.zfile.vip](https://docs.zfile.vip)
预览地址: [https://zfile.jun6.net](https://zfile.jun6.net)
社区地址: [https://bbs.zfile.vip](https://bbs.zfile.vip)
文档地址: [http://docs.zhaojun.im/zfile](http://docs.zhaojun.im/zfile)
项目源码: [https://github.com/zhaojun1998/zfile](https://github.com/zhaojun1998/zfile)
前端源码: [https://github.com/zhaojun1998/zfile-vue](https://github.com/zhaojun1998/zfile-vue)
## 系统特色
* 内存缓存 (免安装)
* 内存数据库 (免安装)
* 个性化配置
* 自定义目录的 readme 说明文件
* 自定义 JS, CSS
* 文件夹密码
* 目录 README 说明
* 文件直链(短链,永久直链,二维码)
* 支持在线浏览文本文件, 视频, 图片, 音乐. (支持 FLV 和 HLS)
* 文件/目录二维码
* 图片模式
* Docker 支持
* 隐藏指定文件夹(通配符支持)
* 自定义 JS, CSS
* 自定义目录 README 说明文件和密码文件名称
* 同时挂载多个存储策略
* 缓存动态开启, ~~缓存自动刷新 (v2.2 及以前版本支持)~~
* ~~全局搜索 (v2.2 及以前版本支持)~~
* 同时挂载多个存储策略
* 支持 阿里云 OSS, FTP, 华为云 OBS, 本地存储, MINIO, OneDrive 国际/家庭/个人版, OneDrive 世纪互联版, 七牛云 KODO, 腾讯云 COS, 又拍云 USS.
* 支持 S3 协议, 阿里云 OSS, FTP, 华为云 OBS, 本地存储, MINIO, OneDrive 国际/家庭/个人版/世纪互联版/SharePoint, , 七牛云 KODO, 腾讯云 COS, 又拍云 USS.
## 快速开始
@@ -54,14 +71,14 @@ apt update && apt install -y adoptopenjdk-8-hotspot-jre
> 如为更新程序, 则请先执行 `~/zfile/bin/stop.sh && rm -rf ~/zfile` 清理旧程序. 首次安装请忽略此选项.
下载项目:
```bash
cd ~
export ZFILE_INSTALL_PATH=~/zfile
mkdir -p $ZFILE_INSTALL_PATH && cd $ZFILE_INSTALL_PATH
wget https://c.jun6.net/ZFILE/zfile-release.war
mkdir zfile && unzip zfile-release.war -d zfile && rm -rf zfile-release.war
chmod +x zfile/bin/*.sh
unzip zfile-release.war && rm -rf zfile-release.war
chmod +x $ZFILE_INSTALL_PATH/bin/*.sh
```
> 下载指定版本可以将 `zfile-release.war` 改为 `zfile-x.x.war`,如 `zfile-2.2.war`。
@@ -83,62 +100,38 @@ chmod +x zfile/bin/*.sh
~/zfile/bin/start.sh
```
篇幅有限, 更详细的安装教程及介绍请参考: [ZFile 文档](http://zhaojun.im/zfile-install)
篇幅有限, 更详细的安装教程及介绍请参考: [ZFile 文档](https://docs.zfile.vip)
访问地址:
用户前台: http://127.0.0.1:8080/#/main
用户前台: http://127.0.0.1:8080/main
初始安装: http://127.0.0.1:8080/#/install
初始安装: http://127.0.0.1:8080/install
管理后台: http://127.0.0.1:8080/#/admin
管理后台: http://127.0.0.1:8080/admin
## 预览
![前台首页](https://cdn.jun6.net/2020/04/19/d590d2bde13bb.png)
![后台设置-驱动器设置](https://cdn.jun6.net/2020/04/19/d58fc2debcce8.png)
![后台设置-驱动器设置](https://cdn.jun6.net/2020/04/19/0f321e47fc18c.png)
![后台设置-显示设置](https://cdn.jun6.net/2020/04/19/6d7c300b89671.png)
## 常见问题
### 默认路径
默认 H2 数据库文件地址: `~/.zfile/db/`, `~` 表示用户目录
windows 为 `C:/Users/用户名/`
linux 为 `/home/用户名/`, root 用户为 `/root/`
> 2.3 及以后版本路径为 `~/.zfile-new/db/`
### 文档文件和加密文件
- 目录文档显示文件名为 `readme.md`
- 目录需要密码访问, 添加文件 `password.txt` (无法拦截此文件被下载, 但可以改名文件)
## 开发计划
- [x] API 支持 [点击查看文档](https://github.com/zhaojun1998/zfile/blob/master/API.md)
- [x] 更方便的部署方式
- [x] 布局优化 - 自定义操作按钮 (现为右键实现)
- [x] 后台优化 - 设置按照其功能进行分离
- [x] 体验优化 - 支持前后端分离部署
- [x] 体验优化 - 文本预览更换 vscode 同款编辑器 monaco editor
- [x] 新功能 - Docker 支持
- [x] 架构调整 - 支持多存储策略
- [ ] 新功能 - 后台支持上传、编辑、删除等操作
- [ ] 新功能 - WebDav 支持
- [ ] 新功能 - 离线下载 (aria2)
- [ ] 体验优化 - 忽略文件列表 (正则表达式)
- [ ] 体验优化 - 自定义支持预览的文件后缀 (正则表达式)
- [ ] 体验优化 - 一键安装脚本
![前台首页](https://cdn.jun6.net/2021/03/23/c1f4631ee2de4.png)
![图片预览](https://cdn.jun6.net/2021/03/23/713741d43b939.png)
![视频预览](https://cdn.jun6.net/2021/03/23/9c724383bb506.png)
![文本预览](https://cdn.jun6.net/2021/03/23/b00efdfb4892e.png)
![音频预览](https://cdn.jun6.net/2021/03/23/d15b14378d3f0.png)
![后台设置-驱动器列表](https://cdn.jun6.net/2021/03/23/b4f76f20ea73a.png)
![后台设置-新增驱动器](https://cdn.jun6.net/2021/03/23/e70e04f8cc5b6.png)
![后台设置-站点设置](https://cdn.jun6.net/2021/03/23/fd946991bb6b9.png)
## 支持作者
如果本项目对你有帮助,请作者喝杯咖啡吧。
<img src="http://cdn.jun6.net/alipay.png" width="200" height="312">
<img src="http://cdn.jun6.net/wechat.png" width="222" height="300">
<img src="https://cdn.jun6.net/2021/03/27/152704e91f13d.png" width="400" alt="赞助我">
## Stargazers over time
[![starcharts stargazers over time](https://starchart.cc/zhaojun1998/zfile.svg)](https://starchart.cc/zhaojun1998/zfile.svg)
## 开发工具赞助
<a href="https://www.jetbrains.com/?from=zfile"><img src="https://cdn.jun6.net/2021/04/21/26e410d60b0b0.png?1=1" width="100px"></a>

49
pom.xml
View File

@@ -6,19 +6,20 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.6.RELEASE</version>
<version>2.5.4</version>
<relativePath/>
</parent>
<groupId>im.zhaojun</groupId>
<artifactId>zfile</artifactId>
<version>2.6</version>
<version>3.2.5</version>
<name>zfile</name>
<packaging>war</packaging>
<description>一个在线的文件浏览系统</description>
<properties>
<java.version>1.8</java.version>
<log4j2.version>2.17.1</log4j2.version>
</properties>
<dependencies>
@@ -32,10 +33,6 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
@@ -60,11 +57,13 @@
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
<version>1.4.197</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
<version>8.0.27</version>
</dependency>
<!-- 工具类 -->
@@ -88,7 +87,6 @@
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.8</version>
</dependency>
<dependency>
<groupId>commons-net</groupId>
@@ -96,6 +94,31 @@
<version>3.6</version>
</dependency>
<!-- WebDav -->
<dependency>
<groupId>io.milton</groupId>
<artifactId>milton-server-ce</artifactId>
<version>3.1.1.413</version>
<exclusions>
<exclusion>
<artifactId>commons-logging</artifactId>
<groupId>commons-logging</groupId>
</exclusion>
<exclusion>
<artifactId>json</artifactId>
<groupId>org.json</groupId>
</exclusion>
</exclusions>
</dependency>
<!-- webdav client -->
<dependency>
<groupId>com.github.lookfirst</groupId>
<artifactId>sardine</artifactId>
<version>5.10</version>
</dependency>
<!-- 其他工具类 -->
<dependency>
<groupId>org.projectlombok</groupId>
@@ -112,7 +135,15 @@
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.61</version>
<version>1.2.69</version>
</dependency>
<!-- Sa-Token 权限认证, 在线文档http://sa-token.dev33.cn/ -->
<dependency>
<groupId>cn.dev33</groupId>
<artifactId>sa-token-spring-boot-starter</artifactId>
<version>1.26.0</version>
</dependency>
</dependencies>
@@ -145,4 +176,4 @@
</plugins>
</build>
</project>
</project>

View File

@@ -3,6 +3,7 @@ package im.zhaojun.zfile;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.scheduling.annotation.EnableAsync;
/**
@@ -10,6 +11,7 @@ import org.springframework.scheduling.annotation.EnableAsync;
*/
@EnableAsync
@SpringBootApplication
@EnableJpaRepositories("im.zhaojun.zfile.repository")
@EnableAspectJAutoProxy(exposeProxy = true)
public class ZfileApplication {

View File

@@ -11,6 +11,7 @@ import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
/**
@@ -19,7 +20,7 @@ import java.util.List;
*/
@Aspect
@Component
public class FileListCacheAop {
public class FileListCacheAspect {
@Resource
private ZFileCache zFileCache;
@@ -50,7 +51,7 @@ public class FileListCacheAop {
if (enableCache) {
List<FileItemDTO> cacheFileList = zFileCache.get(driveId, path);
if (cacheFileList == null) {
result = (List<FileItemDTO>) point.proceed();
result = Collections.unmodifiableList((List<FileItemDTO>) point.proceed());
zFileCache.put(driveId, path, result);
} else {
result = cacheFileList;

View File

@@ -0,0 +1,19 @@
package im.zhaojun.zfile.cache;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* @author zhaojun
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class DriveCacheKey {
private Integer driveId;
private String key;
}

View File

@@ -0,0 +1,55 @@
package im.zhaojun.zfile.cache;
import cn.hutool.cache.impl.CacheObj;
import cn.hutool.cache.impl.TimedCache;
import im.zhaojun.zfile.context.DriveContext;
import im.zhaojun.zfile.service.base.AbstractBaseFileService;
import im.zhaojun.zfile.util.SpringContextHolder;
import lombok.extern.slf4j.Slf4j;
import java.util.Map;
/**
* @author zhaojun
*/
@Slf4j
public class MyTimedCache<K, V> extends TimedCache<K, V> {
private DriveContext driveContext;
public MyTimedCache(long timeout) {
super(timeout);
}
public MyTimedCache(long timeout, Map<K, CacheObj<K, V>> map) {
super(timeout, map);
}
@Override
protected void onRemove(K key, V cachedObject) {
if (driveContext == null) {
driveContext = SpringContextHolder.getBean(DriveContext.class);
}
DriveCacheKey cacheKey = (DriveCacheKey) key;
AbstractBaseFileService baseFileService = driveContext.get(cacheKey.getDriveId());
if (log.isDebugEnabled()) {
log.debug("尝试刷新缓存: {}", cacheKey);
}
if (baseFileService == null) {
log.error("尝试刷新缓存: {}, 时出现异常, 驱动器已不存在", cacheKey);
return;
}
try {
baseFileService.fileList(cacheKey.getKey());
} catch (Exception e) {
log.error("尝试刷新缓存 {} 失败", cacheKey);
e.printStackTrace();
}
}
}

View File

@@ -1,14 +1,13 @@
package im.zhaojun.zfile.cache;
import cn.hutool.cache.CacheUtil;
import cn.hutool.cache.impl.CacheObj;
import cn.hutool.cache.impl.TimedCache;
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 im.zhaojun.zfile.model.entity.DriveConfig;
import im.zhaojun.zfile.repository.DriverConfigRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@@ -28,14 +27,26 @@ import java.util.concurrent.ConcurrentMap;
* @author zhaojun
*/
@Component
@Slf4j
public class ZFileCache {
@Resource
private DriverConfigRepository driverConfigRepository;
/**
* 缓存过期时间
*/
@Value("${zfile.cache.timeout}")
private long timeout;
/**
* 缓存自动刷新间隔
*/
@Value("${zfile.cache.auto-refresh.interval}")
private long autoRefreshInterval;
/**
* 缓存 map 对象.
* 文件/文件对象缓存.
*
* ConcurrentMap<Integer, ConcurrentHashMap<String, List<FileItemDTO>>>
* ConcurrentMap<driveId, ConcurrentHashMap<key, value>>
@@ -44,16 +55,13 @@ public class ZFileCache {
* key: 文件夹路径
* value: 文件夹中内容
*/
private ConcurrentMap<Integer, TimedCache<String, List<FileItemDTO>>> drivesCache = new ConcurrentHashMap<>();
private ConcurrentMap<Integer, MyTimedCache<DriveCacheKey, List<FileItemDTO>>> drivesCache = new ConcurrentHashMap<>();
/**
* 系统设置缓存
*/
private SystemConfigDTO systemConfigCache;
@Resource
private DriverConfigRepository driverConfigRepository;
/**
* 写入缓存
@@ -68,7 +76,7 @@ public class ZFileCache {
* 文件夹中列表
*/
public synchronized void put(Integer driveId, String key, List<FileItemDTO> value) {
getCacheByDriveId(driveId).put(key, value);
getCacheByDriveId(driveId).put(new DriveCacheKey(driveId, key), value);
}
@@ -84,7 +92,7 @@ public class ZFileCache {
* @return 驱动器中文件夹的内容
*/
public List<FileItemDTO> get(Integer driveId, String key) {
return getCacheByDriveId(driveId).get(key, false);
return getCacheByDriveId(driveId).get(new DriveCacheKey(driveId, key), false);
}
@@ -95,6 +103,9 @@ public class ZFileCache {
* 驱动器 ID
*/
public void clear(Integer driveId) {
if (log.isDebugEnabled()) {
log.debug("清空驱动器所有缓存, driveId: {}", driveId);
}
getCacheByDriveId(driveId).clear();
}
@@ -161,17 +172,17 @@ public class ZFileCache {
* @return 所有缓存 key
*/
public Set<String> keySet(Integer driveId) {
Iterator<CacheObj<String, List<FileItemDTO>>> cacheObjIterator = getCacheByDriveId(driveId).cacheObjIterator();
Iterator<CacheObj<DriveCacheKey, List<FileItemDTO>>> cacheObjIterator = getCacheByDriveId(driveId).cacheObjIterator();
Set<String> keys = new HashSet<>();
while (cacheObjIterator.hasNext()) {
keys.add(cacheObjIterator.next().getKey());
keys.add(cacheObjIterator.next().getKey().getKey());
}
return keys;
}
/**
* 从缓存中删除指定存储器的某个路径的缓存
* 从缓存中删除指定驱动器的某个路径的缓存
*
* @param driveId
* 驱动器 ID
@@ -180,7 +191,7 @@ public class ZFileCache {
* 文件夹路径
*/
public void remove(Integer driveId, String key) {
getCacheByDriveId(driveId).remove(key);
getCacheByDriveId(driveId).remove(new DriveCacheKey(driveId, key));
}
@@ -241,14 +252,80 @@ public class ZFileCache {
*
* @return 驱动器对应的缓存
*/
private synchronized TimedCache<String, List<FileItemDTO>> getCacheByDriveId(Integer driveId) {
TimedCache<String, List<FileItemDTO>> driveCache = drivesCache.get(driveId);
private synchronized MyTimedCache<DriveCacheKey, List<FileItemDTO>> getCacheByDriveId(Integer driveId) {
MyTimedCache<DriveCacheKey, List<FileItemDTO>> driveCache = drivesCache.get(driveId);
if (driveCache == null) {
driveCache = CacheUtil.newTimedCache(timeout * 1000);
driveCache = new MyTimedCache<>(timeout * 1000);
drivesCache.put(driveId, driveCache);
startAutoCacheRefresh(driveId);
}
return driveCache;
}
/**
* 获取指定驱动器的缓存命中数
*
* @param driveId
* 驱动器 ID
*
* @return 缓存命中数
*/
public int getHitCount(Integer driveId) {
return getCacheByDriveId(driveId).getHitCount();
}
/**
* 获取指定驱动器的缓存未命中数
*
* @param driveId
* 驱动器 ID
*
* @return 缓存未命中数
*/
public int getMissCount(Integer driveId) {
return getCacheByDriveId(driveId).getMissCount();
}
/**
* 开启缓存自动刷新
*
* @param driveId
* 驱动器 ID
*/
public void startAutoCacheRefresh(Integer driveId) {
if (log.isDebugEnabled()) {
log.debug("开启缓存自动刷新 driveId: {}", driveId);
}
DriveConfig driveConfig = driverConfigRepository.findById(driveId).get();
Boolean autoRefreshCache = driveConfig.getAutoRefreshCache();
if (autoRefreshCache != null && autoRefreshCache) {
MyTimedCache<DriveCacheKey, List<FileItemDTO>> driveCache = drivesCache.get(driveId);
if (driveCache == null) {
driveCache = new MyTimedCache<>(timeout * 1000);
drivesCache.put(driveId, driveCache);
}
driveCache.schedulePrune(autoRefreshInterval * 1000);
}
}
/**
* 停止缓存自动刷新
*
* @param driveId
* 驱动器 ID
*/
public void stopAutoCacheRefresh(Integer driveId) {
if (log.isDebugEnabled()) {
log.debug("停止缓存自动刷新 driveId: {}", driveId);
}
MyTimedCache<DriveCacheKey, List<FileItemDTO>> driveCache = drivesCache.get(driveId);
if (driveCache != null) {
driveCache.cancelPruneSchedule();
}
}
}

View File

@@ -0,0 +1,36 @@
package im.zhaojun.zfile.config;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Configuration;
/**
* 应用上下文配置
*
* @author me
* @date 2022/4/9
*/
@Configuration
public class ApplicationContextConfigure implements ApplicationContextAware {
private static ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
ApplicationContextConfigure.applicationContext = applicationContext;
}
/**
* bean名称获取对象
*/
public static Object getBean(String name) throws BeansException {
return applicationContext.getBean(name);
}
/**
* bean类型获取对象
*/
public static <T> T getBean(Class<T> clazz) throws BeansException {
return applicationContext.getBean(clazz);
}
}

View File

@@ -2,10 +2,7 @@ package im.zhaojun.zfile.config;
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.impl.OneDriveChinaServiceImpl;
import im.zhaojun.zfile.service.impl.OneDriveServiceImpl;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpHeaders;
@@ -14,7 +11,7 @@ import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
/**
* @author zhaojun
@@ -25,13 +22,6 @@ public class OneDriveConfig {
@Resource
private StorageConfigService storageConfigService;
@Resource
private OneDriveServiceImpl oneDriveServiceImpl;
@Resource
private OneDriveChinaServiceImpl oneDriveChinaServiceImpl;
/**
* OneDrive 请求 RestTemplate, 会在请求头中添加 Bearer: Authorization {token} 信息, 用于 API 认证.
*/
@@ -41,7 +31,7 @@ public class OneDriveConfig {
ClientHttpRequestInterceptor interceptor = (httpRequest, bytes, clientHttpRequestExecution) -> {
HttpHeaders headers = httpRequest.getHeaders();
Integer driveId = Integer.valueOf(((LinkedList)headers.get("driveId")).get(0).toString());
Integer driveId = Integer.valueOf(((List)headers.get("driveId")).get(0).toString());
StorageConfig accessTokenConfig =
storageConfigService.findByDriveIdAndKey(driveId, StorageConfigConstant.ACCESS_TOKEN_KEY);

View File

@@ -0,0 +1 @@
package im.zhaojun.zfile.config;

View File

@@ -2,12 +2,19 @@ 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.server.ConfigurableWebServerFactory;
import org.springframework.boot.web.server.ErrorPage;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
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.http.HttpStatus;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import java.util.HashSet;
import java.util.Set;
/**
* @author zhaojun
*/
@@ -31,4 +38,16 @@ public class WebMvcConfig implements WebMvcConfigurer {
return webServerFactory;
}
@Bean
public WebServerFactoryCustomizer<ConfigurableWebServerFactory> webServerFactoryCustomizer(){
return factory -> {
ErrorPage error404Page = new ErrorPage(HttpStatus.NOT_FOUND, "/index.html");
ErrorPage error200Page = new ErrorPage(HttpStatus.OK, "/index.html");
Set<ErrorPage> errorPages = new HashSet<>();
errorPages.add(error404Page);
errorPages.add(error200Page);
factory.setErrorPages(errorPages);
};
}
}

View File

@@ -1,9 +1,14 @@
package im.zhaojun.zfile.config;
import im.zhaojun.zfile.filter.CorsFilter;
import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpHeaders;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.client.RestTemplate;
@@ -18,7 +23,10 @@ public class ZFileConfiguration {
@Bean
public RestTemplate restTemplate(){
RestTemplate restTemplate = new RestTemplate();
HttpComponentsClientHttpRequestFactory httpRequestFactory = new HttpComponentsClientHttpRequestFactory();
HttpClient httpClient = HttpClientBuilder.create().build();
httpRequestFactory.setHttpClient(httpClient);
RestTemplate restTemplate = new RestTemplate(httpRequestFactory);
restTemplate.getMessageConverters().set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8));
restTemplate.setInterceptors(Collections.singletonList((request, body, execution) -> {
@@ -31,4 +39,4 @@ public class ZFileConfiguration {
return restTemplate;
}
}
}

View File

@@ -0,0 +1,60 @@
package im.zhaojun.zfile.config.webdav;
import im.zhaojun.zfile.config.webdav.adapter.WebDavUrlAdapterImpl;
import im.zhaojun.zfile.config.webdav.auth.SystemConfigSecurityManager;
import im.zhaojun.zfile.config.webdav.resolver.WebDavRedirectViewResolver;
import im.zhaojun.zfile.model.constant.ZFileConstant;
import im.zhaojun.zfile.model.dto.SystemConfigDTO;
import im.zhaojun.zfile.service.SystemConfigService;
import io.milton.http.ResourceFactory;
import io.milton.http.SecurityManager;
import io.milton.http.annotated.AnnotationResourceFactory;
import io.milton.http.fs.NullSecurityManager;
import io.milton.servlet.DefaultMiltonConfigurator;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.annotation.Configuration;
import javax.annotation.Resource;
/**
* Milton(webDav)配置
*
* @author me
* @date 2022/4/9
*/
@Configuration
public class MiltonConfiguration extends DefaultMiltonConfigurator implements InitializingBean {
/**
* 安全管理器
*/
private static SecurityManager securityManager = new NullSecurityManager();
@Resource
private SystemConfigService systemConfigService;
/**
* 构建milton初始化配置
*/
@Override
protected void build() {
builder.setSecurityManager(securityManager);
builder.setContextPath(ZFileConstant.WEB_DAV_PREFIX);
builder.setUrlAdapter(new WebDavUrlAdapterImpl());
final ResourceFactory resourceFactory = builder.getResourceFactory();
if (resourceFactory instanceof AnnotationResourceFactory) {
((AnnotationResourceFactory) resourceFactory).setViewResolver(new WebDavRedirectViewResolver());
}
super.build();
}
/**
* 属性初始化完成后,更新安全管理器,使用系统配置鉴权
*/
@Override
public void afterPropertiesSet() throws Exception {
final SystemConfigDTO systemConfig = systemConfigService.getSystemConfig();
if (systemConfig != null) {
securityManager = new SystemConfigSecurityManager(systemConfig);
}
}
}

View File

@@ -0,0 +1,33 @@
package im.zhaojun.zfile.config.webdav;
import im.zhaojun.zfile.model.constant.ZFileConstant;
import io.milton.http.annotated.AnnotationResourceFactory;
import io.milton.servlet.MiltonFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* WebDav配置
*
* @author me
* @date 2022/4/9
*/
@Configuration
public class WebDavConfiguration {
@Bean
public FilterRegistrationBean miltonFilter() {
FilterRegistrationBean registration = new FilterRegistrationBean();
registration.setFilter(new MiltonFilter());
registration.setName("miltonFilter");
registration.addUrlPatterns(ZFileConstant.WEB_DAV_PREFIX + "/*");
registration.addInitParameter("resource.factory.class", AnnotationResourceFactory.class.getName());
registration.addInitParameter("milton.configurator", MiltonConfiguration.class.getName());
registration.addInitParameter("controllerPackagesToScan", "im.zhaojun.zfile.controller.home");
registration.setOrder(1);
return registration;
}
}

View File

@@ -0,0 +1,41 @@
package im.zhaojun.zfile.config.webdav.adapter;
import im.zhaojun.zfile.model.constant.ZFileConstant;
import im.zhaojun.zfile.util.RegexMatchUtils;
import io.milton.http.HttpManager;
import io.milton.http.Request;
import io.milton.http.UrlAdapter;
import java.util.regex.Matcher;
/**
* WebDav路径适配器实现
*
* @author me
* @date 2022/4/10
*/
public class WebDavUrlAdapterImpl implements UrlAdapter {
/**
* 获取url
* eg: domain.com/{webdavPrefix}/{driveId}/{folders}
*
* @param request 请求
* @return {@link String}
*/
@Override
public String getUrl(Request request) {
// 匹配url前缀和驱动器ID
Matcher matcher = RegexMatchUtils.match("^" + ZFileConstant.WEB_DAV_PREFIX + "/(\\d+)(.*)",
HttpManager.decodeUrl(request.getAbsolutePath()));
final String driveId = RegexMatchUtils.getIndexResult(matcher, 1);
if (driveId == null) {
return "";
}
// 获取摘除前缀和驱动器ID后的文件路径
final String realPath = RegexMatchUtils.getIndexResult(matcher, 2);
return realPath != null ? realPath : "";
}
}

View File

@@ -0,0 +1,130 @@
package im.zhaojun.zfile.config.webdav.auth;
import cn.hutool.core.map.MapUtil;
import cn.hutool.crypto.SecureUtil;
import im.zhaojun.zfile.model.dto.SystemConfigDTO;
import io.milton.http.Auth;
import io.milton.http.Request;
import io.milton.http.Request.Method;
import io.milton.http.http11.auth.DigestResponse;
import io.milton.resource.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Map;
/**
* 基于当前系统配置的WebDav鉴权管理器
*
* @author me
* @date 2022/4/10
* @see io.milton.http.fs.SimpleSecurityManager
*/
public class SystemConfigSecurityManager implements io.milton.http.SecurityManager {
private static final Logger log = LoggerFactory.getLogger(SystemConfigSecurityManager.class);
private String realm = "SystemConfig";
private Map<String, String> nameAndPasswords;
/**
* 根据系统配置创建安全管理器
*
* @param systemConfig 系统配置DTO
*/
public SystemConfigSecurityManager(SystemConfigDTO systemConfig) {
if (systemConfig != null) {
this.nameAndPasswords = MapUtil.of(systemConfig.getUsername(), systemConfig.getPassword());
}
}
public Object getUserByName(String name) {
String actualPassword = nameAndPasswords.get(name);
if (actualPassword != null) {
return name;
}
return null;
}
/**
* 用户名+密码身份验证
*
* @param user 用户
* @param password 密码
* @return {@link Object}
*/
@Override
public Object authenticate(String user, String password) {
if (user.contains("@")) {
user = user.substring(0, user.indexOf("@"));
}
String actualPassword = nameAndPasswords.get(user);
if (actualPassword == null) {
log.debug("user not found: " + user);
return null;
} else {
//比对密码MD5摘要
return (actualPassword.equals(SecureUtil.md5(password))) ? user : null;
}
}
/**
* 请求摘要身份验证(不进行换算)
*
* @param digestRequest 消化的请求
* @return {@link Object}
*/
@Override
public Object authenticate(DigestResponse digestRequest) {
String serverResponse = nameAndPasswords.get(digestRequest.getUser());
String clientResponse = digestRequest.getResponseDigest();
//比对密码MD5摘要
if (serverResponse.equals(SecureUtil.md5(clientResponse))) {
return "ok";
} else {
return null;
}
}
@Override
public boolean authorise(Request request, Method method, Auth auth, Resource resource) {
if (auth == null) {
log.trace("authorise: declining because there is no auth object");
return false;
} else {
if (auth.getTag() == null) {
log.trace("authorise: declining because there is no auth.getTag() object");
return false;
} else {
log.trace("authorise: permitting because there is an authenticated user associated with this request");
return true;
}
}
}
@Override
public String getRealm(String host) {
return realm;
}
/**
* @param realm the realm to set
*/
public void setRealm(String realm) {
this.realm = realm;
}
public void setNameAndPasswords(Map<String, String> nameAndPasswords) {
this.nameAndPasswords = nameAndPasswords;
}
@Override
public boolean isDigestAllowed() {
// 关闭请求摘要换算client端请求时若换算为摘要则无法和系统设置中获取的密码MD5比对
return false;
}
}

View File

@@ -0,0 +1,62 @@
package im.zhaojun.zfile.config.webdav.resolver;
import cn.hutool.core.util.URLUtil;
import im.zhaojun.zfile.model.constant.ZFileConstant;
import im.zhaojun.zfile.model.entity.webdav.WebDavFile;
import io.milton.common.View;
import io.milton.http.template.TemplateProcessor;
import io.milton.http.template.ViewResolver;
import io.milton.servlet.OutputStreamWrappingHttpServletResponse;
import io.milton.servlet.ServletResponse;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Map;
/**
* WebDav重定向视图处理器
* Get注解handler返回字符串时将使用本ViewResolver处理
*
* @author me
* @date 2022/4/9
*/
public class WebDavRedirectViewResolver implements ViewResolver {
@Override
public TemplateProcessor resolveView(View view) {
return new RedirectTemplateProcessor();
}
/**
* 重定向模板处理程序
*
* @author me
* @date 2022/04/10
*/
public static class RedirectTemplateProcessor implements TemplateProcessor {
@Override
public void execute(Map<String, Object> model, OutputStream out) {
try {
// 获取要下载的资源文件
final Object resource = model.get("resource");
if (!(resource instanceof WebDavFile)) {
throw new RuntimeException("couldn't get direct url.");
}
final WebDavFile file = (WebDavFile) resource;
// 构造文件直链的路径
final String redirectPath = String.format("/%s/%s%s", ZFileConstant.DIRECT_LINK_PREFIX, file.getDriveId(), file.getFullPath());
// 重定向到直链
HttpServletResponse resp = new OutputStreamWrappingHttpServletResponse(ServletResponse.getResponse(), out);
resp.setStatus(301);
resp.setHeader("Location", URLUtil.encode(redirectPath));
resp.setHeader("Connection", "close");
resp.flushBuffer();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}

View File

@@ -1,10 +1,13 @@
package im.zhaojun.zfile.context;
import com.alibaba.fastjson.JSON;
import im.zhaojun.zfile.exception.InvalidDriveException;
import im.zhaojun.zfile.model.entity.DriveConfig;
import im.zhaojun.zfile.model.enums.StorageTypeEnum;
import im.zhaojun.zfile.service.DriveConfigService;
import im.zhaojun.zfile.service.base.AbstractBaseFileService;
import im.zhaojun.zfile.util.SpringContextHolder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
@@ -12,38 +15,64 @@ import org.springframework.context.annotation.DependsOn;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
/**
* 驱动器上下文环境
* 每个驱动器对应一个 Service, 其中初始化好了与对象存储的连接信息.
* 此驱动器上下文环境用户缓存每个 Service, 避免重复创建连接.
* @author zhaojun
*/
@Component
@DependsOn("springContextHolder")
@Slf4j
public class DriveContext implements ApplicationContextAware {
/**
* Map<Integer, AbstractBaseFileService>
* Map<驱动器 ID, 驱动器连接 Service>
*/
private static Map<Integer, AbstractBaseFileService> drivesServiceMap = new ConcurrentHashMap<>();
private static Map<StorageTypeEnum, Class<AbstractBaseFileService>> storageTypeEnumClassMap = new ConcurrentHashMap<>();
@Resource
private DriveConfigService driveConfigService;
/**
* 项目启动时, 自动调用数据库已存储的所有驱动器进行初始化.
*/
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
List<DriveConfig> list = driveConfigService.list();
for (DriveConfig driveConfig : list) {
try {
init(driveConfig.getId());
log.info("启动时初始化驱动器成功, 驱动器信息: {}", JSON.toJSONString(driveConfig));
} catch (Exception e) {
log.error("启动时初始化驱动器失败, 驱动器信息: {}", JSON.toJSONString(driveConfig), e);
}
}
}
/**
* 初始化指定驱动器的 Service, 添加到上下文环境中.
*
* @param driveId
* 驱动器 ID.
*/
public void initDrive(Integer driveId) {
public void init(Integer driveId) {
AbstractBaseFileService baseFileService = getBeanByDriveId(driveId);
if (baseFileService != null) {
if (log.isDebugEnabled()) {
log.debug("尝试初始化驱动器, driveId: {}", driveId);
}
baseFileService.init(driveId);
if (log.isDebugEnabled()) {
log.debug("初始化驱动器成功, driveId: {}", driveId);
}
drivesServiceMap.put(driveId, baseFileService);
}
}
@@ -57,8 +86,12 @@ public class DriveContext implements ApplicationContextAware {
*
* @return 驱动器对应的 Service
*/
public AbstractBaseFileService getDriveService(Integer driveId) {
return drivesServiceMap.get(driveId);
public AbstractBaseFileService get(Integer driveId) {
AbstractBaseFileService abstractBaseFileService = drivesServiceMap.get(driveId);
if (abstractBaseFileService == null) {
throw new InvalidDriveException("此驱动器不存在或初始化失败, 请检查后台参数配置");
}
return abstractBaseFileService;
}
@@ -68,7 +101,10 @@ public class DriveContext implements ApplicationContextAware {
* @param driveId
* 驱动器 ID
*/
public void destroyDrive(Integer driveId) {
public void destroy(Integer driveId) {
if (log.isDebugEnabled()) {
log.debug("清理驱动器上下文对象, driveId: {}", driveId);
}
drivesServiceMap.remove(driveId);
}
@@ -94,14 +130,18 @@ public class DriveContext implements ApplicationContextAware {
/**
* 项目启动时, 自动调用所有驱动器进行初始化.
* 更新上下文环境中的驱动器 ID
*
* @param updateId
* 驱动器原 ID
*
* @param newId
* 驱动器新 ID
*/
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
List<DriveConfig> list = driveConfigService.list();
for (DriveConfig driveConfig : list) {
initDrive(driveConfig.getId());
}
public void updateDriveId(Integer updateId, Integer newId) {
AbstractBaseFileService fileService = drivesServiceMap.remove(updateId);
fileService.setDriveId(newId);
drivesServiceMap.put(newId, fileService);
}
}

View File

@@ -6,6 +6,7 @@ import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import java.util.Map;

View File

@@ -1,220 +0,0 @@
package im.zhaojun.zfile.controller;
import im.zhaojun.zfile.model.constant.ZFileConstant;
import im.zhaojun.zfile.model.dto.FileItemDTO;
import im.zhaojun.zfile.model.dto.ResultBean;
import im.zhaojun.zfile.model.dto.SystemFrontConfigDTO;
import im.zhaojun.zfile.model.support.FilePageModel;
import im.zhaojun.zfile.service.DriveConfigService;
import im.zhaojun.zfile.service.SystemConfigService;
import im.zhaojun.zfile.service.base.AbstractBaseFileService;
import im.zhaojun.zfile.context.DriveContext;
import im.zhaojun.zfile.util.FileComparator;
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.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.HttpClientErrorException;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
/**
* 前台文件管理
* @author zhaojun
*/
@Slf4j
@RequestMapping("/api")
@RestController
public class FileController {
@Resource
private SystemConfigService systemConfigService;
@Resource
private DriveContext driveContext;
@Resource
private DriveConfigService driveConfigService;
/**
* 滚动加载每页条数.
*/
private static final Integer PAGE_SIZE = 30;
/**
* 获取所有驱动器
*
* @return 所有驱动器
*/
@GetMapping("/drive/list")
public ResultBean drives() {
return ResultBean.success(driveConfigService.list());
}
/**
* 获取某个驱动器下, 指定路径的数据, 每页固定 {@link #PAGE_SIZE} 条数据.
*
* @param driveId
* 驱动器 ID
*
* @param path
* 路径
*
* @param password
* 文件夹密码, 某些文件夹需要密码才能访问, 当不需要密码时, 此参数可以为空
*
* @param page
* 页数
*
* @return 当前路径下所有文件及文件夹
*/
@GetMapping("/list/{driveId}")
public ResultBean list(@PathVariable(name = "driveId") Integer driveId,
@RequestParam(defaultValue = "/") String path,
@RequestParam(required = false) String password,
@RequestParam(defaultValue = "1") Integer page) throws Exception {
AbstractBaseFileService fileService = driveContext.getDriveService(driveId);
List<FileItemDTO> fileItemList = fileService.fileList(StringUtils.removeDuplicateSeparator("/" + path + "/"));
for (FileItemDTO fileItemDTO : fileItemList) {
if (ZFileConstant.PASSWORD_FILE_NAME.equals(fileItemDTO.getName())) {
String expectedPasswordContent;
try {
expectedPasswordContent = HttpUtil.getTextContent(fileItemDTO.getUrl());
} 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)) {
return ResultBean.error("密码错误.", ResultBean.INVALID_PASSWORD);
}
return ResultBean.error("此文件夹需要密码.", ResultBean.REQUIRED_PASSWORD);
}
}
return ResultBean.successData(getSortedPagingData(fileItemList, page));
}
/**
* 获取系统配置信息和当前页的标题, 页面文档信息
*
* @param driveId
* 驱动器 ID
*
* @return 返回指定存储器的系统配置信息
*/
@GetMapping("/config/{driveId}")
public ResultBean getConfig(@PathVariable(name = "driveId") Integer driveId, String path) {
SystemFrontConfigDTO systemConfig = systemConfigService.getSystemFrontConfig(driveId);
AbstractBaseFileService fileService = driveContext.getDriveService(driveId);
String fullPath = StringUtils.removeDuplicateSeparator(path + "/" + ZFileConstant.README_FILE_NAME);
try {
FileItemDTO fileItem = fileService.getFileItem(fullPath);
String readme = HttpUtil.getTextContent(fileItem.getUrl());
systemConfig.setReadme(readme);
} catch (Exception e) {
// ignore
}
return ResultBean.successData(systemConfig);
}
@GetMapping("/search/{driveId}")
public ResultBean search(@RequestParam(value = "name", defaultValue = "/") String name,
@RequestParam(defaultValue = "name") String sortBy,
@RequestParam(defaultValue = "asc") String order,
@RequestParam(defaultValue = "1") Integer page,
@PathVariable("driveId") Integer driveId) {
return ResultBean.error("暂不支持搜索功能");
}
/**
* 过滤文件列表, 去除密码, 文档文件.
*
* @param fileItemList
* 文件列表
*/
private void filterFileList(List<FileItemDTO> fileItemList) {
if (fileItemList == null) {
return;
}
fileItemList.removeIf(fileItem -> ZFileConstant.PASSWORD_FILE_NAME.equals(fileItem.getName())
|| ZFileConstant.README_FILE_NAME.equals(fileItem.getName()));
}
/**
* 对传入的文件列表, 按照文件名进行排序, 然后取相应页数的文件
*
* @param fileItemList
* 文件列表
*
* @param page
* 要取的页数
*
* @return 排序及分页后的那段数据
*/
private FilePageModel getSortedPagingData(List<FileItemDTO> fileItemList, Integer page) {
ArrayList<FileItemDTO> copy = new ArrayList<>(Arrays.asList(new FileItemDTO[fileItemList.size()]));
Collections.copy(copy, fileItemList);
// 排序, 先按照文件类型比较, 文件夹在前, 文件在后, 然后根据 sortBy 字段排序, 默认为升序;
copy.sort(new FileComparator());
filterFileList(copy);
int total = copy.size();
int totalPage = (total + PAGE_SIZE - 1) / PAGE_SIZE;
if (page > totalPage) {
return new FilePageModel(totalPage, Collections.emptyList());
}
int start = (page - 1) * PAGE_SIZE;
int end = page * PAGE_SIZE;
end = Math.min(end, total);
return new FilePageModel(totalPage, copy.subList(start, end));
}
/**
* 获取指定路径下的文件信息内容
*
* @param driveId
* 驱动器 ID
*
* @param path
* 文件全路径
*
* @return 该文件的名称, 路径, 大小, 下载地址等信息.
*/
@GetMapping("/directlink/{driveId}")
public ResultBean directlink(@PathVariable(name = "driveId") Integer driveId, String path) {
AbstractBaseFileService fileService = driveContext.getDriveService(driveId);
return ResultBean.successData(fileService.getFileItem(path));
}
}

View File

@@ -1,30 +1,14 @@
package im.zhaojun.zfile.controller.admin;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ZipUtil;
import im.zhaojun.zfile.context.StorageTypeContext;
import im.zhaojun.zfile.model.dto.ResultBean;
import im.zhaojun.zfile.model.dto.StorageStrategyDTO;
import im.zhaojun.zfile.model.dto.SystemConfigDTO;
import im.zhaojun.zfile.model.entity.StorageConfig;
import im.zhaojun.zfile.model.enums.StorageTypeEnum;
import im.zhaojun.zfile.model.support.SystemMonitorInfo;
import im.zhaojun.zfile.model.support.ResultBean;
import im.zhaojun.zfile.service.SystemConfigService;
import im.zhaojun.zfile.service.base.AbstractBaseFileService;
import im.zhaojun.zfile.util.FileUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
* 管理后台接口
@@ -32,7 +16,6 @@ import java.util.List;
*/
@RestController
@RequestMapping("/admin")
@Slf4j
public class AdminController {
@Resource
@@ -52,7 +35,7 @@ public class AdminController {
* 更新系统配置
*/
@PostMapping("/config")
public ResultBean updateConfig(SystemConfigDTO systemConfigDTO) throws Exception {
public ResultBean updateConfig(SystemConfigDTO systemConfigDTO) {
systemConfigDTO.setId(1);
systemConfigService.updateSystemConfig(systemConfigDTO);
return ResultBean.success();
@@ -68,52 +51,4 @@ public class AdminController {
return ResultBean.success();
}
/**
* 获取指定存储策略的表单域
*
* @param storageType
* 存储策略
*
* @return 所有表单域
*/
@GetMapping("/strategy-form")
public ResultBean getFormByStorageType(StorageTypeEnum storageType) {
AbstractBaseFileService storageTypeService = StorageTypeContext.getStorageTypeService(storageType);
List<StorageConfig> storageConfigList = storageTypeService.storageStrategyConfigList();
return ResultBean.success(storageConfigList);
}
/**
* 返回支持的存储引擎.
*/
@GetMapping("/support-strategy")
public ResultBean supportStrategy() {
List<StorageStrategyDTO> result = new ArrayList<>();
StorageTypeEnum[] values = StorageTypeEnum.values();
return ResultBean.successData(values);
}
/**
* 系统日志下载
*/
@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());
}
}

View File

@@ -1,7 +1,9 @@
package im.zhaojun.zfile.controller.admin;
import im.zhaojun.zfile.model.dto.ResultBean;
import im.zhaojun.zfile.model.dto.CacheInfoDTO;
import im.zhaojun.zfile.model.support.ResultBean;
import im.zhaojun.zfile.service.DriveConfigService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
@@ -21,17 +23,50 @@ public class CacheController {
@Resource
private DriveConfigService driveConfigService;
@PostMapping("/{driveId}/enable")
public ResultBean enableCache(@PathVariable("driveId") Integer driveId) {
driveConfigService.updateCacheStatus(driveId, true);
return ResultBean.success();
}
@PostMapping("/{driveId}/disable")
public ResultBean disableCache(@PathVariable("driveId") Integer driveId) {
driveConfigService.updateCacheStatus(driveId, false);
return ResultBean.success();
}
@GetMapping("/{driveId}/info")
public ResultBean cacheInfo(@PathVariable("driveId") Integer driveId) {
CacheInfoDTO cacheInfo = driveConfigService.findCacheInfo(driveId);
return ResultBean.success(cacheInfo);
}
@PostMapping("/{driveId}/refresh")
public ResultBean refreshCache(@PathVariable("driveId") Integer driveId, String key) throws Exception {
driveConfigService.refreshCache(driveId, key);
return ResultBean.success();
}
@PostMapping("/{driveId}/auto-refresh/start")
public ResultBean enableAutoRefresh(@PathVariable("driveId") Integer driveId) {
driveConfigService.startAutoCacheRefresh(driveId);
return ResultBean.success();
}
@PostMapping("/{driveId}/auto-refresh/stop")
public ResultBean disableAutoRefresh(@PathVariable("driveId") Integer driveId) {
driveConfigService.stopAutoCacheRefresh(driveId);
return ResultBean.success();
}
@PostMapping("/{driveId}/clear")
public ResultBean clearCache(@PathVariable("driveId") Integer driveId) {
driveConfigService.clearCache(driveId);
return ResultBean.success();
}
}

View File

@@ -0,0 +1,33 @@
package im.zhaojun.zfile.controller.admin;
import im.zhaojun.zfile.model.support.ResultBean;
import im.zhaojun.zfile.service.SystemConfigService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
@Controller
public class DebugController {
@Value("${zfile.debug}")
private Boolean debug;
@Resource
private SystemConfigService systemConfigService;
@ResponseBody
@GetMapping("/debug/resetPwd")
public ResultBean resetPwd() {
if (debug) {
systemConfigService.updateUsernameAndPwd("admin", "123456");
return ResultBean.success();
} else {
return ResultBean.error("未开启 DEBUG 模式,不允许进行此操作。");
}
}
}

View File

@@ -1,10 +1,12 @@
package im.zhaojun.zfile.controller.admin;
import com.alibaba.fastjson.JSONObject;
import im.zhaojun.zfile.model.dto.DriveConfigDTO;
import im.zhaojun.zfile.model.dto.ResultBean;
import im.zhaojun.zfile.model.entity.DriveConfig;
import im.zhaojun.zfile.model.entity.FilterConfig;
import im.zhaojun.zfile.model.support.ResultBean;
import im.zhaojun.zfile.service.DriveConfigService;
import lombok.extern.slf4j.Slf4j;
import im.zhaojun.zfile.service.FilterConfigService;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
@@ -17,24 +19,26 @@ import javax.annotation.Resource;
import java.util.List;
/**
* 驱动器 Controller
* 驱动器相关操作 Controller
* @author zhaojun
*/
@RestController
@RequestMapping("/admin")
@Slf4j
public class DriveController {
@Resource
private DriveConfigService driveConfigService;
@Resource
private FilterConfigService filterConfigService;
/**
* 获取所有驱动器列表
*
* @return 驱动器列表
*/
@GetMapping("drives")
@GetMapping("/drives")
public ResultBean driveList() {
List<DriveConfig> list = driveConfigService.list();
return ResultBean.success(list);
@@ -44,14 +48,14 @@ public class DriveController {
/**
* 获取指定驱动器基本信息及其参数
*
* @param id
* @param driveId
* 驱动器 ID
*
* @return 驱动器基本信息信息
* @return 驱动器基本信息
*/
@GetMapping("drive/{id}")
public ResultBean driveItem(@PathVariable Integer id) {
DriveConfigDTO driveConfig = driveConfigService.findDriveConfigDTOById(id);
@GetMapping("/drive/{driveId}")
public ResultBean driveItem(@PathVariable Integer driveId) {
DriveConfigDTO driveConfig = driveConfigService.findDriveConfigDTOById(driveId);
return ResultBean.success(driveConfig);
}
@@ -59,9 +63,9 @@ public class DriveController {
/**
* 保存驱动器设置
*/
@PostMapping("drive")
@PostMapping("/drive")
public ResultBean saveDriveItem(@RequestBody DriveConfigDTO driveConfigDTO) {
driveConfigService.save(driveConfigDTO);
driveConfigService.saveDriveConfigDTO(driveConfigDTO);
return ResultBean.success();
}
@@ -69,12 +73,100 @@ public class DriveController {
/**
* 删除驱动器设置
*
* @param id
* @param driveId
* 驱动器 ID
*/
@DeleteMapping("drive/{id}")
public ResultBean deleteDriveItem(@PathVariable Integer id) {
driveConfigService.deleteById(id);
@DeleteMapping("/drive/{driveId}")
public ResultBean deleteDriveItem(@PathVariable Integer driveId) {
driveConfigService.deleteById(driveId);
return ResultBean.success();
}
/**
* 启用驱动器
*
* @param driveId
* 驱动器 ID
*/
@PostMapping("/drive/{driveId}/enable")
public ResultBean enable(@PathVariable Integer driveId) {
DriveConfig driveConfig = driveConfigService.findById(driveId);
driveConfig.setEnable(true);
driveConfigService.updateDriveConfig(driveConfig);
return ResultBean.success();
}
/**
* 停止驱动器
*
* @param driveId
* 驱动器 ID
*/
@PostMapping("/drive/{driveId}/disable")
public ResultBean disable(@PathVariable Integer driveId) {
DriveConfig driveConfig = driveConfigService.findById(driveId);
driveConfig.setEnable(false);
driveConfigService.updateDriveConfig(driveConfig);
return ResultBean.success();
}
/**
* 根据驱动器 ID 获取过滤文件列表
*
* @param driveId
* 驱动器 ID
*/
@GetMapping("/drive/{driveId}/filters")
public ResultBean getFilters(@PathVariable Integer driveId) {
return ResultBean.success(filterConfigService.findByDriveId(driveId));
}
/**
* 停止驱动器
*
* @param driveId
* 驱动器 ID
*/
@PostMapping("/drive/{driveId}/filters")
public ResultBean saveFilters(@RequestBody List<FilterConfig> filter, @PathVariable Integer driveId) {
filterConfigService.batchSave(filter, driveId);
return ResultBean.success();
}
/**
* 保存拖拽排序信息
*
* @param driveConfigs
* 拖拽排序信息
*/
@PostMapping("/drive/drag")
public ResultBean saveDriveDrag(@RequestBody List<JSONObject> driveConfigs) {
driveConfigService.saveDriveDrag(driveConfigs);
return ResultBean.success();
}
/**
* 更新驱动器 ID
*
* @param updateId
* 驱动器原 ID
*
* @param newId
* 驱动器新 ID
*/
@PostMapping("/drive/updateId")
public ResultBean updateDriveId(Integer updateId, Integer newId) {
DriveConfig driveConfig = driveConfigService.findById(newId);
if (driveConfig != null) {
return ResultBean.error("已存在的 ID请更换 ID 后重试。");
}
driveConfigService.updateId(updateId, newId);
return ResultBean.success();
}

View File

@@ -0,0 +1,38 @@
package im.zhaojun.zfile.controller.admin;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ZipUtil;
import im.zhaojun.zfile.util.FileUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.io.File;
import java.util.Date;
/**
* 日志相关 Controller
* @author zhaojun
*/
@RestController
@RequestMapping("/admin")
@Slf4j
public class LogController {
/**
* 系统日志下载
*/
@GetMapping("/log")
public ResponseEntity<Object> downloadLog() {
if (log.isDebugEnabled()) {
log.debug("下载诊断日志");
}
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.exportSingleThread(fileZip, "ZFile 诊断日志 - " + currentDate + ".zip");
}
}

View File

@@ -0,0 +1 @@
package im.zhaojun.zfile.controller.admin;

View File

@@ -0,0 +1,47 @@
package im.zhaojun.zfile.controller.admin;
import im.zhaojun.zfile.context.StorageTypeContext;
import im.zhaojun.zfile.model.entity.StorageConfig;
import im.zhaojun.zfile.model.enums.StorageTypeEnum;
import im.zhaojun.zfile.model.support.ResultBean;
import im.zhaojun.zfile.service.base.AbstractBaseFileService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* 系统元数据 Controller
* @author zhaojun
*/
@RestController
@RequestMapping("/admin")
public class MateDataController {
/**
* 返回支持的存储引擎.
*/
@GetMapping("/support-strategy")
public ResultBean supportStrategy() {
StorageTypeEnum[] values = StorageTypeEnum.values();
return ResultBean.successData(values);
}
/**
* 获取指定存储策略的表单域
*
* @param storageType
* 存储策略
*
* @return 所有表单域
*/
@GetMapping("/strategy-form")
public ResultBean getFormByStorageType(StorageTypeEnum storageType) {
AbstractBaseFileService storageTypeService = StorageTypeContext.getStorageTypeService(storageType);
List<StorageConfig> storageConfigList = storageTypeService.storageStrategyConfigList();
return ResultBean.success(storageConfigList);
}
}

View File

@@ -0,0 +1,45 @@
package im.zhaojun.zfile.controller.admin;
import im.zhaojun.zfile.model.support.ResultBean;
import im.zhaojun.zfile.service.ShortLinkConfigService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
/**
* 直链管理 Controller
*
* @author zhaojun
*/
@Controller
@RequestMapping("/admin")
public class ShortLinkManagerController {
@Resource
private ShortLinkConfigService shortLinkConfigService;
@GetMapping("/link/list")
@ResponseBody
public ResultBean list(String key,
String url,
String dateFrom,
String dateTo,
Integer page,
Integer limit,
@RequestParam(required = false, defaultValue = "createDate") String orderBy,
@RequestParam(required = false, defaultValue = "desc") String orderDirection) {
return ResultBean.success(shortLinkConfigService.find(key, url, dateFrom, dateTo, page, limit, orderBy, orderDirection));
}
@GetMapping("/link/delete/{id}")
@ResponseBody
public ResultBean deleteById(@PathVariable Integer id) {
shortLinkConfigService.deleteById(id);
return ResultBean.success();
}
}

View File

@@ -1,11 +1,17 @@
package im.zhaojun.zfile.controller;
package im.zhaojun.zfile.controller.home;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.URLUtil;
import im.zhaojun.zfile.context.DriveContext;
import im.zhaojun.zfile.exception.NotEnabledDriveException;
import im.zhaojun.zfile.model.constant.ZFileConstant;
import im.zhaojun.zfile.model.dto.FileItemDTO;
import im.zhaojun.zfile.model.entity.DriveConfig;
import im.zhaojun.zfile.model.enums.FileTypeEnum;
import im.zhaojun.zfile.service.DriveConfigService;
import im.zhaojun.zfile.service.base.AbstractBaseFileService;
import im.zhaojun.zfile.context.DriveContext;
import im.zhaojun.zfile.util.HttpUtil;
import org.springframework.stereotype.Controller;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.bind.annotation.GetMapping;
@@ -14,17 +20,24 @@ import org.springframework.web.servlet.HandlerMapping;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Objects;
/**
* 直链 Controller
* @author Zhao Jun
*/
@Controller
public class PageController {
public class DirectLinkController {
@Resource
private DriveContext driveContext;
@Resource
private DriveConfigService driveConfigService;
/**
* 获取指定驱动器, 某个文件的直链, 然后重定向过去.
* @param driveId
@@ -32,8 +45,16 @@ public class PageController {
*
* @return 重定向至文件直链
*/
@GetMapping("/directlink/{driveId}/**")
public String directlink(@PathVariable("driveId") Integer driveId, final HttpServletRequest request) {
@GetMapping("/${zfile.directLinkPrefix}/{driveId}/**")
public String directlink(@PathVariable("driveId") Integer driveId,
final HttpServletRequest request,
final HttpServletResponse response) throws IOException {
DriveConfig driveConfig = driveConfigService.findById(driveId);
Boolean enable = driveConfig.getEnable();
if (!enable) {
throw new NotEnabledDriveException();
}
String path = (String) request.getAttribute(
HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE);
String bestMatchPattern = (String) request.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE);
@@ -44,11 +65,21 @@ public class PageController {
filePath = "/" + filePath;
}
AbstractBaseFileService fileService = driveContext.getDriveService(driveId);
AbstractBaseFileService fileService = driveContext.get(driveId);
FileItemDTO fileItem = fileService.getFileItem(filePath);
String url = fileItem.getUrl();
if (StrUtil.equalsIgnoreCase(FileUtil.extName(fileItem.getName()), "m3u8")) {
String textContent = HttpUtil.getTextContent(url);
response.setContentType("application/vnd.apple.mpegurl;charset=utf-8");
PrintWriter out = response.getWriter();
out.write(textContent);
out.flush();
out.close();
return null;
}
int queryIndex = url.indexOf('?');
if (queryIndex != -1) {
@@ -67,4 +98,5 @@ public class PageController {
return "redirect:" + url;
}
}
}
}

View File

@@ -0,0 +1,243 @@
package im.zhaojun.zfile.controller.home;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import im.zhaojun.zfile.context.DriveContext;
import im.zhaojun.zfile.exception.NotEnabledDriveException;
import im.zhaojun.zfile.exception.PasswordVerifyException;
import im.zhaojun.zfile.model.constant.ZFileConstant;
import im.zhaojun.zfile.model.dto.DriveListDTO;
import im.zhaojun.zfile.model.dto.FileItemDTO;
import im.zhaojun.zfile.model.dto.FileListDTO;
import im.zhaojun.zfile.model.dto.SystemFrontConfigDTO;
import im.zhaojun.zfile.model.entity.DriveConfig;
import im.zhaojun.zfile.model.enums.StorageTypeEnum;
import im.zhaojun.zfile.model.support.ResultBean;
import im.zhaojun.zfile.model.support.VerifyResult;
import im.zhaojun.zfile.service.DriveConfigService;
import im.zhaojun.zfile.service.FilterConfigService;
import im.zhaojun.zfile.service.SystemConfigService;
import im.zhaojun.zfile.service.base.AbstractBaseFileService;
import im.zhaojun.zfile.util.FileComparator;
import im.zhaojun.zfile.util.HttpUtil;
import im.zhaojun.zfile.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.HttpClientErrorException;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
/**
* 前台文件管理
* @author zhaojun
*/
@Slf4j
@RequestMapping("/api")
@RestController
public class FileController {
@Value("${zfile.debug}")
private Boolean debug;
@Resource
private SystemConfigService systemConfigService;
@Resource
private DriveContext driveContext;
@Resource
private DriveConfigService driveConfigService;
@Resource
private FilterConfigService filterConfigService;
/**
* 获取所有已启用的驱动器
*
* @return 所有已启用驱动器
*/
@GetMapping("/drive/list")
public ResultBean drives() {
List<DriveConfig> driveList = driveConfigService.listOnlyEnable();
boolean isInstall = systemConfigService.getIsInstall();
DriveListDTO driveListDTO = new DriveListDTO(driveList, isInstall);
return ResultBean.success(driveListDTO);
}
/**
* 获取某个驱动器下, 指定路径的数据
*
* @param driveId
* 驱动器 ID
*
* @param path
* 路径
*
* @param password
* 文件夹密码, 某些文件夹需要密码才能访问, 当不需要密码时, 此参数可以为空
*
* @return 当前路径下所有文件及文件夹
*/
@GetMapping("/list/{driveId}")
public ResultBean list(@PathVariable(name = "driveId") Integer driveId,
@RequestParam(defaultValue = "/") String path,
@RequestParam(required = false) String password,
@RequestParam(required = false) String orderBy,
@RequestParam(required = false, defaultValue = "asc") String orderDirection) throws Exception {
AbstractBaseFileService fileService = driveContext.get(driveId);
List<FileItemDTO> fileItemList = fileService.fileList(StringUtils.removeDuplicateSeparator(ZFileConstant.PATH_SEPARATOR + path + ZFileConstant.PATH_SEPARATOR));
// 创建副本, 防止排序和过滤对原数据产生影响
List<FileItemDTO> copyList = new ArrayList<>(fileItemList);
// 校验密码, 如果校验不通过, 则返回错误消息
VerifyResult verifyResult = verifyPassword(copyList, driveId, path, password);
if (!verifyResult.isPassed()) {
return ResultBean.error(verifyResult.getMsg(), verifyResult.getCode());
}
// 过滤掉驱动器配置的表达式中要隐藏的数据
filterFileList(copyList, driveId);
// 按照自然排序
copyList.sort(new FileComparator(orderBy, orderDirection));
// 开始获取参数信息
SystemFrontConfigDTO systemConfig = systemConfigService.getSystemFrontConfig(driveId);
DriveConfig driveConfig = driveConfigService.findById(driveId);
Boolean enable = driveConfig.getEnable();
if (!enable) {
throw new NotEnabledDriveException();
}
systemConfig.setDebugMode(debug);
systemConfig.setDefaultSwitchToImgMode(driveConfig.getDefaultSwitchToImgMode());
systemConfig.setDirectLinkPrefix(ZFileConstant.DIRECT_LINK_PREFIX);
// 如果不是 FTP 模式,则尝试获取当前文件夹中的 README 文件,有则读取,没有则停止
if (!Objects.equals(driveConfig.getType(), StorageTypeEnum.FTP)) {
fileItemList.stream()
.filter(fileItemDTO -> Objects.equals(ZFileConstant.README_FILE_NAME, fileItemDTO.getName()))
.findFirst()
.ifPresent(fileItemDTO -> {
String readme = HttpUtil.getTextContent(fileItemDTO.getUrl());
systemConfig.setReadme(readme);
});
}
return ResultBean.successData(new FileListDTO(copyList, systemConfig));
}
/**
* 校验密码
* @param fileItemList
* 文件列表
* @param driveId
* 驱动器 ID
* @param path
* 请求路径
* @param inputPassword
* 用户输入的密码
* @return 是否校验通过
*/
private VerifyResult verifyPassword(List<FileItemDTO> fileItemList, Integer driveId, String path, String inputPassword) {
AbstractBaseFileService fileService = driveContext.get(driveId);
for (FileItemDTO fileItemDTO : fileItemList) {
if (ZFileConstant.PASSWORD_FILE_NAME.equals(fileItemDTO.getName())) {
String expectedPasswordContent;
try {
expectedPasswordContent = HttpUtil.getTextContent(fileItemDTO.getUrl());
} catch (HttpClientErrorException httpClientErrorException) {
log.trace("尝试重新获取密码文件缓存中链接后仍失败, driveId: {}, path: {}, inputPassword: {}, passwordFile:{} ",
driveId, path, inputPassword, JSON.toJSONString(fileItemDTO), httpClientErrorException);
try {
String pwdFileFullPath = StringUtils.removeDuplicateSeparator(fileItemDTO.getPath() + ZFileConstant.PATH_SEPARATOR + fileItemDTO.getName());
FileItemDTO pwdFileItem = fileService.getFileItem(pwdFileFullPath);
expectedPasswordContent = HttpUtil.getTextContent(pwdFileItem.getUrl());
} catch (Exception e) {
throw new PasswordVerifyException("此文件夹为加密文件夹, 但密码检查异常, 请联系管理员检查密码设置", e);
}
} catch (Exception e) {
throw new PasswordVerifyException("此文件夹为加密文件夹, 但密码检查异常, 请联系管理员检查密码设置", e);
}
if (matchPassword(expectedPasswordContent, inputPassword)) {
break;
}
if (StrUtil.isEmpty(inputPassword)) {
return VerifyResult.fail("此文件夹需要密码.", ResultBean.REQUIRED_PASSWORD);
}
return VerifyResult.fail("密码错误.", ResultBean.INVALID_PASSWORD);
}
}
return VerifyResult.success();
}
/**
* 校验两个密码是否相同, 忽略空白字符
*
* @param expectedPasswordContent
* 预期密码
*
* @param password
* 实际输入密码
*
* @return 是否匹配
*/
private boolean matchPassword(String expectedPasswordContent, String password) {
if (Objects.equals(expectedPasswordContent, password)) {
return true;
}
if (expectedPasswordContent == null) {
return false;
}
if (password == null) {
return false;
}
expectedPasswordContent = expectedPasswordContent.replace("\n", "").trim();
password = password.replace("\n", "").trim();
return Objects.equals(expectedPasswordContent, password);
}
/**
* 过滤文件列表, 去除密码, 文档文件和此驱动器通过规则过滤的文件.
*
* @param fileItemList
* 文件列表
* @param driveId
* 驱动器 ID
*/
private void filterFileList(List<FileItemDTO> fileItemList, Integer driveId) {
if (fileItemList == null) {
return;
}
fileItemList.removeIf(
fileItem -> ZFileConstant.PASSWORD_FILE_NAME.equals(fileItem.getName())
|| ZFileConstant.README_FILE_NAME.equals(fileItem.getName())
|| filterConfigService.filterResultIsHidden(driveId, StringUtils.concatUrl(fileItem.getPath(), fileItem.getName()))
);
}
}

View File

@@ -1,32 +1,19 @@
package im.zhaojun.zfile.controller;
package im.zhaojun.zfile.controller.home;
import im.zhaojun.zfile.model.dto.ResultBean;
import im.zhaojun.zfile.model.enums.StorageTypeEnum;
import im.zhaojun.zfile.util.AudioHelper;
import im.zhaojun.zfile.model.support.ResultBean;
import im.zhaojun.zfile.util.AudioUtil;
import im.zhaojun.zfile.util.HttpUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* 公共 Controller
* 文件解析 Controller
* @author zhaojun
*/
@RestController
@RequestMapping("/common")
public class CommonController {
/**
* 返回系统支持的所有存储策略
*
* @return 存储策略
*/
@GetMapping("/support-strategy")
public ResultBean supportStrategy() {
return ResultBean.successData(StorageTypeEnum.values());
}
public class FileParseController {
/**
* 获取文件内容, 仅限用于 txt, md, ini 等普通文本文件.
@@ -52,7 +39,7 @@ public class CommonController {
*/
@GetMapping("/audio-info")
public ResultBean getAudioInfo(String url) throws Exception {
return ResultBean.success(AudioHelper.getAudioInfo(url));
return ResultBean.success(AudioUtil.getAudioInfo(url));
}
}

View File

@@ -1,10 +1,10 @@
package im.zhaojun.zfile.controller;
package im.zhaojun.zfile.controller.home;
import im.zhaojun.zfile.service.impl.LocalServiceImpl;
import im.zhaojun.zfile.context.DriveContext;
import im.zhaojun.zfile.model.constant.ZFileConstant;
import im.zhaojun.zfile.service.impl.LocalServiceImpl;
import im.zhaojun.zfile.util.FileUtil;
import im.zhaojun.zfile.util.StringUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.bind.annotation.GetMapping;
@@ -14,6 +14,7 @@ import org.springframework.web.servlet.HandlerMapping;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
/**
@@ -31,19 +32,21 @@ public class LocalController {
*
* @param driveId
* 驱动器 ID
*
* @return 文件
* @param type
* 附件预览类型:
* download:下载
* default: 浏览器默认行为
*/
@GetMapping("/file/{driveId}/**")
@ResponseBody
public ResponseEntity<Object> downAttachment(@PathVariable("driveId") Integer driveId, final HttpServletRequest request) {
String path = (String) request.getAttribute(
HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE);
public void downAttachment(@PathVariable("driveId") Integer driveId, String type, final HttpServletRequest request, final HttpServletResponse response) {
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);
LocalServiceImpl localService = (LocalServiceImpl) driveContext.getDriveService(driveId);
return FileUtil.export(new File(StringUtils.concatPath(localService.getFilePath(), filePath)));
LocalServiceImpl localService = (LocalServiceImpl) driveContext.get(driveId);
File file = new File(StringUtils.removeDuplicateSeparator(localService.getFilePath() + ZFileConstant.PATH_SEPARATOR + filePath));
FileUtil.export(request, response, file, type);
}
}

View File

@@ -0,0 +1,107 @@
package im.zhaojun.zfile.controller.home;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.URLUtil;
import im.zhaojun.zfile.model.constant.ZFileConstant;
import im.zhaojun.zfile.model.dto.SystemConfigDTO;
import im.zhaojun.zfile.model.entity.ShortLinkConfig;
import im.zhaojun.zfile.model.support.ResultBean;
import im.zhaojun.zfile.service.ShortLinkConfigService;
import im.zhaojun.zfile.service.SystemConfigService;
import im.zhaojun.zfile.util.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
/**
* 短链 Controller
* @author zhao
*/
@Controller
public class ShortLinkController {
@Resource
private SystemConfigService systemConfigService;
@Resource
private ShortLinkConfigService shortLinkConfigService;
@GetMapping("/api/short-link")
@ResponseBody
public ResultBean shortLink(String driveId, String path) {
SystemConfigDTO systemConfig = systemConfigService.getSystemConfig();
String domain = systemConfig.getDomain();
// 拼接直链地址.
String fullPath = StringUtils.concatUrl(StringUtils.DELIMITER_STR, ZFileConstant.DIRECT_LINK_PREFIX, driveId, path);
ShortLinkConfig shortLinkConfig = shortLinkConfigService.findByUrl(fullPath);
if (shortLinkConfig == null) {
String randomKey;
do {
// 获取短链
randomKey = RandomUtil.randomString(6);
shortLinkConfig = shortLinkConfigService.findByKey(randomKey);
} while (shortLinkConfig != null);
shortLinkConfig = new ShortLinkConfig();
shortLinkConfig.setKey(randomKey);
shortLinkConfig.setUrl(fullPath);
shortLinkConfigService.save(shortLinkConfig);
}
String shortUrl = StringUtils.removeDuplicateSeparator(domain + "/s/" + shortLinkConfig.getKey());
return ResultBean.successData(shortUrl);
}
@GetMapping("/s/{key}")
public String parseShortKey(@PathVariable String key) {
ShortLinkConfig shortLinkConfig = shortLinkConfigService.findByKey(key);
if (shortLinkConfig == null) {
throw new RuntimeException("此直链不存在或已失效.");
}
SystemConfigDTO systemConfig = systemConfigService.getSystemConfig();
String domain = systemConfig.getDomain();
String url = URLUtil.encode(StringUtils.removeDuplicateSeparator(domain + shortLinkConfig.getUrl()));
return "redirect:" + url;
}
@GetMapping("admin/api/short-link/key")
@ResponseBody
public ResultBean updateShortKey(Integer id, String newKey) {
ShortLinkConfig newShortLinkConfig = shortLinkConfigService.findByKey(newKey);
if (newShortLinkConfig != null) {
throw new RuntimeException("您输入的 Key 已存在,请重新输入");
}
ShortLinkConfig shortLinkConfig = shortLinkConfigService.findById(id);
if (shortLinkConfig == null) {
throw new RuntimeException("此直链不存在或已失效.");
}
shortLinkConfig.setKey(newKey);
shortLinkConfigService.save(shortLinkConfig);
return ResultBean.success();
}
/**
* 批量删除直链
*/
@DeleteMapping("admin/api/short-link")
@ResponseBody
public ResultBean batchDelete(@RequestParam("id[]") Integer[] ids) {
for (Integer id : ids) {
shortLinkConfigService.deleteById(id);
}
return ResultBean.success();
}
}

View File

@@ -0,0 +1,159 @@
package im.zhaojun.zfile.controller.home;
import com.alibaba.fastjson.JSON;
import im.zhaojun.zfile.config.ApplicationContextConfigure;
import im.zhaojun.zfile.context.DriveContext;
import im.zhaojun.zfile.model.constant.ZFileConstant;
import im.zhaojun.zfile.model.dto.FileItemDTO;
import im.zhaojun.zfile.model.entity.webdav.WebDavEntity;
import im.zhaojun.zfile.model.entity.webdav.WebDavFile;
import im.zhaojun.zfile.model.entity.webdav.WebDavFolder;
import im.zhaojun.zfile.service.base.AbstractBaseFileService;
import im.zhaojun.zfile.util.RegexMatchUtils;
import io.milton.annotations.*;
import io.milton.http.HttpManager;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import java.util.Collections;
import java.util.Date;
import java.util.List;
/**
* WebDav控制器
*
* @author me
* @date 2022/4/9
*/
@Slf4j
@ResourceController
@ConditionalOnProperty(value = "webdav.enable", havingValue = "true")
public class WebDavController {
private static final Logger LOGGER = LoggerFactory.getLogger(WebDavController.class);
/**
* 获取根目录文件夹
*
* @return {@link WebDavFolder} WebDav文件夹
*/
@Root
public WebDavFolder getRootFolder() {
return new WebDavFolder(ZFileConstant.PATH_SEPARATOR, getDriveId());
}
/**
* 获取根目录子文件/文件夹(控制器)
*
* @param rootFolder 根文件夹
* @return {@link WebDavController} 根控制器
*/
@ChildrenOf
public WebDavController getChildren(WebDavController rootFolder) {
return this;
}
/**
* 获取子文件/文件夹
*
* @param parent 父文件夹
* @return {@link List}<{@link WebDavEntity}> WebDav实体
*/
@ChildrenOf
public List<WebDavEntity> getChildren(WebDavFolder parent) {
if (parent == null) {
return Collections.emptyList();
}
try {
// 获取驱动器文件服务
AbstractBaseFileService fileService = ApplicationContextConfigure.getBean(DriveContext.class).get(parent.getDriveId());
if (fileService == null) {
return Collections.emptyList();
}
// 获取文件列表
List<FileItemDTO> fileItemList = fileService.fileList(parent.getFullPath());
// 转换FileItemDTO为WebDavEntity
return WebDavEntity.convertFromFileItemDTO(fileItemList, parent);
} catch (Exception e) {
LOGGER.warn("get webDav children failed,parent:{},msg:{}", JSON.toJSONString(parent), e.getMessage(), e);
return Collections.emptyList();
}
}
/**
* 获取子文件内容
*
* @param webDavFile WebDav文件
* @return {@link String} ViewResolver模板名称
*/
@Get
public String getChild(WebDavFile webDavFile) {
return JSON.toJSONString(webDavFile);
}
/**
* 获取WebDav实体文件名
*/
@Name
public String getWebDavFile(WebDavEntity webDavEntity) {
return webDavEntity.getName();
}
/**
* 获取WebDav实体展示名称
*/
@DisplayName
public String getDisplayName(WebDavEntity webDavEntity) {
return webDavEntity.getName();
}
/**
* 获取WebDav实体唯一id
*/
@UniqueId
public String getUniqueId(WebDavEntity entity) {
return entity.getId().toString();
}
/**
* 获取WebDav实体修改日期
*/
@ModifiedDate
public Date getModifiedDate(WebDavEntity webDavEntity) {
return webDavEntity.getModifiedDate();
}
/**
* 获取WebDav实体创建日期
*/
@CreatedDate
public Date getCreatedDate(WebDavEntity webDavEntity) {
return webDavEntity.getCreatedDate();
}
/**
* 获取WebDav实体大小
*/
@ContentLength
public Long getContentLength(WebDavEntity entity) {
if (entity instanceof WebDavFile) {
return ((WebDavFile) entity).getSize();
}
// 性能考虑,文件夹暂不进行大小统计
return null;
}
/**
* 获取驱动器id
*
* @return {@link Integer}
*/
private Integer getDriveId() {
String requestUrl = HttpManager.decodeUrl(HttpManager.request().getAbsolutePath());
final String driveId = RegexMatchUtils.matchByIndex("^" + ZFileConstant.WEB_DAV_PREFIX + "/(\\d+)(.*)", requestUrl, 1);
return driveId != null ? Integer.valueOf(driveId) : null;
}
}

View File

@@ -1,10 +1,8 @@
package im.zhaojun.zfile.controller;
package im.zhaojun.zfile.controller.install;
import cn.hutool.crypto.SecureUtil;
import im.zhaojun.zfile.controller.admin.AdminController;
import im.zhaojun.zfile.model.dto.ResultBean;
import im.zhaojun.zfile.model.dto.SystemConfigDTO;
import im.zhaojun.zfile.service.StorageConfigService;
import im.zhaojun.zfile.model.support.ResultBean;
import im.zhaojun.zfile.service.SystemConfigService;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
@@ -23,12 +21,6 @@ public class InstallController {
@Resource
private SystemConfigService systemConfigService;
@Resource
private StorageConfigService storageConfigService;
@Resource
private AdminController adminController;
@GetMapping("/is-installed")
public ResultBean isInstall() {
if (!StringUtils.isEmpty(systemConfigService.getAdminUsername())) {
@@ -38,7 +30,7 @@ public class InstallController {
}
@PostMapping("/install")
@PostMapping("/doInstall")
public ResultBean install(SystemConfigDTO systemConfigDTO) {
if (!StringUtils.isEmpty(systemConfigService.getAdminUsername())) {
return ResultBean.error("请勿重复初始化.");

View File

@@ -1,4 +1,4 @@
package im.zhaojun.zfile.controller;
package im.zhaojun.zfile.controller.onedrive;
import im.zhaojun.zfile.model.support.OneDriveToken;
import im.zhaojun.zfile.service.impl.OneDriveChinaServiceImpl;
@@ -14,8 +14,8 @@ import javax.annotation.Resource;
* @author zhaojun
*/
@Controller
@RequestMapping("/onedrive")
public class OneDriveController {
@RequestMapping(value = {"/onedrive", "/onedirve"})
public class OneDriveCallbackController {
@Resource
private OneDriveServiceImpl oneDriveServiceImpl;
@@ -31,6 +31,13 @@ public class OneDriveController {
return "callback";
}
@GetMapping("/authorize")
public String authorize() {
return "redirect:https://login.microsoftonline.com/common/oauth2/v2.0/authorize?client_id=" + oneDriveServiceImpl.getClientId() +
"&response_type=code&redirect_uri=" + oneDriveServiceImpl.getRedirectUri() +
"&scope=" + oneDriveServiceImpl.getScope();
}
@GetMapping("/china-callback")
public String oneDriveChinaCallback(String code, Model model) {
@@ -40,4 +47,11 @@ public class OneDriveController {
return "callback";
}
@GetMapping("/china-authorize")
public String authorizeChina() {
return "redirect:https://login.chinacloudapi.cn/common/oauth2/v2.0/authorize?client_id=" + oneDriveChinaServiceImpl.getClientId() +
"&response_type=code&redirect_uri=" + oneDriveChinaServiceImpl.getRedirectUri() +
"&scope=" + oneDriveChinaServiceImpl.getScope();
}
}

View File

@@ -0,0 +1,134 @@
package im.zhaojun.zfile.controller.onedrive;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import im.zhaojun.zfile.model.dto.SharePointInfoVO;
import im.zhaojun.zfile.model.support.ResultBean;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.HashMap;
import java.util.Objects;
/**
* @author zhaojun
* SharePoint 工具类
*/
@Controller
@RequestMapping("/sharepoint")
public class SharePointHelperController {
/**
* 根据 AccessToken 获取域名前缀
*/
@PostMapping("/getDomainPrefix")
@ResponseBody
public ResultBean getDomainPrefix(@RequestBody SharePointInfoVO sharePointInfoVO) {
String host = "";
// 判断是标准版还是世纪互联版
if (Objects.equals(sharePointInfoVO.getType(), "Standard")) {
host = "graph.microsoft.com";
} else if (Objects.equals(sharePointInfoVO.getType(), "China")) {
host = "microsoftgraph.chinacloudapi.cn";
}
// 请求 URL
String requestUrl = StrUtil.format("https://{}/v1.0/sites/root", host);
// 构建请求认证 Token 信息
String tokenValue = String.format("%s %s", "Bearer", sharePointInfoVO.getAccessToken());
HashMap<String, String> headers = new HashMap<>();
headers.put("Authorization", tokenValue);
// 请求接口
HttpRequest getRequest = HttpUtil.createGet(requestUrl);
HttpResponse execute = getRequest.addHeaders(headers).execute();
String body = execute.body();
if (execute.getStatus() != HttpStatus.OK.value()) {
return ResultBean.error(body);
}
// 解析前缀
JSONObject jsonObject = JSONObject.parseObject(body);
String hostname = jsonObject.getJSONObject("siteCollection").getString("hostname");
String domainPrefix = StrUtil.subBefore(hostname, ".sharepoint", false);
return ResultBean.successData(domainPrefix);
}
@PostMapping("/getSiteId")
@ResponseBody
public ResultBean getSiteId(@RequestBody SharePointInfoVO sharePointInfoVO) {
// 判断必填参数
if (sharePointInfoVO == null || sharePointInfoVO.getAccessToken() == null || sharePointInfoVO.getSiteName() == null) {
return ResultBean.error("参数不全");
}
String host = "";
// 判断是标准版还是世纪互联版
if (Objects.equals(sharePointInfoVO.getType(), "Standard")) {
host = "graph.microsoft.com";
sharePointInfoVO.setDomainType("com");
} else if (Objects.equals(sharePointInfoVO.getType(), "China")) {
host = "microsoftgraph.chinacloudapi.cn";
sharePointInfoVO.setDomainType("cn");
} else {
return ResultBean.error("参数不全");
}
// 构建请求认证 Token 信息
String tokenValue = String.format("%s %s", "Bearer", sharePointInfoVO.getAccessToken());
HashMap<String, String> authorizationHeaders = new HashMap<>();
authorizationHeaders.put("Authorization", tokenValue);
// 如果没有域名前缀, 则先获取
if (sharePointInfoVO.getDomainPrefix() == null || sharePointInfoVO.getDomainType() == null) {
String requestUrl = StrUtil.format("https://{}/v1.0/sites/root", host);
HttpRequest getRequest = HttpUtil.createGet(requestUrl);
HttpResponse execute = getRequest.addHeaders(authorizationHeaders).execute();
String body = execute.body();
if (execute.getStatus() != HttpStatus.OK.value()) {
return ResultBean.error(body);
}
JSONObject jsonObject = JSONObject.parseObject(body);
String hostname = jsonObject.getJSONObject("siteCollection").getString("hostname");
String domainPrefix = StrUtil.subBefore(hostname, ".sharepoint", false);
sharePointInfoVO.setDomainPrefix(domainPrefix);
}
if (StrUtil.isEmpty(sharePointInfoVO.getSiteType())) {
sharePointInfoVO.setSiteType("/sites/");
}
// 请求接口
String requestUrl = StrUtil.format("https://{}/v1.0/sites/{}.sharepoint.{}:/{}/{}", host,
sharePointInfoVO.getDomainPrefix(),
sharePointInfoVO.getDomainType(),
sharePointInfoVO.getSiteType(),
sharePointInfoVO.getSiteName());
HttpRequest getRequest = HttpUtil.createGet(requestUrl);
HttpResponse execute = getRequest.addHeaders(authorizationHeaders).execute();
String body = execute.body();
// 解析数据
if (execute.getStatus() != HttpStatus.OK.value()) {
return ResultBean.error(body);
}
JSONObject jsonObject = JSONObject.parseObject(body);
return ResultBean.successData(jsonObject.getString("id"));
}
}

View File

@@ -1,16 +1,18 @@
package im.zhaojun.zfile.exception;
import im.zhaojun.zfile.model.dto.ResultBean;
import cn.dev33.satoken.exception.NotLoginException;
import im.zhaojun.zfile.model.support.ResultBean;
import org.apache.catalina.connector.ClientAbortException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.web.HttpMediaTypeNotAcceptableException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import java.net.ConnectException;
/**
* 全局异常处理器
* @author zhaojun
@@ -20,49 +22,60 @@ public class GlobleExceptionHandler {
private static final Logger log = LoggerFactory.getLogger(GlobleExceptionHandler.class);
@ExceptionHandler(SearchDisableException.class)
/**
* 不存在的文件异常
*/
@ExceptionHandler({NotEnabledDriveException.class})
@ResponseBody
@ResponseStatus(code= HttpStatus.INTERNAL_SERVER_ERROR)
public ResultBean searchDisableExceptionHandler(SearchDisableException e) {
if (log.isDebugEnabled()) {
log.debug(e.getMessage(), e);
}
return ResultBean.error(e.getMessage());
public ResultBean notEnabledDrive() {
return ResultBean.error("驱动器已关闭");
}
@ExceptionHandler
@ResponseBody
@ResponseStatus
public ResultBean searchDisableExceptionHandler(StorageStrategyUninitializedException e) {
if (log.isDebugEnabled()) {
log.debug(e.getMessage(), e);
}
return ResultBean.error(e.getMessage());
}
/**
* 不存在的文件异常
*/
@ExceptionHandler({NotExistFileException.class})
@ResponseBody
public ResultBean notExistFile(Exception ex) {
public ResultBean notExistFile() {
return ResultBean.error("文件不存在");
}
/**
* 捕获 ClientAbortException 异常, 不做任何处理, 防止出现大量堆栈日志输出, 此异常不影响功能.
*/
@ExceptionHandler({HttpMediaTypeNotAcceptableException.class, ClientAbortException.class})
@ResponseBody
@ResponseStatus
public void clientAbortException(Exception ex) {
public void clientAbortException() {
// if (log.isDebugEnabled()) {
// log.debug("出现了断开异常:", ex);
// }
}
/**
* 文件预览异常
*/
@ExceptionHandler({PasswordVerifyException.class})
@ResponseBody
@ResponseStatus
public ResultBean passwordVerifyException(PasswordVerifyException ex) {
return ResultBean.error(ex.getMessage());
}
/**
* 无效的驱动器异常
*/
@ExceptionHandler({InvalidDriveException.class})
@ResponseBody
@ResponseStatus
public ResultBean invalidDriveException(InvalidDriveException ex) {
return ResultBean.error(ex.getMessage());
}
/**
* 文件预览异常
*/
@@ -77,24 +90,46 @@ public class GlobleExceptionHandler {
/**
* 初始化异常
*/
@ExceptionHandler({InitializeException.class})
@ExceptionHandler({InitializeDriveException.class})
@ResponseBody
@ResponseStatus
public ResultBean initializeException(InitializeException ex) {
public ResultBean initializeException(InitializeDriveException ex) {
return ResultBean.error(ex.getMessage());
}
/**
* 登录异常拦截器
*/
@ExceptionHandler(NotLoginException.class)
@ResponseBody
@ResponseStatus
public ResultBean handlerNotLoginException(NotLoginException e) {
return ResultBean.error("未登录");
}
/**
* 登录异常拦截器
*/
@ExceptionHandler(ConnectException.class)
@ResponseBody
public ResultBean handlerConnectException(ConnectException e) {
return ResultBean.error("请求失败, 清稍后再试");
}
@ExceptionHandler
@ResponseBody
@ResponseStatus(code= HttpStatus.INTERNAL_SERVER_ERROR)
public ResultBean searchDisableExceptionHandler(Exception e) {
if (log.isDebugEnabled()) {
log.debug(e.getMessage(), e);
@ResponseStatus
public ResultBean extraExceptionHandler(Exception e) {
log.error(e.getMessage(), e);
if (e.getClass() == Exception.class) {
return ResultBean.error("系统异常, 请联系管理员");
} else {
return ResultBean.error(e.getMessage());
}
return ResultBean.error("系统异常, 请联系管理员");
}
}

View File

@@ -0,0 +1,29 @@
package im.zhaojun.zfile.exception;
/**
* 对象存储初始化异常
* @author zhaojun
*/
public class InitializeDriveException extends RuntimeException {
private static final long serialVersionUID = -1920550904063819880L;
public InitializeDriveException() {
}
public InitializeDriveException(String message) {
super(message);
}
public InitializeDriveException(String message, Throwable cause) {
super(message, cause);
}
public InitializeDriveException(Throwable cause) {
super(cause);
}
public InitializeDriveException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
}

View File

@@ -1,29 +0,0 @@
package im.zhaojun.zfile.exception;
/**
* 对象存储初始化异常
* @author zhaojun
*/
public class InitializeException extends RuntimeException {
private static final long serialVersionUID = -1920550904063819880L;
public InitializeException() {
}
public InitializeException(String message) {
super(message);
}
public InitializeException(String message, Throwable cause) {
super(message, cause);
}
public InitializeException(Throwable cause) {
super(cause);
}
public InitializeException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
}

View File

@@ -0,0 +1,27 @@
package im.zhaojun.zfile.exception;
/**
* 无效的驱动器异常
* @author zhaojun
*/
public class InvalidDriveException extends RuntimeException {
public InvalidDriveException() {
}
public InvalidDriveException(String message) {
super(message);
}
public InvalidDriveException(String message, Throwable cause) {
super(message, cause);
}
public InvalidDriveException(Throwable cause) {
super(cause);
}
public InvalidDriveException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
}

View File

@@ -0,0 +1,26 @@
package im.zhaojun.zfile.exception;
/**
* 无效的直链异常
* @author zhaojun
*/
public class InvalidShortLinkException extends RuntimeException {
public InvalidShortLinkException() {
}
public InvalidShortLinkException(String message) {
super(message);
}
public InvalidShortLinkException(String message, Throwable cause) {
super(message, cause);
}
public InvalidShortLinkException(Throwable cause) {
super(cause);
}
public InvalidShortLinkException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
}

View File

@@ -0,0 +1,26 @@
package im.zhaojun.zfile.exception;
/**
* 文件不允许下载异常
* @author zhaojun
*/
public class NotAllowedDownloadException extends RuntimeException {
public NotAllowedDownloadException() {
}
public NotAllowedDownloadException(String message) {
super(message);
}
public NotAllowedDownloadException(String message, Throwable cause) {
super(message, cause);
}
public NotAllowedDownloadException(Throwable cause) {
super(cause);
}
public NotAllowedDownloadException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
}

View File

@@ -0,0 +1,27 @@
package im.zhaojun.zfile.exception;
/**
* 未启用的驱动器异常
*/
public class NotEnabledDriveException extends RuntimeException {
public NotEnabledDriveException() {
}
public NotEnabledDriveException(String message) {
super(message);
}
public NotEnabledDriveException(String message, Throwable cause) {
super(message, cause);
}
public NotEnabledDriveException(Throwable cause) {
super(cause);
}
public NotEnabledDriveException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
}

View File

@@ -1,6 +1,7 @@
package im.zhaojun.zfile.exception;
/**
* 不存在的文件异常
* @author zhaojun
*/
public class NotExistFileException extends RuntimeException {

View File

@@ -0,0 +1,27 @@
package im.zhaojun.zfile.exception;
/**
* 密码校验失败异常
* @author zhaojun
*/
public class PasswordVerifyException extends RuntimeException {
public PasswordVerifyException() {
}
public PasswordVerifyException(String message) {
super(message);
}
public PasswordVerifyException(String message, Throwable cause) {
super(message, cause);
}
public PasswordVerifyException(Throwable cause) {
super(cause);
}
public PasswordVerifyException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
}

View File

@@ -0,0 +1,29 @@
package im.zhaojun.zfile.exception;
/**
* 刷新缓存时出现的异常信息
*
* @author zhaojun
*/
public class RefreshCacheException extends RuntimeException {
public RefreshCacheException() {
}
public RefreshCacheException(String message) {
super(message);
}
public RefreshCacheException(String message, Throwable cause) {
super(message, cause);
}
public RefreshCacheException(Throwable cause) {
super(cause);
}
public RefreshCacheException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
}

View File

@@ -1,26 +0,0 @@
package im.zhaojun.zfile.exception;
/**
* @author zhaojun
*/
public class SearchDisableException extends RuntimeException {
public SearchDisableException() {
}
public SearchDisableException(String message) {
super(message);
}
public SearchDisableException(String message, Throwable cause) {
super(message, cause);
}
public SearchDisableException(Throwable cause) {
super(cause);
}
public SearchDisableException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
}

View File

@@ -26,4 +26,5 @@ public class StorageStrategyUninitializedException extends RuntimeException {
public StorageStrategyUninitializedException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
}
}

View File

@@ -0,0 +1,29 @@
package im.zhaojun.zfile.exception;
/**
* 文件解析异常
* @author zhaojun
*/
public class TextParseException extends RuntimeException {
public TextParseException() {
super();
}
public TextParseException(String message) {
super(message);
}
public TextParseException(String message, Throwable cause) {
super(message, cause);
}
public TextParseException(Throwable cause) {
super(cause);
}
protected TextParseException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
}

View File

@@ -1,29 +0,0 @@
package im.zhaojun.zfile.exception;
/**
* 未知的存储类型异常
* @author zhaojun
*/
public class UnknownStorageTypeException extends RuntimeException {
private static final long serialVersionUID = -4853756482605773655L;
public UnknownStorageTypeException() {
}
public UnknownStorageTypeException(String message) {
super(message);
}
public UnknownStorageTypeException(String message, Throwable cause) {
super(message, cause);
}
public UnknownStorageTypeException(Throwable cause) {
super(cause);
}
public UnknownStorageTypeException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
}

View File

@@ -1,6 +1,8 @@
package im.zhaojun.zfile.filter;
import org.springframework.core.annotation.Order;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Component;
import org.springframework.web.cors.CorsUtils;
import org.springframework.web.filter.GenericFilterBean;
@@ -8,6 +10,7 @@ import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@@ -16,6 +19,9 @@ import java.io.IOException;
* 开启跨域支持. 一般用于开发环境, 或前后端分离部署时开启.
* @author zhaojun
*/
@Order(1)
@WebFilter(value = "/*")
@Component
public class CorsFilter extends GenericFilterBean {
@Override

View File

@@ -1,26 +0,0 @@
package im.zhaojun.zfile.filter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
/**
* @author zhaojun
*/
@Configuration
public class MyCorsFilter {
@Bean
public CorsFilter corsFilter() {
final UrlBasedCorsConfigurationSource urlBasedCorsConfigurationSource = new UrlBasedCorsConfigurationSource();
final CorsConfiguration corsConfiguration = new CorsConfiguration();
corsConfiguration.setAllowCredentials(true);
corsConfiguration.addAllowedOrigin("*");
corsConfiguration.addAllowedHeader("*");
corsConfiguration.addAllowedMethod("*");
urlBasedCorsConfigurationSource.registerCorsConfiguration("/**", corsConfiguration);
return new CorsFilter(urlBasedCorsConfigurationSource);
}
}

View File

@@ -0,0 +1 @@
package im.zhaojun.zfile.filter;

View File

@@ -0,0 +1 @@
package im.zhaojun.zfile.model.constant;

View File

@@ -23,6 +23,12 @@ public class StorageConfigConstant {
public static final String PASSWORD_KEY = "password";
public static final String WEBDAV_USERNAME = "webdavUsername";
public static final String WEBDAV_PASSWORD = "webdavPassword";
public static final String WEBDAV_URL = "webdavUrl";
public static final String HOST_KEY = "host";
public static final String PORT_KEY = "port";
@@ -33,8 +39,16 @@ public class StorageConfigConstant {
public static final String REFRESH_TOKEN_KEY = "refreshToken";
public static final String SHAREPOINT_SITE_ID = "siteId";
public static final String SHAREPOINT_SITE_NAME = "siteName";
public static final String PATH_STYLE = "pathStyle";
public static final String IS_PRIVATE = "isPrivate";
public static final String PROXY_DOMAIN = "proxyDomain";
public static final String REGION_KEY = "region";
}

View File

@@ -12,12 +12,25 @@ public class ZFileConstant {
public final static String USER_HOME = System.getProperty("user.home");
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 DIRECT_LINK_PREFIX = "directlink";
/**
* WebDav前缀
*/
public static String WEB_DAV_PREFIX = "/webdav";
/**
* 系统产生的临时文件路径
*/
public static String TMP_FILE_PATH = "/.zfile/tmp2/";
/**
* 页面文档文件
*/
@@ -38,6 +51,11 @@ public class ZFileConstant {
*/
public static Long TEXT_MAX_FILE_SIZE_KB = 100L;
@Autowired(required = false)
public void setTmpFilePath(@Value("${zfile.tmp.path}") String tmpFilePath) {
ZFileConstant.TMP_FILE_PATH = tmpFilePath;
}
@Autowired(required = false)
public void setHeaderFileName(@Value("${zfile.constant.readme}") String headerFileName) {
@@ -59,5 +77,9 @@ public class ZFileConstant {
ZFileConstant.TEXT_MAX_FILE_SIZE_KB = maxFileSizeKb;
}
@Autowired(required = false)
public void setDirectLinkPrefix(@Value("${zfile.directLinkPrefix}") String directLinkPrefix) {
ZFileConstant.DIRECT_LINK_PREFIX = directLinkPrefix;
}
}
}

View File

@@ -24,4 +24,4 @@ public class AudioInfoDTO {
return audioInfoDTO;
}
}
}

View File

@@ -1,19 +0,0 @@
package im.zhaojun.zfile.model.dto;
import lombok.Data;
import java.util.Date;
import java.util.Set;
/**
* @author zhaojun
*/
@Data
public class CacheConfigDTO {
private Boolean enableCache;
private Boolean cacheFinish;
private Set<String> cacheKeys;
private Integer cacheDirectoryCount;
private Integer cacheFileCount;
private Date lastCacheAutoRefreshDate;
}

View File

@@ -0,0 +1,23 @@
package im.zhaojun.zfile.model.dto;
import lombok.AllArgsConstructor;
import lombok.Data;
import java.util.Set;
/**
* @author zhaojun
*/
@Data
@AllArgsConstructor
public class CacheInfoDTO {
private Integer cacheCount;
private Integer hitCount;
private Integer missCount;
private Set<String> cacheKeys;
}

View File

@@ -18,6 +18,8 @@ public class DriveConfigDTO {
@JsonDeserialize(using = StorageTypeEnumJsonDeSerializerConvert.class)
private StorageTypeEnum type;
private Boolean enable;
private boolean enableCache;
private boolean autoRefreshCache;
@@ -28,6 +30,10 @@ public class DriveConfigDTO {
private boolean searchContainEncryptedFile;
private Integer orderNum;
private StorageStrategyConfig storageStrategyConfig;
private boolean defaultSwitchToImgMode;
}

View File

@@ -0,0 +1,21 @@
package im.zhaojun.zfile.model.dto;
import im.zhaojun.zfile.model.entity.DriveConfig;
import lombok.AllArgsConstructor;
import lombok.Data;
import java.util.List;
/**
* @author Zhao Jun
* 2021/5/26 15:17
*/
@Data
@AllArgsConstructor
public class DriveListDTO {
private List<DriveConfig> driveList;
private Boolean isInstall;
}

View File

@@ -0,0 +1,18 @@
package im.zhaojun.zfile.model.dto;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.List;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class FileListDTO {
private List<FileItemDTO> files;
private SystemFrontConfigDTO config;
}

View File

@@ -1,77 +0,0 @@
package im.zhaojun.zfile.model.dto;
import im.zhaojun.zfile.model.enums.StorageTypeEnum;
import java.util.Map;
/**
* @author zhaojun
*/
public class InstallModelDTO {
private String siteName;
private StorageTypeEnum storageStrategy;
private String username;
private String password;
private String domain;
private Map<String, String> storageStrategyConfig;
public String getSiteName() {
return siteName;
}
public void setSiteName(String siteName) {
this.siteName = siteName;
}
public StorageTypeEnum getStorageStrategy() {
return storageStrategy;
}
public void setStorageStrategy(StorageTypeEnum storageStrategy) {
this.storageStrategy = storageStrategy;
}
public Map<String, String> getStorageStrategyConfig() {
return storageStrategyConfig;
}
public void setStorageStrategyConfig(Map<String, String> storageStrategyConfig) {
this.storageStrategyConfig = storageStrategyConfig;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getDomain() {
return domain;
}
public void setDomain(String domain) {
this.domain = domain;
}
@Override
public String toString() {
return "InstallModelDTO{" +
"siteName='" + siteName + '\'' +
", storageStrategy=" + storageStrategy +
", username='" + username + '\'' +
", password='" + password + '\'' +
", domain='" + domain + '\'' +
", storageStrategyConfig=" + storageStrategyConfig +
'}';
}
}

View File

@@ -0,0 +1,20 @@
package im.zhaojun.zfile.model.dto;
import lombok.Data;
@Data
public class SharePointInfoVO {
private String type;
private String accessToken;
private String domainPrefix;
private String siteType;
private String siteName;
private String domainType;
}

View File

@@ -1,23 +0,0 @@
// package im.zhaojun.zfile.model.dto;
//
// import com.fasterxml.jackson.annotation.JsonProperty;
// import lombok.Data;
// import lombok.ToString;
//
// import java.io.Serializable;
//
// /**
// * @author zhaojun
// */
// @Data
// @ToString
// public class SiteConfigDTO implements Serializable {
//
// private static final long serialVersionUID = 8811196207046121740L;
//
// private String readme;
//
// @JsonProperty("viewConfig")
// private SystemConfigDTO systemConfigDTO;
//
// }

View File

@@ -40,4 +40,20 @@ public class StorageStrategyConfig {
private String basePath;
private String siteId;
private String siteName;
private String siteType;
private String proxyDomain;
private String region;
private String webdavUsername;
private String webdavPassword;
private String webdavUrl;
}

View File

@@ -46,4 +46,10 @@ public class SystemConfigDTO {
private Boolean showAnnouncement;
private String layout;
private Boolean showLinkBtn;
private Boolean showShortLink;
private Boolean showPathLink;
}

View File

@@ -19,9 +19,6 @@ public class SystemFrontConfigDTO {
private Boolean searchEnable;
// @JsonSerialize(using = StorageTypeEnumSerializerConvert.class)
// private StorageTypeEnum storageStrategy;
private String username;
private String domain;
@@ -44,4 +41,16 @@ public class SystemFrontConfigDTO {
private String readme;
private Boolean debugMode;
private Boolean defaultSwitchToImgMode;
private Boolean showLinkBtn;
private Boolean showShortLink;
private Boolean showPathLink;
private String directLinkPrefix;
}

View File

@@ -4,8 +4,6 @@ import im.zhaojun.zfile.model.enums.StorageTypeEnum;
import lombok.Data;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
/**
@@ -18,9 +16,10 @@ import javax.persistence.Id;
public class DriveConfig {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private Boolean enable;
private String name;
private Boolean enableCache;
@@ -35,4 +34,8 @@ public class DriveConfig {
private Boolean searchContainEncryptedFile;
private Integer orderNum;
private Boolean defaultSwitchToImgMode;
}

View File

@@ -0,0 +1,25 @@
package im.zhaojun.zfile.model.entity;
import lombok.Data;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
/**
* @author zhaojun
*/
@Entity(name = "FILTER_CONFIG")
@Data
public class FilterConfig {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private Integer driveId;
private String expression;
}

View File

@@ -0,0 +1,22 @@
package im.zhaojun.zfile.model.entity;
import lombok.Data;
import javax.persistence.*;
import java.util.Date;
@Entity(name = "SHORT_LINK")
@Data
public class ShortLinkConfig {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String key;
private String url;
private Date createDate;
}

View File

@@ -2,6 +2,7 @@ package im.zhaojun.zfile.model.entity;
import im.zhaojun.zfile.model.enums.StorageTypeEnum;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.Column;
import javax.persistence.Entity;
@@ -15,6 +16,7 @@ import javax.persistence.Lob;
*/
@Entity(name = "STORAGE_CONFIG")
@Data
@NoArgsConstructor
public class StorageConfig {
@Id
@@ -38,43 +40,4 @@ public class StorageConfig {
this.title = title;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public StorageTypeEnum getType() {
return type;
}
public void setType(StorageTypeEnum type) {
this.type = type;
}
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}

View File

@@ -28,35 +28,4 @@ public class SystemConfig {
private String remark;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public String getRemark() {
return remark;
}
public void setRemark(String remark) {
this.remark = remark;
}
}

View File

@@ -0,0 +1,112 @@
package im.zhaojun.zfile.model.entity.webdav;
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.util.StringUtils;
import lombok.Data;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;
/**
* WebDav实体
*
* @author me
* @date 2022/4/9
*/
@Data
public class WebDavEntity {
/**
* 唯一ID
*/
private UUID id;
/**
* 驱动器ID
*/
private Integer driveId;
/**
* 名称
*/
private String name;
/**
* 创建时间
*/
private Date createdDate;
/**
* 修改时间
*/
private Date modifiedDate;
/**
* 是否是目录
*/
private boolean isDirectory;
/**
* 父文件夹
*/
private WebDavFolder parent;
public WebDavEntity() {
}
public WebDavEntity(String name, WebDavFolder parent) {
this.id = UUID.randomUUID();
this.name = name;
this.parent = parent;
this.createdDate = new Date();
this.modifiedDate = new Date();
this.isDirectory = true;
}
public WebDavEntity(UUID id, String name, Date createdDate, Date modifiedDate,
WebDavFolder parent) {
this.id = id;
this.name = name;
this.parent = parent;
this.createdDate = createdDate;
this.modifiedDate = modifiedDate;
this.isDirectory = true;
}
/**
* 获取全路径
*/
public String getFullPath() {
if (this.getParent() != null) {
final String parentFullPath = this.getParent().getFullPath();
return StringUtils.removeDuplicateSeparator(parentFullPath + ZFileConstant.PATH_SEPARATOR + this.getName());
} else {
return ZFileConstant.PATH_SEPARATOR;
}
}
public static List<WebDavEntity> convertFromFileItemDTO(List<FileItemDTO> fileItemList, WebDavFolder parent) {
List<WebDavEntity> result = new ArrayList<>();
if (fileItemList == null || fileItemList.size() == 0) {
return result;
}
for (FileItemDTO each : fileItemList) {
WebDavEntity entity = convertFromFileItemDTO(each, parent);
result.add(entity);
}
return result;
}
public static WebDavEntity convertFromFileItemDTO(FileItemDTO fileItemDTO, WebDavFolder parent) {
if (fileItemDTO == null) {
return null;
}
WebDavEntity entity;
if (fileItemDTO.getType() == FileTypeEnum.FOLDER) {
entity = new WebDavFolder(fileItemDTO.getName(), parent);
} else {
entity = new WebDavFile(fileItemDTO.getName(), fileItemDTO.getSize(), parent);
}
entity.setModifiedDate(fileItemDTO.getTime());
entity.setDriveId(parent.getDriveId());
return entity;
}
}

View File

@@ -0,0 +1,37 @@
package im.zhaojun.zfile.model.entity.webdav;
import lombok.Data;
import lombok.EqualsAndHashCode;
import java.util.Date;
import java.util.UUID;
/**
* WebDav文件实体
*
* @author me
* @date 2022/4/9
*/
@EqualsAndHashCode(callSuper = true)
@Data
public class WebDavFile extends WebDavEntity {
/**
* 大小
*/
private Long size;
/**
* 内容类型
*/
private String contentType;
public WebDavFile(String fileName, Long size, WebDavFolder parent) {
super(fileName, parent);
this.setSize(size);
this.setDirectory(false);
}
public WebDavFile(UUID id, String name, Date createdDate, Date modifiedDate, WebDavFolder parent) {
super(id, name, createdDate, modifiedDate, parent);
this.setDirectory(false);
}
}

View File

@@ -0,0 +1,41 @@
package im.zhaojun.zfile.model.entity.webdav;
import lombok.Data;
import lombok.EqualsAndHashCode;
import java.util.Date;
import java.util.UUID;
/**
* WebDav文件夹实体
*
* @author me
* @date 2022/4/9
*/
@EqualsAndHashCode(callSuper = true)
@Data
public class WebDavFolder extends WebDavEntity {
public WebDavFolder(String folderName, WebDavFolder parent) {
super(folderName, parent);
}
public WebDavFolder(String folderName, Integer driveId) {
super(folderName, null);
setDriveId(driveId);
}
public WebDavFolder(UUID id, String name, Date createdDate, Date modifiedDate, WebDavFolder parent) {
super(id, name, createdDate, modifiedDate, parent);
}
public WebDavFile addFile(String fileName, Long size) {
WebDavFile file = new WebDavFile(fileName, size, this);
file.setDirectory(false);
return file;
}
public WebDavFolder addFolder(String folderName) {
return new WebDavFolder(folderName, this);
}
}

View File

@@ -28,4 +28,5 @@ public enum FileTypeEnum {
public void setValue(String value) {
this.value = value;
}
}

View File

@@ -17,6 +17,7 @@ public enum StorageTypeEnum {
LOCAL("local", "本地存储"),
ALIYUN("aliyun", "阿里云 OSS"),
TENCENT("tencent", "腾讯云 COS"),
WebDAV("webdav", "WebDAV"),
UPYUN("upyun", "又拍云 USS"),
FTP("ftp", "FTP"),
UFILE("ufile", "UFile"),
@@ -25,6 +26,8 @@ public enum StorageTypeEnum {
S3("s3", "S3通用协议"),
ONE_DRIVE("onedrive", "OneDrive"),
ONE_DRIVE_CHINA("onedrive-china", "OneDrive 世纪互联"),
SHAREPOINT_DRIVE("sharepoint", "SharePoint"),
SHAREPOINT_DRIVE_CHINA("sharepoint-china", "SharePoint 世纪互联"),
QINIU("qiniu", "七牛云 KODO");
private String key;
@@ -63,4 +66,4 @@ public enum StorageTypeEnum {
return enumMap.get(value.toLowerCase());
}
}
}

View File

@@ -1,39 +0,0 @@
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");
}
}

View File

@@ -1,41 +0,0 @@
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;
}
}

View File

@@ -1,4 +1,4 @@
package im.zhaojun.zfile.model.dto;
package im.zhaojun.zfile.model.support;
import java.io.Serializable;

View File

@@ -1,58 +0,0 @@
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);
}
}

View File

@@ -1,34 +0,0 @@
package im.zhaojun.zfile.model.support;
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();
}
}

View File

@@ -0,0 +1,47 @@
package im.zhaojun.zfile.model.support;
import lombok.Data;
/**
* 用于表示校验结果的类
*/
@Data
public class VerifyResult {
/**
* 是否成功
*/
private boolean passed;
/**
* 消息
*/
private String msg;
/**
* 代码
*/
private Integer code;
public static VerifyResult success() {
VerifyResult verifyResult = new VerifyResult();
verifyResult.setPassed(true);
return verifyResult;
}
public static VerifyResult fail(String msg) {
VerifyResult verifyResult = new VerifyResult();
verifyResult.setPassed(false);
verifyResult.setMsg(msg);
return verifyResult;
}
public static VerifyResult fail(String msg, Integer code) {
VerifyResult verifyResult = new VerifyResult();
verifyResult.setPassed(false);
verifyResult.setMsg(msg);
verifyResult.setCode(code);
return verifyResult;
}
}

View File

@@ -3,6 +3,8 @@ package im.zhaojun.zfile.repository;
import im.zhaojun.zfile.model.entity.DriveConfig;
import im.zhaojun.zfile.model.enums.StorageTypeEnum;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import java.util.List;
@@ -19,8 +21,45 @@ public interface DriverConfigRepository extends JpaRepository<DriveConfig, Integ
* @param type
* 存储类型
*
* @return 指定存储类型的存储
* @return 指定存储类型的驱动
*/
List<DriveConfig> findByType(StorageTypeEnum type);
/**
* 更新驱动器 ID 的排序值
*
* @param orderNum
* 排序值
*
* @param id
* 驱动器 ID
*/
@Modifying
@Query(value="update DRIVER_CONFIG set orderNum = :orderNum where id = :id")
void updateSetOrderNumById(Integer orderNum, Integer id);
/**
* 查询驱动器最大的 ID
*
* @return 驱动器最大 ID
*/
@Query(nativeQuery = true, value = "select max(id) max from DRIVER_CONFIG")
Integer selectMaxId();
/**
* 更新驱动器 ID
*
* @param updateId
* 驱动器原 ID
*
* @param newId
* 驱动器新 ID
*/
@Modifying
@Query(value="update DRIVER_CONFIG set id = :newId where id = :updateId")
void updateId(Integer updateId, Integer newId);
}

View File

@@ -0,0 +1,44 @@
package im.zhaojun.zfile.repository;
import im.zhaojun.zfile.model.entity.FilterConfig;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import java.util.List;
/**
* @author zhaojun
*/
@Repository
public interface FilterConfigRepository extends JpaRepository<FilterConfig, Integer> {
/**
* 获取驱动器下的所有规则
* @param driveId
* 驱动器 ID
*/
List<FilterConfig> findByDriveId(Integer driveId);
/**
* 根据驱动器 ID 删除其所有的规则
* @param driveId
* 驱动器 ID
*/
void deleteByDriveId(Integer driveId);
/**
* 更新驱动器 ID 对应的参数设置为新的驱动器 ID
*
* @param updateId
* 驱动器原 ID
*
* @param newId
* 驱动器新 ID
*/
@Modifying
@Query(value="update FILTER_CONFIG set driveId = :newId where driveId = :updateId")
void updateDriveId(Integer updateId, Integer newId);
}

View File

@@ -0,0 +1,75 @@
package im.zhaojun.zfile.repository;
import im.zhaojun.zfile.model.entity.ShortLinkConfig;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import java.util.Date;
/**
* @author zhaojun
*/
@Repository
public interface ShortLinkConfigRepository extends JpaRepository<ShortLinkConfig, Integer>, JpaSpecificationExecutor<ShortLinkConfig> {
/**
* 获取驱动器下的所有规则
*
* @param key
* 短链 Key
*/
ShortLinkConfig findFirstByKey(String key);
@Query(nativeQuery = true,
value = " select * from SHORT_LINK where " +
" key like concat('%', :key,'%') " +
" and url like concat('%', :url,'%') " +
" and (:dateFrom is null or create_date >= :dateFrom" +
" and (:dateTo is null or create_date <= :dateTo) ",
countQuery = " select count(1) from SHORT_LINK where " +
" key like concat('%', :key,'%') " +
" and url like concat('%', :url,'%') " +
" and (:dateFrom is null or create_date >= :dateFrom" +
" and (:dateTo is null or create_date <= :dateTo) "
)
// @Query(nativeQuery = true,
// value = " select * from SHORT_LINK where " +
// " key like concat('%', :key,'%') " +
// " and url like concat('%', :url,'%') " +
// " and (:dateFrom is null or date_format(create_date, '%Y-%m-%d') >= date_format(:dateFrom, '%Y-%m-%d'))" +
// " and (:dateTo is null or date_format(create_date, '%Y-%m-%d') <= date_format(:dateTo, '%Y-%m-%d')) ) ",
// countQuery = " select count(1) from SHORT_LINK where " +
// " key like concat('%', :key,'%') " +
// " and url like concat('%', :url,'%') " +
// " and (:dateFrom is null or date_format(create_date, '%Y-%m-%d') >= date_format(:dateFrom, '%Y-%m-%d'))" +
// " and (:dateTo is null or date_format(create_date, '%Y-%m-%d') <= date_format(:dateTo, '%Y-%m-%d')) ) "
// )
Page<ShortLinkConfig> findByPage(String key, String url, Date dateFrom, Date dateTo, Pageable pageable);
/**
* 获取驱动器下的所有规则
*
* @param url
* 短链 URL
*/
ShortLinkConfig findFirstByUrl(String url);
/**
* 更新驱动器 ID
*
* @param updateSubPath
* 原路径部分名称
*
* @param newSubPath
* 修改后路径部分名称
*/
@Modifying
@Query(value="update SHORT_LINK set url = replace(url, :updateSubPath, :newSubPath)")
void updateUrlDriveId(String updateSubPath, String newSubPath);
}

View File

@@ -3,6 +3,8 @@ package im.zhaojun.zfile.repository;
import im.zhaojun.zfile.model.entity.StorageConfig;
import im.zhaojun.zfile.model.enums.StorageTypeEnum;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import java.util.List;
@@ -68,4 +70,18 @@ public interface StorageConfigRepository extends JpaRepository<StorageConfig, In
*/
StorageConfig findByDriveIdAndKey(Integer driveId, String key);
/**
* 更新驱动器 ID 对应的参数设置为新的驱动器 ID
*
* @param updateId
* 驱动器原 ID
*
* @param newId
* 驱动器新 ID
*/
@Modifying
@Query(value="update STORAGE_CONFIG set driveId = :newId where driveId = :updateId")
void updateDriveId(Integer updateId, Integer newId);
}

View File

@@ -1,19 +1,17 @@
package im.zhaojun.zfile.schedule;
import com.alibaba.fastjson.JSON;
import im.zhaojun.zfile.context.DriveContext;
import im.zhaojun.zfile.model.entity.DriveConfig;
import im.zhaojun.zfile.model.enums.StorageTypeEnum;
import im.zhaojun.zfile.service.DriveConfigService;
import im.zhaojun.zfile.service.base.AbstractOneDriveServiceBase;
import im.zhaojun.zfile.service.impl.OneDriveChinaServiceImpl;
import im.zhaojun.zfile.service.impl.OneDriveServiceImpl;
import im.zhaojun.zfile.context.DriveContext;
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.List;
/**
@@ -23,13 +21,7 @@ import java.util.List;
@Configuration
@EnableScheduling
@Slf4j
public class GlobalScheduleTask {
@Resource
private OneDriveServiceImpl oneDriveServiceImpl;
@Resource
private OneDriveChinaServiceImpl oneDriveChinaServiceImpl;
public class OneDriveTokenRefreshSchedule {
@Resource
private DriveConfigService driveConfigService;
@@ -48,23 +40,22 @@ public class GlobalScheduleTask {
List<DriveConfig> driveConfigList = driveConfigService.findByType(StorageTypeEnum.ONE_DRIVE);
driveConfigList.addAll(driveConfigService.findByType(StorageTypeEnum.ONE_DRIVE_CHINA));
driveConfigList.addAll(driveConfigService.findByType(StorageTypeEnum.SHAREPOINT_DRIVE));
driveConfigList.addAll(driveConfigService.findByType(StorageTypeEnum.SHAREPOINT_DRIVE_CHINA));
driveConfigList.forEach(driveConfig -> {
StorageTypeEnum storageType = driveConfig.getType();
String name = driveConfig.getName();
try {
AbstractOneDriveServiceBase driveService = (AbstractOneDriveServiceBase) driveContext.getDriveService(driveConfig.getId());
AbstractOneDriveServiceBase driveService = (AbstractOneDriveServiceBase) driveContext.get(driveConfig.getId());
driveService.refreshOneDriveToken();
log.info("刷新驱动器 {}, {} key 时间: {}", name, storageType.getDescription(), LocalDateTime.now());
log.info("尝试刷新 OneDrive Token, DriveInfo: {}", JSON.toJSONString(driveConfig));
} catch (Exception e) {
log.debug("刷新驱动器 " + name + " Token 失败.", e);
log.error("刷新 OneDrive Token 失败, DriveInfo: {}", JSON.toJSONString(driveConfig), e);
}
});
} catch (Throwable e) {
log.debug("尝试调用 OneDrive 自动刷新 AccessToken 定时任务出现未知异常", e);
log.error("尝试调用 OneDrive 自动刷新 AccessToken 定时任务出现未知异常", e);
}
}

View File

@@ -1,20 +0,0 @@
package im.zhaojun.zfile.security;
import cn.hutool.crypto.SecureUtil;
import org.springframework.security.crypto.password.PasswordEncoder;
/**
* @author zhaojun
*/
public class Md5PasswordEncoder implements PasswordEncoder {
@Override
public String encode(CharSequence rawPassword) {
return SecureUtil.md5(rawPassword.toString());
}
@Override
public boolean matches(CharSequence rawPassword, String encodedPassword) {
return SecureUtil.md5(rawPassword.toString()).equals(encodedPassword);
}
}

View File

@@ -1,134 +0,0 @@
package im.zhaojun.zfile.security;
import com.fasterxml.jackson.databind.ObjectMapper;
import im.zhaojun.zfile.model.dto.ResultBean;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpMethod;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.firewall.HttpFirewall;
import org.springframework.security.web.firewall.StrictHttpFirewall;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
/**
* 自定义 Security 配置类
*
* @author zhaojun
*/
@EnableWebSecurity
public class MySecurityConfig extends WebSecurityConfigurerAdapter {
@Resource
private ObjectMapper objectMapper;
@Override
protected void configure(HttpSecurity http) throws Exception {
http
// .authenticationProvider(authenticationProvider())
.exceptionHandling()
// 未登录时,进行 json 格式的提示.
.authenticationEntryPoint((request, response, authException) -> {
response.setContentType("application/json;charset=utf-8");
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
PrintWriter out = response.getWriter();
out.write(objectMapper.writeValueAsString(ResultBean.error("未登录")));
out.flush();
out.close();
})
.and()
.authorizeRequests()
.antMatchers("/").permitAll()
.antMatchers("/admin/**").authenticated()
.and()
.formLogin() // 使用自带的登录
// 登录失败返回json
.failureHandler((request, response, ex) -> {
response.setContentType("application/json;charset=utf-8");
response.setStatus(HttpServletResponse.SC_FORBIDDEN);
PrintWriter out = response.getWriter();
String msg;
if (ex instanceof UsernameNotFoundException || ex instanceof BadCredentialsException) {
msg = "用户名或密码错误";
} else {
msg = "登录失败";
}
out.write(objectMapper.writeValueAsString(ResultBean.error(msg)));
out.flush();
out.close();
})
// 登录成功返回json
.successHandler((request, response, authentication) -> {
response.setContentType("application/json;charset=utf-8");
PrintWriter out = response.getWriter();
out.write(objectMapper.writeValueAsString(ResultBean.success(authentication)));
out.flush();
out.close();
})
.and()
.exceptionHandling()
// 没有权限返回json
.accessDeniedHandler((request, response, ex) -> {
response.setContentType("application/json;charset=utf-8");
response.setStatus(HttpServletResponse.SC_FORBIDDEN);
PrintWriter out = response.getWriter();
out.write(objectMapper.writeValueAsString(ResultBean.error("权限不足")));
out.flush();
out.close();
})
.and()
.logout()
// 退出成功,返回 json
.logoutSuccessHandler((request, response, authentication) -> {
response.setContentType("application/json;charset=utf-8");
PrintWriter out = response.getWriter();
out.write(objectMapper.writeValueAsString(ResultBean.error("注销成功")));
out.flush();
out.close();
})
.and()
.logout().permitAll();
http.cors();
http.csrf().disable();
http.headers().frameOptions().sameOrigin();
}
@Bean
public HttpFirewall allowUrlEncodedSlashHttpFirewall() {
StrictHttpFirewall firewall = new StrictHttpFirewall();
firewall.setAllowUrlEncodedPercent(true);
return firewall;
}
@Override
public void configure(AuthenticationManagerBuilder web) throws Exception {
web.userDetailsService(myUserDetailsServiceImpl()).passwordEncoder(passwordEncoder());
}
@Bean
public MyUserDetailsServiceImpl myUserDetailsServiceImpl() {
return new MyUserDetailsServiceImpl();
}
@Override
public void configure(WebSecurity web) {
// 对于在 header 里面增加 token 等类似情况,放行所有 OPTIONS 请求。
web.ignoring().antMatchers(HttpMethod.OPTIONS, "/**");
web.httpFirewall(allowUrlEncodedSlashHttpFirewall());
}
@Bean
public static PasswordEncoder passwordEncoder() {
return new Md5PasswordEncoder();
}
}

View File

@@ -1,34 +0,0 @@
package im.zhaojun.zfile.security;
import im.zhaojun.zfile.model.dto.SystemConfigDTO;
import im.zhaojun.zfile.service.SystemConfigService;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import javax.annotation.Resource;
import java.util.Collections;
import java.util.Objects;
/**
* @author zhaojun
*/
public class MyUserDetailsServiceImpl implements UserDetailsService {
@Resource
private SystemConfigService systemConfigService;
/**
* 授权的时候是对角色授权,认证的时候应该基于资源,而不是角色,因为资源是不变的,而用户的角色是会变的
*/
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
SystemConfigDTO systemConfig = systemConfigService.getSystemConfig();
if (!Objects.equals(systemConfig.getUsername(), username)) {
throw new UsernameNotFoundException("用户名不存在");
}
return new User(systemConfig.getUsername(), systemConfig.getPassword(), Collections.emptyList());
}
}

View File

@@ -1,19 +1,28 @@
package im.zhaojun.zfile.service;
import com.alibaba.fastjson.JSONObject;
import im.zhaojun.zfile.cache.ZFileCache;
import im.zhaojun.zfile.context.DriveContext;
import im.zhaojun.zfile.context.StorageTypeContext;
import im.zhaojun.zfile.exception.InitializeException;
import im.zhaojun.zfile.exception.InitializeDriveException;
import im.zhaojun.zfile.model.constant.StorageConfigConstant;
import im.zhaojun.zfile.model.constant.ZFileConstant;
import im.zhaojun.zfile.model.dto.CacheInfoDTO;
import im.zhaojun.zfile.model.dto.DriveConfigDTO;
import im.zhaojun.zfile.model.dto.StorageStrategyConfig;
import im.zhaojun.zfile.model.entity.DriveConfig;
import im.zhaojun.zfile.model.entity.StorageConfig;
import im.zhaojun.zfile.model.enums.StorageTypeEnum;
import im.zhaojun.zfile.repository.DriverConfigRepository;
import im.zhaojun.zfile.repository.FilterConfigRepository;
import im.zhaojun.zfile.repository.ShortLinkConfigRepository;
import im.zhaojun.zfile.repository.StorageConfigRepository;
import im.zhaojun.zfile.service.base.AbstractBaseFileService;
import im.zhaojun.zfile.context.DriveContext;
import im.zhaojun.zfile.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@@ -21,6 +30,7 @@ import javax.annotation.Resource;
import java.lang.reflect.Field;
import java.util.List;
import java.util.Objects;
import java.util.Set;
/**
* 驱动器 Service 类
@@ -36,18 +46,43 @@ public class DriveConfigService {
@Resource
private StorageConfigRepository storageConfigRepository;
@Resource
private FilterConfigRepository filterConfigRepository;
@Resource
private ShortLinkConfigRepository shortLinkConfigRepository;
@Resource
private DriveContext driveContext;
@Resource
private ZFileCache zFileCache;
public static final Class<StorageStrategyConfig> STORAGE_STRATEGY_CONFIG_CLASS = StorageStrategyConfig.class;
/**
* 获取所有驱动器列表
*
* @return 驱动器列表
*/
public List<DriveConfig> list() {
return driverConfigRepository.findAll();
Sort sort = Sort.by(Sort.Direction.ASC,"orderNum");
return driverConfigRepository.findAll(sort);
}
/**
* 获取所有已启用的驱动器列表
*
* @return 已启用的驱动器列表
*/
public List<DriveConfig> listOnlyEnable() {
DriveConfig driveConfig = new DriveConfig();
driveConfig.setEnable(true);
Example<DriveConfig> example = Example.of(driveConfig);
Sort sort = Sort.by(Sort.Direction.ASC,"orderNum");
return driverConfigRepository.findAll(example, sort);
}
@@ -60,7 +95,7 @@ public class DriveConfigService {
* @return 驱动器设置
*/
public DriveConfig findById(Integer id) {
return driverConfigRepository.getOne(id);
return driverConfigRepository.findById(id).orElse(null);
}
@@ -78,6 +113,10 @@ public class DriveConfigService {
DriveConfigDTO driveConfigDTO = new DriveConfigDTO();
List<StorageConfig> storageConfigList = storageConfigRepository.findByDriveId(driveConfig.getId());
Boolean defaultSwitchToImgMode = driveConfig.getDefaultSwitchToImgMode();
if (defaultSwitchToImgMode == null) {
driveConfig.setDefaultSwitchToImgMode(false);
}
BeanUtils.copyProperties(driveConfig, driveConfigDTO);
StorageStrategyConfig storageStrategyConfig = new StorageStrategyConfig();
@@ -95,9 +134,7 @@ public class DriveConfigService {
declaredField.set(storageStrategyConfig, value);
}
} catch (NoSuchFieldException | IllegalAccessException e) {
if (log.isDebugEnabled()) {
log.debug("通过反射, 将字段 {" + key + "}注入 DriveConfigDTO 时出现异常:", e);
}
log.error("通过反射, 将字段 {} 注入 DriveConfigDTO 时出现异常:", key, e);
}
}
@@ -106,6 +143,7 @@ public class DriveConfigService {
return driveConfigDTO;
}
/**
* 获取指定驱动器的存储策略.
*
@@ -115,18 +153,26 @@ public class DriveConfigService {
* @return 驱动器对应的存储策略.
*/
public StorageTypeEnum findStorageTypeById(Integer id) {
// return findById(id).getType();
return driverConfigRepository.findById(id).get().getType();
}
/**
* 更新驱动器设置
* @param driveConfig 驱动器设置
*/
public void updateDriveConfig(DriveConfig driveConfig) {
driverConfigRepository.save(driveConfig);
}
/**
* 保存驱动器基本信息及其对应的参数设置
*
* @param driveConfigDTO 驱动器 DTO 对象
*/
@Transactional(rollbackFor = Exception.class)
public void save(DriveConfigDTO driveConfigDTO) {
public void saveDriveConfigDTO(DriveConfigDTO driveConfigDTO) {
// 判断是新增还是修改
boolean updateFlag = driveConfigDTO.getId() != null;
@@ -135,6 +181,11 @@ public class DriveConfigService {
DriveConfig driveConfig = new DriveConfig();
StorageTypeEnum storageType = driveConfigDTO.getType();
BeanUtils.copyProperties(driveConfigDTO, driveConfig);
if (driveConfig.getId() == null) {
Integer nextId = selectNextId();
driveConfig.setId(nextId);
}
driverConfigRepository.save(driveConfig);
// 保存存储策略设置.
@@ -142,12 +193,8 @@ public class DriveConfigService {
AbstractBaseFileService storageTypeService = StorageTypeContext.getStorageTypeService(storageType);
List<StorageConfig> storageConfigList;
if (updateFlag) {
storageConfigList = storageConfigRepository.findByDriveId(driveConfigDTO.getId());
} else {
storageConfigList = storageTypeService.storageStrategyConfigList();
}
List<StorageConfig> storageConfigList = storageTypeService.storageStrategyConfigList();
storageConfigRepository.deleteByDriveId(driveConfigDTO.getId());
for (StorageConfig storageConfig : storageConfigList) {
String key = storageConfig.getKey();
@@ -161,20 +208,65 @@ public class DriveConfigService {
storageConfig.setType(storageType);
storageConfig.setDriveId(driveConfig.getId());
} catch (IllegalAccessException | NoSuchFieldException e) {
if (log.isDebugEnabled()) {
log.debug("通过反射, 从 StorageStrategyConfig 中获取字段 {" + key + "} 时出现异常:", e);
}
log.error("通过反射, 从 StorageStrategyConfig 中获取字段 {} 时出现异常:", key, e);
}
}
storageConfigRepository.saveAll(storageConfigList);
driveContext.initDrive(driveConfig.getId());
driveContext.init(driveConfig.getId());
AbstractBaseFileService driveService = driveContext.getDriveService(driveConfig.getId());
AbstractBaseFileService driveService = driveContext.get(driveConfig.getId());
if (driveService.getIsUnInitialized()) {
throw new InitializeException("初始化异常, 请检查配置是否正确.");
throw new InitializeDriveException("初始化异常, 请检查配置是否正确.");
}
if (driveConfig.getAutoRefreshCache()) {
startAutoCacheRefresh(driveConfig.getId());
} else if (updateFlag){
stopAutoCacheRefresh(driveConfig.getId());
}
}
/**
* 查询驱动器最大的 ID
*
* @return 驱动器最大 ID
*/
public Integer selectNextId() {
Integer maxId = driverConfigRepository.selectMaxId();
if (maxId == null) {
return 1;
} else {
return maxId + 1;
}
}
/**
* 更新驱动器 ID
*
* @param updateId
* 驱动器原 ID
*
* @param newId
* 驱动器新 ID
*/
@Transactional
public void updateId(Integer updateId, Integer newId) {
zFileCache.clear(updateId);
driverConfigRepository.updateId(updateId, newId);
storageConfigRepository.updateDriveId(updateId, newId);
filterConfigRepository.updateDriveId(updateId, newId);
String updateSubPath = StringUtils.concatUrl(StringUtils.DELIMITER_STR, ZFileConstant.DIRECT_LINK_PREFIX, String.valueOf(updateId));
String newSubPath = StringUtils.concatUrl(StringUtils.DELIMITER_STR, ZFileConstant.DIRECT_LINK_PREFIX, String.valueOf(newId));
shortLinkConfigRepository.updateUrlDriveId(updateSubPath, newSubPath);
driveContext.updateDriveId(updateId, newId);
}
@@ -186,9 +278,20 @@ public class DriveConfigService {
*/
@Transactional(rollbackFor = Exception.class)
public void deleteById(Integer id) {
if (log.isDebugEnabled()) {
log.debug("尝试删除驱动器, driveId: {}", id);
}
DriveConfig driveConfig = driverConfigRepository.getOne(id);
driverConfigRepository.deleteById(id);
storageConfigRepository.deleteByDriveId(id);
driveContext.destroyDrive(id);
if (driveConfig.getEnableCache()) {
zFileCache.stopAutoCacheRefresh(id);
zFileCache.clear(id);
}
driveContext.destroy(id);
if (log.isDebugEnabled()) {
log.debug("尝试删除驱动器成功, 已清理相关数据, driveId: {}", id);
}
}
@@ -198,7 +301,7 @@ public class DriveConfigService {
* @param type
* 存储类型
*
* @return 指定存储类型的存储
* @return 指定存储类型的驱动
*/
public List<DriveConfig> findByType(StorageTypeEnum type) {
return driverConfigRepository.findByType(type);
@@ -221,4 +324,91 @@ public class DriveConfigService {
driverConfigRepository.save(driveConfig);
}
}
/**
* 获取指定驱动器的缓存信息
* @param driveId
* 驱动器 ID
* @return 缓存信息
*/
public CacheInfoDTO findCacheInfo(Integer driveId) {
int hitCount = zFileCache.getHitCount(driveId);
int missCount = zFileCache.getMissCount(driveId);
Set<String> keys = zFileCache.keySet(driveId);
int cacheCount = keys.size();
return new CacheInfoDTO(cacheCount, hitCount, missCount, keys);
}
/**
* 刷新指定 key 的缓存:
* 1. 清空此 key 的缓存.
* 2. 重新调用方法写入缓存.
*
* @param driveId
* 驱动器 ID
*
* @param key
* 缓存 key (文件夹名称)
*/
public void refreshCache(Integer driveId, String key) throws Exception {
if (log.isDebugEnabled()) {
log.debug("手动刷新缓存 driveId: {}, key: {}", driveId, key);
}
zFileCache.remove(driveId, key);
AbstractBaseFileService baseFileService = driveContext.get(driveId);
baseFileService.fileList(key);
}
/**
* 开启缓存自动刷新
*
* @param driveId
* 驱动器 ID
*/
public void startAutoCacheRefresh(Integer driveId) {
DriveConfig driveConfig = findById(driveId);
driveConfig.setAutoRefreshCache(true);
driverConfigRepository.save(driveConfig);
zFileCache.startAutoCacheRefresh(driveId);
}
/**
* 停止缓存自动刷新
*
* @param driveId
* 驱动器 ID
*/
public void stopAutoCacheRefresh(Integer driveId) {
DriveConfig driveConfig = findById(driveId);
driveConfig.setAutoRefreshCache(false);
driverConfigRepository.save(driveConfig);
zFileCache.stopAutoCacheRefresh(driveId);
}
/**
* 清理缓存
*
* @param driveId
* 驱动器 ID
*/
public void clearCache(Integer driveId) {
zFileCache.clear(driveId);
}
/**
* 交换驱动器排序
*/
@Transactional(rollbackFor = Exception.class)
public void saveDriveDrag(List<JSONObject> driveConfigs) {
for (int i = 0; i < driveConfigs.size(); i++) {
JSONObject item = driveConfigs.get(i);
driverConfigRepository.updateSetOrderNumById(i, item.getInteger("id"));
}
}
}

View File

@@ -0,0 +1,68 @@
package im.zhaojun.zfile.service;
import cn.hutool.core.util.StrUtil;
import im.zhaojun.zfile.model.entity.FilterConfig;
import im.zhaojun.zfile.repository.FilterConfigRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.nio.file.FileSystems;
import java.nio.file.PathMatcher;
import java.nio.file.Paths;
import java.util.List;
/**
* @author zhaojun
*/
@Slf4j
@Service
public class FilterConfigService {
@Resource
private FilterConfigRepository filterConfigRepository;
public List<FilterConfig> findByDriveId(Integer driveId) {
return filterConfigRepository.findByDriveId(driveId);
}
@Transactional(rollbackFor = Exception.class)
public void batchSave(List<FilterConfig> filterConfigList, Integer driveId) {
filterConfigRepository.deleteByDriveId(driveId);
filterConfigRepository.saveAll(filterConfigList);
}
/**
* 指定驱动器下的文件名称, 根据过滤表达式判断是否会显示, 如果符合任意一条表达式, 则不显示, 反之则显示.
* @param driveId
* 驱动器 ID
* @param fileName
* 文件名
* @return 是否显示
*/
public boolean filterResultIsHidden(Integer driveId, String fileName) {
List<FilterConfig> filterConfigList = findByDriveId(driveId);
for (FilterConfig filterConfig : filterConfigList) {
String expression = filterConfig.getExpression();
if (StrUtil.isEmpty(expression)) {
return false;
}
try {
PathMatcher pathMatcher = FileSystems.getDefault().getPathMatcher("glob:" + expression);
boolean match = pathMatcher.matches(Paths.get(fileName));
if (match) {
return true;
}
log.debug("regex: {}, name {}, contains: {}", expression, fileName, match);
} catch (Exception e) {
log.debug("regex: {}, name {}, parse error, skip expression", expression, fileName);
}
}
return false;
}
}

View File

@@ -0,0 +1,84 @@
package im.zhaojun.zfile.service;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import im.zhaojun.zfile.model.entity.ShortLinkConfig;
import im.zhaojun.zfile.repository.ShortLinkConfigRepository;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import javax.persistence.criteria.Predicate;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;
@Service
public class ShortLinkConfigService {
@Resource
private ShortLinkConfigRepository shortLinkConfigRepository;
public ShortLinkConfig findByKey(String key) {
return shortLinkConfigRepository.findFirstByKey(key);
}
public ShortLinkConfig findById(Integer id) {
Optional<ShortLinkConfig> shortLinkConfigOptional = shortLinkConfigRepository.findById(id);
return shortLinkConfigOptional.orElse(null);
}
public ShortLinkConfig findByUrl(String url) {
return shortLinkConfigRepository.findFirstByUrl(url);
}
public void save(ShortLinkConfig shortLinkConfig) {
shortLinkConfig.setCreateDate(new Date());
shortLinkConfigRepository.save(shortLinkConfig);
}
public Page<ShortLinkConfig> find(String key,
String url,
String dateFrom,
String dateTo,
Integer page,
Integer limit,
String orderBy,
String orderDirection) {
Sort sort = Sort.by("desc".equals(orderDirection) ? Sort.Direction.DESC : Sort.Direction.ASC, orderBy);
Pageable pageable = PageRequest.of(page - 1, limit, sort);
Specification<ShortLinkConfig> specification = (root, criteriaQuery, criteriaBuilder) -> {
List<Predicate> predicates = new ArrayList<>();
if (StrUtil.isNotEmpty(dateFrom) && StrUtil.isNotEmpty(dateTo)) {
predicates.add(criteriaBuilder.between(root.get("createDate"),
DateUtil.parseDateTime(dateFrom + " 00:00:00"),
DateUtil.parseDateTime(dateTo + " 23:59:59")));
}
if (StrUtil.isNotEmpty(key)) {
predicates.add(criteriaBuilder.like(root.get("key"), "%" + key + '%'));
}
if (StrUtil.isNotEmpty(url)) {
predicates.add(criteriaBuilder.like(root.get("url"), "%" + url + '%'));
}
return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
};
return shortLinkConfigRepository.findAll(specification, pageable);
}
public void deleteById(Integer id) {
shortLinkConfigRepository.deleteById(id);
}
}

View File

@@ -1,8 +1,10 @@
package im.zhaojun.zfile.service;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import im.zhaojun.zfile.cache.ZFileCache;
import im.zhaojun.zfile.exception.InvalidDriveException;
import im.zhaojun.zfile.model.constant.SystemConfigConstant;
import im.zhaojun.zfile.model.dto.SystemConfigDTO;
import im.zhaojun.zfile.model.dto.SystemFrontConfigDTO;
@@ -61,9 +63,7 @@ public class SystemConfigService {
Object convertVal = Convert.convert(field.getType(), strVal);
field.set(systemConfigDTO, convertVal);
} catch (NoSuchFieldException | IllegalAccessException e) {
if (log.isDebugEnabled()) {
log.debug("通过反射, 将字段 {" + key + "}注入 SystemConfigDTO 时出现异常:", e);
}
log.error("通过反射, 将字段 {} 注入 SystemConfigDTO 时出现异常:", key, e);
}
}
@@ -93,9 +93,7 @@ public class SystemConfigService {
try {
val = field.get(systemConfigDTO);
} catch (IllegalAccessException e) {
if (log.isDebugEnabled()) {
log.debug("通过反射, 从 SystemConfigDTO 获取字段 {" + key + "} 时出现异常:", e);
}
log.error("通过反射, 从 SystemConfigDTO 获取字段 {} 时出现异常:", key, e);
}
if (val != null) {
@@ -124,6 +122,9 @@ public class SystemConfigService {
BeanUtils.copyProperties(systemConfig, systemFrontConfigDTO);
DriveConfig driveConfig = driveConfigService.findById(driveId);
if (driveConfig == null) {
throw new InvalidDriveException("此驱动器不存在或初始化失败, 请检查后台参数配置");
}
systemFrontConfigDTO.setSearchEnable(driveConfig.getSearchEnable());
return systemFrontConfigDTO;
}
@@ -163,4 +164,24 @@ public class SystemConfigService {
return systemConfigDTO.getUsername();
}
/**
* 获取站点域名
*
* @return 站点域名
*/
public String getDomain() {
SystemConfigDTO systemConfigDTO = getSystemConfig();
return systemConfigDTO.getDomain();
}
/**
* 获取是否已安装初始化
*
* @return 是否已安装初始化
*/
public boolean getIsInstall() {
SystemConfigDTO systemConfigDTO = getSystemConfig();
return StrUtil.isNotEmpty(systemConfigDTO.getUsername());
}
}

View File

@@ -1,69 +0,0 @@
// 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.context.DriveContext;
// 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 DriveContext driveContext;
//
// /**
// * 构建指定路径下标题, 页面文档信息
// * @param path 路径
// */
// public SiteConfigDTO getConfig(Integer driveId, String path) throws Exception {
//
// SiteConfigDTO siteConfigDTO = new SiteConfigDTO();
//
// AbstractBaseFileService fileService = driveContext.getDriveService(driveId);
//
// List<FileItemDTO> fileItemList;
//
// if (Objects.equals(fileService.getStorageTypeEnum(), 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;
// }
//
// }

Some files were not shown because too many files have changed in this diff Show More