新增obs文件对接

封装获取userId
新增白名单
master
Diyu0904 2025-02-20 14:14:06 +08:00
parent cb5dbc7919
commit 97a9ebdf56
11 changed files with 243 additions and 22 deletions

View File

@ -1,6 +1,7 @@
package com.mcwl.web.controller.resource;
import com.mcwl.common.core.domain.AjaxResult;
import com.mcwl.common.utils.obs.ObsUtils;
import com.mcwl.resource.service.impl.FileServiceImpl;
import com.mcwl.web.controller.common.OssUtil;
import io.swagger.annotations.Api;
@ -9,6 +10,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
@ -28,6 +30,9 @@ public class FileController {
@Autowired
private FileServiceImpl fileService;
@Autowired
private ObsUtils obsUtils;
/***
*
*
@ -57,11 +62,7 @@ public class FileController {
@PostMapping("/fileUpload")
public AjaxResult fileUpload(@RequestParam MultipartFile file) {
String s = OssUtil.uploadMultipartFile(file);
String fileName = file.getOriginalFilename();
Map<String, String> map = new HashMap<>();
map.put("fileName", fileName);
map.put("url", s);
Map<String, String> map = obsUtils.uploadFile(file);
return AjaxResult.success(map);
}
@ -102,10 +103,26 @@ public class FileController {
* @return
*/
@GetMapping("/selectFileName")
@ApiOperation(value = "根据文件名查找是否存在")
@ApiOperation(value = "根据文件名查找是否存在并返回秘钥")
public AjaxResult selectFileName(@RequestParam String name,String type){
return fileService.selectFileName(name,type);
}
@GetMapping("/selectFile")
@ApiOperation(value = "根据文件名查找是否存在")
public AjaxResult selectFile(@RequestParam String name,String type){
return fileService.selectFile(name,type);
}
@GetMapping("/download")
@ApiOperation(value = "读取文件内容")
public AjaxResult download(@RequestParam String name) throws IOException {
obsUtils.download(name);
return AjaxResult.success();
}
}

View File

@ -133,6 +133,14 @@ mall:
notifyUrl: https://7d014da8.r27.cpolar.top/ali/pay/notify
# 沙箱支付宝网关
gatewayUrl: https://openapi-sandbox.dl.alipaydev.com/gateway.do
huawei:
obs:
ak: NWXJ93POA9NCDVV1WQYJ
sk: 3aBuzYdUgSsxdNnycD2mESnVwtcyjAKPxqewE79N
bucketName: mcwl
upload:
endPoint: obs.cn-south-1.myhuaweicloud.com
## 线上环境
#mall:

View File

@ -395,6 +395,12 @@
<version>5.3.25</version> <!-- 请确保与 spring-web 使用相同的主要版本 -->
</dependency>
<!-- obs文件存储 -->
<dependency>
<groupId>com.huaweicloud</groupId>
<artifactId>esdk-obs-java</artifactId>
<version>3.19.7</version>
</dependency>
</dependencies>

View File

@ -1,23 +1,26 @@
package com.mcwl.common.utils;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.util.PatternMatchUtils;
import com.mcwl.common.constant.Constants;
import com.mcwl.common.constant.HttpStatus;
import com.mcwl.common.core.domain.entity.SysRole;
import com.mcwl.common.core.domain.model.LoginUser;
import com.mcwl.common.exception.ServiceException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.util.PatternMatchUtils;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
/**
*
*
*
* @author mcwl
*/
@Slf4j
public class SecurityUtils
{
@ -36,6 +39,22 @@ public class SecurityUtils
}
}
public static Long getUserIdMax(){
Long userId = null;
try {
userId = SecurityUtils.getUserId();
if (userId == null){
userId = 0L;
}
}catch (Exception e){
log.error(e.getMessage());
}
return userId;
}
/**
* ID
**/
@ -116,7 +135,7 @@ public class SecurityUtils
/**
*
*
*
* @param userId ID
* @return
*/
@ -127,7 +146,7 @@ public class SecurityUtils
/**
*
*
*
* @param permission
* @return
*/
@ -138,7 +157,7 @@ public class SecurityUtils
/**
*
*
*
* @param authorities
* @param permission
* @return
@ -151,7 +170,7 @@ public class SecurityUtils
/**
*
*
*
* @param role
* @return
*/
@ -164,7 +183,7 @@ public class SecurityUtils
/**
*
*
*
* @param roles
* @param role
* @return

View File

@ -0,0 +1,33 @@
package com.mcwl.common.utils.obs;
import com.obs.services.ObsClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.annotation.PreDestroy;
/**
* obs
* @author DaiZibo
* @date 2025/2/18
* @apiNote
*/
@Configuration
public class ObsConfig {
@Value("${huawei.obs.ak}")
private String ak;
@Value("${huawei.obs.sk}")
private String sk;
@Value("${huawei.obs.upload.endPoint}")
private String endPoint;
@Bean
public ObsClient getObsClient() {
ObsClient obsClient = new ObsClient(ak, sk, endPoint);
return obsClient;
}
}

View File

@ -0,0 +1,104 @@
package com.mcwl.common.utils.obs;
import com.obs.services.ObsClient;
import com.obs.services.model.ObsObject;
import com.obs.services.model.PutObjectResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.PreDestroy;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
/**
* obs
*
* @author DaiZibo
* @date 2025/2/18
* @apiNote
*/
@Component
public class ObsUtils {
@Autowired
private ObsClient obsClient;
@Value("${huawei.obs.bucketName}")
private String bucketName;
//上传文件multipartFile就是你要的文件
//objectKey就是文件名如果桶中有文件夹的话如往test文件上传test.txt文件那么objectKey就是test/test.txt
public Map<String,String> uploadFile(MultipartFile multipartFile){
HashMap<String, String> map = new HashMap<>();
try {
InputStream inputStream = multipartFile.getInputStream();
String uuid = UUID.randomUUID().toString();
String ossDefaultPath = getOssDefaultPath(multipartFile.getOriginalFilename(),uuid);
PutObjectResult putObjectResult = obsClient.putObject(bucketName, ossDefaultPath, inputStream);
inputStream.close();
map.put("path",putObjectResult.getObjectUrl());
map.put("objectKey",putObjectResult.getObjectKey());
} catch (IOException e) {
throw new RuntimeException(e);
}
return map;
}
public void deleteFile(String objectKey) throws Exception {
obsClient.deleteObject(bucketName, objectKey);
obsClient.close();
}
public String getOssDefaultPath(String name,String uuid) {
LocalDateTime now = LocalDateTime.now();
String url =
now.getYear() + "/" +
now.getMonth() + "/" +
now.getDayOfMonth() + "/"
+ uuid + "_"
+ name;
return url;
}
public void download(String name) throws IOException {
ObsObject obsObject = obsClient.getObject(bucketName, name);
InputStream content = obsObject.getObjectContent();
if (content != null) {
BufferedReader reader = new BufferedReader(new InputStreamReader(content));
while (true) {
String line = reader.readLine();
if (line == null){
break;
}
System.out.println("\n" + line);
}
reader.close();
}
}
@PreDestroy
public void cleanup() {
if (obsClient != null) {
try {
obsClient.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}

View File

@ -130,7 +130,8 @@ public class SecurityConfig
permitAllUrl.getUrls().forEach(url -> requests.antMatchers(url).permitAll());
// 对于登录login 注册register 验证码captchaImage 允许匿名访问
requests.antMatchers("/login", "/register", "/captchaImage","/ali/pay/doPay","/ali/pay/notify",
"/ali/pay/callback","/file/**").permitAll()
"/ali/pay/callback","/file/**","/model/modelSquare","/image/imageList","/WorkFlow/workFlowList",
"/system/dict/data/type/**","").permitAll()
// 静态资源,可匿名访问
.antMatchers(HttpMethod.GET, "/", "/*.html", "/**/*.html", "/**/*.css", "/**/*.js", "/profile/**").permitAll()
.antMatchers("/swagger-ui.html", "/swagger-resources/**", "/webjars/**", "/*/api-docs", "/druid/**").permitAll()

View File

@ -50,7 +50,7 @@ public class Report {
*
*/
@ApiModelProperty(value = "举报类型 字典值")
private Integer reportId;
private Integer reportType;
/**
* (1 2 3)

View File

@ -10,4 +10,6 @@ import com.mcwl.common.core.domain.AjaxResult;
public interface FileService {
AjaxResult selectFileName(String name, String type);
AjaxResult selectFile(String name, String type);
}

View File

@ -54,4 +54,34 @@ public class FileServiceImpl implements FileService {
return AjaxResult.error("type不合法");
}
@Override
public AjaxResult selectFile(String name, String type) {
if (StringUtils.isEmpty(type)){
return AjaxResult.error("请输入type");
}
if (type.equals("model")) {
ModelVersion modelVersion = versionMapper.selectByFileName(name);
if (modelVersion != null) {
return AjaxResult.success(0);
}
return AjaxResult.success(1);
}else if (type.equals("workflow")){
WorkFlowVersion workFlowVersion = workFlowVersionMapper.selectByFileName(name);
if (workFlowVersion != null) {
return AjaxResult.success(0);
}
return AjaxResult.success(1);
}
return AjaxResult.error("type不合法");
}
}

View File

@ -167,8 +167,9 @@ public class ModelImageLikeServiceImpl extends ServiceImpl<ModelImageLikeMapper,
String[] split = responseModelImage.getImagePaths().split(",");
responseModelImage.setPath(split[0]);
//查询模型是否点赞
ModelImageLike likeImage = baseMapper.getLikeImage(SecurityUtils.getUserId(), responseModelImage.getId());
ModelImageLike likeImage = baseMapper.getLikeImage(SecurityUtils.getUserIdMax(), responseModelImage.getId());
if (likeImage == null){
responseModelImage.setIsLike(0);
}else {