shikeyin
2024-01-11 65da8373531677b1c37a98f53eaa30c892f35e5a
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package com.iplatform.file;
 
import com.iplatform.base.ArgumentsConstants;
import com.iplatform.base.Constants;
import com.iplatform.base.FileOperateSpi;
import com.iplatform.base.config.FileProperties;
import com.iplatform.core.BeanContextAware;
import com.iplatform.file.util.FileStoreUtils;
import com.walker.file.FileInfo;
import com.walker.file.FileStoreType;
import com.walker.infrastructure.arguments.ArgumentsManager;
import com.walker.infrastructure.arguments.ElementNotFoundException;
import com.walker.infrastructure.arguments.Variable;
import com.walker.infrastructure.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
import java.io.InputStream;
import java.util.List;
 
/**
 * 默认的文件操作服务提供者实现。
 * @author 时克英
 * @date 2023-02-15
 */
public class DefaultFileOperateSpi implements FileOperateSpi {
 
    protected final transient Logger logger = LoggerFactory.getLogger(getClass());
 
    private static final int FILE_CONTEXT_LENGTH = Constants.FILE_CONTEXT_PATH.length();
 
    /**
     * 清除给定文件地址的CDN前缀。如:https://qnyun.com/oss/12345678
     * <pre>
     *     去掉前缀后,只剩下"12345678"
     * </pre>
     * @param path 给定的文件资源地址
     * @return
     * @date 2023-05-17
     */
    @Override
    public String clearCdnPrefix(String path){
        if(StringUtils.isEmpty(path)){
            return path;
        }
 
        // 如果URL路径中包含本地识别部分(/file/)则直接替换,2023-06-10
        int fileIndex = path.indexOf(Constants.FILE_CONTEXT_PATH);
        if(fileIndex > 0){
            return path.substring(fileIndex + FILE_CONTEXT_LENGTH, path.length());
        }
 
        // 如果是其他远程存储方式,则根据存储类型分别获取配置前缀
        String prefix = this.getCdnUrl() + StringUtils.FOLDER_SEPARATOR;
        if(!prefix.endsWith(StringUtils.FOLDER_SEPARATOR)){
            prefix += StringUtils.FOLDER_SEPARATOR;
        }
        if(path.contains(prefix)){
            if (path.contains("callback/alipay")) {
                return path;
            }
            return path.replace(prefix, StringUtils.EMPTY_STRING);
        }
        return path;
    }
 
    /**
     * 获取上传文件的CDN地址,根据使用的不同第三方服务从配置中查找。
     * @return
     * @date 2023-05-17
     */
    @Override
    public String getCdnUrl(){
        Variable uploadTypeVar = this.acquireArgumentsManager().getVariable(ArgumentsConstants.CONFIG_UPLOAD_TYPE);
        if(uploadTypeVar == null){
            throw new ElementNotFoundException("参数未找到:" + ArgumentsConstants.CONFIG_UPLOAD_TYPE);
        }
 
        String uploadUrl = null;
        String uploadType = uploadTypeVar.getStringValue();
//        if(uploadType.equals("1") || uploadType.equals(FileStoreType.INDEX_FS)){
//            uploadUrl = ArgumentsConstants.CONFIG_LOCAL_UPLOAD_URL;
//        } else if(uploadType.equals("2") || uploadType.equals(FileStoreType.INDEX_OSS_QI_NIU)){
//            uploadUrl = ArgumentsConstants.CONFIG_QN_UPLOAD_URL;
//        } else if(uploadType.equals("3") || uploadType.equals(FileStoreType.INDEX_OSS_ALI)){
//            uploadUrl = ArgumentsConstants.CONFIG_AL_UPLOAD_URL;
//        } else if(uploadType.equals("4") || uploadType.equals(FileStoreType.INDEX_OSS_TX)){
//            uploadUrl = ArgumentsConstants.CONFIG_TX_UPLOAD_URL;
//        } else {
//            throw new UnsupportedOperationException("不支持的上传类型:" + uploadType);
//        }
        uploadUrl = FileStoreUtils.getFileUrlPrefixKey(uploadType);
        return this.acquireArgumentsManager().getVariable(uploadUrl).getStringValue();
    }
 
    /**
     * 上传文件你到本地磁盘。
     * @param inputStream
     * @param fileName
     * @param groupId
     * @param fileSize
     * @param businessType
     * @param owner
     * @return
     * @throws Exception
     * @date 2023-06-09
     */
    @Override
    public FileInfo uploadFileToLocal(InputStream inputStream
            , String fileName, String groupId, long fileSize, Integer businessType, String owner) throws Exception{
        return this.acquireFileEngineFactory().uploadFileToLocal(inputStream, fileName, groupId, fileSize, businessType, owner);
    }
 
    @Override
    public FileInfo[] uploadFileToLocal(InputStream[] inputStream, String[] fileName, String groupId, long[] fileSize, Integer businessType, String owner) throws Exception {
        return this.acquireFileEngineFactory().uploadFileToLocal(inputStream, fileName, groupId, fileSize, businessType, owner);
    }
 
    @Override
    public FileInfo uploadFileToFtp(InputStream inputStream
            , String fileName, String groupId, long fileSize, Integer businessType, String owner) throws Exception{
        return this.acquireFileEngineFactory().uploadFileToFtp(inputStream, fileName, groupId, fileSize, businessType, owner);
    }
 
    @Override
    public FileInfo[] uploadFileToFtp(InputStream[] inputStream, String[] fileName, String groupId, long[] fileSize, Integer businessType, String owner) throws Exception {
        return this.acquireFileEngineFactory().uploadFileToFtp(inputStream, fileName, groupId, fileSize, businessType, owner);
    }
 
    @Override
    public FileInfo uploadFileToOss(InputStream inputStream
            , String fileName, String groupId, long fileSize, Integer businessType, String owner, FileStoreType ossType) throws Exception{
        return this.acquireFileEngineFactory().uploadFileToOss(inputStream, fileName, groupId, fileSize, businessType, owner, ossType);
    }
 
    @Override
    public FileInfo[] uploadFileToOss(InputStream[] inputStream, String[] fileName, String groupId, long[] fileSize, Integer businessType, String owner, FileStoreType ossType) throws Exception {
        return this.acquireFileEngineFactory().uploadFileToOss(inputStream, fileName, groupId, fileSize, businessType, owner, ossType);
    }
 
    @Deprecated
    @Override
    public FileInfo uploadFileToSystem(InputStream inputStream, String fileName, String groupId, long fileSize) throws Exception {
        return this.acquireFileEngineFactory().uploadFileToSystem(inputStream, fileName, groupId, fileSize);
    }
 
    @Deprecated
    @Override
    public FileInfo uploadFileToFtp(InputStream inputStream, String fileName, String groupId, long fileSize) throws Exception {
        return this.acquireFileEngineFactory().uploadFileToFtp(inputStream, fileName, groupId, fileSize);
    }
 
    @Override
    public FileInfo getFileInfo(long id) {
        return this.acquireFileEngineFactory().getFileInfo(String.valueOf(id));
    }
 
    @Override
    public List<FileInfo> getFileInfoList(List<String> ids) {
        return this.acquireFileEngineFactory().getFileInfoList(ids);
    }
 
    @Override
    public String getFileRootConfig() {
        if(this.localFileRoot == null){
            this.localFileRoot = this.getFileProperties().getFileRoot();
        }
        return this.localFileRoot;
    }
 
    @Override
    public boolean isRemoteAsLocal() {
        if(this.remoteAsLocal == null){
            this.remoteAsLocal = this.getFileProperties().isRemoteAsLocal();
        }
        return this.remoteAsLocal;
    }
 
    private FileProperties getFileProperties(){
        return BeanContextAware.getBeanByType(FileProperties.class);
    }
 
    private FileEngineFactory acquireFileEngineFactory(){
        return BeanContextAware.getBeanByType(FileEngineFactory.class);
    }
 
    /**
     * 引入参数配置管理器。
     * @date 2023-05-17
     */
    private ArgumentsManager acquireArgumentsManager(){
        return BeanContextAware.getBeanByType(ArgumentsManager.class);
    }
 
    private Boolean remoteAsLocal = null;
    private String localFileRoot = null;
 
//    /**
//     * 引入参数配置管理器。
//     * @param argumentsManager
//     * @date 2023-05-17
//     */
//    public void setArgumentsManager(ArgumentsManager argumentsManager) {
//        this.argumentsManager = argumentsManager;
//    }
//
//    private ArgumentsManager argumentsManager;
}