文件管理优化,用户组管理

master
chen 2 years ago
parent fd25c70c41
commit f1aac4d77c
  1. 6
      pom.xml
  2. 2
      src/main/java/com/huoran/iasf/controller/CaptchaController.java
  3. 2
      src/main/java/com/huoran/iasf/controller/DeptController.java
  4. 2
      src/main/java/com/huoran/iasf/controller/PermissionController.java
  5. 2
      src/main/java/com/huoran/iasf/controller/RoleController.java
  6. 2
      src/main/java/com/huoran/iasf/controller/RolePermissionController.java
  7. 6
      src/main/java/com/huoran/iasf/controller/SysFilesController.java
  8. 2
      src/main/java/com/huoran/iasf/controller/UserController.java
  9. 2
      src/main/java/com/huoran/iasf/controller/UserRoleController.java
  10. 4
      src/main/java/com/huoran/iasf/entity/SysFilesEntity.java
  11. 6
      src/main/java/com/huoran/iasf/service/SysFilesService.java
  12. 56
      src/main/java/com/huoran/iasf/service/impl/SysFilesServiceImpl.java
  13. 3
      src/main/java/com/huoran/iasf/vo/req/FileParameters.java
  14. 4
      src/main/resources/application-dev.yml
  15. 80
      src/test/java/com/company/project/CodeGenerator.java

@ -94,6 +94,12 @@
<artifactId>mybatis-plus-boot-starter</artifactId> <artifactId>mybatis-plus-boot-starter</artifactId>
<version>${mybatis-plus.version}</version> <version>${mybatis-plus.version}</version>
</dependency> </dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>${mybatis-plus.version}</version>
</dependency>
<dependency> <dependency>
<groupId>mysql</groupId> <groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId> <artifactId>mysql-connector-java</artifactId>

@ -25,7 +25,7 @@ import java.io.IOException;
* @version V1.0 * @version V1.0
* @date 2022年7月28日 * @date 2022年7月28日
*/ */
@Api(tags = "验证码相关") @Api(tags = "登录验证码")
@RestController @RestController
@Slf4j @Slf4j
@RequestMapping("/sys") @RequestMapping("/sys")

@ -24,7 +24,7 @@ import java.util.List;
*/ */
@RequestMapping("/sys") @RequestMapping("/sys")
@RestController @RestController
@Api(tags = "组织模块-机构管理") @Api(tags = "部门管理")
public class DeptController { public class DeptController {
@Resource @Resource
private DeptService deptService; private DeptService deptService;

@ -25,7 +25,7 @@ import javax.validation.Valid;
*/ */
@RequestMapping("/sys") @RequestMapping("/sys")
@RestController @RestController
@Api(tags = "组织模块-菜单权限管理") @Api(tags = "菜单权限管理")
public class PermissionController { public class PermissionController {
@Resource @Resource

@ -29,7 +29,7 @@ import java.util.List;
*/ */
@RequestMapping("/sys") @RequestMapping("/sys")
@RestController @RestController
@Api(tags = "组织模块-角色管理") @Api(tags = "角色管理")
public class RoleController { public class RoleController {
@Resource @Resource
private RoleService roleService; private RoleService roleService;

@ -25,7 +25,7 @@ import javax.validation.Valid;
*/ */
@RequestMapping("/sys") @RequestMapping("/sys")
@RestController @RestController
@Api(tags = "组织管理-角色菜单关联接口") @Api(tags = "角色菜单关联接口")
public class RolePermissionController { public class RolePermissionController {
@Resource @Resource
private RolePermissionService rolePermissionService; private RolePermissionService rolePermissionService;

@ -49,20 +49,20 @@ public class SysFilesController {
@ApiOperation(value = "文件预览") @ApiOperation(value = "文件预览")
@GetMapping("/preview/{id}") @GetMapping("/preview/{id}")
public void preview(@PathVariable Integer id, HttpServletRequest req, HttpServletResponse res) { public void preview(@PathVariable String id, HttpServletRequest req, HttpServletResponse res) {
sysFilesService.preview(id, req, res); sysFilesService.preview(id, req, res);
} }
@ApiOperation(value = "文件下载") @ApiOperation(value = "文件下载")
@GetMapping("/download/{id}") @GetMapping("/download/{id}")
public void download(@PathVariable Integer id, HttpServletRequest req, HttpServletResponse res) { public void download(@PathVariable String id, HttpServletRequest req, HttpServletResponse res) {
sysFilesService.download(id, req, res); sysFilesService.download(id, req, res);
} }
@ApiOperation(value = "删除") @ApiOperation(value = "删除")
@DeleteMapping("/delete") @DeleteMapping("/delete")
@RequiresPermissions("sysFiles:delete") @RequiresPermissions("sysFiles:delete")
public R delete(@RequestBody @ApiParam(value = "id集合") List<Integer> ids) { public R delete(@RequestBody @ApiParam(value = "id集合") List<String> ids) {
sysFilesService.removeByIdsAndFiles(ids); sysFilesService.removeByIdsAndFiles(ids);
return R.success(); return R.success();
} }

@ -33,7 +33,7 @@ import java.util.List;
* @date 2022年7月28日 * @date 2022年7月28日
*/ */
@RestController @RestController
@Api(tags = "组织模块-用户管理") @Api(tags = "用户管理")
@RequestMapping("/sys") @RequestMapping("/sys")
@Slf4j @Slf4j
public class UserController { public class UserController {

@ -23,7 +23,7 @@ import javax.validation.Valid;
*/ */
@RequestMapping("/sys") @RequestMapping("/sys")
@RestController @RestController
@Api(tags = "组织管理-用户角色关联接口") @Api(tags = "用户角色关联接口")
public class UserRoleController { public class UserRoleController {
@Resource @Resource
private UserRoleService userRoleService; private UserRoleService userRoleService;

@ -22,8 +22,8 @@ public class SysFilesEntity extends BaseEntity implements Serializable {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
@ApiModelProperty(value = "主键") @ApiModelProperty(value = "主键")
@TableId(value = "id", type = IdType.AUTO) @TableId(value = "id")
private int id; private String id;
@ApiModelProperty(value = "url地址") @ApiModelProperty(value = "url地址")
private String url; private String url;

@ -21,10 +21,10 @@ public interface SysFilesService extends IService<SysFilesEntity> {
R saveFile(MultipartFile file, FileParameters fileParameters); R saveFile(MultipartFile file, FileParameters fileParameters);
void removeByIdsAndFiles(List<Integer> ids); void removeByIdsAndFiles(List<String> ids);
void preview(Integer id, HttpServletRequest req, HttpServletResponse res); void preview(String id, HttpServletRequest req, HttpServletResponse res);
void download(Integer id, HttpServletRequest req, HttpServletResponse res); void download(String id, HttpServletRequest req, HttpServletResponse res);
} }

@ -2,6 +2,7 @@ package com.huoran.iasf.service.impl;
import cn.hutool.core.io.FileUtil; import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil; import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huoran.iasf.common.config.FileUploadProperties; import com.huoran.iasf.common.config.FileUploadProperties;
import com.huoran.iasf.common.exception.BusinessException; import com.huoran.iasf.common.exception.BusinessException;
@ -15,6 +16,8 @@ import org.apache.commons.io.FileUtils;
import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource; import javax.annotation.Resource;
@ -24,6 +27,7 @@ import javax.servlet.http.HttpServletResponse;
import java.io.*; import java.io.*;
import java.net.URLEncoder; import java.net.URLEncoder;
import java.util.*; import java.util.*;
import java.util.regex.Pattern;
/** /**
* 文件上传 服务类 * 文件上传 服务类
@ -41,6 +45,9 @@ public class SysFilesServiceImpl extends ServiceImpl<SysFilesMapper, SysFilesEnt
@Override @Override
public R saveFile(MultipartFile file, FileParameters fileParameters) { public R saveFile(MultipartFile file, FileParameters fileParameters) {
if (ObjectUtil.isEmpty(file) || file.getSize() <= 0) {
throw new BusinessException("上传内容为空");
}
//存储文件夹 //存储文件夹
String createTime = DateUtils.format(new Date(), DateUtils.DATEPATTERN); String createTime = DateUtils.format(new Date(), DateUtils.DATEPATTERN);
String newPath = fileUploadProperties.getPath() + createTime + File.separator; String newPath = fileUploadProperties.getPath() + createTime + File.separator;
@ -70,15 +77,17 @@ public class SysFilesServiceImpl extends ServiceImpl<SysFilesMapper, SysFilesEnt
SysFilesEntity sysFilesEntity = new SysFilesEntity(); SysFilesEntity sysFilesEntity = new SysFilesEntity();
sysFilesEntity.setFileName(fileName); sysFilesEntity.setFileName(fileName);
sysFilesEntity.setFilePath(newFilePathName); sysFilesEntity.setFilePath(newFilePathName);
// sysFilesEntity.setUrl(url);
sysFilesEntity.setFileSize(fileSize); sysFilesEntity.setFileSize(fileSize);
sysFilesEntity.setFormat(suffixName); sysFilesEntity.setFormat(suffixName);
sysFilesEntity.setUploader(fileParameters.getUploader()); sysFilesEntity.setUploader(fileParameters.getUploader());
sysFilesEntity.setQuote(fileParameters.getQuote()); sysFilesEntity.setQuote(fileParameters.getQuote());
sysFilesEntity.setSite(fileParameters.getSite()); sysFilesEntity.setSite(fileParameters.getSite());
sysFilesEntity.setType(fileParameters.getType()); sysFilesEntity.setType(fileCheck(suffixName));
this.save(sysFilesEntity); this.save(sysFilesEntity);
String url = fileUploadProperties.getUrl() + "/preview" + "/" + sysFilesEntity.getId(); // String url = fileUploadProperties.getUrl() + "/preview" + "/" + sysFilesEntity.getId();
String url = "http://" + getRemoteIP() + fileUploadProperties.getUrl() + sysFilesEntity.getId();
sysFilesEntity.setUrl(url);
this.updateById(sysFilesEntity);
Map<String, String> resultMap = new HashMap<>(); Map<String, String> resultMap = new HashMap<>();
resultMap.put("url", url); resultMap.put("url", url);
return R.success(resultMap); return R.success(resultMap);
@ -87,8 +96,43 @@ public class SysFilesServiceImpl extends ServiceImpl<SysFilesMapper, SysFilesEnt
} }
} }
/**
* 获取请求ip地址
* @throws Exception Exception
*/
private String getRemoteIP() throws Exception {
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
String ip = "";
if (request.getHeader("x-forwarded-for") == null) {
ip = request.getRemoteAddr();
}else{
ip = request.getHeader("x-forwarded-for");
}
return ip;
}
/**
* 校验文件类型的正则
**/
public static String fileCheck(String suffixName) {
String reg1 = "(mp4|flv|avi|mov|rm|rmvb|wmv|hevc)";
Pattern pattern1 = Pattern.compile(reg1);
boolean flag1 = pattern1.matcher(suffixName.toLowerCase()).find();
if (flag1){
return "视频";
}
String reg = "(jpg|png|tiff|webp|heif|gif|bmp)";
Pattern pattern = Pattern.compile(reg);
boolean flag = pattern.matcher(suffixName.toLowerCase()).find();
if (flag){
return "图片";
}
return "文件";
}
@Override @Override
public void preview(Integer id, HttpServletRequest req, HttpServletResponse res) { public void preview(String id, HttpServletRequest req, HttpServletResponse res) {
if (id != null) { if (id != null) {
SysFilesEntity entity = this.getById(id); SysFilesEntity entity = this.getById(id);
String path = entity.getFilePath(); String path = entity.getFilePath();
@ -112,7 +156,7 @@ public class SysFilesServiceImpl extends ServiceImpl<SysFilesMapper, SysFilesEnt
} }
@Override @Override
public void download(Integer id, HttpServletRequest req, HttpServletResponse res) { public void download(String id, HttpServletRequest req, HttpServletResponse res) {
if (id != null) { if (id != null) {
SysFilesEntity entity = this.getById(id); SysFilesEntity entity = this.getById(id);
String path = entity.getFilePath(); String path = entity.getFilePath();
@ -161,7 +205,7 @@ public class SysFilesServiceImpl extends ServiceImpl<SysFilesMapper, SysFilesEnt
} }
@Override @Override
public void removeByIdsAndFiles(List<Integer> ids) { public void removeByIdsAndFiles(List<String> ids) {
List<SysFilesEntity> list = this.listByIds(ids); List<SysFilesEntity> list = this.listByIds(ids);
list.forEach(entity -> { list.forEach(entity -> {
//如果之前的文件存在,删除 //如果之前的文件存在,删除

@ -20,7 +20,4 @@ public class FileParameters {
@ApiModelProperty(value = "引用地方",example = "某某文章") @ApiModelProperty(value = "引用地方",example = "某某文章")
private String quote; private String quote;
@ApiModelProperty(value = "文件类型",example = "图片")
private String type;
} }

@ -26,5 +26,5 @@ file:
#文件上传目录 绝对路径 末尾请加 / #文件上传目录 绝对路径 末尾请加 /
path: D:/files/ #windows path: D:/files/ #windows
#path: /data/files/ #linux #path: /data/files/ #linux
#文件预览、下载的url, 末尾请勿加 / #文件预览url
url: http://localhost:10000/iasf/sysFiles url: :10000/iasf/sysFiles/preview/

@ -0,0 +1,80 @@
package com.company.project;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import org.junit.Test;
/**
* @Author chen
* @DATE 2021/8/10 15:42
* @Version 1.0
*/
public class CodeGenerator {
@Test
public void run() {
// 1、创建代码生成器
AutoGenerator mpg = new AutoGenerator();
// 2、全局配置
GlobalConfig gc = new GlobalConfig();
String projectPath = System.getProperty("user.dir");
gc.setOutputDir(projectPath + "/src/main/java");
gc.setAuthor("cheney");//设置作者
gc.setOpen(false); //生成后是否打开资源管理器
gc.setFileOverride(false); //重新生成时文件是否覆盖
gc.setServiceName("%sService"); //去掉Service接口的首字母I
gc.setIdType(IdType.AUTO); //主键策略
gc.setDateType(DateType.ONLY_DATE);//定义生成的实体类中日期类型
gc.setSwagger2(true);//开启Swagger2模式
mpg.setGlobalConfig(gc);
// 3、数据源配置
DataSourceConfig dsc = new DataSourceConfig();
dsc.setUrl("jdbc:mysql://139.9.47.170:3306/iasf?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8");
dsc.setDriverName("com.mysql.cj.jdbc.Driver");
dsc.setUsername("root");
dsc.setPassword("HuoRan@2021");
dsc.setDbType(DbType.MYSQL);
mpg.setDataSource(dsc);
// 4、包配置
PackageConfig pc = new PackageConfig();
pc.setParent("com.huoran.iasf");
//pc.setModuleName("occupationlab"); //模块名
pc.setController("controller");
pc.setEntity("entity");
pc.setService("service");
pc.setServiceImpl("service.impl");
pc.setMapper("mapper");
mpg.setPackageInfo(pc);
// 5、策略配置
StrategyConfig strategy = new StrategyConfig();
strategy.setInclude("sys_user_group");
strategy.setNaming(NamingStrategy.underline_to_camel);//数据库表映射到实体的命名策略
strategy.setTablePrefix("sys_"); //生成实体时去掉表前缀
strategy.setColumnNaming(NamingStrategy.underline_to_camel);//数据库表字段映射到实体的命名策略
strategy.setEntityLombokModel(true); // lombok 模型 @Accessors(chain = true) setter链式操作
strategy.setRestControllerStyle(true); //restful api风格控制器
strategy.setControllerMappingHyphenStyle(true); //url中驼峰转连字符
mpg.setStrategy(strategy);
// 6、执行
mpg.execute();
}
}
Loading…
Cancel
Save