From 72654046a87f4d649e495a01dbb432bbc62f0d6f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=86=B7=E5=86=B7?= Date: Mon, 31 Dec 2018 02:42:48 +0800 Subject: [PATCH] =?UTF-8?q?:sparkles:=20minio=20=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E3=80=81=E8=8E=B7=E5=8F=96=20=E7=BB=93?= =?UTF-8?q?=E5=90=88=E9=80=9A=E8=BF=87=E5=A4=B4=E5=83=8F=E4=B8=8A=E4=BC=A0?= =?UTF-8?q?=E5=8F=8A=E5=85=B6=E5=89=8D=E7=AB=AFimg=20=E6=9D=83=E9=99=90?= =?UTF-8?q?=E6=8E=A7=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- doc/md/modules.md | 1 + .../common/core/constant/CommonConstants.java | 9 +- .../resources/config/pigx-upms-biz-dev.yml | 7 ++ pigx-upms/pigx-upms-biz/pom.xml | 6 ++ .../pigx/admin/controller/FileController.java | 96 +++++++++++++++++++ 5 files changed, 117 insertions(+), 2 deletions(-) create mode 100644 pigx-upms/pigx-upms-biz/src/main/java/com/pig4cloud/pigx/admin/controller/FileController.java diff --git a/doc/md/modules.md b/doc/md/modules.md index 19fc31d39..f54e5dd25 100644 --- a/doc/md/modules.md +++ b/doc/md/modules.md @@ -8,6 +8,7 @@ pigx ├ ├── pigx-common-data -- 数据相关 ├ ├── pigx-common-job -- 定时任务 ├ ├── pigx-common-log -- 日志服务 +├ ├── pigx-common-minio -- 文件系统 ├ └── pigx-common-security -- 安全工具类 ├ └── pigx-common-swagger -- Swagger Api文档生成 ├ └── pigx-common-transaction -- 分布式事务工具包 diff --git a/pigx-common/pigx-common-core/src/main/java/com/pig4cloud/pigx/common/core/constant/CommonConstants.java b/pigx-common/pigx-common-core/src/main/java/com/pig4cloud/pigx/common/core/constant/CommonConstants.java index a13489b24..b08e7da6d 100644 --- a/pigx-common/pigx-common-core/src/main/java/com/pig4cloud/pigx/common/core/constant/CommonConstants.java +++ b/pigx-common/pigx-common-core/src/main/java/com/pig4cloud/pigx/common/core/constant/CommonConstants.java @@ -80,9 +80,14 @@ public interface CommonConstants { /** * 成功标记 */ - Integer SUCCESS=0; + Integer SUCCESS = 0; /** * 失败标记 */ - Integer FAIL=1; + Integer FAIL = 1; + + /** + * 默认存储bucket + */ + String BUCKET_NAME = "lengleng"; } diff --git a/pigx-config/src/main/resources/config/pigx-upms-biz-dev.yml b/pigx-config/src/main/resources/config/pigx-upms-biz-dev.yml index 27883ad06..c944f8ee2 100644 --- a/pigx-config/src/main/resources/config/pigx-upms-biz-dev.yml +++ b/pigx-config/src/main/resources/config/pigx-upms-biz-dev.yml @@ -33,6 +33,13 @@ swagger: description: 'read all' - scope: 'write' description: 'access all' + +# 文件系统 +minio: + url: http://139.224.200.249:9090 + access-key: lengleng + secret-key: lengleng + # Logger Config logging: level: diff --git a/pigx-upms/pigx-upms-biz/pom.xml b/pigx-upms/pigx-upms-biz/pom.xml index 74881c42b..33cd42eb0 100644 --- a/pigx-upms/pigx-upms-biz/pom.xml +++ b/pigx-upms/pigx-upms-biz/pom.xml @@ -62,6 +62,12 @@ pigx-common-swagger 2.0.0 + + + com.pig4cloud + pigx-common-minio + 2.0.0 + org.springframework.cloud 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 new file mode 100644 index 000000000..d2f7f28e2 --- /dev/null +++ b/pigx-upms/pigx-upms-biz/src/main/java/com/pig4cloud/pigx/admin/controller/FileController.java @@ -0,0 +1,96 @@ +/* + * 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.IoUtil; +import cn.hutool.core.util.RandomUtil; +import cn.hutool.core.util.StrUtil; +import com.pig4cloud.common.minio.service.MinioTemplate; +import com.pig4cloud.pigx.common.core.constant.CommonConstants; +import com.pig4cloud.pigx.common.core.util.R; +import lombok.AllArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.web.bind.annotation.*; +import org.springframework.web.multipart.MultipartFile; + +import javax.servlet.http.HttpServletResponse; +import java.io.InputStream; +import java.util.HashMap; +import java.util.Map; + +/** + * @author lengleng + * @date 2018-12-30 + *

+ * 文件管理 + */ +@Slf4j +@RestController +@AllArgsConstructor +@RequestMapping("/file") +public class FileController { + private final MinioTemplate minioTemplate; + + /** + * 上传文件 + * + * @param file 资源 + * @return R(bucketName, filename) + */ + @PostMapping("/upload") + public R upload(@RequestParam("file") MultipartFile file) { + String originalFilename = file.getOriginalFilename(); + String newName = RandomUtil.randomString(16) + originalFilename; + + Map resultMap = new HashMap<>(4); + resultMap.put("bucketName", CommonConstants.BUCKET_NAME); + resultMap.put("fileName", newName); + + try { + minioTemplate.putObject(CommonConstants.BUCKET_NAME, newName, file.getInputStream()); + } catch (Exception e) { + log.error("上传失败", e); + return R.builder().code(CommonConstants.FAIL) + .msg(e.getLocalizedMessage()).build(); + } + return R.builder().data(resultMap).build(); + } + + /** + * 获取文件 + * + * @param fileName 文件空间/名称 + * @param response + * @return + */ + @GetMapping("/{fileName}") + public R file(@PathVariable String fileName, HttpServletResponse response) { + String[] nameArray = StrUtil.split(fileName, StrUtil.DASHED); + + try (InputStream inputStream = minioTemplate.getObject(nameArray[0], nameArray[1])) { + response.setContentType("application/octet-stream; charset=UTF-8"); + IoUtil.copy(inputStream, response.getOutputStream()); + } catch (Exception e) { + log.error("文件读取异常", e); + return R.builder().code(CommonConstants.FAIL) + .msg(e.getLocalizedMessage()).build(); + } + + return R.builder().build(); + } +}