P15GEN2\59518
2025-10-18 56638c01bb2cc61a92f5e03c9a1001be5b5d3699
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
package foundation.io;
 
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
 
import org.apache.commons.io.FileUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
 
import foundation.io.object.FileItemAgent;
import foundation.server.config.Configer;
import foundation.token.IOnlineUser;
import foundation.token.IUser;
import foundation.util.Util;
 
public class FileRepository {
    
    private static String Server_Name = Configer.getParam("ServerUrl");
    protected static Logger logger;
    public static String Default_Template = "empty.xlsx";
    
    static {
        logger = LogManager.getLogger(FileRepository.class);
    }
    
    //获取目录--模板存放目录
    public static File getWebTemplatePath() {
        String path = Configer.getParam("PathWebTemp") + "template/";
        
        File file = new File(path);
        
        if (!file.exists()) {
            file.mkdirs();
        }
        
        return file;
    }
    
    //获取目录--模板存放目录
    public static File getTemplatePath(String rootType) {
        if (Util.isEmpty(rootType)) {
            rootType = "PathRepository";
        }
        
        String path = Configer.getParam(rootType) + "template/";
        
        File file = new File(path);
        
        if (!file.exists()) {
            file.mkdirs();
        }
        
        return file;
    }
    
    //获取目录--接口目录
    public static File getICallTempPath() {
        String path = Configer.getParam("PathRepository") + "temp/icall/" + Util.newDateTimeStr("yyyy/MM") + "/";
        
        File file = new File(path);
        
        if (!file.exists()) {
            file.mkdirs();
        }
        
        return file;
    }
    
    //获取目录--用户临时目录
    public static File getUserTempPath(IUser user) {
        String path = Configer.getParam("PathRepository") + "temp/" + user.getCode() + "/" + Util.newDateTimeStr("yyyy/MM") + "/";
        
        File file = new File(path);
        
        if (!file.exists()) {
            file.mkdirs();
        }
        
        return file;
    }
    
    public static String urlToWebPath(String fileURL) {
        String path = Configer.getParam("PathWebTemp") + fileURL;
        return path;
    }
    
    public static String urlToPath(String fileURL) {
        String path = Configer.getParam("PathRepository") + fileURL;
        return path;
    }
 
    public static String pathToURL(File file) {
        String root = new File(Configer.getParam("PathRepository")).toString();
        String path = file.toString();
        
        path = path.substring(root.length() + 1);
        return path;
    }
    
    public static String pathToURL(String path) {
        String pathWebTemp = Configer.getParam("PathWebTemp").replace("/", "\\");
        
        if (!Util.isEmpty(path) && path.startsWith(pathWebTemp)) {
            return path.replace(pathWebTemp, Server_Name).replace("\\", "/");
        }
        
        return path;
    }
    
    public static String pathToURL(String indexId, String dataName) {
        String url = Server_Name + "root/io/downloadOneFile?category=org&action=saveas&file_index_id=" + indexId + "&dataname=" + dataName;
        return url;
    }
    
    //获取目录--上载文件目录
    public static File getUploadPath(IOnlineUser user, String filePath) throws IOException {
        String userName = user.getName();
        
        if(Util.isEmpty(filePath)) {
            filePath = "upload/" + userName +  Util.newDateTimeStr("/yyyy/MM");
        }
        String path = Configer.getParam("PathRepository") + filePath;
        
        File file = new File(path);
        
        if (!file.exists()) {
            FileUtils.forceMkdir(file);
        }
        
        return file;
    }
    
    //获取文件--模板文件
    public static File getTemplateFile(String templateName) {
        File template =  new File(getTemplatePath(null), templateName);
        return template;
    }
    
    //创建文件--上载临时文件
    public static File createUploadRepositoryFile(IOnlineUser user, String fileName, String filePath) throws Exception {
        if (Util.isEmpty(fileName)) {
            throw new Exception("can not create upload repository file: file name is empty");
        }
        
        //1. path
        File path = getUploadPath(user, filePath);
        
        //2. file name
        fileName = addTimeStamp(fileName);
        File file = new File(path, fileName);
 
        return file;    
    }
    
    //创建文件--数据导入临时文件
    public static File createImportTempFile(IUser user, FileItemAgent fileForm) throws Exception {
        //1. get user temporary path
        File tempPath = getUserTempPath(user);
        
        //2. get file
        String fileName = fileForm.getName();
        fileName = addTimeStamp(fileName);
        File tempFile = new File(tempPath, fileName);  
        
        //3. save to file
        OutputStream outputStream = new FileOutputStream(tempFile);  
        InputStream inputStream = fileForm.getInputStream();
        try {
            byte buf[] = new byte[1024];
            
            int length = 0;    
            while((length = inputStream.read(buf)) > 0){    
                outputStream.write(buf, 0, length);    
            }  
        }
        finally {
            try {
                outputStream.flush();
                outputStream.close();
            }
            catch (Exception e) {
            }
            
            try {
                inputStream.close();
            }
            catch (Exception e) {
            }           
        }
        
        return tempFile;
    }
    
    //创建文件--下载临时文件
    public static File createExportTempFile(IUser user, String templateName, String fileName) throws Exception {
        //1. get template
        if (Util.isEmpty(templateName)) {
            templateName = Default_Template;
        }
        
        File template =  new File(getTemplatePath(null), clearVariants(templateName));
        
        if (!template.exists()) {
            throw new Exception("template file not exists: " + template);
        }
        
        //2. get temporary path
        File tempPath = getUserTempPath(user);
        
        //3. get temporary file
        fileName = fillVariants(fileName);
        fileName = addSuffix(fileName, templateName);
        
        File tempFile = new File(tempPath, fileName);
        
        if (tempFile.exists()) {
            tempFile.delete();
        }
        
        //4. copy template to 
        FileUtils.copyFile(template, tempFile);
        return tempFile;
    }
    
    //创建文件--模板注入临时文件
    public static File createInjectTempFile(IUser user, String templateName) throws Exception {
        //1. get user temporary path
        File tempPath = getUserTempPath(user);
        
        //2. get template
        templateName = addTimeStamp(templateName);
        File tempFile =  new File(tempPath, templateName);
        
        if (tempFile.exists()) {
            tempFile.delete();
        }
        
        return tempFile;
    }
    
    private static String clearVariants(String fileName) {
        //KA医院管理-@{YYYYMMdd}
        
        if (fileName == null) {
            return null;
        }
        
        fileName = fileName.trim();
        
        int begin = fileName.indexOf("@{");
        if (begin <= 0) {
            return fileName;
        }
        
        int end = fileName.indexOf("}");
        if (end <= 0) {
            end = fileName.length();
        }
        
        String result = fileName.substring(0, begin) + fileName.substring(end + 1);
        return result;
    }
    
    public static String addTimeStamp(String fileName) {
        //KA医院管理-20230102-101234180
        int pos = fileName.lastIndexOf(".");
        
        if (pos < 0) {
            return fileName;
        }
        
        String timeStamp = Util.newDateTimeStr("-yyyyMMdd-kkmmssSSS");
        String result = fileName.substring(0, pos) + timeStamp + fileName.substring(pos);
        return result;
    }
 
    public static String fillVariants(String fileName) {
        //KA医院管理-@{YYYYMMdd}
        
        if (fileName == null) {
            return null;
        }
        
        fileName = fileName.trim();
        
        int begin = fileName.indexOf("@{");
        if (begin <= 0) {
            return fileName;
        }
        
        int end = fileName.indexOf("}");
        if (end <= 0) {
            end = fileName.length();
        }
        
        String result = fileName.substring(0, begin) + parseVariant(fileName.substring(begin + 2, end)) + fileName.substring(end + 1);
        return result;
    }
    
    private static String addSuffix(String fileName, String templateName) {
        int pos = fileName.lastIndexOf(".");
        
        if (pos > 0) {
            return fileName;
        }
        
        pos = templateName.lastIndexOf(".");
        
        if (pos < 0) {
            return fileName;
        }
        
        String suffix = templateName.substring(pos, templateName.length());
        fileName = fileName + suffix;
        return fileName;
    }
 
    private static String parseVariant(String value) {
        return Util.newDateTimeStr(value);
    }
 
}