mirror of
http://git.pig4cloud.com/pig/pigx.git
synced 2026-11-08 12:39:05 +08:00
♻️ 重构代码。 Controll 名称修改,升级 hutool 4.5.15
This commit is contained in:
1 parent
31d0d13818
commit
04f620dc8a
19 files changed
+187
-263
No files matched your search
+1
-1
@@ -78,7 +78,7 @@ public class SqlFilterArgumentResolver implements HandlerMethodArgumentResolver
|
||||
}
|
||||
|
||||
if (StrUtil.isNotBlank(size)) {
|
||||
page.setCurrent(Long.parseLong(size));
|
||||
page.setSize(Long.parseLong(size));
|
||||
}
|
||||
|
||||
page.setAsc(sqlInject(ascs));
|
||||
|
||||
+5
-4
@@ -19,7 +19,7 @@ package com.pig4cloud.pigx.common.gateway.configuration;
|
||||
|
||||
import com.pig4cloud.pigx.common.gateway.rule.DiscoveryEnabledRule;
|
||||
import com.pig4cloud.pigx.common.gateway.rule.MetadataAwareRule;
|
||||
import com.pig4cloud.pigx.common.gateway.support.MicaRibbonRuleProperties;
|
||||
import com.pig4cloud.pigx.common.gateway.support.PigxRibbonRuleProperties;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
|
||||
@@ -37,14 +37,15 @@ import org.springframework.context.annotation.Scope;
|
||||
* Mica ribbon rule auto configuration.
|
||||
*
|
||||
* @author L.cm
|
||||
* @link https://github.com/lets-mica/mica
|
||||
*/
|
||||
@Configuration
|
||||
@RequiredArgsConstructor
|
||||
@ConditionalOnClass(NacosServer.class)
|
||||
@AutoConfigureBefore(RibbonClientConfiguration.class)
|
||||
@EnableConfigurationProperties(MicaRibbonRuleProperties.class)
|
||||
@ConditionalOnProperty(value = "mica.ribbon.rule.enabled", matchIfMissing = true)
|
||||
public class MicaRibbonRuleAutoConfiguration {
|
||||
@EnableConfigurationProperties(PigxRibbonRuleProperties.class)
|
||||
@ConditionalOnProperty(value = "ribbon.rule.enabled", matchIfMissing = true)
|
||||
public class PigxRibbonRuleAutoConfiguration {
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
+2
-2
@@ -19,7 +19,7 @@ package com.pig4cloud.pigx.common.gateway.predicate;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.pig4cloud.pigx.common.core.util.SpringContextHolder;
|
||||
import com.pig4cloud.pigx.common.gateway.support.MicaRibbonRuleProperties;
|
||||
import com.pig4cloud.pigx.common.gateway.support.PigxRibbonRuleProperties;
|
||||
import org.springframework.cloud.alibaba.nacos.ribbon.NacosServer;
|
||||
|
||||
import java.util.Map;
|
||||
@@ -36,7 +36,7 @@ public class MetadataAwarePredicate extends DiscoveryEnabledPredicate {
|
||||
final Map<String, String> metadata = server.getMetadata();
|
||||
|
||||
// 获取配置
|
||||
MicaRibbonRuleProperties properties = SpringContextHolder.getBean(MicaRibbonRuleProperties.class);
|
||||
PigxRibbonRuleProperties properties = SpringContextHolder.getBean(PigxRibbonRuleProperties.class);
|
||||
// 服务里的配置
|
||||
String localTag = properties.getTag();
|
||||
|
||||
|
||||
+2
-2
@@ -24,7 +24,7 @@ import com.netflix.loadbalancer.Server;
|
||||
import com.pig4cloud.pigx.common.core.util.SpringContextHolder;
|
||||
import com.pig4cloud.pigx.common.gateway.predicate.DiscoveryEnabledPredicate;
|
||||
import com.pig4cloud.pigx.common.gateway.predicate.MetadataAwarePredicate;
|
||||
import com.pig4cloud.pigx.common.gateway.support.MicaRibbonRuleProperties;
|
||||
import com.pig4cloud.pigx.common.gateway.support.PigxRibbonRuleProperties;
|
||||
import org.springframework.util.PatternMatchUtils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
@@ -48,7 +48,7 @@ public class MetadataAwareRule extends DiscoveryEnabledRule {
|
||||
|
||||
@Override
|
||||
public List<Server> filterServers(List<Server> serverList) {
|
||||
MicaRibbonRuleProperties ribbonProperties = SpringContextHolder.getBean(MicaRibbonRuleProperties.class);
|
||||
PigxRibbonRuleProperties ribbonProperties = SpringContextHolder.getBean(PigxRibbonRuleProperties.class);
|
||||
List<String> priorIpPattern = ribbonProperties.getPriorIpPattern();
|
||||
|
||||
// 1. 查找是否有本机 ip
|
||||
|
||||
+6
-28
@@ -17,10 +17,9 @@
|
||||
|
||||
package com.pig4cloud.pigx.admin.api.entity;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.TableField;
|
||||
import com.baomidou.mybatisplus.annotation.IdType;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableLogic;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import com.baomidou.mybatisplus.extension.activerecord.Model;
|
||||
import io.swagger.annotations.ApiModel;
|
||||
import lombok.Data;
|
||||
@@ -43,37 +42,20 @@ public class SysFile extends Model<SysFile> {
|
||||
/**
|
||||
* 编号
|
||||
*/
|
||||
@TableId
|
||||
private String id;
|
||||
@TableId(type = IdType.AUTO)
|
||||
private Long id;
|
||||
/**
|
||||
* 文件名
|
||||
*/
|
||||
@TableField("`name`")
|
||||
private String name;
|
||||
/**
|
||||
* 保存路径
|
||||
*/
|
||||
private String path;
|
||||
/**
|
||||
* 绝对路径
|
||||
*/
|
||||
private String absolutePath;
|
||||
/**
|
||||
* 上传服务器前缀地址
|
||||
*/
|
||||
private String prefix;
|
||||
private String fileName;
|
||||
/**
|
||||
* 原文件名
|
||||
*/
|
||||
private String original;
|
||||
/**
|
||||
* MD5值
|
||||
* 容器名称
|
||||
*/
|
||||
private String md5;
|
||||
/**
|
||||
* 后缀名
|
||||
*/
|
||||
private String suffix;
|
||||
private String bucketName;
|
||||
/**
|
||||
* 文件类型
|
||||
*/
|
||||
@@ -82,10 +64,6 @@ public class SysFile extends Model<SysFile> {
|
||||
* 文件大小
|
||||
*/
|
||||
private Long fileSize;
|
||||
/**
|
||||
* 显示大小
|
||||
*/
|
||||
private String displaySize;
|
||||
/**
|
||||
* 上传人
|
||||
*/
|
||||
|
||||
-143
@@ -1,143 +0,0 @@
|
||||
/*
|
||||
* 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.pigx.admin.controller;
|
||||
|
||||
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.pig4cloud.common.minio.service.MinioTemplate;
|
||||
import com.pig4cloud.pigx.admin.api.entity.SysFile;
|
||||
import com.pig4cloud.pigx.admin.service.SysFileService;
|
||||
import com.pig4cloud.pigx.admin.util.FileKit;
|
||||
import com.pig4cloud.pigx.common.core.constant.CommonConstants;
|
||||
import com.pig4cloud.pigx.common.core.util.R;
|
||||
import io.swagger.annotations.Api;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.codec.digest.DigestUtils;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
;
|
||||
|
||||
/**
|
||||
* @author lengleng
|
||||
* @date 2018-12-30
|
||||
* <p>
|
||||
* 文件管理
|
||||
*/
|
||||
@Slf4j
|
||||
@RestController
|
||||
@AllArgsConstructor
|
||||
@RequestMapping("/file")
|
||||
@Api(value = "file", tags = "文件管理模块")
|
||||
public class FileController {
|
||||
private final MinioTemplate minioTemplate;
|
||||
private final SysFileService sysFileService;
|
||||
/**
|
||||
* 上传文件
|
||||
* 文件名采用uuid,避免原始文件名中带"-"符号导致下载的时候解析出现异常
|
||||
*
|
||||
* @param file 资源
|
||||
* @return R(bucketName, filename)
|
||||
*/
|
||||
@PostMapping("/upload")
|
||||
public R upload(@RequestParam("file") MultipartFile file, HttpServletRequest request) {
|
||||
String fileName = IdUtil.simpleUUID() + StrUtil.DOT + FileUtil.extName(file.getOriginalFilename());
|
||||
Map<String, String> resultMap = new HashMap<>(4);
|
||||
resultMap.put("bucketName", CommonConstants.BUCKET_NAME);
|
||||
resultMap.put("fileName", fileName);
|
||||
|
||||
try {
|
||||
minioTemplate.putObject(CommonConstants.BUCKET_NAME, fileName, file.getInputStream());
|
||||
String tenantId = request.getHeader(CommonConstants.TENANT_ID);
|
||||
log.debug("获取header中的租户ID为:{}", tenantId);
|
||||
//文件管理数据记录,收集管理追踪文件
|
||||
fileLog(file, fileName,tenantId);
|
||||
} catch (Exception e) {
|
||||
log.error("上传失败", e);
|
||||
return R.failed(e.getLocalizedMessage());
|
||||
}
|
||||
return R.ok(resultMap);
|
||||
}
|
||||
|
||||
/**
|
||||
* 文件管理数据记录,收集管理追踪文件
|
||||
*
|
||||
* @param file 上传文件格式
|
||||
* @param fileName 文件名
|
||||
*/
|
||||
private void fileLog(MultipartFile file, String fileName, String tenantId) throws IOException {
|
||||
SysFile sysFile = new SysFile();
|
||||
//存储文件名
|
||||
String name = CommonConstants.BUCKET_NAME + StrUtil.DASHED + fileName;
|
||||
//原文件名
|
||||
String original = file.getOriginalFilename();
|
||||
//文件大小
|
||||
Long size = file.getSize();
|
||||
//后缀名
|
||||
String suffix = FileKit.getSuffix(name);
|
||||
//文件类型
|
||||
String type = FileKit.getType(name);
|
||||
//显示大小
|
||||
String displaySize = FileKit.getDisplaySize(file.getSize());
|
||||
|
||||
sysFile.setName(name);
|
||||
sysFile.setOriginal(original);
|
||||
sysFile.setFileSize(size);
|
||||
sysFile.setSuffix(suffix);
|
||||
sysFile.setType(type);
|
||||
sysFile.setDisplaySize(displaySize);
|
||||
sysFile.setMd5(DigestUtils.md5Hex(file.getBytes()));
|
||||
|
||||
// if (StrUtil.isNotBlank(tenantId)) {
|
||||
// sysFile.setTenantId(Integer.parseInt(tenantId));
|
||||
// } else {
|
||||
// sysFile.setTenantId(CommonConstants.TENANT_ID_1);
|
||||
// }
|
||||
|
||||
sysFileService.save(sysFile);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取文件
|
||||
*
|
||||
* @param fileName 文件空间/名称
|
||||
* @param response
|
||||
* @return
|
||||
*/
|
||||
@GetMapping("/{fileName}")
|
||||
public void file(@PathVariable String fileName, HttpServletResponse response) {
|
||||
Integer separator = fileName.lastIndexOf(StrUtil.DASHED);
|
||||
try (InputStream inputStream = minioTemplate.getObject(fileName.substring(0, separator), fileName.substring(separator + 1, fileName.length()))) {
|
||||
response.setContentType("application/octet-stream; charset=UTF-8");
|
||||
IoUtil.copy(inputStream, response.getOutputStream());
|
||||
} catch (Exception e) {
|
||||
log.error("文件读取异常", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
+7
-7
@@ -44,8 +44,8 @@ import javax.validation.Valid;
|
||||
@AllArgsConstructor
|
||||
@RequestMapping("/client")
|
||||
@Api(value = "client", tags = "客户端管理模块")
|
||||
public class OauthClientDetailsController {
|
||||
private final SysOauthClientDetailsService sysOauthClientDetailsService;
|
||||
public class SysClientController {
|
||||
private final SysOauthClientDetailsService clientDetailsService;
|
||||
|
||||
/**
|
||||
* 通过ID查询
|
||||
@@ -55,7 +55,7 @@ public class OauthClientDetailsController {
|
||||
*/
|
||||
@GetMapping("/{id}")
|
||||
public R getById(@PathVariable Integer id) {
|
||||
return R.ok(sysOauthClientDetailsService.getById(id));
|
||||
return R.ok(clientDetailsService.getById(id));
|
||||
}
|
||||
|
||||
|
||||
@@ -68,7 +68,7 @@ public class OauthClientDetailsController {
|
||||
*/
|
||||
@GetMapping("/page")
|
||||
public R getOauthClientDetailsPage(Page page, SysOauthClientDetails sysOauthClientDetails) {
|
||||
return R.ok(sysOauthClientDetailsService.page(page, Wrappers.query(sysOauthClientDetails)));
|
||||
return R.ok(clientDetailsService.page(page, Wrappers.query(sysOauthClientDetails)));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -81,7 +81,7 @@ public class OauthClientDetailsController {
|
||||
@PostMapping
|
||||
@PreAuthorize("@pms.hasPermission('sys_client_add')")
|
||||
public R add(@Valid @RequestBody SysOauthClientDetails sysOauthClientDetails) {
|
||||
return R.ok(sysOauthClientDetailsService.save(sysOauthClientDetails));
|
||||
return R.ok(clientDetailsService.save(sysOauthClientDetails));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -94,7 +94,7 @@ public class OauthClientDetailsController {
|
||||
@DeleteMapping("/{id}")
|
||||
@PreAuthorize("@pms.hasPermission('sys_client_del')")
|
||||
public R removeById(@PathVariable String id) {
|
||||
return R.ok(sysOauthClientDetailsService.removeClientDetailsById(id));
|
||||
return R.ok(clientDetailsService.removeClientDetailsById(id));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -107,6 +107,6 @@ public class OauthClientDetailsController {
|
||||
@PutMapping
|
||||
@PreAuthorize("@pms.hasPermission('sys_client_edit')")
|
||||
public R update(@Valid @RequestBody SysOauthClientDetails sysOauthClientDetails) {
|
||||
return R.ok(sysOauthClientDetailsService.updateClientDetailsById(sysOauthClientDetails));
|
||||
return R.ok(clientDetailsService.updateClientDetailsById(sysOauthClientDetails));
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -43,7 +43,7 @@ import java.time.LocalDateTime;
|
||||
@AllArgsConstructor
|
||||
@RequestMapping("/dept")
|
||||
@Api(value = "dept", tags = "部门管理模块")
|
||||
public class DeptController {
|
||||
public class SysDeptController {
|
||||
private final SysDeptService sysDeptService;
|
||||
|
||||
/**
|
||||
+1
-1
@@ -51,7 +51,7 @@ import javax.validation.Valid;
|
||||
@AllArgsConstructor
|
||||
@RequestMapping("/dict")
|
||||
@Api(value = "dict", tags = "字典管理模块")
|
||||
public class DictController {
|
||||
public class SysDictController {
|
||||
private final SysDictService sysDictService;
|
||||
private final SysDictItemService sysDictItemService;
|
||||
|
||||
+29
-43
@@ -28,6 +28,9 @@ 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;
|
||||
|
||||
|
||||
/**
|
||||
@@ -41,7 +44,6 @@ import org.springframework.web.bind.annotation.*;
|
||||
@RequestMapping("/sys-file")
|
||||
@Api(value = "sys-file", tags = "文件管理")
|
||||
public class SysFileController {
|
||||
|
||||
private final SysFileService sysFileService;
|
||||
|
||||
/**
|
||||
@@ -58,46 +60,6 @@ public class SysFileController {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 通过id查询文件管理
|
||||
*
|
||||
* @param id id
|
||||
* @return R
|
||||
*/
|
||||
@ApiOperation(value = "通过id查询文件管理", notes = "通过id查询文件管理")
|
||||
@GetMapping("/{id}")
|
||||
public R getById(@PathVariable("id") String id) {
|
||||
return R.ok(sysFileService.getById(id));
|
||||
}
|
||||
|
||||
/**
|
||||
* 新增文件管理
|
||||
*
|
||||
* @param sysFile 文件管理
|
||||
* @return R
|
||||
*/
|
||||
@ApiOperation(value = "新增文件管理", notes = "新增文件管理")
|
||||
@SysLog("新增文件管理")
|
||||
@PostMapping
|
||||
@PreAuthorize("@pms.hasPermission('sys_file_add')")
|
||||
public R save(@RequestBody SysFile sysFile) {
|
||||
return R.ok(sysFileService.save(sysFile));
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改文件管理
|
||||
*
|
||||
* @param sysFile 文件管理
|
||||
* @return R
|
||||
*/
|
||||
@ApiOperation(value = "修改文件管理", notes = "修改文件管理")
|
||||
@SysLog("修改文件管理")
|
||||
@PutMapping
|
||||
@PreAuthorize("@pms.hasPermission('sys_file_edit')")
|
||||
public R updateById(@RequestBody SysFile sysFile) {
|
||||
return R.ok(sysFileService.updateById(sysFile));
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过id删除文件管理
|
||||
*
|
||||
@@ -108,8 +70,32 @@ public class SysFileController {
|
||||
@SysLog("删除文件管理")
|
||||
@DeleteMapping("/{id}")
|
||||
@PreAuthorize("@pms.hasPermission('sys_file_del')")
|
||||
public R removeById(@PathVariable String id) {
|
||||
return R.ok(sysFileService.removeById(id));
|
||||
public R removeById(@PathVariable Long id) {
|
||||
return R.ok(sysFileService.deleteFile(id));
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传文件
|
||||
* 文件名采用uuid,避免原始文件名中带"-"符号导致下载的时候解析出现异常
|
||||
*
|
||||
* @param file 资源
|
||||
* @return R(bucketName, filename)
|
||||
*/
|
||||
@PostMapping("/upload")
|
||||
public R upload(@RequestParam("file") MultipartFile file) {
|
||||
return sysFileService.uploadFile(file);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取文件
|
||||
*
|
||||
* @param fileName 文件空间/名称
|
||||
* @param response
|
||||
* @return
|
||||
*/
|
||||
@GetMapping("/{fileName}")
|
||||
public void file(@PathVariable String fileName, HttpServletResponse response) {
|
||||
sysFileService.getFile(fileName, response);
|
||||
}
|
||||
|
||||
}
|
||||
+1
-1
@@ -47,7 +47,7 @@ import java.util.List;
|
||||
@AllArgsConstructor
|
||||
@RequestMapping("/log")
|
||||
@Api(value = "log", tags = "日志管理模块")
|
||||
public class LogController {
|
||||
public class SysLogController {
|
||||
private final SysLogService sysLogService;
|
||||
|
||||
/**
|
||||
+1
-1
@@ -49,7 +49,7 @@ import java.util.stream.Collectors;
|
||||
@AllArgsConstructor
|
||||
@RequestMapping("/menu")
|
||||
@Api(value = "menu", tags = "菜单管理模块")
|
||||
public class MenuController {
|
||||
public class SysMenuController {
|
||||
private final SysMenuService sysMenuService;
|
||||
|
||||
/**
|
||||
+1
-1
@@ -41,7 +41,7 @@ import javax.validation.Valid;
|
||||
@AllArgsConstructor
|
||||
@RequestMapping("/role")
|
||||
@Api(value = "role", tags = "角色管理模块")
|
||||
public class RoleController {
|
||||
public class SysRoleController {
|
||||
private final SysRoleService sysRoleService;
|
||||
private final SysRoleMenuService sysRoleMenuService;
|
||||
|
||||
+1
-1
@@ -37,7 +37,7 @@ import java.util.Map;
|
||||
@AllArgsConstructor
|
||||
@RequestMapping("/token")
|
||||
@Api(value = "token", tags = "令牌管理模块")
|
||||
public class TokenController {
|
||||
public class SysTokenController {
|
||||
private final RemoteTokenService remoteTokenService;
|
||||
|
||||
/**
|
||||
+1
-1
@@ -46,7 +46,7 @@ import javax.validation.Valid;
|
||||
@AllArgsConstructor
|
||||
@RequestMapping("/user")
|
||||
@Api(value = "user", tags = "用户管理模块")
|
||||
public class UserController {
|
||||
public class SysUserController {
|
||||
private final SysUserService userService;
|
||||
|
||||
/**
|
||||
+27
@@ -19,6 +19,10 @@ package com.pig4cloud.pigx.admin.service;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import com.pig4cloud.pigx.admin.api.entity.SysFile;
|
||||
import com.pig4cloud.pigx.common.core.util.R;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
/**
|
||||
* 文件管理
|
||||
@@ -28,4 +32,27 @@ import com.pig4cloud.pigx.admin.api.entity.SysFile;
|
||||
*/
|
||||
public interface SysFileService extends IService<SysFile> {
|
||||
|
||||
/**
|
||||
* 上传文件
|
||||
*
|
||||
* @param file
|
||||
* @return
|
||||
*/
|
||||
R uploadFile(MultipartFile file);
|
||||
|
||||
/**
|
||||
* 读取文件
|
||||
*
|
||||
* @param fileName
|
||||
* @param response
|
||||
*/
|
||||
void getFile(String fileName, HttpServletResponse response);
|
||||
|
||||
/**
|
||||
* 删除文件
|
||||
*
|
||||
* @param id
|
||||
* @return
|
||||
*/
|
||||
Boolean deleteFile(Long id);
|
||||
}
|
||||
+99
@@ -16,11 +16,29 @@
|
||||
*/
|
||||
package com.pig4cloud.pigx.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.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.pig4cloud.common.minio.service.MinioTemplate;
|
||||
import com.pig4cloud.pigx.admin.api.entity.SysFile;
|
||||
import com.pig4cloud.pigx.admin.mapper.SysFileMapper;
|
||||
import com.pig4cloud.pigx.admin.service.SysFileService;
|
||||
import com.pig4cloud.pigx.common.core.constant.CommonConstants;
|
||||
import com.pig4cloud.pigx.common.core.util.R;
|
||||
import com.pig4cloud.pigx.common.security.util.SecurityUtils;
|
||||
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.io.InputStream;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 文件管理
|
||||
@@ -28,7 +46,88 @@ import org.springframework.stereotype.Service;
|
||||
* @author Luckly
|
||||
* @date 2019-06-18 17:18:42
|
||||
*/
|
||||
@Slf4j
|
||||
@Service
|
||||
@AllArgsConstructor
|
||||
public class SysFileServiceImpl extends ServiceImpl<SysFileMapper, SysFile> implements SysFileService {
|
||||
private final MinioTemplate 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", CommonConstants.BUCKET_NAME);
|
||||
resultMap.put("fileName", fileName);
|
||||
|
||||
try {
|
||||
minioTemplate.putObject(CommonConstants.BUCKET_NAME, fileName, file.getInputStream());
|
||||
//文件管理数据记录,收集管理追踪文件
|
||||
fileLog(file, fileName);
|
||||
} catch (Exception e) {
|
||||
log.error("上传失败", e);
|
||||
return R.failed(e.getLocalizedMessage());
|
||||
}
|
||||
return R.ok(resultMap);
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取文件
|
||||
*
|
||||
* @param fileName
|
||||
* @param response
|
||||
*/
|
||||
@Override
|
||||
public void getFile(String fileName, HttpServletResponse response) {
|
||||
int separator = fileName.lastIndexOf(StrUtil.DASHED);
|
||||
try (InputStream inputStream = minioTemplate.getObject(fileName.substring(0, separator),
|
||||
fileName.substring(separator + 1))) {
|
||||
response.setContentType("application/octet-stream; charset=UTF-8");
|
||||
IoUtil.copy(inputStream, response.getOutputStream());
|
||||
} catch (Exception e) {
|
||||
log.error("文件读取异常", e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 删除文件
|
||||
*
|
||||
* @param id
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
@SneakyThrows
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public Boolean deleteFile(Long id) {
|
||||
SysFile file = this.getById(id);
|
||||
minioTemplate.removeObject(CommonConstants.BUCKET_NAME, file.getFileName());
|
||||
return this.removeById(id);
|
||||
}
|
||||
|
||||
/**
|
||||
* 文件管理数据记录,收集管理追踪文件
|
||||
*
|
||||
* @param file 上传文件格式
|
||||
* @param fileName 文件名
|
||||
*/
|
||||
private void fileLog(MultipartFile file, String fileName) {
|
||||
SysFile sysFile = new SysFile();
|
||||
//原文件名
|
||||
String original = file.getOriginalFilename();
|
||||
sysFile.setFileName(fileName);
|
||||
sysFile.setOriginal(original);
|
||||
sysFile.setFileSize(file.getSize());
|
||||
sysFile.setType(FileUtil.extName(original));
|
||||
sysFile.setBucketName(CommonConstants.BUCKET_NAME);
|
||||
sysFile.setCreateUser(SecurityUtils.getUser().getUsername());
|
||||
this.save(sysFile);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -23,22 +23,4 @@
|
||||
|
||||
<mapper namespace="com.pig4cloud.pigx.admin.mapper.SysFileMapper">
|
||||
|
||||
<resultMap id="sysFileMap" type="com.pig4cloud.pigx.admin.api.entity.SysFile">
|
||||
<id property="id" column="id"/>
|
||||
<result property="name" column="file_name"/>
|
||||
<result property="path" column="path"/>
|
||||
<result property="absolutePath" column="absolute_path"/>
|
||||
<result property="prefix" column="prefix"/>
|
||||
<result property="original" column="original"/>
|
||||
<result property="md5" column="md5"/>
|
||||
<result property="suffix" column="suffix"/>
|
||||
<result property="type" column="type"/>
|
||||
<result property="fileSize" column="file_size"/>
|
||||
<result property="displaySize" column="display_size"/>
|
||||
<result property="createUser" column="create_user"/>
|
||||
<result property="createTime" column="create_time"/>
|
||||
<result property="updateUser" column="update_user"/>
|
||||
<result property="updateTime" column="update_time"/>
|
||||
<result property="delFlag" column="del_flag"/>
|
||||
</resultMap>
|
||||
</mapper>
|
||||
@@ -51,7 +51,7 @@
|
||||
<maven.compiler.target>1.8</maven.compiler.target>
|
||||
<spring-boot-admin.version>2.1.5</spring-boot-admin.version>
|
||||
<easyexcel.version>1.1.2-beta5</easyexcel.version>
|
||||
<hutool.version>4.5.13</hutool.version>
|
||||
<hutool.version>4.5.15</hutool.version>
|
||||
<kaptcha.version>0.0.9</kaptcha.version>
|
||||
<swagger.fox.version>2.9.2</swagger.fox.version>
|
||||
<swagger-bootstrap-ui.version>1.9.4</swagger-bootstrap-ui.version>
|
||||
@@ -62,8 +62,7 @@
|
||||
<ttl.version>2.10.2</ttl.version>
|
||||
<minio.version>6.0.7</minio.version>
|
||||
<elastic-job-lite.version>2.1.5</elastic-job-lite.version>
|
||||
<security.oauth.version>2.3.5.RELEASE</security.oauth.version>
|
||||
<security.oauth.auto.version>2.1.2.RELEASE</security.oauth.auto.version>
|
||||
<security.oauth.version>2.3.6.RELEASE</security.oauth.version>
|
||||
<activiti.version>5.22.0</activiti.version>
|
||||
<zipkin.version>2.12.2</zipkin.version>
|
||||
<zipkin-storage-mysql.version>2.8.4</zipkin-storage-mysql.version>
|
||||
@@ -168,11 +167,6 @@
|
||||
<artifactId>spring-security-oauth2</artifactId>
|
||||
<version>${security.oauth.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.security.oauth.boot</groupId>
|
||||
<artifactId>spring-security-oauth2-autoconfigure</artifactId>
|
||||
<version>${security.oauth.auto.version}</version>
|
||||
</dependency>
|
||||
<!--web 模块-->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
|
||||
Reference in new issue
Block a user