From 1648307cdce7e7e3781ac5a1e629a7e9c92c9cff Mon Sep 17 00:00:00 2001 From: luliqiang <kidgrow> Date: Sat, 15 Aug 2020 19:52:11 +0800 Subject: [PATCH] 修改OSS路径中医院ID和科室ID不能获取的Bug --- kidgrow-business/kidgrow-filecenter/kidgrow-filecenter-server/src/main/java/com/kidgrow/filecenter/config/AliyunOSSAutoConfigure.java | 97 +++++++++++++++++++++++++++++++++++++++++++++++- 1 files changed, 94 insertions(+), 3 deletions(-) diff --git a/kidgrow-business/kidgrow-filecenter/kidgrow-filecenter-server/src/main/java/com/kidgrow/filecenter/config/AliyunOSSAutoConfigure.java b/kidgrow-business/kidgrow-filecenter/kidgrow-filecenter-server/src/main/java/com/kidgrow/filecenter/config/AliyunOSSAutoConfigure.java index 7d188d4..a443a97 100644 --- a/kidgrow-business/kidgrow-filecenter/kidgrow-filecenter-server/src/main/java/com/kidgrow/filecenter/config/AliyunOSSAutoConfigure.java +++ b/kidgrow-business/kidgrow-filecenter/kidgrow-filecenter-server/src/main/java/com/kidgrow/filecenter/config/AliyunOSSAutoConfigure.java @@ -2,18 +2,26 @@ import com.aliyun.oss.OSSClient; import com.aliyun.oss.common.auth.DefaultCredentialProvider; +import com.aliyun.oss.model.GetObjectRequest; +import com.aliyun.oss.model.OSSObject; +import com.kidgrow.common.constant.SecurityConstants; import com.kidgrow.common.utils.DateUtil; +import com.kidgrow.common.utils.StringUtils; import com.kidgrow.filecenter.model.FileInfo; import com.kidgrow.filecenter.properties.FileServerProperties; import com.kidgrow.filecenter.service.impl.AbstractIFileService; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; +import javax.servlet.http.HttpServletRequest; +import java.io.InputStream; import java.util.Date; +import java.util.UUID; /** * 石家庄喜高科技有限责任公司 版权所有 © Copyright 2020<br> @@ -28,6 +36,7 @@ public class AliyunOSSAutoConfigure { @Autowired private FileServerProperties fileProperties; + /** * 阿里云文件存储client @@ -46,16 +55,35 @@ @Autowired private OSSClient ossClient; + @Autowired + private HttpServletRequest httpServletRequest; + @Override protected String fileType() { return fileProperties.getType(); } + @Value("${spring.profiles.active}") + private String OssPathEn; + @Override protected void uploadFile(MultipartFile file, FileInfo fileInfo) throws Exception { - String folderByDate =DateUtil.formatDate(new Date()); - ossClient.putObject(fileProperties.getOss().getBucketName(), fileProperties.getOss().getFolder()+ folderByDate+"/"+fileInfo.getName(), file.getInputStream()); - fileInfo.setUrl(fileProperties.getOss().getDomain() + fileProperties.getOss().getFolder()+ folderByDate+"/" + fileInfo.getName()); + String clientID = httpServletRequest.getHeader(SecurityConstants.CLIENT_HEADER); + String hospitalID = httpServletRequest.getHeader(SecurityConstants.USER_HOSPITAL_ID_HEADER); + String depID = httpServletRequest.getHeader(SecurityConstants.USER_DEP_ID_HEADER); + String folderByDate = DateUtil.formatDate(new Date()); + int begin = file.getOriginalFilename().indexOf("."); + int last = file.getOriginalFilename().length(); + String fileType = file.getOriginalFilename().substring(begin, last); + String fileName = UUID.randomUUID().toString().replaceAll("-", "") + fileType; + String fileFolder = ""; + if (clientID.equals("hospital")) {//医院端平台, + fileFolder = FilePath(fileInfo.getImgType(), folderByDate, hospitalID, depID); + fileUpLoadOss(fileInfo, fileFolder + fileName, file.getInputStream()); + } else { + fileFolder = FilePath(fileInfo.getImgType(), folderByDate, hospitalID, depID); + fileUpLoadOss(fileInfo, fileFolder + fileName, file.getInputStream()); + } } @Override @@ -64,5 +92,68 @@ ossClient.deleteObject(fileProperties.getOss().getBucketName(), fileInfo.getName()); return true; } + + /** + * 给OSS上上传文件 + * + * @param fileInfo 返回对象 + * @param newFilePath 上传到的文件路径 + * @param fileStream 要上传的文件流 + */ + public void fileUpLoadOss(FileInfo fileInfo, String newFilePath, InputStream fileStream) { + ossClient.putObject(fileProperties.getOss().getBucketName(), newFilePath, fileStream); + fileInfo.setUrl(fileProperties.getOss().getDomain() + newFilePath); + + } + + public OSSObject down(String url) { + GetObjectRequest request = new GetObjectRequest(fileProperties.getOss().getBucketName(), url); + return ossClient.getObject(request); + } + + /** + * 组合sso上的文件路径 + * + * @param imgType + * @return + */ + public String FilePath(String imgType, String folderByDate, String hospitalId, String departmentId) { + //返回的文件路径 + String fileFolder = OssPathEn+"/"; + + + if (StringUtils.isNotBlank(imgType)) { + // 光片需要按照根据医院ID,科室ID,日期进行OSS存储 + if (imgType.toLowerCase().equals("xray")) { + + if (StringUtils.isNotBlank(hospitalId) && + StringUtils.isNotBlank(departmentId)) { + fileFolder += "Xray/" + hospitalId + "/" + departmentId + "/" + folderByDate + "/"; + } else { + fileFolder += "Xray/" + folderByDate + "/"; + } + + } else if (imgType.toLowerCase().equals("app")) { + //移动端APP上传 + if (StringUtils.isNotBlank(hospitalId) && + StringUtils.isNotBlank(departmentId)) { + fileFolder += "AppXray/" + hospitalId + "/" + departmentId + "/" + folderByDate + "/"; + } else { + fileFolder += "AppXray/" + folderByDate + "/"; + } + } + // 医院Logo只保存在一个目录中 + else if (imgType.toLowerCase().equals("logo")) { + fileFolder += "HospitalLogo/"; + } else if (imgType.toLowerCase().equals("doctor")) { + fileFolder += "DoctorImage/"; + } else { + fileFolder += "OtherImage/" + folderByDate + "/"; + } + } else { + fileFolder += "OtherImage/" + folderByDate + "/"; + } + return fileFolder; + } } } -- Gitblit v1.8.0