mirror of https://gitee.com/maxjhandsome/pig
✨ Introducing new features. closed #I470I7 文件管理
This commit is contained in:
parent
4a47c492ad
commit
3071371e28
23
README.md
23
README.md
|
@ -22,7 +22,7 @@
|
|||
| Spring Cloud Alibaba | 2021.1 |
|
||||
| Spring Security OAuth2 | 2.3.6 |
|
||||
| Mybatis Plus | 3.4.3.2 |
|
||||
| hutool | 5.7.9 |
|
||||
| hutool | 5.7.12 |
|
||||
| Avue | 2.6.18 |
|
||||
|
||||
### 模块说明
|
||||
|
@ -71,6 +71,11 @@ pig 提供了详细的[部署文档 wiki.pig4cloud.com](https://www.yuque.com/pi
|
|||
|
||||
请务必**完全按照**文档部署运行章节 进行操作,减少踩坑弯路!!
|
||||
|
||||
### 定制自己微服务
|
||||
|
||||
[PIG DIY](https://diy.pig4cloud.com)
|
||||
[PIG ARCHETYPE](https://archetype.pig4cloud.com)
|
||||
|
||||
### Docker 运行
|
||||
|
||||
```
|
||||
|
@ -87,22 +92,6 @@ cd pig-ui && npm install -g cnpm --registry=https://registry.npm.taobao.org
|
|||
npm run build:docker && docker-compose up -d
|
||||
```
|
||||
|
||||
### 快速构架微服务
|
||||
|
||||
```bash
|
||||
<!-- pig-gen archetype -->
|
||||
# 在空文件夹执行以下命令,注意 windows 下 \ 修改成 ^
|
||||
mvn archetype:generate \
|
||||
-DgroupId=com.pig4cloud \
|
||||
-DartifactId=demo \
|
||||
-Dversion=1.0.0-SNAPSHOT \
|
||||
-Dpackage=com.pig4cloud.pig.demo \
|
||||
-DarchetypeGroupId=com.pig4cloud.archetype \
|
||||
-DarchetypeArtifactId=pig-gen \
|
||||
-DarchetypeVersion=3.3.2 \
|
||||
-DarchetypeCatalog=local
|
||||
```
|
||||
|
||||
## 免费公开课
|
||||
|
||||
<table>
|
||||
|
|
25
db/pig.sql
25
db/pig.sql
|
@ -227,6 +227,10 @@ INSERT INTO `sys_menu` VALUES (2401, '客户端新增', 'sys_client_add', NULL,
|
|||
INSERT INTO `sys_menu` VALUES (2402, '客户端修改', 'sys_client_edit', NULL, 2400, NULL, NULL, 1, '0', '1', '0', NULL, '2018-05-15 21:37:06', NULL, '2021-05-25 06:48:34');
|
||||
INSERT INTO `sys_menu` VALUES (2403, '客户端删除', 'sys_client_del', NULL, 2400, NULL, NULL, 1, '0', '1', '0', NULL, '2018-05-15 21:39:16', NULL, '2021-05-25 06:48:34');
|
||||
INSERT INTO `sys_menu` VALUES (2500, '服务监控', NULL, 'http://localhost:5001', 2000, 'icon-server', NULL, 10, '0', '0', '0', NULL, '2018-06-26 10:50:32', NULL, '2019-02-01 20:41:30');
|
||||
INSERT INTO `sys_menu` VALUES (2600, '文件管理', NULL, '/admin/file/index', 2000, 'icon-wenjianguanli', NULL, 10, '0', '0', '0', NULL, '2018-06-26 10:50:32', NULL, '2019-02-01 20:41:30');
|
||||
INSERT INTO `sys_menu` VALUES (2601, '文件删除', 'sys_file_del', NULL, 2600, NULL, NULL, 1, '0', '1', '0', NULL, '2017-11-29 11:30:11', NULL, '2021-05-25 06:48:34');
|
||||
INSERT INTO `sys_menu` VALUES (2602, '文件新增', 'sys_file_add', NULL, 2600, NULL, NULL, 1, '0', '1', '0', NULL, '2018-05-11 22:34:55', NULL, '2021-05-25 06:48:34');
|
||||
INSERT INTO `sys_menu` VALUES (2603, '文件修改', 'sys_file_edit', NULL, 2600, NULL, NULL, 1, '0', '1', '0', NULL, '2018-05-11 22:36:03', NULL, '2021-05-25 06:48:34');
|
||||
INSERT INTO `sys_menu` VALUES (3000, '开发平台', NULL, '/gen', -1, 'icon-shejiyukaifa-', NULL, 3, '1', '0', '0', NULL, '2020-03-11 22:15:40', NULL, '2020-03-11 23:52:54');
|
||||
INSERT INTO `sys_menu` VALUES (3100, '数据源管理', NULL, '/gen/datasource', 3000, 'icon-mysql', NULL, 1, '1', '0', '0', NULL, '2020-03-11 22:17:05', NULL, '2020-03-12 00:16:09');
|
||||
INSERT INTO `sys_menu` VALUES (3200, '代码生成', NULL, '/gen/index', 3000, 'icon-weibiaoti46', NULL, 2, '0', '0', '0', NULL, '2020-03-11 22:23:42', NULL, '2020-03-12 00:16:14');
|
||||
|
@ -346,7 +350,10 @@ INSERT INTO `sys_role_menu` VALUES (1, 2400);
|
|||
INSERT INTO `sys_role_menu` VALUES (1, 2401);
|
||||
INSERT INTO `sys_role_menu` VALUES (1, 2402);
|
||||
INSERT INTO `sys_role_menu` VALUES (1, 2403);
|
||||
INSERT INTO `sys_role_menu` VALUES (1, 2500);
|
||||
INSERT INTO `sys_role_menu` VALUES (1, 2600);
|
||||
INSERT INTO `sys_role_menu` VALUES (1, 2601);
|
||||
INSERT INTO `sys_role_menu` VALUES (1, 2602);
|
||||
INSERT INTO `sys_role_menu` VALUES (1, 2603);
|
||||
INSERT INTO `sys_role_menu` VALUES (1, 3000);
|
||||
INSERT INTO `sys_role_menu` VALUES (1, 3100);
|
||||
INSERT INTO `sys_role_menu` VALUES (1, 3200);
|
||||
|
@ -404,4 +411,20 @@ BEGIN;
|
|||
INSERT INTO `sys_user_role` VALUES (1, 1);
|
||||
COMMIT;
|
||||
|
||||
DROP TABLE IF EXISTS `sys_file`;
|
||||
CREATE TABLE `sys_file` (
|
||||
`id` bigint NOT NULL AUTO_INCREMENT COMMENT '编号',
|
||||
`file_name` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL,
|
||||
`bucket_name` varchar(200) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL,
|
||||
`original` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL,
|
||||
`type` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL,
|
||||
`file_size` bigint DEFAULT NULL COMMENT '文件大小',
|
||||
`del_flag` char(1) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT '0' COMMENT '0-正常,1-删除',
|
||||
`create_time` datetime DEFAULT NULL COMMENT '创建时间',
|
||||
`update_time` datetime DEFAULT NULL COMMENT '修改时间',
|
||||
`create_by` varchar(64) COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '创建者',
|
||||
`update_by` varchar(64) COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '更新人',
|
||||
PRIMARY KEY (`id`) USING BTREE
|
||||
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='文件管理表';
|
||||
|
||||
SET FOREIGN_KEY_CHECKS = 1;
|
||||
|
|
|
@ -27,6 +27,7 @@
|
|||
<mybatis-plus.version>3.4.3.3</mybatis-plus.version>
|
||||
<nacos.version>2.0.3</nacos.version>
|
||||
<excel.version>1.0.0</excel.version>
|
||||
<oss.version>1.0.1</oss.version>
|
||||
</properties>
|
||||
|
||||
<!-- 定义全局jar版本,模块使用需要再次引入但不用写版本号-->
|
||||
|
@ -116,6 +117,13 @@
|
|||
<artifactId>excel-spring-boot-starter</artifactId>
|
||||
<version>${excel.version}</version>
|
||||
</dependency>
|
||||
<!--mybatis-plus-->
|
||||
<dependency>
|
||||
<groupId>com.pig4cloud.plugin</groupId>
|
||||
<artifactId>oss-spring-boot-starter</artifactId>
|
||||
<version>${oss.version}</version>
|
||||
</dependency>
|
||||
<!--mybatis-plus-->
|
||||
<dependency>
|
||||
<groupId>com.baomidou</groupId>
|
||||
<artifactId>mybatis-plus-boot-starter</artifactId>
|
||||
|
|
|
@ -0,0 +1,76 @@
|
|||
/*
|
||||
* Copyright (c) 2018-2025, lengleng All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* Neither the name of the pig4cloud.com developer nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from
|
||||
* this software without specific prior written permission.
|
||||
* Author: lengleng (wangiegie@gmail.com)
|
||||
*/
|
||||
|
||||
package com.pig4cloud.pig.admin.api.entity;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.IdType;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableLogic;
|
||||
import com.pig4cloud.pig.common.mybatis.base.BaseEntity;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
|
||||
/**
|
||||
* 文件管理
|
||||
*
|
||||
* @author Luckly
|
||||
* @date 2019-06-18 17:18:42
|
||||
*/
|
||||
@Data
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
public class SysFile extends BaseEntity {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* 编号
|
||||
*/
|
||||
@TableId(type = IdType.AUTO)
|
||||
private Long id;
|
||||
|
||||
/**
|
||||
* 文件名
|
||||
*/
|
||||
private String fileName;
|
||||
|
||||
/**
|
||||
* 原文件名
|
||||
*/
|
||||
private String original;
|
||||
|
||||
/**
|
||||
* 容器名称
|
||||
*/
|
||||
private String bucketName;
|
||||
|
||||
/**
|
||||
* 文件类型
|
||||
*/
|
||||
private String type;
|
||||
|
||||
/**
|
||||
* 文件大小
|
||||
*/
|
||||
private Long fileSize;
|
||||
|
||||
/**
|
||||
* 删除标识:1-删除,0-正常
|
||||
*/
|
||||
@TableLogic
|
||||
private Integer delFlag;
|
||||
|
||||
}
|
|
@ -26,6 +26,7 @@ import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
|
|||
import com.pig4cloud.pig.common.mybatis.base.BaseEntity;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
|
||||
import javax.validation.constraints.NotBlank;
|
||||
|
||||
|
@ -38,6 +39,7 @@ import javax.validation.constraints.NotBlank;
|
|||
* @since 2019/2/1
|
||||
*/
|
||||
@Data
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
public class SysLog extends BaseEntity {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
|
|
@ -23,6 +23,7 @@ import com.fasterxml.jackson.annotation.JsonIgnore;
|
|||
import com.pig4cloud.pig.common.mybatis.base.BaseEntity;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
|
@ -33,6 +34,7 @@ import lombok.Data;
|
|||
* @since 2019/2/1
|
||||
*/
|
||||
@Data
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
public class SysUser extends BaseEntity {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
|
|
@ -35,6 +35,11 @@
|
|||
<groupId>com.pig4cloud</groupId>
|
||||
<artifactId>pig-upms-api</artifactId>
|
||||
</dependency>
|
||||
<!--文件管理-->
|
||||
<dependency>
|
||||
<groupId>com.pig4cloud.plugin</groupId>
|
||||
<artifactId>oss-spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
<!--安全模块-->
|
||||
<dependency>
|
||||
<groupId>com.pig4cloud</groupId>
|
||||
|
|
|
@ -1,36 +1,98 @@
|
|||
/*
|
||||
* Copyright (c) 2018-2025, lengleng All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* Neither the name of the pig4cloud.com developer nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from
|
||||
* this software without specific prior written permission.
|
||||
* Author: lengleng (wangiegie@gmail.com)
|
||||
*/
|
||||
|
||||
package com.pig4cloud.pig.admin.controller;
|
||||
|
||||
import cn.hutool.core.io.IoUtil;
|
||||
import lombok.SneakyThrows;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
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.RestController;
|
||||
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.pig4cloud.pig.admin.api.entity.SysFile;
|
||||
import com.pig4cloud.pig.admin.service.SysFileService;
|
||||
import com.pig4cloud.pig.common.core.util.R;
|
||||
import com.pig4cloud.pig.common.log.annotation.SysLog;
|
||||
import com.pig4cloud.pig.common.security.annotation.Inner;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import lombok.AllArgsConstructor;
|
||||
import org.springframework.security.access.prepost.PreAuthorize;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
/**
|
||||
* 文件管理
|
||||
*
|
||||
* @author lengleng
|
||||
* @date 2021/8/23
|
||||
* @author Luckly
|
||||
* @date 2021-09-11
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/file")
|
||||
@AllArgsConstructor
|
||||
@RequestMapping("/sys-file")
|
||||
@Api(value = "sys-file", tags = "文件管理")
|
||||
public class FileController {
|
||||
|
||||
private final SysFileService sysFileService;
|
||||
|
||||
/**
|
||||
* 获取本地文件
|
||||
* @param fileName 文件名称
|
||||
* @param response 本地文件
|
||||
* 分页查询
|
||||
* @param page 分页对象
|
||||
* @param sysFile 文件管理
|
||||
* @return
|
||||
*/
|
||||
@SneakyThrows
|
||||
@GetMapping("/local-file/{fileName}")
|
||||
public void localFile(@PathVariable String fileName, HttpServletResponse response) {
|
||||
ClassPathResource resource = new ClassPathResource("file/" + fileName);
|
||||
response.setContentType("application/octet-stream; charset=UTF-8");
|
||||
IoUtil.copy(resource.getInputStream(), response.getOutputStream());
|
||||
@ApiOperation(value = "分页查询", notes = "分页查询")
|
||||
@GetMapping("/page")
|
||||
public R getSysFilePage(Page page, SysFile sysFile) {
|
||||
return R.ok(sysFileService.page(page, Wrappers.query(sysFile)));
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过id删除文件管理
|
||||
* @param id id
|
||||
* @return R
|
||||
*/
|
||||
@ApiOperation(value = "通过id删除文件管理", notes = "通过id删除文件管理")
|
||||
@SysLog("删除文件管理")
|
||||
@DeleteMapping("/{id}")
|
||||
@PreAuthorize("@pms.hasPermission('sys_file_del')")
|
||||
public R removeById(@PathVariable Long id) {
|
||||
return R.ok(sysFileService.deleteFile(id));
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传文件 文件名采用uuid,避免原始文件名中带"-"符号导致下载的时候解析出现异常
|
||||
* @param file 资源
|
||||
* @return R(/ admin / bucketName / filename)
|
||||
*/
|
||||
@PostMapping(value = "/upload")
|
||||
public R upload(@RequestPart("file") MultipartFile file) {
|
||||
return sysFileService.uploadFile(file);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取文件
|
||||
* @param bucket 桶名称
|
||||
* @param fileName 文件空间/名称
|
||||
* @param response
|
||||
* @return
|
||||
*/
|
||||
@Inner(false)
|
||||
@GetMapping("/{bucket}/{fileName}")
|
||||
public void file(@PathVariable String bucket, @PathVariable String fileName, HttpServletResponse response) {
|
||||
sysFileService.getFile(bucket, fileName, response);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,33 @@
|
|||
/*
|
||||
* Copyright (c) 2018-2025, lengleng All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* Neither the name of the pig4cloud.com developer nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from
|
||||
* this software without specific prior written permission.
|
||||
* Author: lengleng (wangiegie@gmail.com)
|
||||
*/
|
||||
|
||||
package com.pig4cloud.pig.admin.mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.pig4cloud.pig.admin.api.entity.SysFile;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
/**
|
||||
* 文件管理
|
||||
*
|
||||
* @author Luckly
|
||||
* @date 2019-06-18 17:18:42
|
||||
*/
|
||||
@Mapper
|
||||
public interface SysFileMapper extends BaseMapper<SysFile> {
|
||||
|
||||
}
|
|
@ -0,0 +1,57 @@
|
|||
/*
|
||||
* Copyright (c) 2018-2025, lengleng All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* Neither the name of the pig4cloud.com developer nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from
|
||||
* this software without specific prior written permission.
|
||||
* Author: lengleng (wangiegie@gmail.com)
|
||||
*/
|
||||
|
||||
package com.pig4cloud.pig.admin.service;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import com.pig4cloud.pig.admin.api.entity.SysFile;
|
||||
import com.pig4cloud.pig.common.core.util.R;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
/**
|
||||
* 文件管理
|
||||
*
|
||||
* @author Luckly
|
||||
* @date 2019-06-18 17:18:42
|
||||
*/
|
||||
public interface SysFileService extends IService<SysFile> {
|
||||
|
||||
/**
|
||||
* 上传文件
|
||||
* @param file
|
||||
* @return
|
||||
*/
|
||||
R uploadFile(MultipartFile file);
|
||||
|
||||
/**
|
||||
* 读取文件
|
||||
* @param bucket 桶名称
|
||||
* @param fileName 文件名称
|
||||
* @param response 输出流
|
||||
*/
|
||||
void getFile(String bucket, String fileName, HttpServletResponse response);
|
||||
|
||||
/**
|
||||
* 删除文件
|
||||
* @param id
|
||||
* @return
|
||||
*/
|
||||
Boolean deleteFile(Long id);
|
||||
|
||||
}
|
|
@ -0,0 +1,128 @@
|
|||
/*
|
||||
* Copyright (c) 2018-2025, lengleng All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* Neither the name of the pig4cloud.com developer nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from
|
||||
* this software without specific prior written permission.
|
||||
* Author: lengleng (wangiegie@gmail.com)
|
||||
*/
|
||||
package com.pig4cloud.pig.admin.service.impl;
|
||||
|
||||
import cn.hutool.core.io.FileUtil;
|
||||
import cn.hutool.core.io.IoUtil;
|
||||
import cn.hutool.core.util.IdUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.amazonaws.services.s3.model.S3Object;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.pig4cloud.pig.admin.api.entity.SysFile;
|
||||
import com.pig4cloud.pig.admin.mapper.SysFileMapper;
|
||||
import com.pig4cloud.pig.admin.service.SysFileService;
|
||||
import com.pig4cloud.pig.common.core.util.R;
|
||||
import com.pig4cloud.plugin.oss.OssProperties;
|
||||
import com.pig4cloud.plugin.oss.service.OssTemplate;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.SneakyThrows;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 文件管理
|
||||
*
|
||||
* @author Luckly
|
||||
* @date 2019-06-18 17:18:42
|
||||
*/
|
||||
@Slf4j
|
||||
@Service
|
||||
@AllArgsConstructor
|
||||
public class SysFileServiceImpl extends ServiceImpl<SysFileMapper, SysFile> implements SysFileService {
|
||||
|
||||
private final OssProperties ossProperties;
|
||||
|
||||
private final OssTemplate minioTemplate;
|
||||
|
||||
/**
|
||||
* 上传文件
|
||||
* @param file
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public R uploadFile(MultipartFile file) {
|
||||
String fileName = IdUtil.simpleUUID() + StrUtil.DOT + FileUtil.extName(file.getOriginalFilename());
|
||||
Map<String, String> resultMap = new HashMap<>(4);
|
||||
resultMap.put("bucketName", ossProperties.getBucketName());
|
||||
resultMap.put("fileName", fileName);
|
||||
resultMap.put("url", String.format("/admin/sys-file/%s/%s", ossProperties.getBucketName(), fileName));
|
||||
|
||||
try {
|
||||
minioTemplate.putObject(ossProperties.getBucketName(), fileName, file.getInputStream());
|
||||
// 文件管理数据记录,收集管理追踪文件
|
||||
fileLog(file, fileName);
|
||||
}
|
||||
catch (Exception e) {
|
||||
log.error("上传失败", e);
|
||||
return R.failed(e.getLocalizedMessage());
|
||||
}
|
||||
return R.ok(resultMap);
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取文件
|
||||
* @param bucket
|
||||
* @param fileName
|
||||
* @param response
|
||||
*/
|
||||
@Override
|
||||
public void getFile(String bucket, String fileName, HttpServletResponse response) {
|
||||
try (S3Object s3Object = minioTemplate.getObject(bucket, fileName)) {
|
||||
response.setContentType("application/octet-stream; charset=UTF-8");
|
||||
IoUtil.copy(s3Object.getObjectContent(), response.getOutputStream());
|
||||
}
|
||||
catch (Exception e) {
|
||||
log.error("文件读取异常: {}", e.getLocalizedMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除文件
|
||||
* @param id
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
@SneakyThrows
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public Boolean deleteFile(Long id) {
|
||||
SysFile file = this.getById(id);
|
||||
minioTemplate.removeObject(ossProperties.getBucketName(), file.getFileName());
|
||||
return this.removeById(id);
|
||||
}
|
||||
|
||||
/**
|
||||
* 文件管理数据记录,收集管理追踪文件
|
||||
* @param file 上传文件格式
|
||||
* @param fileName 文件名
|
||||
*/
|
||||
private void fileLog(MultipartFile file, String fileName) {
|
||||
SysFile sysFile = new SysFile();
|
||||
sysFile.setFileName(fileName);
|
||||
sysFile.setOriginal(file.getOriginalFilename());
|
||||
sysFile.setFileSize(file.getSize());
|
||||
sysFile.setType(FileUtil.extName(file.getOriginalFilename()));
|
||||
sysFile.setBucketName(ossProperties.getBucketName());
|
||||
this.save(sysFile);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,26 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
<!--
|
||||
~
|
||||
~ Copyright (c) 2018-2025, lengleng All rights reserved.
|
||||
~
|
||||
~ Redistribution and use in source and binary forms, with or without
|
||||
~ modification, are permitted provided that the following conditions are met:
|
||||
~
|
||||
~ Redistributions of source code must retain the above copyright notice,
|
||||
~ this list of conditions and the following disclaimer.
|
||||
~ Redistributions in binary form must reproduce the above copyright
|
||||
~ notice, this list of conditions and the following disclaimer in the
|
||||
~ documentation and/or other materials provided with the distribution.
|
||||
~ Neither the name of the pig4cloud.com developer nor the names of its
|
||||
~ contributors may be used to endorse or promote products derived from
|
||||
~ this software without specific prior written permission.
|
||||
~ Author: lengleng (wangiegie@gmail.com)
|
||||
~
|
||||
-->
|
||||
|
||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
|
||||
<mapper namespace="com.pig4cloud.pig.admin.mapper.SysFileMapper">
|
||||
|
||||
</mapper>
|
Loading…
Reference in New Issue