zhuguifei
2025-04-28 442928123f63ee497d766f9a7a14f0a6ee067e25
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
/**
 *
 * (c) Copyright Ascensio System SIA 2021
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
 
package org.jeecg.modules.doc.util;
 
 
import com.alibaba.fastjson.JSONObject;
 
import org.springframework.stereotype.Component;
 
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.net.InetAddress;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.*;
 
@Component
public class DocumentManager
{
 
    private static HttpServletRequest request;
 
    public static void Init(HttpServletRequest req, HttpServletResponse resp)
    {
        request = req;
    }
 
    public static long GetMaxFileSize()
    {
        long size;
 
        try
        {
            size = Long.parseLong(ConfigManager.GetProperty("filesize-max"));
        }
        catch (Exception ex)
        {
            size = 0;
        }
 
        return size > 0 ? size : 5 * 1024 * 1024;
    }
 
    public static List<String> GetFileExts()
    {
        List<String> res = new ArrayList<>();
 
        res.addAll(GetViewedExts());
        res.addAll(GetEditedExts());
        res.addAll(GetConvertExts());
 
        return res;
    }
 
    public static List<String> GetViewedExts()
    {
        String exts = ConfigManager.GetProperty("files.docservice.viewed-docs");
        return Arrays.asList(exts.split("\\|"));
    }
 
    public static List<String> GetEditedExts()
    {
        String exts = ConfigManager.GetProperty("files.docservice.edited-docs");
        return Arrays.asList(exts.split("\\|"));
    }
 
    public static List<String> GetConvertExts()
    {
        String exts = ConfigManager.GetProperty("files.docservice.convert-docs");
        return Arrays.asList(exts.split("\\|"));
    }
 
    public static String CurUserHostAddress(String userAddress)
    {
        if(userAddress == null)
        {
            try
            {
                userAddress = InetAddress.getLocalHost().getHostAddress();
            }
            catch (Exception ex)
            {
                userAddress = "";
            }
        }
 
        return userAddress.replaceAll("[^0-9a-zA-Z.=]", "_");
    }
 
    public static String FilesRootPath(String userAddress)
    {
        String hostAddress = CurUserHostAddress(userAddress);
        String serverPath = request.getSession().getServletContext().getRealPath("");
        String storagePath = ConfigManager.GetProperty("storage-folder");
        String directory = serverPath + storagePath + File.separator + hostAddress + File.separator;
 
        File file = new File(directory);
 
        if (!file.exists())
        {
            file.mkdirs();
        }
 
        return directory;
    }
 
    public static String StoragePath(String fileName, String userAddress)
    {
        String directory = FilesRootPath(userAddress);
        return directory + FileUtility.GetFileName(fileName);
    }
 
    public static String ForcesavePath(String fileName, String userAddress, Boolean create)
    {
        String hostAddress = CurUserHostAddress(userAddress);
        String serverPath = request.getSession().getServletContext().getRealPath("");
        String storagePath = ConfigManager.GetProperty("storage-folder");
 
        String directory = serverPath + storagePath + File.separator + hostAddress + File.separator;
 
        File file = new File(directory);
        if (!file.exists()) return "";
 
        directory = directory + fileName + "-hist" + File.separator;
        file = new File(directory);
        if (!create && !file.exists()) return "";
 
        file.mkdirs();
 
        directory = directory + fileName;
        file = new File(directory);
        if (!create && !file.exists()) {
            return "";
        }
 
        return directory;
    }
 
    public static String HistoryDir(String storagePath)
    {
        return storagePath += "-hist";
    }
 
    public static String VersionDir(String histPath, Integer version)
    {
        return histPath + File.separator + Integer.toString(version);
    }
 
    public static String VersionDir(String fileName, String userAddress, Integer version)
    {
        return VersionDir(HistoryDir(StoragePath(fileName, userAddress)), version);
    }
 
    public static Integer GetFileVersion(String historyPath)
    {
        File dir = new File(historyPath);
 
        if (!dir.exists()) return 0;
 
        File[] dirs = dir.listFiles(new FileFilter() {
            @Override
            public boolean accept(File pathname) {
                return pathname.isDirectory();
            }
        });
 
        return dirs.length + 1;
    }
 
    public static int GetFileVersion(String fileName, String userAddress)
    {
        return GetFileVersion(HistoryDir(StoragePath(fileName, userAddress)));
    }
 
    public static String GetCorrectName(String fileName, String userAddress)
    {
        String baseName = FileUtility.GetFileNameWithoutExtension(fileName);
        String ext = FileUtility.GetFileExtension(fileName);
        String name = baseName + ext;
 
        File file = new File(StoragePath(name, userAddress));
 
        for (int i = 1; file.exists(); i++)
        {
            name = baseName + " (" + i + ")" + ext;
            file = new File(StoragePath(name, userAddress));
        }
 
        return name;
    }
 
    public static void CreateMeta(String fileName, String uid, String uname, String userAddress) throws Exception
    {
        String histDir = HistoryDir(StoragePath(fileName, userAddress));
 
        File dir = new File(histDir);
        dir.mkdir();
 
        JSONObject json = new JSONObject();
        json.put("created", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        json.put("id", (uid == null || uid.isEmpty()) ? "uid-1" : uid);
        if (json.get("id").equals("uid-0")) {
            json.put("name", null);
        } else {
            json.put("name", (uname == null || uname.isEmpty()) ? "John Smith" : uname);
        }
 
        File meta = new File(histDir + File.separator + "createdInfo.json");
        try (FileWriter writer = new FileWriter(meta)) {
//            json.writeJSONString(writer);
        }
    }
 
    public static File[] GetStoredFiles(String userAddress)
    {
        String directory = FilesRootPath(userAddress);
 
        File file = new File(directory);
        return file.listFiles(new FileFilter() {
            @Override
            public boolean accept(File pathname) {
                return pathname.isFile();
            }
        });
    }
 
    public static String CreateDemo(String fileExt, Boolean sample, String uid, String uname) throws Exception
    {
        String demoName = (sample ? "sample." : "new.") + fileExt;
        String demoPath = "assets" + File.separator + (sample ? "sample" : "new") + File.separator;
        String fileName = GetCorrectName(demoName, null);
 
        InputStream stream = Thread.currentThread().getContextClassLoader().getResourceAsStream(demoPath + demoName);
 
        File file = new File(StoragePath(fileName, null));
 
        try (FileOutputStream out = new FileOutputStream(file))
        {
            int read;
            final byte[] bytes = new byte[1024];
            while ((read = stream.read(bytes)) != -1)
            {
                out.write(bytes, 0, read);
            }
            out.flush();
        }
 
        CreateMeta(fileName, uid, uname, null);
 
        return fileName;
    }
 
    public static String GetFileUri(String fileName, Boolean forDocumentServer)
    {
        try
        {
            String serverPath = GetServerUrl(forDocumentServer);
            String storagePath = ConfigManager.GetProperty("storage-folder");
            String hostAddress = CurUserHostAddress(null);
 
            String filePath = serverPath + "/" + storagePath + "/" + hostAddress + "/" + URLEncoder.encode(fileName, java.nio.charset.StandardCharsets.UTF_8.toString()).replace("+", "%20");
 
            return filePath;
        }
        catch (UnsupportedEncodingException e)
        {
            return "";
        }
    }
//
//    public ArrayList<Map<String, Object>> GetFilesInfo(){
//        ArrayList<Map<String, Object>> files = new ArrayList<>();
//
//        for(File file : GetStoredFiles(null)){
//            Map<String, Object> map = new LinkedHashMap<>();
//            map.put("version", GetFileVersion(file.getName(), null));
//            map.put("id", officeConverterService.GenerateRevisionId(CurUserHostAddress(null) + "/" + file.getName() + "/" + Long.toString(new File(StoragePath(file.getName(), null)).lastModified())));
//            map.put("contentLength", new BigDecimal(String.valueOf((file.length()/1024.0))).setScale(2, RoundingMode.HALF_UP) + " KB");
//            map.put("pureContentLength", file.length());
//            map.put("title", file.getName());
//            map.put("updated", String.valueOf(new Date(file.lastModified())));
//            files.add(map);
//        }
//
//        return files;
//    }
//
//    public ArrayList<Map<String, Object>> GetFilesInfo(String fileId){
//        ArrayList<Map<String, Object>> file = new ArrayList<>();
//
//        for (Map<String, Object> map : GetFilesInfo()){
//            if (map.get("id").equals(fileId)){
//                file.add(map);
//                break;
//            }
//        }
//
//        return file;
//    }
 
    public static String GetPathUri(String path)
    {
        String serverPath = GetServerUrl(true);
        String storagePath = ConfigManager.GetProperty("storage-folder");
        String hostAddress = CurUserHostAddress(null);
 
        String filePath = serverPath + "/" + storagePath + "/" + hostAddress + "/" + path.replace(File.separator, "/").substring(FilesRootPath(null).length()).replace(" ", "%20");
 
        return filePath;
    }
 
 
    public static String GetServerUrl(Boolean forDocumentServer) {
        if (forDocumentServer && !ConfigManager.GetProperty("files.docservice.url.example").equals("")) {
            return ConfigManager.GetProperty("files.docservice.url.example");
        } else {
            return request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath();
        }
    }
 
    public static String GetCallback(String fileName)
    {
        String serverPath = GetServerUrl(true);
        String hostAddress = CurUserHostAddress(null);
        try
        {
            String query = "?type=track&fileName=" + URLEncoder.encode(fileName, java.nio.charset.StandardCharsets.UTF_8.toString()) + "&userAddress=" + URLEncoder.encode(hostAddress, java.nio.charset.StandardCharsets.UTF_8.toString());
 
            return serverPath + "/IndexServlet" + query;
        }
        catch (UnsupportedEncodingException e)
        {
            return "";
        }
    }
 
//    public static String GetInternalExtension(FileType fileType)
//    {
//        if (fileType.equals(FileType.Word))
//            return ".docx";
//
//        if (fileType.equals(FileType.Cell))
//            return ".xlsx";
//
//        if (fileType.equals(FileType.Slide))
//            return ".pptx";
//
//        return ".docx";
//    }
 
//    public static String CreateToken(Map<String, Object> payloadClaims)
//    {
//        jwtComp.createJWT(payloadClaims);
//
//        try
//        {
//            Signer signer = HMACSigner.newSHA256Signer(GetTokenSecret());
//            JWT jwt = new JWT();
//            for (String key : payloadClaims.keySet())
//            {
//                jwt.addClaim(key, payloadClaims.get(key));
//            }
//            return JWT.getEncoder().encode(jwt, signer);
//        }
//        catch (Exception e)
//        {
//            return "";
//        }
//    }
//
//    public static JWT ReadToken(String token)
//    {
//        try
//        {
//            Verifier verifier = HMACVerifier.newVerifier(GetTokenSecret());
//            return JWT.getDecoder().decode(token, verifier);
//        }
//        catch (Exception exception)
//        {
//            return null;
//        }
//    }
 
    public static Boolean TokenEnabled()
    {
        String secret = GetTokenSecret();
        return secret != null && !secret.isEmpty();
    }
 
    private static String GetTokenSecret()
    {
        return ConfigManager.GetProperty("files.docservice.secret");
    }
}