Skip to content

Commit

Permalink
优化代码及配置
Browse files Browse the repository at this point in the history
  • Loading branch information
suninformation committed Feb 21, 2022
1 parent 128b4de commit bd13f08
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 104 deletions.
217 changes: 124 additions & 93 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
### YMP-FileUploader
# YMATE-MODULE-FILEUPLOADER

> 文件上传及资源访问服务模块,特性如下:
>
> - 支持文件指纹匹配,秒传;
> - 支持图片文件多种规则等比例压缩;
> - 支持视频文件截图;
> - 支持上传文件`ContentType`白名单过滤;
> - 支持主从负载模式配置;
> - 支持自定义响应报文内容;
> - 支持自定义扩展文件存储策略;
> - 支持跨域上传文件及用户身份验证;
[![Maven Central status](https://img.shields.io/maven-central/v/net.ymate.module/ymate-module-fileuploader.svg)](https://search.maven.org/artifact/net.ymate.module/ymate-module-fileuploader)
[![LICENSE](https://img.shields.io/github/license/suninformation/ymate-module-fileuploader.svg)](https://gitee.com/suninformation/ymate-module-fileuploader/blob/master/LICENSE)

#### Maven包依赖

基于 YMP 框架实现的文件上传及资源访问服务模块,特性如下:

- 支持文件指纹匹配,秒传;
- 支持图片文件多种规则等比例压缩;
- 支持视频文件截图;
- 支持上传文件 `ContentType` 白名单过滤;
- 支持主从负载模式配置;
- 支持自定义响应报文内容;
- 支持自定义扩展文件存储策略;
- 支持跨域上传文件及用户身份验证;
- 支持 MongoDB 文件存储;



## Maven包依赖

```xml
<dependency>
Expand All @@ -21,7 +28,10 @@
</dependency>
```

### 模块配置参数说明


## 模块配置参数说明

```properties
#-------------------------------------
# module.fileuploader 模块初始化参数
Expand All @@ -45,7 +55,7 @@ ymp.configs.module.fileuploader.service_enabled=
# 是否开启代理模式, 默认值: false
ymp.configs.module.fileuploader.proxy_mode=

# 代理服务基准URL路径(若开启代理模式则此项必填), 必须以'http://'或'https://'开始并以'/'结束, 如: http://www.ymate.net/fileupload/, 默认值: 空
# 代理服务基准URL路径(若开启代理模式则此项必填), 必须以 http://https:// 开始并以'/'结束, 如: http://www.ymate.net/fileupload/, 默认值: 空
ymp.configs.module.fileuploader.proxy_service_base_url=

# 代理客户端与服务端之间通讯请求参数签名密钥, 默认值: ""
Expand All @@ -57,7 +67,7 @@ ymp.configs.module.fileuploader.file_storage_path=
# 缩略图文件存储根路径(根据存储适配器接口实现决定其值具体含义), 默认存储适配器取值与上传文件存储根路径值相同
ymp.configs.module.fileuploader.thumb_storage_path=

# 静态资源引用基准URL路径, 必须以'http://'或'https://'开始并以'/'结束, 如: http://www.ymate.net/static/resources/, 默认值: 空(即不使用静态资源引用路径)
# 静态资源引用基准URL路径, 必须以 http://https:// 开始并以'/'结束, 如: http://www.ymate.net/static/resources/, 默认值: 空(即不使用静态资源引用路径)
ymp.configs.module.fileuploader.resources_base_url=

# 文件存储适配器接口实现, 若未提供则使用系统默认, 此类需实现net.ymate.module.fileuploader.IFileStorageAdapter接口
Expand All @@ -75,7 +85,7 @@ ymp.configs.module.fileuploader.thumb_create_on_uploaded=
# 是否允许自定义缩略图尺寸, 默认值: false
ymp.configs.module.fileuploader.allow_custom_thumb_size=

# 缩略图尺寸列表, 该尺寸列表在允许自定义缩略图尺寸时生效, 若列表不为空则自定义尺寸不能超过此范围, 如: 600_4801024_0 (0表示等比缩放, 不支持0_0), 默认值: 空
# 缩略图尺寸列表, 该尺寸列表在允许自定义缩略图尺寸时生效, 若列表不为空则自定义尺寸不能超过此范围, 如: 600_480|1024_0 (0表示等比缩放, 不支持0_0), 默认值: 空
ymp.configs.module.fileuploader.thumb_size_list=

# 缩略图清晰度, 如: 0.70f, 默认值: 0f
Expand All @@ -85,101 +95,122 @@ ymp.configs.module.fileuploader.thumb_quality=
ymp.configs.module.fileuploader.allow_content_types=
```

#### 示例代码:

- 上传文件,以POST方式请求URL地址:

http://localhost:8080/uploads/push
## 示例代码:

> 参数说明:
>
> - file: 上传文件流数据;
> - type: 指定请求结果处理器,若未提供则采用默认,可选值: `fileupload`
**示例一:**上传文件,以 POST 方式请求 URL 地址:

响应:
```shell
http://localhost:8080/uploads/push
```

- 未指定`type`参数:
参数说明:

- file: 上传文件流数据;
- type: 指定请求结果处理器,若未提供则采用默认,可选值: `fileupload`

响应:

- 未指定 `type` 参数时:

```json
{
"ret": 0,
"data": {
"createTime": 1638200758000,
"extension": "mp4",
"filename": "a1175d94f245b9a142955b42ac285dc2.mp4",
"hash": "a1175d94f245b9a142955b42ac285dc2",
"lastModifyTime": 1638200758000,
"mimeType": "video/mp4",
"size": 21672966,
"sourcePath": "video/a1/17/a1175d94f245b9a142955b42ac285dc2.mp4",
"status": 0,
"type": "VIDEO",
"url": "http://localhost:8080/uploads/resources/video/a1175d94f245b9a142955b42ac285dc2"
}
}
```

```json
{
"ret": 0,
"data": {
"createTime": 1638200758000,
"extension": "mp4",
"filename": "a1175d94f245b9a142955b42ac285dc2.mp4",
"hash": "a1175d94f245b9a142955b42ac285dc2",
"lastModifyTime": 1638200758000,
"mimeType": "video/mp4",
- 指定 `type=fileupload` 时:

```json
{
"files": [
{
"size": 21672966,
"sourcePath": "video/a1/17/a1175d94f245b9a142955b42ac285dc2.mp4",
"status": 0,
"type": "VIDEO",
"url": "http://localhost:8080/uploads/resources/video/a1175d94f245b9a142955b42ac285dc2"
"name": "a1175d94f245b9a142955b42ac285dc2.mp4",
"type": "video",
"hash": "a1175d94f245b9a142955b42ac285dc2",
"thumbnailUrl": "http://localhost:8080/uploads/resources/video/a1175d94f245b9a142955b42ac285dc2"
}
}
```

- 指定`type=fileupload`:

```json
{
"files": [
{
"size": 21672966,
"name": "a1175d94f245b9a142955b42ac285dc2.mp4",
"type": "video",
"hash": "a1175d94f245b9a142955b42ac285dc2",
"thumbnailUrl": "http://localhost:8080/uploads/resources/video/a1175d94f245b9a142955b42ac285dc2"
}
]
}
```
]
}
```

- 文件指纹匹配,以POST方式请求URL地址:

http://localhost:8080/uploads/match

> 参数说明:
>
> - hash: 文件哈希值(MD5),必选参数;
**示例二:**文件指纹匹配,以 POST 方式请求 URL 地址:

返回值:若匹配成功则返回该文件的描述信息;
```shell
http://localhost:8080/uploads/match
```

```json
{
"ret": 0,
"matched": true,
"data": {
"createTime": 1638200758000,
"extension": "mp4",
"filename": "a1175d94f245b9a142955b42ac285dc2.mp4",
"hash": "a1175d94f245b9a142955b42ac285dc2",
"lastModifyTime": 1638200758000,
"mimeType": "video/mp4",
"size": 21672966,
"sourcePath": "video/a1/17/a1175d94f245b9a142955b42ac285dc2.mp4",
"status": 0,
"type": "VIDEO",
"url": "http://localhost:8080/uploads/resources/video/a1175d94f245b9a142955b42ac285dc2"
}
参数说明:

- hash: 文件哈希值(MD5),必选参数;

响应:

若匹配成功则返回该文件的描述信息;

```json
{
"ret": 0,
"matched": true,
"data": {
"createTime": 1638200758000,
"extension": "mp4",
"filename": "a1175d94f245b9a142955b42ac285dc2.mp4",
"hash": "a1175d94f245b9a142955b42ac285dc2",
"lastModifyTime": 1638200758000,
"mimeType": "video/mp4",
"size": 21672966,
"sourcePath": "video/a1/17/a1175d94f245b9a142955b42ac285dc2.mp4",
"status": 0,
"type": "VIDEO",
"url": "http://localhost:8080/uploads/resources/video/a1175d94f245b9a142955b42ac285dc2"
}
```
}
```



**示例三:**文件资源访问,以 GET 方式请求 URL 地址:

```shell
http://localhost:8080/uploads/resources/{type}/{hash}
```

参数说明:

- type: 文件类型,必选参数,可选值范围:`image``video``audio``text``application``thumb`

- hash: 文件哈希值(MD5),必选参数;

> ****:若需要强制浏览器下载资源,只需在请求参数中添加`?attach`即可,并支持通过?attach=<FILE_NAME>方式自定义文件名称(文件名称必须合法有效,不能包含特殊字符,否则将使用默认文件名称)。

- 文件资源访问,以GET方式请求URL地址:

http://localhost:8080/uploads/resources/{type}/{hash}
## One More Thing

> 参数说明:
>
> - type: 文件类型,必选参数,可选值范围:`image`、 `video`、`audio`、`text`、`application`、`thumb`
> - hash: 文件哈希值(MD5),必选参数;
>
> **注**:若需要强制浏览器下载资源,只需在请求参数中添加`?attach`即可,并支持通过?attach=<FILE_NAME>方式自定义文件名称(文件名称必须合法有效,不能包含特殊字符,否则将使用默认文件名称)。
YMP 不仅提供便捷的 Web 及其它 Java 项目的快速开发体验,也将不断提供更多丰富的项目实践经验。

#### One More Thing
感兴趣的小伙伴儿们可以加入官方 QQ 群:[480374360](https://qm.qq.com/cgi-bin/qm/qr?k=3KSXbRoridGeFxTVA8HZzyhwU_btZQJ2),一起交流学习,帮助 YMP 成长!

YMP不仅提供便捷的Web及其它Java项目的快速开发体验,也将不断提供更多丰富的项目实践经验。
如果喜欢 YMP,希望得到你的支持和鼓励!

感兴趣的小伙伴儿们可以加入 官方QQ群480374360,一起交流学习,帮助YMP成长!
![Donation Code](https://ymate.net/img/donation_code.png)

了解更多有关YMP框架的内容,请访问官网:https://www.ymate.net/
了解更多有关 YMP 框架的内容,请访问官网:[https://ymate.net](https://ymate.net)
6 changes: 3 additions & 3 deletions misc/ymp-conf.properties
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ ymp.configs.module.fileuploader.service_enabled=
# \u662F\u5426\u5F00\u542F\u4EE3\u7406\u6A21\u5F0F, \u9ED8\u8BA4\u503C: false
ymp.configs.module.fileuploader.proxy_mode=

# \u4EE3\u7406\u670D\u52A1\u57FA\u51C6URL\u8DEF\u5F84(\u82E5\u5F00\u542F\u4EE3\u7406\u6A21\u5F0F\u5219\u6B64\u9879\u5FC5\u586B), \u5FC5\u987B\u4EE5'http://'\u6216'https://'\u5F00\u59CB\u5E76\u4EE5'/'\u7ED3\u675F, \u5982: http://www.ymate.net/fileupload/, \u9ED8\u8BA4\u503C: \u7A7A
# \u4EE3\u7406\u670D\u52A1\u57FA\u51C6URL\u8DEF\u5F84(\u82E5\u5F00\u542F\u4EE3\u7406\u6A21\u5F0F\u5219\u6B64\u9879\u5FC5\u586B), \u5FC5\u987B\u4EE5 http:// \u6216 https:// \u5F00\u59CB\u5E76\u4EE5'/'\u7ED3\u675F, \u5982: http://www.ymate.net/fileupload/, \u9ED8\u8BA4\u503C: \u7A7A
ymp.configs.module.fileuploader.proxy_service_base_url=

# \u4EE3\u7406\u5BA2\u6237\u7AEF\u4E0E\u670D\u52A1\u7AEF\u4E4B\u95F4\u901A\u8BAF\u8BF7\u6C42\u53C2\u6570\u7B7E\u540D\u5BC6\u94A5, \u9ED8\u8BA4\u503C: ""
Expand All @@ -32,7 +32,7 @@ ymp.configs.module.fileuploader.file_storage_path=
# \u7F29\u7565\u56FE\u6587\u4EF6\u5B58\u50A8\u6839\u8DEF\u5F84\uFF08\u6839\u636E\u5B58\u50A8\u9002\u914D\u5668\u63A5\u53E3\u5B9E\u73B0\u51B3\u5B9A\u5176\u503C\u5177\u4F53\u542B\u4E49\uFF09, \u9ED8\u8BA4\u5B58\u50A8\u9002\u914D\u5668\u53D6\u503C\u4E0E\u4E0A\u4F20\u6587\u4EF6\u5B58\u50A8\u6839\u8DEF\u5F84\u503C\u76F8\u540C
ymp.configs.module.fileuploader.thumb_storage_path=

# \u9759\u6001\u8D44\u6E90\u5F15\u7528\u57FA\u51C6URL\u8DEF\u5F84, \u5FC5\u987B\u4EE5'http://'\u6216'https://'\u5F00\u59CB\u5E76\u4EE5'/'\u7ED3\u675F, \u5982: http://www.ymate.net/static/resources/, \u9ED8\u8BA4\u503C: \u7A7A(\u5373\u4E0D\u4F7F\u7528\u9759\u6001\u8D44\u6E90\u5F15\u7528\u8DEF\u5F84)
# \u9759\u6001\u8D44\u6E90\u5F15\u7528\u57FA\u51C6URL\u8DEF\u5F84, \u5FC5\u987B\u4EE5 http:// \u6216 https:// \u5F00\u59CB\u5E76\u4EE5'/'\u7ED3\u675F, \u5982: http://www.ymate.net/static/resources/, \u9ED8\u8BA4\u503C: \u7A7A(\u5373\u4E0D\u4F7F\u7528\u9759\u6001\u8D44\u6E90\u5F15\u7528\u8DEF\u5F84)
ymp.configs.module.fileuploader.resources_base_url=

# \u6587\u4EF6\u5B58\u50A8\u9002\u914D\u5668\u63A5\u53E3\u5B9E\u73B0, \u82E5\u672A\u63D0\u4F9B\u5219\u4F7F\u7528\u7CFB\u7EDF\u9ED8\u8BA4, \u6B64\u7C7B\u9700\u5B9E\u73B0net.ymate.module.fileuploader.IFileStorageAdapter\u63A5\u53E3
Expand All @@ -50,7 +50,7 @@ ymp.configs.module.fileuploader.thumb_create_on_uploaded=
# \u662F\u5426\u5141\u8BB8\u81EA\u5B9A\u4E49\u7F29\u7565\u56FE\u5C3A\u5BF8, \u9ED8\u8BA4\u503C: false
ymp.configs.module.fileuploader.allow_custom_thumb_size=

# \u7F29\u7565\u56FE\u5C3A\u5BF8\u5217\u8868, \u8BE5\u5C3A\u5BF8\u5217\u8868\u5728\u5141\u8BB8\u81EA\u5B9A\u4E49\u7F29\u7565\u56FE\u5C3A\u5BF8\u65F6\u751F\u6548, \u82E5\u5217\u8868\u4E0D\u4E3A\u7A7A\u5219\u81EA\u5B9A\u4E49\u5C3A\u5BF8\u4E0D\u80FD\u8D85\u8FC7\u6B64\u8303\u56F4, \u5982: 600_480\u30011024_0 (0\u8868\u793A\u7B49\u6BD4\u7F29\u653E, \u4E0D\u652F\u63010_0), \u9ED8\u8BA4\u503C: \u7A7A
# \u7F29\u7565\u56FE\u5C3A\u5BF8\u5217\u8868, \u8BE5\u5C3A\u5BF8\u5217\u8868\u5728\u5141\u8BB8\u81EA\u5B9A\u4E49\u7F29\u7565\u56FE\u5C3A\u5BF8\u65F6\u751F\u6548, \u82E5\u5217\u8868\u4E0D\u4E3A\u7A7A\u5219\u81EA\u5B9A\u4E49\u5C3A\u5BF8\u4E0D\u80FD\u8D85\u8FC7\u6B64\u8303\u56F4, \u5982: 600_480|1024_0 (0\u8868\u793A\u7B49\u6BD4\u7F29\u653E, \u4E0D\u652F\u63010_0), \u9ED8\u8BA4\u503C: \u7A7A
ymp.configs.module.fileuploader.thumb_size_list=

# \u7F29\u7565\u56FE\u6E05\u6670\u5EA6, \u5982: 0.70f, \u9ED8\u8BA4\u503C: 0f
Expand Down
10 changes: 5 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<ymate.platform.version>2.1.0</ymate.platform.version>
<ymate.maven.plugin.version>1.1-SNAPSHOT</ymate.maven.plugin.version>
<ymate.maven.plugin.version>1.0.0</ymate.maven.plugin.version>
</properties>

<developers>
Expand Down Expand Up @@ -147,7 +147,7 @@
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.1</version>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<dependency>
Expand All @@ -159,7 +159,7 @@
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.5.12</version>
<version>4.5.13</version>
<exclusions>
<exclusion>
<groupId>commons-codec</groupId>
Expand All @@ -174,14 +174,14 @@
<dependency>
<groupId>net.coobird</groupId>
<artifactId>thumbnailator</artifactId>
<version>0.4.14</version>
<version>0.4.16</version>
</dependency>
<!--
<dependency>
<groupId>com.madgag</groupId>
<artifactId>animated-gif-lib</artifactId>
<version>1.4</version>
</dependency>
<!--
<dependency>
<groupId>com.github.nintha</groupId>
<artifactId>webp-imageio-core</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package net.ymate.module.fileuploader;

import net.ymate.platform.commons.util.ParamUtils;
import org.apache.commons.lang3.StringUtils;

/**
* @author 刘镇 ([email protected]) on 2021/12/3 12:48 下午
Expand Down Expand Up @@ -50,7 +51,7 @@ public interface IFileUploaderClientConfig {
* @return 返回完整的请求URL地址
*/
default String buildServiceUrl(String serviceUrl) {
return String.format("%s%s", getServiceBaseUrl(), serviceUrl);
return StringUtils.join(getServiceBaseUrl(), serviceUrl);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,11 @@ public UploadFileMeta matchHash(String hash) throws Exception {
fileMeta = doMatchHash(hash, null);
if (fileMeta != null) {
doPutElementToCache(hash, fileMeta);
//
owner.getOwner().getEvents().fireEvent(new FileUploadEvent(owner, FileUploadEvent.EVENT.FILE_MATCHED).setEventSource(fileMeta));
}
}
if (fileMeta != null) {
owner.getOwner().getEvents().fireEvent(new FileUploadEvent(owner, FileUploadEvent.EVENT.FILE_MATCHED).setEventSource(fileMeta));
}
}
return fileMeta;
}
Expand Down

0 comments on commit bd13f08

Please sign in to comment.