1821349743@qq.com
2023-02-20 a387ff8978bd0e738e83e0277cfa38d7c3ab3080
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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
 
package com.product.data.sync.service.media;
 
import com.product.core.service.support.AbstractBaseService;
import org.springframework.stereotype.Service;
 
import java.io.File;
import java.io.IOException;
import java.text.Collator;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Locale;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@Service
public class GdMediaUtil extends AbstractBaseService{
    private static String root = "D:/Media/";
    private static String testString = "^(\\d{0,4}`)|(\\d{0,3}\\^\\d{0,4}`)";
    private static String startWith = "FE#ENC#";
    private static String key = "fe520";
    public String templete = "TemplateOfCreate";
    public String taohongTemplete = "TemplateOfTaohong";
    public String faceTempLate = "FaceTempLate";
    static Encryption encryption;
 
    static {
        encryption = new Encryption(key);
        root = SysSetup.getMediaParameter("MEDIA_PATH");
        if (root == null) {
            root = "D:\\OA\\FEGOV\\Media\\";
        }
 
    }
 
    public GdMediaUtil() {
    }
 
    private String getPath(String code) {
        if (code.length() < 15) {
            return null;
        } else {
            code = code.substring(0, 14);
            return code.substring(0, 4) + "/" + code.substring(4, 6) + "/" + code.substring(6, 8) + "/" + code;
        }
    }
 
    private String getRoot(String code) {
        String out = root + "/" + this.getPath(code) + "/";
        this.mvdir(out);
        return out;
    }
 
    public static boolean mvdir(String path) {
        if (path != null && !path.equals("")) {
            path = path.replace(File.separatorChar, '/');
            boolean allFolder = false;
            if (path.endsWith("/")) {
                allFolder = true;
            }
 
            String[] folders = path.split("/");
            boolean success = true;
            String root = null;
            int index = allFolder ? folders.length : folders.length - 1;
 
            for(int i = 0; i < index; ++i) {
                if (root == null) {
                    root = folders[0];
                } else {
                    root = root + "/" + folders[i];
                }
 
                File file = new File(root);
                if (!file.exists()) {
                    success = file.mkdir();
                }
 
                if (!success) {
                    return success;
                }
            }
 
            return success;
        } else {
            return false;
        }
    }
 
    public File[] getMediaTemplete() {
        return this.getMediaTemplete((String)null);
    }
 
    public File[] getMediaTemplete(String parent) {
        File file = null;
        if (parent != null && !parent.equals("")) {
            file = new File(parent);
        } else {
            file = new File(root + "/" + this.templete);
        }
 
        if (!file.exists()) {
            file.mkdir();
        }
 
        return file.listFiles();
    }
 
    public String getParent(String filepath) {
        if (filepath == null) {
            return null;
        } else {
            File file = new File(root + "/" + this.templete);
            File file1 = new File(filepath);
            return file.equals(file1) ? null : file1.getParent().replaceAll("\\\\", "/");
        }
    }
 
    public String getRelationDir(String filepath) {
        if (filepath == null) {
            return "";
        } else {
            filepath = filepath.replaceAll("\\\\", "/");
            int i = filepath.indexOf(this.templete);
            return i < 0 ? filepath : filepath.substring(i + this.templete.length() + 1);
        }
    }
 
    public File[] getTaohongTemplete() {
        return this.getTaohongTemplete("");
    }
 
    public File[] getTaohongTemplete(String subfloder) {
        File file = new File(root + "/" + this.taohongTemplete + "/" + subfloder);
        if (!file.exists()) {
            file.mkdir();
        }
 
        File[] allfile = file.listFiles();
        this.sort(allfile);
        return allfile;
    }
 
    private void sort(File[] files) {
        Arrays.sort(files, new Comparator<File>() {
            public int compare(File arg0, File arg1) {
                if (arg0.isDirectory() && arg1.isDirectory()) {
                    return GdMediaUtil.this.compareByName(arg0, arg1);
                } else if (arg0.isDirectory() && !arg1.isDirectory()) {
                    return -1;
                } else {
                    return !arg0.isDirectory() && arg1.isDirectory() ? 1 : GdMediaUtil.this.compareByName(arg0, arg1);
                }
            }
        });
    }
 
    private int compareByName(File object1, File object2) {
        Comparator<Object> cmp = Collator.getInstance(Locale.CHINA);
        return cmp.compare(object1.getName(), object2.getName());
    }
 
    public File[] getFaceTempLate() {
        File file = new File(root + "/" + this.faceTempLate + "/");
        if (!file.exists()) {
            file.mkdir();
        }
 
        return file.listFiles();
    }
 
    public File getTemplateFile(String name, String path) {
        File file = null;
        if (path != null && path.length() > 0) {
            file = new File(root + "/" + path + "/" + name);
        } else {
            file = new File(root + "/" + this.templete + "/" + name);
        }
 
        return file;
    }
 
    public static void main(String[] args) {
        GdMediaUtil gdMediaUtil = new GdMediaUtil();
        gdMediaUtil.getMediaFiles("2022032535010101");
    }
 
    public Map<String, File> getMediaFiles(String code) {
        int c = this.getVersion(code);
        File file = new File(this.getRoot(code));
        if (file.exists()) {
            File[] files = file.listFiles();
            Map<String, Integer> map = new HashMap();
            Map<String, File> filemap = new LinkedHashMap();
 
            for(int i = 0; i < files.length; ++i) {
                GdMediaUtil.MediaFile mediaFile = new GdMediaUtil.MediaFile(files[i].getAbsolutePath());
                if (!mediaFile.isDirectory()) {
                    int version = mediaFile.getVersion();
                    if (version <= c) {
                        String filename = mediaFile.getName();
                        if (filename.indexOf(".") != 0) {
                            Integer integer = (Integer)map.get(filename);
                            if (integer == null) {
                                map.put(filename, new Integer(version));
                                if (!mediaFile.isDelete()) {
                                    filemap.put(filename, mediaFile);
                                }
                            } else if (integer <= version) {
                                map.put(filename, new Integer(version));
                                if (!mediaFile.isDelete()) {
                                    if (integer == version) {
                                        GdMediaUtil.MediaFile omediaFile = (GdMediaUtil.MediaFile)filemap.get(filename);
                                        if (omediaFile != null && omediaFile.lastModified() < mediaFile.lastModified()) {
                                            filemap.put(filename, mediaFile);
                                        }
                                    } else {
                                        filemap.put(filename, mediaFile);
                                    }
                                } else if (integer != version) {
                                    filemap.remove(filename);
                                }
                            }
                        }
                    }
                }
            }
            return filemap;
        } else {
            return null;
        }
    }
 
    public boolean deleteMediaFile(String code, String name) {
        File file = new File(this.getRoot(code));
        int c = this.getVersion(code);
        if (file.exists()) {
            File[] files = file.listFiles();
            int maxVersion = 0;
            GdMediaUtil.MediaFile currentMediaFile = null;
 
            for(int i = 0; i < files.length; ++i) {
                String filename = files[i].getName();
                if (filename.indexOf(".") != 0) {
                    GdMediaUtil.MediaFile mediaFile = new GdMediaUtil.MediaFile(files[i].getAbsolutePath());
                    if (!mediaFile.isDelete() && mediaFile.getName().equals(name)) {
                        int thisv = mediaFile.getVersion();
                        if (thisv <= c && maxVersion < thisv) {
                            maxVersion = thisv;
                            currentMediaFile = mediaFile;
                        }
                    }
                }
            }
 
            if (currentMediaFile == null) {
                return true;
            } else if (maxVersion == c) {
                return currentMediaFile.delete();
            } else {
                return currentMediaFile.delete(c);
            }
        } else {
            return true;
        }
    }
 
    public File createMediaFile(String code, String name) {
        int c = this.getVersion(code);
        name = this.encryptFileName(name);
        name = c + "`" + name;
        File file = new GdMediaUtil.MediaFile(this.getRoot(code) + "/" + name);
        return file;
    }
 
 
 
    public String encryptFileName(String fileName) {
        String version = "";
        String nfileName = "";
        Pattern p = Pattern.compile(testString);
        Matcher m = p.matcher(fileName);
        if (m.find()) {
            version = m.group();
            nfileName = fileName.substring(m.end());
        } else {
            nfileName = fileName;
        }
 
        if (nfileName.startsWith(startWith)) {
            return fileName;
        } else {
            nfileName = startWith + encryption.encryptString(nfileName, true);
            return version + nfileName;
        }
    }
 
    public String decryptFileName(String fileName) {
        String version = "";
        String nfileName = "";
        Pattern p = Pattern.compile(testString);
        Matcher m = p.matcher(fileName);
        if (m.find()) {
            version = m.group();
            nfileName = fileName.substring(m.end());
        } else {
            nfileName = fileName;
        }
        if (nfileName.startsWith(startWith)) {
            nfileName = encryption.decryptionString(nfileName.substring(startWith.length()), true);
            return version + nfileName;
        } else {
            return fileName;
        }
    }
 
    public String getRoot() {
        return root;
    }
 
    public void setRoot(String root) {
        GdMediaUtil.root = root;
    }
 
    private int getVersion(String code) {
        if (code.length() < 15) {
            return 0;
        } else {
            String ban = code.substring(14);
            int c = 0;
 
            try {
                c = this.converFiftyInt(ban);
            } catch (Exception var5) {
            }
 
            return c;
        }
    }
 
    public static int converFiftyInt(String iData) {
        int value = 0;
 
        for(int i = iData.length() - 1; i > -1; --i) {
            value = (int)((double)value + (double)"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ()[]{}_+-=!@#$%&".indexOf(iData.charAt(i)) * Math.pow((double)"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ()[]{}_+-=!@#$%&".length(), (double)(iData.length() - i - 1)));
        }
 
        return value;
    }
    public String getTemplete() {
        return this.templete;
    }
 
    public void setTemplete(String templete) {
        this.templete = templete;
    }
 
    class MediaFile extends File {
        private static final long serialVersionUID = -5914321855770345214L;
 
        public MediaFile(String path) {
            super(path);
        }
 
        public String getName() {
            String name = this.getRealName();
            name = GdMediaUtil.this.decryptFileName(name);
            return name.replaceFirst(GdMediaUtil.testString, "");
        }
 
        public String getRealName() {
            return super.getName();
        }
 
        public int getVersion() {
            String name = this.getRealName();
            int index = name.indexOf("`");
            String tempCode = name.substring(0, index);
            int index1 = tempCode.indexOf("^");
            if (index1 > 0) {
                tempCode = tempCode.substring(0, index1);
            }
 
            boolean var5 = false;
 
            try {
                int v = Integer.parseInt(tempCode);
                return v;
            } catch (Exception var7) {
                return 0;
            }
        }
 
        public boolean isDelete() {
            return this.getRealName().indexOf("^") > -1;
        }
 
        public boolean delete() {
            return this.delete(this.getVersion());
        }
 
        public boolean delete(int version) {
            File newFile;
            if (version == 1) {
                newFile = new File(this.getParentFile().getAbsolutePath() + "/" + this.getRealName());
                return newFile.exists() ? newFile.delete() : false;
            } else {
                newFile = new File(this.getParentFile().getAbsolutePath() + "/" + version + "^" + this.getRealName());
 
                try {
                    if (version != this.getVersion()) {
                        return newFile.createNewFile();
                    }
 
                    if (!newFile.exists()) {
                        return this.renameTo(newFile);
                    }
 
                    newFile = new File(this.getAbsolutePath());
                    if (newFile.isFile() && newFile.exists()) {
                        newFile.delete();
                    }
                } catch (IOException var4) {
                }
 
                return false;
            }
        }
 
        public boolean rename(String name) {
            String oldName = this.getRealName();
            int start = oldName.indexOf("`");
            if (start < 0) {
                return false;
            } else {
                int stop = oldName.lastIndexOf(".");
                if (stop < 0) {
                    stop = oldName.length();
                }
 
                String newName = oldName.substring(0, start + 1) + name + oldName.substring(stop);
                File newFile = new File(this.getParentFile().getAbsolutePath() + "/" + newName);
                return this.renameTo(newFile);
            }
        }
    }
}