| | |
| | | |
| | | 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> |
| | |
| | | public class AliyunOSSAutoConfigure { |
| | | @Autowired |
| | | private FileServerProperties fileProperties; |
| | | |
| | | |
| | | /** |
| | | * 阿里云文件存储client |
| | |
| | | @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().lastIndexOf("."); |
| | | 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(),fileFolder); |
| | | } else { |
| | | fileFolder = FilePath(fileInfo.getImgType(), folderByDate, hospitalID, depID); |
| | | fileUpLoadOss(fileInfo, fileFolder + fileName, file.getInputStream(),fileFolder); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | |
| | | ossClient.deleteObject(fileProperties.getOss().getBucketName(), fileInfo.getName()); |
| | | return true; |
| | | } |
| | | |
| | | /** |
| | | * 给OSS上上传文件 |
| | | * |
| | | * @param fileInfo 返回对象 |
| | | * @param newFilePath 上传到的文件路径 |
| | | * @param fileStream 要上传的文件流 |
| | | */ |
| | | @Override |
| | | public void fileUpLoadOss(FileInfo fileInfo, String newFilePath, InputStream fileStream,String fileFolder) { |
| | | ossClient.putObject(fileProperties.getOss().getBucketName(), newFilePath, fileStream); |
| | | fileInfo.setUrl(fileProperties.getOss().getDomain() + newFilePath); |
| | | |
| | | } |
| | | @Override |
| | | public OSSObject down(String url) { |
| | | GetObjectRequest request = new GetObjectRequest(fileProperties.getOss().getBucketName(), url); |
| | | return ossClient.getObject(request); |
| | | } |
| | | |
| | | /** |
| | | * 组合sso上的文件路径 |
| | | * |
| | | * @param imgType |
| | | * @return |
| | | */ |
| | | @Override |
| | | 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 if (imgType.toLowerCase().equals("customerhospital")) { |
| | | fileFolder += "Customer/Hospital/" + folderByDate + "/"; |
| | | } else if (imgType.toLowerCase().equals("customerhead")) { |
| | | fileFolder += "Customer/HeadImg/" + folderByDate + "/"; |
| | | }else { |
| | | fileFolder += "OtherImage/" + folderByDate + "/"; |
| | | } |
| | | } else { |
| | | fileFolder += "OtherImage/" + folderByDate + "/"; |
| | | } |
| | | return fileFolder; |
| | | } |
| | | } |
| | | } |