forked from kidgrow-microservices-platform

zhaoxiaohao
2020-06-16 3053f3c6a8ea26d204e83e9f17a418d6928da5bc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package com.kidgrow.filecenter.config;
 
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.common.auth.DefaultCredentialProvider;
import com.kidgrow.common.constant.SecurityConstants;
import com.kidgrow.common.utils.DateUtil;
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.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.util.Date;
import java.util.UUID;
 
/**
 * 石家庄喜高科技有限责任公司 版权所有 © Copyright 2020<br>
 *
 * @Description: 阿里云配置<br>
 * @Project: <br>
 * @CreateDate: Created in 2020/2/18 11:24 <br>
 * @Author: <a href="4345453@kidgrow.com">liuke</a>
 */
@Configuration
@ConditionalOnProperty(name = "kidgrow.file-server.type", havingValue = "aliyun")
public class AliyunOSSAutoConfigure {
    @Autowired
    private FileServerProperties fileProperties;
 
 
 
    /**
     * 阿里云文件存储client
     * 只有配置了aliyun.oss.access-key才可以使用
     */
    @Bean
    public OSSClient ossClient() {
        OSSClient ossClient = new OSSClient(fileProperties.getOss().getEndpoint()
                , new DefaultCredentialProvider(fileProperties.getOss().getAccessKey(), fileProperties.getOss().getAccessKeySecret())
                , null);
        return ossClient;
    }
 
    @Service
    public class AliyunOssServiceImpl extends AbstractIFileService {
        @Autowired
        private OSSClient ossClient;
 
        @Autowired
        private HttpServletRequest httpServletRequest;
 
        @Override
        protected String fileType() {
            return fileProperties.getType();
        }
 
        @Override
        protected void uploadFile(MultipartFile file, FileInfo fileInfo) throws Exception {
            String clientID= httpServletRequest.getHeader(SecurityConstants.CLIENT_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")){//医院端平台,
 
                if ((fileInfo.getImgType() != null)) {
                    // 光片需要按照根据医院ID,科室ID,日期进行OSS存储
                    if (fileInfo.getImgType().toLowerCase().equals("xray")) {
                        String hospitalID = httpServletRequest.getHeader(SecurityConstants.USER_HOSPITAL_ID_HEADER);
                        String depID = httpServletRequest.getHeader(SecurityConstants.USER_DEP_ID_HEADER);
                        fileFolder = "Xray/" + hospitalID + "/" + depID + "/"+ folderByDate + "/";
                    }
                    // 医院Logo只保存在一个目录中
                    else if (fileInfo.getImgType().toLowerCase().equals("logo")) {
                        fileFolder = "HospitalLogo/";
                    }else if (fileInfo.getImgType().toLowerCase().equals("doctor")) {
                        fileFolder = "DoctorImage/";
                    }
                    else {
                        fileFolder = "OtherImage/"+ folderByDate + "/";
                    }
 
                } else {
                    fileFolder = "OtherImage/"+ folderByDate + "/";
                }
 
                ossClient.putObject(fileProperties.getOss().getBucketName(), fileFolder +  fileName, file.getInputStream());
                fileInfo.setUrl(fileProperties.getOss().getDomain() + fileFolder  + fileName);
 
            }
            else {
                ossClient.putObject(fileProperties.getOss().getBucketName(), fileProperties.getOss().getFolder() + folderByDate + "/" + fileName, file.getInputStream());
                fileInfo.setUrl(fileProperties.getOss().getDomain() + fileProperties.getOss().getFolder() + folderByDate + "/" + fileName);
            }
        }
 
        @Override
        protected boolean deleteFile(FileInfo fileInfo) {
 
            ossClient.deleteObject(fileProperties.getOss().getBucketName(), fileInfo.getName());
            return true;
        }
    }
}