From e29538946ab9609295359758d44b3bcc462d45bb Mon Sep 17 00:00:00 2001 From: wangxiang <1827945911@qq.com> Date: Wed, 4 May 2022 04:31:00 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=93=80=20=E9=87=8D=E6=9E=84=E5=89=8D?= =?UTF-8?q?=E7=AB=AF=E6=A1=86=E6=9E=B6=E8=BF=9B=E8=A1=8C=E9=80=82=E9=85=8D?= =?UTF-8?q?=E5=90=8E=E7=AB=AF=E6=A1=86=E6=9E=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cloud/kicc/system/api/entity/File.java | 10 +-- .../system/controller/FileController.java | 69 ++++++++----------- .../kicc/system/service/FileService.java | 2 +- .../system/service/impl/FileServiceImpl.java | 40 +++-------- .../src/main/resources/bootstrap.yml | 10 --- kicc-ui/.env.development | 2 +- kicc-ui/.env.production | 2 +- .../api/platform/system/controller/file.ts | 26 +++++++ .../src/api/platform/system/entity/file.ts | 26 +++++++ 9 files changed, 97 insertions(+), 90 deletions(-) create mode 100644 kicc-ui/src/api/platform/system/controller/file.ts create mode 100644 kicc-ui/src/api/platform/system/entity/file.ts diff --git a/kicc-platform/kicc-platform-api/kicc-system-api/src/main/java/com/cloud/kicc/system/api/entity/File.java b/kicc-platform/kicc-platform-api/kicc-system-api/src/main/java/com/cloud/kicc/system/api/entity/File.java index cb6dcbe9..b6954f58 100644 --- a/kicc-platform/kicc-platform-api/kicc-system-api/src/main/java/com/cloud/kicc/system/api/entity/File.java +++ b/kicc-platform/kicc-platform-api/kicc-system-api/src/main/java/com/cloud/kicc/system/api/entity/File.java @@ -9,7 +9,7 @@ import lombok.experimental.Accessors; /** *

- * 文件管理 + * oss文件管理 *

* * @Author: entfrm开发团队-王翔 @@ -35,14 +35,14 @@ public class File extends CommonEntity { private String fileName; /** - * 原文件名 + * 容器名称 */ - private String original; + private String bucketName; /** - * 容器名称 + * 原文件名 */ - private String bucketName; + private String original; /** * 文件类型 diff --git a/kicc-platform/kicc-platform-biz/kicc-system-biz/src/main/java/com/cloud/kicc/system/controller/FileController.java b/kicc-platform/kicc-platform-biz/kicc-system-biz/src/main/java/com/cloud/kicc/system/controller/FileController.java index 70fa93ca..2cdf2ee6 100644 --- a/kicc-platform/kicc-platform-biz/kicc-system-biz/src/main/java/com/cloud/kicc/system/controller/FileController.java +++ b/kicc-platform/kicc-platform-biz/kicc-system-biz/src/main/java/com/cloud/kicc/system/controller/FileController.java @@ -1,10 +1,14 @@ package com.cloud.kicc.system.controller; import cn.hutool.core.io.IoUtil; +import cn.hutool.core.util.StrUtil; +import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.cloud.kicc.system.api.entity.File; +import com.cloud.kicc.system.api.entity.Role; +import com.cloud.kicc.system.api.entity.RoleMenu; import com.cloud.kicc.system.service.FileService; import com.cloud.kicc.common.core.api.R; import com.cloud.kicc.common.core.constant.AppConstants; @@ -20,6 +24,7 @@ import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletResponse; +import java.util.Arrays; /** *

@@ -31,65 +36,36 @@ import javax.servlet.http.HttpServletResponse; */ @RestController @AllArgsConstructor -@RequestMapping(AppConstants.APP_SYSTEM + "/sysFile") -@Api(value = "sysFile", tags = "文件管理") +@RequestMapping(AppConstants.APP_SYSTEM + "/file") +@Api(value = "file", tags = "文件管理") public class FileController { private final FileService fileService; - /** - * 分页查询 - * @param page 分页对象 - * @param file 文件管理 - * @return - */ - @GetMapping("/list") - @ApiOperation(value = "分页查询", notes = "分页查询") - public R> getSysFilePage(Page page, File file) { - return R.ok(fileService.page(page, Wrappers.query(file))); + private LambdaQueryWrapper getQueryWrapper(File file) { + return new LambdaQueryWrapper() + .like(StrUtil.isNotBlank(file.getFileName()), File::getFileName, file.getFileName()); } - /** - * 通过id删除文件管理 - * @param id id - * @return R - */ - @SysLog("删除文件管理") - @DeleteMapping("/{id:\\w+}") - @PreAuthorize("@pms.hasPermission('sys_file_del')") - @ApiOperation(value = "通过id删除文件管理", notes = "通过id删除文件管理") - public R removeById(@PathVariable String id) { - return R.ok(fileService.deleteFile(id)); + @GetMapping("/list") + @ApiOperation(value = "分页查询", notes = "分页查询") + public R list(Page page, File file) { + IPage fileIPage = fileService.page(page, getQueryWrapper(file)); + return R.ok(fileIPage.getRecords(), fileIPage.getTotal()); } - /** - * 上传文件 文件名采用uuid,避免原始文件名中带"-"符号导致下载的时候解析出现异常 - * @param file 资源 - * @return R(/ admin / bucketName / filename) - */ @PostMapping("/upload") public R upload(@RequestPart("file") MultipartFile file) { return fileService.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) { + public void getById(@PathVariable String bucket, @PathVariable String fileName, HttpServletResponse response) { fileService.getFile(bucket, fileName, response); } - /** - * 获取本地(resources)文件 - * @param fileName 文件名称 - * @param response 本地文件 - */ + /** 获取上传模板 */ @SneakyThrows @GetMapping("/local/{fileName}") public void localFile(@PathVariable String fileName, HttpServletResponse response) { @@ -98,4 +74,15 @@ public class FileController { IoUtil.copy(resource.getInputStream(), response.getOutputStream()); } + @SysLog("删除文件管理") + @DeleteMapping("/remove/{ids:[\\w,]+}") + @PreAuthorize("@pms.hasPermission('sys_file_del')") + @ApiOperation(value = "通过id删除文件管理", notes = "通过id删除文件管理") + public R remove(@PathVariable String[] ids) { + for (int i = 0; i < ids.length; ++i) { + fileService.deleteFile(ids[i]); + } + return R.ok(); + } + } diff --git a/kicc-platform/kicc-platform-biz/kicc-system-biz/src/main/java/com/cloud/kicc/system/service/FileService.java b/kicc-platform/kicc-platform-biz/kicc-system-biz/src/main/java/com/cloud/kicc/system/service/FileService.java index 7c041442..31515d87 100644 --- a/kicc-platform/kicc-platform-biz/kicc-system-biz/src/main/java/com/cloud/kicc/system/service/FileService.java +++ b/kicc-platform/kicc-platform-biz/kicc-system-biz/src/main/java/com/cloud/kicc/system/service/FileService.java @@ -35,7 +35,7 @@ public interface FileService extends IService { /** * 删除文件 * @param id - * @return + * @return Boolean */ Boolean deleteFile(String id); diff --git a/kicc-platform/kicc-platform-biz/kicc-system-biz/src/main/java/com/cloud/kicc/system/service/impl/FileServiceImpl.java b/kicc-platform/kicc-platform-biz/kicc-system-biz/src/main/java/com/cloud/kicc/system/service/impl/FileServiceImpl.java index b2667974..a9c70ce2 100644 --- a/kicc-platform/kicc-platform-biz/kicc-system-biz/src/main/java/com/cloud/kicc/system/service/impl/FileServiceImpl.java +++ b/kicc-platform/kicc-platform-biz/kicc-system-biz/src/main/java/com/cloud/kicc/system/service/impl/FileServiceImpl.java @@ -36,14 +36,8 @@ import java.util.Map; public class FileServiceImpl extends ServiceImpl implements FileService { private final OssProperties ossProperties; - private final OssTemplate ossTemplate; - /** - * 上传文件 - * @param file - * @return - */ @Override public R uploadFile(MultipartFile file) { String fileName = IdUtil.simpleUUID() + StrUtil.DOT + FileUtil.extName(file.getOriginalFilename()); @@ -51,42 +45,27 @@ public class FileServiceImpl extends ServiceImpl implements Fi resultMap.put("bucketName", ossProperties.getBucketName()); resultMap.put("fileName", fileName); resultMap.put("url", String.format("/admin/sys-file/%s/%s", ossProperties.getBucketName(), fileName)); - try { - ossTemplate.putObject(ossProperties.getBucketName(), fileName, file.getContentType(), - file.getInputStream()); + ossTemplate.putObject(ossProperties.getBucketName(), fileName, file.getContentType(), file.getInputStream()); // 文件管理数据记录,收集管理追踪文件 fileLog(file, fileName); - } - catch (Exception e) { + } catch (Exception e) { log.error("上传失败", e); return R.error(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 = ossTemplate.getObject(bucket, fileName)) { response.setContentType("application/octet-stream; charset=UTF-8"); IoUtil.copy(s3Object.getObjectContent(), response.getOutputStream()); - } - catch (Exception e) { + } catch (Exception e) { log.error("文件读取异常: {}", e.getLocalizedMessage()); } } - /** - * 删除文件 - * @param id - * @return - */ @Override @SneakyThrows @Transactional(rollbackFor = Exception.class) @@ -102,13 +81,12 @@ public class FileServiceImpl extends ServiceImpl implements Fi * @param fileName 文件名 */ private void fileLog(MultipartFile file, String fileName) { - File sysFile = new File(); - sysFile.setFileName(fileName); - sysFile.setOriginal(file.getOriginalFilename()); - sysFile.setFileSize(file.getSize()); - sysFile.setType(FileUtil.extName(file.getOriginalFilename())); - sysFile.setBucketName(ossProperties.getBucketName()); - this.save(sysFile); + this.save(new File() + .setFileName(fileName) + .setOriginal(file.getOriginalFilename()) + .setFileSize(file.getSize()) + .setType(FileUtil.extName(file.getOriginalFilename())) + .setBucketName(ossProperties.getBucketName())); } } diff --git a/kicc-platform/kicc-platform-biz/kicc-system-biz/src/main/resources/bootstrap.yml b/kicc-platform/kicc-platform-biz/kicc-system-biz/src/main/resources/bootstrap.yml index f5ca6255..7ee73a86 100644 --- a/kicc-platform/kicc-platform-biz/kicc-system-biz/src/main/resources/bootstrap.yml +++ b/kicc-platform/kicc-platform-biz/kicc-system-biz/src/main/resources/bootstrap.yml @@ -15,13 +15,3 @@ spring: namespace: @profiles.namespace@ shared-configs: - application.${spring.cloud.nacos.config.file-extension} - -# 文件上传相关 支持阿里云、华为云、腾讯、minio -oss: - endpoint: https://oss-cn-hangzhou.aliyuncs.com - access-Key: LTAI4FdQ9kVVXjxTKk3WnYmC - secret-Key: Elc2pUFkSJVjpVU5p1YJSRTNn5dt2s - region: cn-hangzhou - path-style-access: false - bucket-name: kl-kicc-dev - diff --git a/kicc-ui/.env.development b/kicc-ui/.env.development index 0dcfd6a1..5be9756c 100644 --- a/kicc-ui/.env.development +++ b/kicc-ui/.env.development @@ -4,7 +4,7 @@ VITE_PUBLIC_PATH = / # 本地开发代理,可以解决跨域及多地址代理 # 如果接口地址匹配到,则会转发到http://localhost:3000,防止本地出现跨域问题 # 可以有多个,注意多个不能换行,否则代理将会失效 -VITE_PROXY = [["/api","http://localhost:9999"],["/upload","http://localhost:3300/upload"]] +VITE_PROXY = [["/api","http://localhost:9999"],["/upload","http://localhost:9999/system_proxy/system/file/upload"]] # 是否删除console.log VITE_DROP_CONSOLE = false diff --git a/kicc-ui/.env.production b/kicc-ui/.env.production index 22b9e3b3..6cb0fb37 100644 --- a/kicc-ui/.env.production +++ b/kicc-ui/.env.production @@ -4,7 +4,7 @@ VITE_PUBLIC_PATH = / # 本地开发代理,可以解决跨域及多地址代理 # 如果接口地址匹配到,则会转发到http://localhost:3000,防止本地出现跨域问题 # 可以有多个,注意多个不能换行,否则代理将会失效 -VITE_PROXY = [["/prod-api","http://localhost:9999"],["/prod-upload","http://localhost:3300/upload"]] +VITE_PROXY = [["/prod-api","http://localhost:9999"],["/prod-upload","http://localhost:9999/system_proxy/system/file/upload"]] # 是否删除console.log VITE_DROP_CONSOLE = true diff --git a/kicc-ui/src/api/platform/system/controller/file.ts b/kicc-ui/src/api/platform/system/controller/file.ts new file mode 100644 index 00000000..ccab8f5d --- /dev/null +++ b/kicc-ui/src/api/platform/system/controller/file.ts @@ -0,0 +1,26 @@ +/** + * 提供api模板规范代码参考,请尽量保证编写代码风格跟模板规范代码一致 + * Copyright © 2020-2022 entfrm All rights reserved. + * author entfrm开发团队-王翔 + */ +import type { FileParams, FileResult } from '/@/api/platform/system/entity/file'; +import { defHttp } from '/@/utils/http/axios'; + +enum Api { + list = '/system_proxy/system/file/list', + get = '/system_proxy/system/file', + getLocal = '/system_proxy/system/file/local', + del = '/system_proxy/system/file/remove' +} + +/** 查询文件列表 */ +export const listFile = (params?: Partial) => defHttp.get({ url: Api.list, params }, { isReturnResultResponse: true }); + +/** 获取文件 */ +export const getFile = (bucket: string, fileName: string) => defHttp.get({ url: `${Api.get}/${bucket}/${fileName}` }); + +/** 获取本地模板文件 */ +export const getLocalFile = (fileName: string) => defHttp.get({ url: `${Api.getLocal}/${fileName}` }); + +/** 删除文件 */ +export const delFile = (ids: string) => defHttp.delete({ url: `${Api.del}/${ids}` }); diff --git a/kicc-ui/src/api/platform/system/entity/file.ts b/kicc-ui/src/api/platform/system/entity/file.ts new file mode 100644 index 00000000..18bd1ad1 --- /dev/null +++ b/kicc-ui/src/api/platform/system/entity/file.ts @@ -0,0 +1,26 @@ +/** + * @program: kicc-ui + * @description: 文件实体类 + * 类型定义 + * @author: entfrm开发团队-王翔 + * @create: 2022/4/8 + */ +import { R } from '/#/axios'; +import { CommonEntity, Page } from '/@/api/common/data/entity'; + +/** 文件查询参数 */ +export type FileParams = Page & File; + +/** 文件对象 */ +export interface File extends CommonEntity { + id: string; + fileName: string; + bucketName: string; + original: string; + type: string; + fileSize: number; + [key: string]: any; +} + +/** 文件响应对象 */ +export type FileResult = R;