feat(新增): 上传文件,配置minio

This commit is contained in:
bunny 2024-03-23 23:49:14 +08:00
parent 6a482f8e3c
commit 2e3cce4576
15 changed files with 192 additions and 13 deletions

View File

@ -7,7 +7,7 @@ import org.springframework.context.annotation.Configuration;
import java.util.List;
@Configuration
@ConfigurationProperties(prefix = "bunny.spzx")
@ConfigurationProperties(prefix = "bunny")
@Data
public class InterceptorsProperties {
private List<String> noAuthUrls;

View File

@ -0,0 +1,22 @@
package com.atguigu.properties;
import io.minio.MinioClient;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@ConfigurationProperties(prefix = "bunny.minio")
@Data
public class MinioProperties {
private String endpointUrl;
private String accessKey;
private String secretKey;
private String bucketName;
@Bean
public MinioClient minioClient() {
return MinioClient.builder().endpoint(endpointUrl).credentials(accessKey, secretKey).build();
}
}

View File

@ -0,0 +1,27 @@
package com.atguigu.spzx.manger.controller;
import com.atguigu.spzx.manger.service.FileUploadService;
import com.atguigu.spzx.model.vo.result.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
@Tag(name = "上传文件接口")
@RestController
@RequestMapping("/admin/system")
public class FileUploadController {
@Autowired
private FileUploadService fileUploadService;
@Operation(summary = "上传文件", description = "上传文件内容")
@PostMapping("fileUpload")
public Result<String> fileUploadService(@RequestParam(value = "file") MultipartFile file) {
String fileUrl = fileUploadService.uploadFile(file);
return Result.success(fileUrl);
}
}

View File

@ -0,0 +1,13 @@
package com.atguigu.spzx.manger.service;
import org.springframework.web.multipart.MultipartFile;
public interface FileUploadService {
/**
* 上传文件
*
* @param file 上传的文件
* @return file的地址
*/
String uploadFile(MultipartFile file);
}

View File

@ -0,0 +1,57 @@
package com.atguigu.spzx.manger.service.impl;
import cn.hutool.core.date.DateUtil;
import com.atguigu.exception.BunnyException;
import com.atguigu.properties.MinioProperties;
import com.atguigu.spzx.manger.service.FileUploadService;
import io.minio.BucketExistsArgs;
import io.minio.MakeBucketArgs;
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.util.Date;
import java.util.UUID;
@Service
@Slf4j
public class FileUploadServiceImpl implements FileUploadService {
@Autowired
MinioClient minioClient;
@Autowired
private MinioProperties minioProperties;
/**
* 上传文件
*
* @param file 上传的文件
* @return file的地址
*/
@Override
public String uploadFile(MultipartFile file) {
String bucketName = minioProperties.getBucketName();
String dir = DateUtil.format(new Date(), "yyyy-MM-dd");
String uuid = UUID.randomUUID().toString();
String filename = dir + "/" + uuid + "-" + file.getOriginalFilename();
try {
boolean bucketExists = minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
// 判断桶是否存在
if (!bucketExists) {
log.warn("minio桶不存在{}", bucketName);
minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());
}
minioClient.putObject(PutObjectArgs.builder().bucket(bucketName)
.stream(file.getInputStream(), file.getSize(), -1)
.object(filename).build());
return minioProperties.getEndpointUrl() + "/" + minioProperties.getBucketName() + "/" + filename;
} catch (Exception exception) {
throw new BunnyException(exception.getMessage());
}
}
}

View File

@ -11,8 +11,13 @@ bunny:
port: 6379
database: 2
spzx:
noAuthUrls:
- /admin/system/index/login
- /admin/system/index/generateValidateCode
- /v3/**
minio:
endpointUrl: "http://129.211.31.58:9000"
bucket-name: spzx-bucket
accessKey: "bunny"
secretKey: "02120212"
noAuthUrls:
- /admin/system/index/login
- /admin/system/index/generateValidateCode
- /v3/**

View File

@ -37,4 +37,12 @@ mybatis:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
map-underscore-to-camel-case: true
auto-mapping-behavior: full
auto-mapping-behavior: full
bunny:
minio:
endpointUrl: ${bunny.minio.endpointUrl}
accessKey: ${bunny.minio.accessKey}
secretKey: ${bunny.minio.secretKey}
bucket-name: ${bunny.minio.bucket-name}

View File

@ -11,8 +11,13 @@ bunny:
port: 6379
database: 2
spzx:
noAuthUrls:
- /admin/system/index/login
- /admin/system/index/generateValidateCode
- /v3/**
minio:
endpointUrl: "http://129.211.31.58:9000"
bucket-name: spzx-bucket
accessKey: "bunny"
secretKey: "02120212"
noAuthUrls:
- /admin/system/index/login
- /admin/system/index/generateValidateCode
- /v3/**

View File

@ -37,4 +37,12 @@ mybatis:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
map-underscore-to-camel-case: true
auto-mapping-behavior: full
auto-mapping-behavior: full
bunny:
minio:
endpointUrl: ${bunny.minio.endpointUrl}
accessKey: ${bunny.minio.accessKey}
secretKey: ${bunny.minio.secretKey}
bucket-name: ${bunny.minio.bucket-name}

View File

@ -12,6 +12,40 @@
values (#{id}, #{userName}, #{password}, #{name}, #{phone}, #{avatar}, #{description}, #{status});
</insert>
<!-- 修改用户 -->
<update id="updateSysUser">
update sys_user
set
<if test="userName != null and userName != ''">
username = #{userName},
</if>
<if test="password != null and password != ''">
password = #{password},
</if>
<if test="name != null and name != ''">
name = #{name},
</if>
<if test="phone != null and phone != ''">
phone = #{phone},
</if>
<if test="description != null and description != ''">
description = #{description},
</if>
<if test="status != null and status != ''">
status = #{status},
</if>
update_time = now()
where id = #{id}
</update>
<!-- 根据id删除用户 -->
<update id="deleteById">
update sys_user
set is_deleted = 1,
update_time = now()
where id = #{id}
</update>
<!-- 根据username查询用户信息 -->
<select id="selectByUsername" resultType="com.atguigu.spzx.model.entity.system.SysUser">
select