forked from kidgrow-microservices-platform

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
package com.kidgrow.filecenter.config;
 
import cn.hutool.core.util.StrUtil;
import com.aliyun.oss.model.OSSObject;
import com.kidgrow.filecenter.model.FileInfo;
import com.kidgrow.filecenter.properties.FileServerProperties;
import com.kidgrow.filecenter.service.impl.AbstractIFileService;
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
 
import java.io.InputStream;
 
/**
 * 石家庄喜高科技有限责任公司 版权所有 © Copyright 2020<br>
 *
 * @Description: FastDfs配置<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 = "fastdfs")
public class FastdfsAutoConfigure {
    @Autowired
    private FileServerProperties fileProperties;
 
    @Service
    public class FastdfsServiceImpl extends AbstractIFileService {
        @Autowired
        private FastFileStorageClient storageClient;
 
        @Override
        protected String fileType() {
            return fileProperties.getType();
        }
 
        @Override
        protected void uploadFile(MultipartFile file, FileInfo fileInfo) throws Exception {
            StorePath storePath = storageClient.uploadFile(file.getInputStream(), file.getSize(), FilenameUtils.getExtension(file.getOriginalFilename()), null);
            fileInfo.setUrl("http://" + fileProperties.getFdfs().getWebUrl() + "/" + storePath.getFullPath());
            fileInfo.setPath(storePath.getFullPath());
        }
 
        @Override
        protected void fileUpLoadOss(FileInfo fileInfo, String newFilePath, InputStream fileStream,String fileFolder) {
 
        }
 
        @Override
        protected String FilePath(String imgType, String folderByDate,String hospitalId,String departmentId) {
            return null;
        }
 
        @Override
        protected OSSObject down(String url) {
            return null;
        }
 
        @Override
        protected boolean deleteFile(FileInfo fileInfo) {
            if (fileInfo != null && StrUtil.isNotEmpty(fileInfo.getPath())) {
                StorePath storePath = StorePath.parseFromUrl(fileInfo.getPath());
                storageClient.deleteFile(storePath.getGroup(), storePath.getPath());
            }
            return true;
        }
    }
}