init
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
package com.yupi.springbootinit.config;
|
||||
|
||||
import io.minio.MinioClient;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
public class MinioConfig {
|
||||
|
||||
@Value("${minio.endpoint}")
|
||||
private String endpoint;
|
||||
|
||||
@Value("${minio.access-key}")
|
||||
private String accessKey;
|
||||
|
||||
@Value("${minio.secret-key}")
|
||||
private String secretKey;
|
||||
|
||||
@Bean
|
||||
public MinioClient minioClient() {
|
||||
return MinioClient.builder()
|
||||
.endpoint(endpoint)
|
||||
.credentials(accessKey, secretKey)
|
||||
.build();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,6 +12,7 @@ import com.yupi.springbootinit.model.dto.dish.DishRandomRequest;
|
||||
import com.yupi.springbootinit.model.dto.dish.DishUpdataRequest;
|
||||
import com.yupi.springbootinit.model.entity.Dish;
|
||||
import com.yupi.springbootinit.service.DishService;
|
||||
import com.yupi.springbootinit.service.MinioService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
@@ -25,6 +26,9 @@ import java.util.List;
|
||||
@RequestMapping("/dish")
|
||||
@Slf4j
|
||||
public class DishController {
|
||||
@Resource
|
||||
private MinioService minioService;
|
||||
|
||||
@Resource
|
||||
private DishService dishService;
|
||||
|
||||
@@ -32,13 +36,18 @@ public class DishController {
|
||||
private CosManager cosManager;
|
||||
|
||||
@PostMapping("/add")
|
||||
public BaseResponse<Long> add(@RequestBody DishAddRequest dishAddRequest, HttpServletRequest request) {
|
||||
public BaseResponse<Long> add(@ModelAttribute DishAddRequest dishAddRequest, HttpServletRequest request) {
|
||||
if (dishAddRequest == null) {
|
||||
throw new BusinessException(ErrorCode.PARAMS_ERROR);
|
||||
}
|
||||
validateDishType(dishAddRequest.getDishType());
|
||||
Dish dish = new Dish();
|
||||
// 上传图片并获取URL
|
||||
String imageUrl = minioService.uploadFile(dishAddRequest.getDishImage());
|
||||
// 将DishAddRequest中的属性复制到dish对象中
|
||||
BeanUtils.copyProperties(dishAddRequest, dish);
|
||||
// 设置图片URL
|
||||
dish.setDishImage(imageUrl);
|
||||
// 设置创建时间和更新时间为当前时间
|
||||
dish.setCreateTime(new Date());
|
||||
dish.setUpdateTime(new Date());
|
||||
@@ -66,7 +75,7 @@ public class DishController {
|
||||
|
||||
//更改菜品数据方法
|
||||
@PostMapping("/update")
|
||||
public BaseResponse<Boolean> update(@RequestBody DishUpdataRequest dishUpdataRequest, HttpServletRequest request) {
|
||||
public BaseResponse<Boolean> update(@ModelAttribute DishUpdataRequest dishUpdataRequest, HttpServletRequest request) {
|
||||
if (dishUpdataRequest == null || dishUpdataRequest.getId() <= 0) {
|
||||
throw new BusinessException(ErrorCode.PARAMS_ERROR);
|
||||
}
|
||||
@@ -88,8 +97,10 @@ public class DishController {
|
||||
if (dishUpdataRequest.getDishIngredients() != null && !dishUpdataRequest.getDishIngredients().trim().isEmpty()) {
|
||||
oldDish.setDishIngredients(dishUpdataRequest.getDishIngredients());
|
||||
}
|
||||
if (dishUpdataRequest.getDishImage() != null && !dishUpdataRequest.getDishImage().trim().isEmpty()) {
|
||||
oldDish.setDishImage(dishUpdataRequest.getDishImage());
|
||||
// 上传图片并获取URL
|
||||
if (dishUpdataRequest.getDishImage() != null && !dishUpdataRequest.getDishImage().isEmpty()) {
|
||||
String imageUrl = minioService.uploadFile(dishUpdataRequest.getDishImage());
|
||||
oldDish.setDishImage(imageUrl);
|
||||
}
|
||||
oldDish.setUpdateTime(new Date());
|
||||
Boolean result = dishService.updateById(oldDish);
|
||||
@@ -108,6 +119,8 @@ public class DishController {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
// //按已经定好的权重比例随机挑选菜品
|
||||
// @GetMapping("/random")
|
||||
// public BaseResponse<List> randomDish(Integer num){
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.yupi.springbootinit.model.dto.dish;
|
||||
|
||||
import lombok.Data;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import javax.validation.constraints.NotNull;
|
||||
import java.io.Serializable;
|
||||
@@ -31,5 +32,5 @@ public class DishAddRequest implements Serializable {
|
||||
/**
|
||||
* 菜品图片
|
||||
*/
|
||||
private String dishImage;
|
||||
private MultipartFile dishImage;
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.yupi.springbootinit.model.dto.dish;
|
||||
|
||||
import lombok.Data;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import javax.validation.constraints.NotNull;
|
||||
import java.io.Serializable;
|
||||
@@ -31,5 +32,5 @@ public class DishUpdataRequest implements Serializable {
|
||||
/**
|
||||
* 菜品图片
|
||||
*/
|
||||
private String dishImage;
|
||||
private MultipartFile dishImage;
|
||||
}
|
||||
@@ -2,6 +2,7 @@ package com.yupi.springbootinit.model.entity;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.*;
|
||||
import lombok.Data;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.yupi.springbootinit.service;
|
||||
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
public interface MinioService {
|
||||
public String uploadFile(MultipartFile file);
|
||||
public String getFileUrl(String fileName);
|
||||
public void deleteFile(String fileName);
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
package com.yupi.springbootinit.service.impl;
|
||||
|
||||
import com.yupi.springbootinit.config.MinioConfig;
|
||||
import com.yupi.springbootinit.service.MinioService;
|
||||
import io.minio.*;
|
||||
import io.minio.http.Method;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
@Service
|
||||
public class MinioServiceimpl implements MinioService {
|
||||
|
||||
private final MinioClient minioClient;
|
||||
|
||||
@Value("${minio.bucket-name}")
|
||||
private String bucketName;
|
||||
|
||||
@Value("${minio.endpoint}") // MinIO 服务器地址(比如 http://127.0.0.1:9000)
|
||||
private String minioEndpoint;
|
||||
|
||||
public MinioServiceimpl(MinioClient minioClient) {
|
||||
this.minioClient = minioClient;
|
||||
}
|
||||
@Override
|
||||
public String uploadFile(MultipartFile file) {
|
||||
try {
|
||||
String fileName = UUID.randomUUID() + "-" + file.getOriginalFilename();
|
||||
InputStream inputStream = file.getInputStream();
|
||||
|
||||
// 确保存储桶存在
|
||||
boolean found = minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
|
||||
if (!found) {
|
||||
minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());
|
||||
}
|
||||
|
||||
// 上传文件
|
||||
minioClient.putObject(PutObjectArgs.builder()
|
||||
.bucket(bucketName)
|
||||
.object(fileName)
|
||||
.stream(inputStream, file.getSize(), -1)
|
||||
.contentType(file.getContentType())
|
||||
.build());
|
||||
|
||||
String url = minioEndpoint+ "/" + bucketName + "/" + fileName;
|
||||
return url;
|
||||
// );
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("上传文件失败: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getFileUrl(String fileName) {
|
||||
try {
|
||||
return minioEndpoint+ "/" + bucketName + "/" + fileName;
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("获取文件 URL 失败: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteFile(String fileName) {
|
||||
try {
|
||||
minioClient.removeObject(RemoveObjectArgs.builder()
|
||||
.bucket(bucketName)
|
||||
.object(fileName)
|
||||
.build());
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("删除文件失败: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -92,6 +92,6 @@ cos:
|
||||
bucket: xxx
|
||||
minio:
|
||||
endpoint: http://192.168.100.113:9000
|
||||
accessKey: YOUR-ACCESSKEY
|
||||
secretKey: YOUR-SECRETKEY
|
||||
bucketName: your-bucket-name
|
||||
accessKey: an3QBebaTB4N0CbIWXCm
|
||||
secretKey: yEEQKNw1WzBjBEgdCrVSe3AFT7ktzkrxzpHH3MZl
|
||||
bucketName: order
|
||||
Reference in New Issue
Block a user