From 7b2609408d6911249bdc0b5b5b575e1c6c4f55e9 Mon Sep 17 00:00:00 2001 From: huangxutao Date: Tue, 18 Jun 2019 18:54:03 +0800 Subject: [PATCH] =?UTF-8?q?:sparkles:=20=E6=B7=BB=E5=8A=A0=E6=96=B0?= =?UTF-8?q?=E7=89=B9=E6=80=A7=E3=80=82=E6=96=87=E4=BB=B6=E7=AE=A1=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../pigx/admin/api/entity/SysFile.java | 112 ++++++++++++ .../pigx/admin/controller/FileController.java | 53 +++++- .../admin/controller/SysFileController.java | 110 ++++++++++++ .../pigx/admin/mapper/SysFileMapper.java | 31 ++++ .../pigx/admin/service/SysFileService.java | 31 ++++ .../service/impl/SysFileServiceImpl.java | 34 ++++ .../pig4cloud/pigx/admin/util/FileKit.java | 160 ++++++++++++++++++ .../main/resources/mapper/SysFileMapper.xml | 44 +++++ 8 files changed, 573 insertions(+), 2 deletions(-) create mode 100644 pigx-upms/pigx-upms-api/src/main/java/com/pig4cloud/pigx/admin/api/entity/SysFile.java create mode 100644 pigx-upms/pigx-upms-biz/src/main/java/com/pig4cloud/pigx/admin/controller/SysFileController.java create mode 100644 pigx-upms/pigx-upms-biz/src/main/java/com/pig4cloud/pigx/admin/mapper/SysFileMapper.java create mode 100644 pigx-upms/pigx-upms-biz/src/main/java/com/pig4cloud/pigx/admin/service/SysFileService.java create mode 100644 pigx-upms/pigx-upms-biz/src/main/java/com/pig4cloud/pigx/admin/service/impl/SysFileServiceImpl.java create mode 100644 pigx-upms/pigx-upms-biz/src/main/java/com/pig4cloud/pigx/admin/util/FileKit.java create mode 100644 pigx-upms/pigx-upms-biz/src/main/resources/mapper/SysFileMapper.xml diff --git a/pigx-upms/pigx-upms-api/src/main/java/com/pig4cloud/pigx/admin/api/entity/SysFile.java b/pigx-upms/pigx-upms-api/src/main/java/com/pig4cloud/pigx/admin/api/entity/SysFile.java new file mode 100644 index 000000000..5f37e423d --- /dev/null +++ b/pigx-upms/pigx-upms-api/src/main/java/com/pig4cloud/pigx/admin/api/entity/SysFile.java @@ -0,0 +1,112 @@ +/* + * 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.api.entity; + +import com.baomidou.mybatisplus.annotation.TableField; +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; +import lombok.EqualsAndHashCode; + +import java.time.LocalDateTime; + +/** + * 文件管理 + * + * @author Luckly + * @date 2019-06-18 17:18:42 + */ +@Data +@TableName("sys_file") +@EqualsAndHashCode(callSuper = true) +@ApiModel(description = "文件管理") +public class SysFile extends Model { + private static final long serialVersionUID = 1L; + + /** + * 编号 + */ + @TableId + private String id; + /** + * 文件名 + */ + @TableField("`file_name`") + private String name; + /** + * 保存路径 + */ + private String path; + /** + * 绝对路径 + */ + private String absolutePath; + /** + * 上传服务器前缀地址 + */ + private String prefix; + /** + * 原文件名 + */ + private String original; + /** + * MD5值 + */ + private String md5; + /** + * 后缀名 + */ + private String suffix; + /** + * 文件类型 + */ + private String type; + /** + * 文件大小 + */ + @TableField("`file_size`") + private Long fileSize; + /** + * 显示大小 + */ + private String displaySize; + /** + * 上传人 + */ + private String createUser; + /** + * 上传时间 + */ + private LocalDateTime createTime; + /** + * 更新人 + */ + private String updateUser; + /** + * 更新时间 + */ + private LocalDateTime updateTime; + /** + * 删除标识:1-删除,0-正常 + */ + @TableLogic + private Integer delFlag; +} diff --git a/pigx-upms/pigx-upms-biz/src/main/java/com/pig4cloud/pigx/admin/controller/FileController.java b/pigx-upms/pigx-upms-biz/src/main/java/com/pig4cloud/pigx/admin/controller/FileController.java index bbe36559f..de4b99401 100644 --- a/pigx-upms/pigx-upms-biz/src/main/java/com/pig4cloud/pigx/admin/controller/FileController.java +++ b/pigx-upms/pigx-upms-biz/src/main/java/com/pig4cloud/pigx/admin/controller/FileController.java @@ -22,15 +22,21 @@ 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; @@ -50,7 +56,7 @@ import java.util.Map; @Api(value = "file", tags = "文件管理模块") public class FileController { private final MinioTemplate minioTemplate; - + private final SysFileService sysFileService; /** * 上传文件 * 文件名采用uuid,避免原始文件名中带"-"符号导致下载的时候解析出现异常 @@ -59,7 +65,7 @@ public class FileController { * @return R(bucketName, filename) */ @PostMapping("/upload") - public R upload(@RequestParam("file") MultipartFile file) { + public R upload(@RequestParam("file") MultipartFile file, HttpServletRequest request) { String fileName = IdUtil.simpleUUID() + StrUtil.DOT + FileUtil.extName(file.getOriginalFilename()); Map resultMap = new HashMap<>(4); resultMap.put("bucketName", CommonConstants.BUCKET_NAME); @@ -67,6 +73,10 @@ public class FileController { 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()); @@ -74,6 +84,45 @@ public class FileController { 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); + } + + /** * 获取文件 * diff --git a/pigx-upms/pigx-upms-biz/src/main/java/com/pig4cloud/pigx/admin/controller/SysFileController.java b/pigx-upms/pigx-upms-biz/src/main/java/com/pig4cloud/pigx/admin/controller/SysFileController.java new file mode 100644 index 000000000..25bce80c9 --- /dev/null +++ b/pigx-upms/pigx-upms-biz/src/main/java/com/pig4cloud/pigx/admin/controller/SysFileController.java @@ -0,0 +1,110 @@ +/* + * 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 com.baomidou.mybatisplus.core.toolkit.Wrappers; +import com.baomidou.mybatisplus.extension.plugins.pagination.Page; +import com.pig4cloud.pigx.common.core.util.R; +import com.pig4cloud.pigx.common.log.annotation.SysLog; +import com.pig4cloud.pigx.admin.api.entity.SysFile; +import com.pig4cloud.pigx.admin.service.SysFileService; +import org.springframework.security.access.prepost.PreAuthorize; +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import io.swagger.annotations.ApiParam; +import lombok.AllArgsConstructor; +import org.springframework.web.bind.annotation.*; + + +/** + * 文件管理 + * + * @author Luckly + * @date 2019-06-18 17:18:42 + */ +@RestController +@AllArgsConstructor +@RequestMapping("/sysfile" ) +@Api(value = "sysfile", tags = "文件管理") +public class SysFileController { + + private final SysFileService sysFileService; + + /** + * 分页查询 + * @param page 分页对象 + * @param sysFile 文件管理 + * @return + */ + @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查询文件管理") + @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('admin_sysfile_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('admin_sysfile_edit')" ) + public R updateById(@RequestBody SysFile sysFile) { + return R.ok(sysFileService.updateById(sysFile)); + } + + /** + * 通过id删除文件管理 + * @param id id + * @return R + */ + @SysLog("删除文件管理" ) + @DeleteMapping("/{id}" ) + @PreAuthorize("@pms.hasPermission('admin_sysfile_del')" ) + public R removeById(@PathVariable String id) { + return R.ok(sysFileService.removeById(id)); + } + +} diff --git a/pigx-upms/pigx-upms-biz/src/main/java/com/pig4cloud/pigx/admin/mapper/SysFileMapper.java b/pigx-upms/pigx-upms-biz/src/main/java/com/pig4cloud/pigx/admin/mapper/SysFileMapper.java new file mode 100644 index 000000000..3516b06ba --- /dev/null +++ b/pigx-upms/pigx-upms-biz/src/main/java/com/pig4cloud/pigx/admin/mapper/SysFileMapper.java @@ -0,0 +1,31 @@ +/* + * 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.mapper; + +import com.baomidou.mybatisplus.core.mapper.BaseMapper; +import com.pig4cloud.pigx.admin.api.entity.SysFile; + +/** + * 文件管理 + * + * @author Luckly + * @date 2019-06-18 17:18:42 + */ +public interface SysFileMapper extends BaseMapper { + +} diff --git a/pigx-upms/pigx-upms-biz/src/main/java/com/pig4cloud/pigx/admin/service/SysFileService.java b/pigx-upms/pigx-upms-biz/src/main/java/com/pig4cloud/pigx/admin/service/SysFileService.java new file mode 100644 index 000000000..dab76d4b6 --- /dev/null +++ b/pigx-upms/pigx-upms-biz/src/main/java/com/pig4cloud/pigx/admin/service/SysFileService.java @@ -0,0 +1,31 @@ +/* + * 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.service; + +import com.baomidou.mybatisplus.extension.service.IService; +import com.pig4cloud.pigx.admin.api.entity.SysFile; + +/** + * 文件管理 + * + * @author Luckly + * @date 2019-06-18 17:18:42 + */ +public interface SysFileService extends IService { + +} diff --git a/pigx-upms/pigx-upms-biz/src/main/java/com/pig4cloud/pigx/admin/service/impl/SysFileServiceImpl.java b/pigx-upms/pigx-upms-biz/src/main/java/com/pig4cloud/pigx/admin/service/impl/SysFileServiceImpl.java new file mode 100644 index 000000000..c0b9008f1 --- /dev/null +++ b/pigx-upms/pigx-upms-biz/src/main/java/com/pig4cloud/pigx/admin/service/impl/SysFileServiceImpl.java @@ -0,0 +1,34 @@ +/* + * 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.service.impl; + +import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import com.pig4cloud.pigx.admin.api.entity.SysFile; +import com.pig4cloud.pigx.admin.mapper.SysFileMapper; +import com.pig4cloud.pigx.admin.service.SysFileService; +import org.springframework.stereotype.Service; + +/** + * 文件管理 + * + * @author Luckly + * @date 2019-06-18 17:18:42 + */ +@Service +public class SysFileServiceImpl extends ServiceImpl implements SysFileService { + +} diff --git a/pigx-upms/pigx-upms-biz/src/main/java/com/pig4cloud/pigx/admin/util/FileKit.java b/pigx-upms/pigx-upms-biz/src/main/java/com/pig4cloud/pigx/admin/util/FileKit.java new file mode 100644 index 000000000..ac460480b --- /dev/null +++ b/pigx-upms/pigx-upms-biz/src/main/java/com/pig4cloud/pigx/admin/util/FileKit.java @@ -0,0 +1,160 @@ +package com.pig4cloud.pigx.admin.util; + +import java.io.File; +import java.math.BigDecimal; + +/** + * @author: Luckly + * @date: 2019-06-18 + * @description: 文件套件 + */ +public class FileKit { + + private final static String[] TYPE_IMAGE = new String[]{".png", ".jpg", ".jpeg", ".gif", ".bmp"}; + private final static String[] TYPE_VIDEO = new String[]{".flv", ".swf", ".mkv", ".avi", ".rm", ".rmvb", ".mpeg", ".mpg", ".ogv", ".mov", ".wmv", ".mp4", ".webm"}; + private final static String[] TYPE_AUDIO = new String[]{".mp3", ".wav", ".mid", ".ogg"}; + private final static String[] TYPE_DOC = new String[]{".doc", ".docx", ".xls", ".xlsx", ".ppt", ".pptx", ".pdf"}; + + + /** + * 判断文件大小 + * + * @param len 文件长度 + * @param size 限制大小 + * @param unit 限制单位(B,K,M,G) + * @return + */ + public static boolean checkFileSize(Long len, int size, String unit) { + double fileSize = 0; + if ("B".equals(unit.toUpperCase())) { + fileSize = (double) len; + } else if ("K".equals(unit.toUpperCase())) { + fileSize = (double) len / 1024; + } else if ("M".equals(unit.toUpperCase())) { + fileSize = (double) len / 1048576; + } else if ("G".equals(unit.toUpperCase())) { + fileSize = (double) len / 1073741824; + } else { + fileSize = (double) len; + } + if (fileSize > size) { + return false; + } + return true; + } + + /** + * 转换文件大小为友好显示 + * + * @param size + * @return + */ + public static String getDisplaySize(long size) { + String displaySize = size + ""; + if (size < 1024) { + displaySize = size + "B"; + } else if (size < 1024 * 1024) { + displaySize = getDisplaySizeKB(size); + } else if (size < 1024 * 1024 * 1024) { + displaySize = getDisplaySizeMB(size); + } else if (size < 1024.0 * 1024 * 1024 * 1024) { + displaySize = getDisplaySizeGB(size); + } else if (size < 1024.0 * 1024 * 1024 * 1024 * 1024) { + displaySize = getDisplaySizeTB(size); + } + return displaySize; + } + + /** + * 转换文件大小为友好显示 + * + * @param size + * @return + */ + public static String getDisplaySizeKB(long size) { + return new BigDecimal(size).divide(new BigDecimal(1024)).setScale(2, BigDecimal.ROUND_HALF_DOWN).doubleValue() + "KB"; + } + + /** + * 转换文件大小为友好显示 + * + * @param size + * @return + */ + public static String getDisplaySizeMB(long size) { + return new BigDecimal(size).divide(new BigDecimal(1024 * 1024)).setScale(2, BigDecimal.ROUND_HALF_DOWN).doubleValue() + "MB"; + } + + /** + * 转换文件大小为友好显示 + * + * @param size + * @return + */ + public static String getDisplaySizeGB(long size) { + return new BigDecimal(size).divide(new BigDecimal(1024 * 1024 * 1024.0)).setScale(2, BigDecimal.ROUND_HALF_DOWN).doubleValue() + "GB"; + } + + /** + * 转换文件大小为友好显示 + * + * @param size + * @return + */ + public static String getDisplaySizeTB(long size) { + return new BigDecimal(size).divide(new BigDecimal(1024.0 * 1024 * 1024 * 1024)).setScale(2, BigDecimal.ROUND_HALF_DOWN).doubleValue() + "TB"; + } + + + /** + * 获取文件类型 + * + * @param path + * @return + */ + public static String getType(String path) { + String type = "file"; + String suffix = getSuffix(path); + if (eqSuffix(suffix, TYPE_IMAGE)) + type = "image"; + else if (eqSuffix(suffix, TYPE_VIDEO)) + type = "video"; + else if (eqSuffix(suffix, TYPE_AUDIO)) + type = "audio"; + else if (eqSuffix(suffix, TYPE_DOC)) + type = "doc"; + return type; + } + + /** + * 获取文件后缀名 + * + * @param path 文件绝对路径 + * @return + */ + public static String getSuffix(String path) { + int idx = path.lastIndexOf("."); + if (idx < 0) + return ""; + return path.substring(path.lastIndexOf(".")).toLowerCase(); + } + + private static boolean eqSuffix(String suffix, String[] types) { + for (String type : types) { + if (type.equals(suffix)) + return true; + } + return false; + } + + /** + * 获取文件后缀名 + * + * @param file 文件 + * @return + */ + public static String getSuffix(File file) { + return getSuffix(file.getName()); + } + +} diff --git a/pigx-upms/pigx-upms-biz/src/main/resources/mapper/SysFileMapper.xml b/pigx-upms/pigx-upms-biz/src/main/resources/mapper/SysFileMapper.xml new file mode 100644 index 000000000..8580044bb --- /dev/null +++ b/pigx-upms/pigx-upms-biz/src/main/resources/mapper/SysFileMapper.xml @@ -0,0 +1,44 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + +