Skip to content

Commit

Permalink
Merge pull request #31 from 1171736840/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
1171736840 authored Aug 12, 2022
2 parents 9a29685 + 688d117 commit 9ad344d
Show file tree
Hide file tree
Showing 27 changed files with 638 additions and 394 deletions.
59 changes: 38 additions & 21 deletions docs/基础功能.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ fileStorageService.of(file)
fileStorageService.of(file)
.setObjectId("0") // 关联对象id,为了方便管理,不需要可以不写
.setObjectType("0") // 关联对象类型,为了方便管理,不需要可以不写
.putAttr("role","admin") //保存一些属性,可以在切面、保存上传记录、自定义存储平台等地方获取使用,不需要可以不写
.putAttr("username","007")
.upload();

// 上传到指定的存储平台
Expand Down Expand Up @@ -57,9 +59,15 @@ public class FileDetailService extends ServiceImpl<FileDetailMapper, FileDetail>
/**
* 保存文件信息到数据库
*/
@SneakyThrows
@Override
public boolean record(FileInfo info) {
FileDetail detail = BeanUtil.copyProperties(info,FileDetail.class);
FileDetail detail = BeanUtil.copyProperties(info,FileDetail.class,"attr");

//这是手动获 取附加属性字典 并转成 json 字符串,方便存储在数据库中
if (info.getAttr() != null) {
detail.setAttr(new ObjectMapper().writeValueAsString(info.getAttr()));
}
boolean b = save(detail);
if (b) {
info.setId(detail.getId());
Expand All @@ -70,9 +78,17 @@ public class FileDetailService extends ServiceImpl<FileDetailMapper, FileDetail>
/**
* 根据 url 查询文件信息
*/
@SneakyThrows
@Override
public FileInfo getByUrl(String url) {
return BeanUtil.copyProperties(getOne(new QueryWrapper<FileDetail>().eq(FileDetail.COL_URL,url)),FileInfo.class);
FileDetail detail = getOne(new QueryWrapper<FileDetail>().eq(FileDetail.COL_URL,url));
FileInfo info = BeanUtil.copyProperties(detail,FileInfo.class,"attr");

//这是手动获取数据库中的 json 字符串 并转成 附加属性字典,方便使用
if (StrUtil.isNotBlank(detail.getAttr())) {
info.setAttr(new ObjectMapper().readValue(detail.getAttr(),Dict.class));
}
return info;
}

/**
Expand All @@ -91,27 +107,28 @@ public class FileDetailService extends ServiceImpl<FileDetailMapper, FileDetail>
-- 这里使用的是 mysql
CREATE TABLE `file_detail`
(
`id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '文件id',
`url` varchar(256) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '文件访问地址',
`size` bigint(20) NULL DEFAULT NULL COMMENT '文件大小,单位字节',
`filename` varchar(256) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '文件名称',
`original_filename` varchar(256) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '原始文件名',
`base_path` varchar(256) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '基础存储路径',
`path` varchar(256) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '存储路径',
`ext` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '文件扩展名',
`platform` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '存储平台',
`th_url` varchar(256) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '缩略图访问路径',
`th_filename` varchar(256) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '缩略图名称',
`th_size` bigint(20) NULL DEFAULT NULL COMMENT '缩略图大小,单位字节',
`object_id` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '文件所属对象id',
`object_type` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '文件所属对象类型,例如用户头像,评价图片',
`create_time` datetime(0) NULL DEFAULT NULL COMMENT '创建时间',
`id` varchar(32) NOT NULL COMMENT '文件id',
`url` varchar(512) NOT NULL COMMENT '文件访问地址',
`size` bigint(20) DEFAULT NULL COMMENT '文件大小,单位字节',
`filename` varchar(256) DEFAULT NULL COMMENT '文件名称',
`original_filename` varchar(256) DEFAULT NULL COMMENT '原始文件名',
`base_path` varchar(256) DEFAULT NULL COMMENT '基础存储路径',
`path` varchar(256) DEFAULT NULL COMMENT '存储路径',
`ext` varchar(32) DEFAULT NULL COMMENT '文件扩展名',
`content_type` varchar(32) DEFAULT NULL COMMENT 'MIME类型',
`platform` varchar(32) DEFAULT NULL COMMENT '存储平台',
`th_url` varchar(512) DEFAULT NULL COMMENT '缩略图访问路径',
`th_filename` varchar(256) DEFAULT NULL COMMENT '缩略图名称',
`th_size` bigint(20) DEFAULT NULL COMMENT '缩略图大小,单位字节',
`th_content_type` varchar(32) DEFAULT NULL COMMENT '缩略图MIME类型',
`object_id` varchar(32) DEFAULT NULL COMMENT '文件所属对象id',
`object_type` varchar(32) DEFAULT NULL COMMENT '文件所属对象类型,例如用户头像,评价图片',
`attr` text COMMENT '附加属性',
`create_time` datetime DEFAULT NULL COMMENT '创建时间',
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB
AUTO_INCREMENT = 1
CHARACTER SET = utf8
COLLATE = utf8_general_ci COMMENT = '文件记录表'
ROW_FORMAT = Dynamic;
DEFAULT CHARSET = utf8
ROW_FORMAT = DYNAMIC COMMENT ='文件记录表';
```


Expand Down
11 changes: 8 additions & 3 deletions docs/存储平台.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@ public class LocalFileStorage implements FileStorage {
/* 访问域名 */
private String domain;

/**
* 释放资源
*/
@Override
public void close() {
}

/**
* 保存文件
*/
Expand Down Expand Up @@ -166,7 +173,5 @@ list.add(storage);
//删除
FileStorage myLocal = fileStorageService.getFileStorage("my-local-1");
list.remove(myLocal);

//条件删除
list.removeIf(item -> item instanceof LocalFileStorage);
myLocal.close();//释放资源
```
4 changes: 2 additions & 2 deletions docs/常见问题.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
</dependency>
```

### 如果根据数据库配置加载存储平台
### 如何根据数据库配置加载存储平台

参考 [动态增减存储平台](存储平台?id=动态增减存储平台)

### 如果自定义缩略图后缀
### 如何自定义缩略图后缀

参考 [多种上传方式](基础功能?id=多种上传方式) 中的缩略图上传方式,可以自己调整缩略图后缀
19 changes: 10 additions & 9 deletions docs/快速入门.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,63 +10,63 @@
<dependency>
<groupId>cn.xuyanwu</groupId>
<artifactId>spring-file-storage</artifactId>
<version>0.4.0</version>
<version>0.5.0</version>
</dependency>

<!-- 华为云 OBS 不使用的情况下可以不引入 -->
<dependency>
<groupId>com.huaweicloud</groupId>
<artifactId>esdk-obs-java</artifactId>
<version>3.20.6.1</version>
<version>3.22.3.1</version>
</dependency>

<!-- 阿里云 OSS 不使用的情况下可以不引入 -->
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
<version>3.6.0</version>
<version>3.15.1</version>
</dependency>

<!-- 七牛云 Kodo 不使用的情况下可以不引入 -->
<dependency>
<groupId>com.qiniu</groupId>
<artifactId>qiniu-java-sdk</artifactId>
<version>7.4.0</version>
<version>7.11.0</version>
</dependency>

<!-- 腾讯云 COS 不使用的情况下可以不引入 -->
<dependency>
<groupId>com.qcloud</groupId>
<artifactId>cos_api</artifactId>
<version>5.6.38</version>
<version>5.6.98</version>
</dependency>

<!-- 百度云 BOS 不使用的情况下可以不引入 -->
<dependency>
<groupId>com.baidubce</groupId>
<artifactId>bce-java-sdk</artifactId>
<version>0.10.162</version>
<version>0.10.218</version>
</dependency>

<!-- 又拍云 USS 不使用的情况下可以不引入 -->
<dependency>
<groupId>com.upyun</groupId>
<artifactId>java-sdk</artifactId>
<version>4.2.2</version>
<version>4.2.3</version>
</dependency>

<!-- MinIO 不使用的情况下可以不引入 -->
<dependency>
<groupId>io.minio</groupId>
<artifactId>minio</artifactId>
<version>7.0.2</version>
<version>8.4.3</version>
</dependency>

<!-- AWS S3 不使用的情况下可以不引入 -->
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-s3</artifactId>
<version>1.11.1034</version>
<version>1.12.272</version>
</dependency>

</dependencies>
Expand Down Expand Up @@ -214,6 +214,7 @@ public class FileDetailController {
.setPath("upload/") //保存到相对路径下,为了方便管理,不需要可以不写
.setObjectId("0") //关联对象id,为了方便管理,不需要可以不写
.setObjectType("0") //关联对象类型,为了方便管理,不需要可以不写
.putAttr("role","admin") //保存一些属性,可以在切面、保存上传记录、自定义存储平台等地方获取使用,不需要可以不写
.upload(); //将文件上传到对应地方
return fileInfo == null ? "上传失败!" : fileInfo.getUrl();
}
Expand Down
8 changes: 8 additions & 0 deletions docs/更新记录.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# 更新记录

## 0.5.0
2022-08-11
- 增加对 ContentType 和 ContentLength 的支持
- 增加 FileInfo 属性:attr,这是一个字典对象,可以更方便的关联数据
- 将 FileInfo 的 id 属性由 Integer 改为 String,使其更通用
- 其它性能优化
- 升级相关依赖版本

## 0.4.0
2021-06-09
- 增加对 AWS S3 的支持
Expand Down
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<groupId>cn.xuyanwu</groupId>
<artifactId>spring-file-storage-parent</artifactId>
<packaging>pom</packaging>
<version>0.4.0</version>
<version>0.5.0</version>

<name>spring-file-storage-parent</name>
<description>A File Storage Service</description>
Expand Down Expand Up @@ -52,7 +52,7 @@

<properties>
<java.version>1.8</java.version>
<hutool.version>5.5.2</hutool.version>
<hutool.version>5.8.5</hutool.version>
<lombok.version>1.18.16</lombok.version>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
Expand Down
26 changes: 13 additions & 13 deletions spring-file-storage-test/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.0</version>
<version>2.7.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

<groupId>cn.xuyanwu</groupId>
<artifactId>spring-file-storage-test</artifactId>
<version>0.4.0</version>
<version>0.5.0</version>
<name>spring-file-storage-test</name>
<description>spring-file-storage 的测试和演示模块</description>

Expand All @@ -26,69 +26,69 @@
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-s3</artifactId>
<version>1.11.1034</version>
<version>1.12.272</version>
</dependency>

<!-- MinIO -->
<dependency>
<groupId>io.minio</groupId>
<artifactId>minio</artifactId>
<version>7.0.2</version>
<version>8.4.3</version>
</dependency>

<!-- 又拍云 USS -->
<dependency>
<groupId>com.upyun</groupId>
<artifactId>java-sdk</artifactId>
<version>4.2.2</version>
<version>4.2.3</version>
</dependency>

<!-- 百度云 BOS -->
<dependency>
<groupId>com.baidubce</groupId>
<artifactId>bce-java-sdk</artifactId>
<version>0.10.162</version>
<version>0.10.218</version>
</dependency>

<!-- 腾讯云 COS -->
<dependency>
<groupId>com.qcloud</groupId>
<artifactId>cos_api</artifactId>
<version>5.6.38</version>
<version>5.6.98</version>
</dependency>

<!-- 七牛云 Kodo -->
<dependency>
<groupId>com.qiniu</groupId>
<artifactId>qiniu-java-sdk</artifactId>
<version>7.4.0</version>
<version>7.11.0</version>
</dependency>

<!-- 阿里云 OSS -->
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
<version>3.6.0</version>
<version>3.15.1</version>
</dependency>

<!-- 华为云 OBS -->
<dependency>
<groupId>com.huaweicloud</groupId>
<artifactId>esdk-obs-java</artifactId>
<version>3.20.6.1</version>
<version>3.22.3.1</version>
</dependency>

<!--糊涂工具类核心-->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-core</artifactId>
<version>5.5.2</version>
<version>5.8.5</version>
</dependency>

<dependency>
<groupId>cn.xuyanwu</groupId>
<artifactId>spring-file-storage</artifactId>
<version>0.4.0</version>
<version>0.5.0</version>
</dependency>

<dependency>
Expand All @@ -105,7 +105,7 @@
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.1</version>
<version>3.5.2</version>
</dependency>

<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,29 @@
<resultMap id="BaseResultMap" type="cn.xuyanwu.spring.file.storage.test.model.FileDetail">
<!--@mbg.generated-->
<!--@Table file_detail-->
<id column="id" jdbcType="INTEGER" property="id" />
<id column="id" jdbcType="VARCHAR" property="id" />
<result column="url" jdbcType="VARCHAR" property="url" />
<result column="size" jdbcType="BIGINT" property="size" />
<result column="filename" jdbcType="VARCHAR" property="filename" />
<result column="original_filename" jdbcType="VARCHAR" property="originalFilename" />
<result column="base_path" jdbcType="VARCHAR" property="basePath" />
<result column="path" jdbcType="VARCHAR" property="path" />
<result column="ext" jdbcType="VARCHAR" property="ext" />
<result column="content_type" jdbcType="VARCHAR" property="contentType" />
<result column="platform" jdbcType="VARCHAR" property="platform" />
<result column="th_url" jdbcType="VARCHAR" property="thUrl" />
<result column="th_filename" jdbcType="VARCHAR" property="thFilename" />
<result column="th_size" jdbcType="BIGINT" property="thSize" />
<result column="th_content_type" jdbcType="VARCHAR" property="thContentType" />
<result column="object_id" jdbcType="VARCHAR" property="objectId" />
<result column="object_type" jdbcType="VARCHAR" property="objectType" />
<result column="attr" jdbcType="LONGVARCHAR" property="attr" />
<result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
</resultMap>
<sql id="Base_Column_List">
<!--@mbg.generated-->
id, url, `size`, filename, original_filename, base_path, `path`, ext, platform, th_url,
th_filename, th_size, object_id, object_type, create_time
id, url, `size`, filename, original_filename, base_path, `path`, ext, content_type,
platform, th_url, th_filename, th_size, th_content_type, object_id, object_type,
attr, create_time
</sql>
</mapper>
Loading

0 comments on commit 9ad344d

Please sign in to comment.