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
package org.jeecg.modules.doc.service.impl;
 
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.common.util.MD5Util;
import org.jeecg.modules.doc.component.PathFileUtil;
import org.jeecg.modules.doc.component.UfopConfig;
import org.jeecg.modules.doc.constant.Constant;
import org.jeecg.modules.doc.dto.UploadFileDTO;
import org.jeecg.modules.doc.entity.DocFile;
import org.jeecg.modules.doc.entity.DocFilePath;
import org.jeecg.modules.doc.entity.LbDocEntity;
import org.jeecg.modules.doc.mapper.LbDocMapper;
import org.jeecg.modules.doc.service.IDocFilePathService;
import org.jeecg.modules.doc.service.IDocFileService;
import org.jeecg.modules.doc.service.ILbDocService;
import org.jeecg.modules.doc.util.MD5;
import org.jeecg.modules.doc.util.UFOPUtils;
import org.jeecg.modules.doc.vo.CompareFileVo;
import org.jeecg.modules.doc.vo.DocTree;
import org.jeecg.modules.doc.vo.QiwenFile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
 
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.*;
 
/**
 * 测试Service
 */
@Service
public class LbDocServiceImpl extends ServiceImpl<LbDocMapper, LbDocEntity> implements ILbDocService {
 
 
    @Override
    public String hello() {
        return "hello jeecg!";
    }
 
    @Autowired
    private UfopConfig ufopConfig;
 
    @Autowired
    private IDocFilePathService filePathService;
 
    @Autowired
    private IDocFileService docFileService;
 
    @Autowired
    private RestTemplate restTemplate;
 
 
    @Override
    public DocTree genDocTree(DocTree docTree) {
 
        int level = Integer.valueOf(docTree.getKey().substring(0, docTree.getKey().indexOf("-")));
        level++;
        File file = new File(docTree.getPath());
        if (!file.exists()) {
            System.err.println("文件不存在!!");
        }
 
        docTree.setTitle(file.getName());
        File[] files = file.listFiles();
 
        if (files.length != 0) {
            List<DocTree> docs = new ArrayList<>();
            DocTree dt = null;
            for (int i = 0; i < files.length; i++) {
                File f = files[i];
                dt = new DocTree();
                dt.setKey(level + "-" + i);
                dt.setTitle(f.getName());
                dt.setPath(f.getPath());
                if (f.isDirectory()) {
 
                    // genDocTree(f.getAbsolutePath(), level,dt);
                } else {
 
                    dt.setLeaf(true);
 
                }
                docs.add(dt);
            }
            docTree.setChildren(docs);
        }
 
        return docTree;
    }
 
 
 
    public void initDocSize(Map<String, Long> map, String releasePath, DocFilePath parent) {
       // System.out.println("文件:" + fsize +" 文件夹:" + dsize);
        File file = new File(releasePath);
        if (!file.exists()) {
            System.err.println("文件不存在!!");
        }
        File[] files = file.listFiles();
        DocFilePath filePath = null;
        if (files != null && files.length != 0) {
            for (int i = 0; i < files.length; i++) {
                File f = files[i];
                if (!"#recycle".equals(f.getName()) && !"$RECYCLE.BIN".equals(f.getName()) && !"System Volume Information".equals(f.getName())) {
                    if (f.isDirectory()) {
                        map.put("dsize", map.get("dsize")+1);
                        initDocSize(map,f.getPath(), filePath);
                    } else {
                        map.put("fsize", map.get("fsize")+1);
                    }
                }
 
            }
        }
    }
 
 
    public void initDir(Map<String, String> paths, List<DocFilePath> filePathList, String releasePath, DocFilePath parent) {
        File file = new File(releasePath);
        if (!file.exists()) {
            System.err.println("文件不存在!!");
        }
        File[] files = file.listFiles();
        DocFilePath filePath = null;
 
//        System.out.println("当前文件夹:" + file.getPath());
        if (files != null && files.length != 0) {
            for (int i = 0; i < files.length; i++) {
                File f = files[i];
             //   System.out.println(f.getPath());
              //  System.out.println("r\\/"+f.getPath().replace("\\","/"));
               // System.out.println("ddd" + f.getPath().replace("\\","/").replace(ufopConfig.getReleasePath(),"/07 开发三部"));
                String filepath = f.getPath().replace("\\","/").replace(ufopConfig.getReleasePath(),"");
                //String filepath = QiwenFile.separator+QiwenFile.formatPath(f.getPath().replace("\\","/")).replace(ufopConfig.getReleasePath(),"/07 开发三部");
                filepath = QiwenFile.separator+(filepath.substring(0,filepath.lastIndexOf("/")));
                if (!"#recycle".equals(f.getName()) && !"$RECYCLE.BIN".equals(f.getName()) && !"System Volume Information".equals(f.getName())) {
                    if (f.isDirectory()) {
                        //System.out.println("文件:" + f.getName());
                        filePath = PathFileUtil.getDir(filepath,f.getName(), parent);
                        filePath.setIsDir(Constant.IS_DIR);
                        String s = paths.get(filePath.getFilePath() + filePath.getFileName() + (filePath.getExtendName()==null?"":filePath.getExtendName()) + filePath.getIsDir());
 
                        if (s == null) {
                            filePathList.add(filePath);
                        } else {
                            filePath.setPathId(s);
                        }
 
                        initDir(paths, filePathList,f.getPath(), filePath);
//                        if (s != null) {
//                            continue;
//                        }
                    } else {
                        continue;
 
                    }
 
 
                }
            }
 
        }
 
    }
 
    @Override
    public void initDoc(Map<String, String> paths, String releasePath, DocFilePath parent) {
 
        File file = new File(releasePath);
        if (!file.exists()) {
            System.err.println("文件不存在!!");
        }
        File[] files = file.listFiles();
        DocFilePath filePath = null;
        DocFile docFile = null;
        List<DocFilePath> filePathList = new ArrayList<>();
        if (files != null && files.length != 0) {
            for (int i = 0; i < files.length; i++) {
                File f = files[i];
                String filepath = QiwenFile.separator+QiwenFile.formatPath(f.getPath().replace("\\","/")).replaceFirst(ufopConfig.getReleasePath(),"");
                filepath = QiwenFile.separator+(filepath.substring(0,filepath.lastIndexOf("/")));
 
                if (!"#recycle".equals(f.getName()) && !"$RECYCLE.BIN".equals(f.getName())
                        && !"System Volume Information".equals(f.getName())
                        // && !"档案资料区".equals(f.getName())
                         && !"02 研发一部".equals(f.getName())
                        && !"03 研发二部".equals(f.getName())
                        && !"04 研发三部".equals(f.getName())
                                               && !"05 开发一部".equals(f.getName())
                          && !"06 开发二部".equals(f.getName())
                         && !"07 开发三部".equals(f.getName())
                        && !"08 工业设计部".equals(f.getName())
                         && !"09 研发管理部".equals(f.getName())
                         && !"10 兰浦公司".equals(f.getName())
                        && !"01 公共区域".equals(f.getName())
                ) {
 
                    if (f.isDirectory()) {
                        filePath = PathFileUtil.getDir(filepath,f.getName(), parent);
                        filePath.setIsDir(Constant.IS_DIR);
                        initDoc(paths, f.getPath(), filePath);
                    } else {
 
                        int lastIndex = f.getName().lastIndexOf(".");
 
                        filePath = PathFileUtil.getDir(filepath,f.getName().substring(0,lastIndex>0?lastIndex:0), parent);
                        //System.out.println("同步文件:" + filePath.getFileName());
                        filePath.setIsDir(Constant.IS_NOT_DIR);
                        filePath.setExtendName(f.getName().substring(f.getName().lastIndexOf(".") + 1));
 
 
                        String key = filePath.getFilePath().replace("/192.168.0.9/研发中心", "") + filePath.getFileName() + (filePath.getExtendName() == null ? "" : filePath.getExtendName()) + filePath.getIsDir();
                        String s = paths.get(key);
 
 
                        if (s != null) {
                            //System.out.println("该文件已存在");
                            docFile = new DocFile();
                            continue;
//                            docFile.setFileId(s);
                        } else {
                            long length = f.length();
                            if (length > 1000000000l) {
                                log.error("小文件:[" + f.getPath() +"]>>>["+f.length()+ "]");
                                continue;
                            }
                            // String dfId;
                            String md5 = MD5.getMD5(f);
 
                            // 查询是否已上传过
                            List<DocFile> fileList = docFileService.list(new LambdaQueryWrapper<DocFile>().eq(DocFile::getIdentifier,md5));
                            if (fileList!=null && fileList.size()>0) {
                                docFile = new DocFile();
                                // dfId = fileList.get(0).getFileId();
                                docFile.setFileId(fileList.get(0).getFileId());
                            } else {
                            //    String url = UFOPUtils.ENCRYPT_PATH + "/fileTransfer/encryptUpload";
                                try {
 
                                    // 1、封装请求头
//                                    HttpHeaders headers = new HttpHeaders();
//                                    MediaType type = MediaType.parseMediaType("multipart/form-data");
//                                    headers.setContentType(type);
//                                    headers.setContentLength(length);
//                                    headers.setContentDispositionFormData("media", f.getName());
//                                    // 2、封装请求体
//                                    MultiValueMap<String, Object> param = new LinkedMultiValueMap<>();
//                                    FileSystemResource resource = new FileSystemResource(f);
//                                    param.add("file", resource);
//                                    param.add("identifier", md5);
//                                    // 3、封装整个请求报文
//                                    HttpEntity<MultiValueMap<String, Object>> formEntity = new HttpEntity<>(param, headers);
//                                    // 4、发送请求
//                                    ResponseEntity<DocFile> data = restTemplate.postForEntity(url, formEntity, DocFile.class);
//                                    // 5、请求结果处理
//                                    docFile = data.getBody();
                                    docFile = docFileService.saveFile(f, md5);
 
                                } catch (Exception e) {
                                    log.error("异常文件:PATH=" + f.getPath() + "[md5]=" + md5);
                                    log.error(e.getMessage());
                                    //e.printStackTrace();
                                    continue;
                                }
                            }
                        }
                        filePath.setFileId(docFile.getFileId());
                    }
                    String s = paths.get(filePath.getFilePath().replace("/192.168.0.9/研发中心","") + filePath.getFileName() + (filePath.getExtendName()==null?"":filePath.getExtendName()) + filePath.getIsDir());
                    if (s != null) {
                        continue;
                    }
                    //filePath.setPathId(s);
//                    try {
//                        filePathService.saveOrUpdate(filePath);
//                    }catch (Exception e) {
//                        log.error("获取相同文件:::"+filePath.getFilePath() + filePath.getFileName() + filePath.getExtendName() + filePath.getIsDir());
//                        log.error("保存文件失败:" + filePath.getFilePath() + ">>" + filePath.getFileName() + ">>>" + filePath.getExtendName());
//                        log.error(e.getMessage());
//                    }
                    filePathList.add(filePath);
                }
 
            }
            System.out.println("当前文件夹:" + file.getPath());
            System.out.println("保存" + filePathList.size() +"个文件:" + System.currentTimeMillis());
            try {
                filePathService.saveOrUpdateBatch(filePathList);
            } catch (Exception e) {
                log.error("保存失败");
                log.error(e.getMessage());
            }
 
            System.out.println("保存完成:" +System.currentTimeMillis());
        }
    }
 
 
 
 
 
    private static void copyFileUsingStream(File source, File dest) throws IOException {
        InputStream is = null;
        OutputStream os = null;
        try {
            is = new FileInputStream(source);
            os = new FileOutputStream(dest);
            byte[] buffer = new byte[1024];
            int length;
            while ((length = is.read(buffer)) > 0) {
                os.write(buffer, 0, length);
            }
        } finally {
            is.close();
            os.close();
        }
 
    }
 
 
    @Override
    public CompareFileVo compareFile(UploadFileDTO uploadFileDto)  {
        CompareFileVo compareFileVo = new CompareFileVo();
        compareFileVo.setFileName(uploadFileDto.getRelativePath());
        compareFileVo.setIdentifier(uploadFileDto.getIdentifier());
        String relativePath = uploadFileDto.getRelativePath().substring(uploadFileDto.getRelativePath().indexOf("/"));
        String filePath = (uploadFileDto.getFilePath() + relativePath).replace("/" + uploadFileDto.getFilename(), "");
        int lastIndex = uploadFileDto.getFilename().lastIndexOf(".");
        String fileName = uploadFileDto.getFilename().substring(0, lastIndex > 0 ? lastIndex : 0);
        String extendName = uploadFileDto.getFilename().substring(lastIndex + 1);
        List<DocFilePath> list = filePathService.list(new LambdaQueryWrapper<DocFilePath>()
 
                .eq(DocFilePath::getFilePath, filePath)
                .eq(DocFilePath::getFileName, fileName)
                        .eq(DocFilePath::getExtendName, extendName)
                        .eq(DocFilePath::getDeleteFlag, 0)
                .eq(DocFilePath::getIsDir, 0)
        );
        if (list != null && list.size() == 1) {
            DocFile byId = docFileService.getById(list.get(0).getFileId());
            File file = new File(UFOPUtils.getStaticPath() + byId.getFileUrl());
            String md5 = MD5.getMD5(file);
            compareFileVo.setComIdentifier(md5);
            compareFileVo.setComFileSize(file.length());
        }
        return compareFileVo;
 
    }
 
    @Override
    public void checkFile(String filePath) {
            File file = new File(filePath);
            if (!file.exists()) {
                System.err.println("文件不存在!!");
            }
 
            File[] files = file.listFiles();
            if (files != null && files.length != 0) {
                for (int i = 0; i < files.length; i++) {
                    File f = files[i];
 
 
 
 
                        if (f.isDirectory()) {
                            System.out.println(f.getPath());
 
                                checkFile(f.getPath());
 
                        } else {
                            try {
//                                long l = f.lastModified();
//                                String dat = "2023-10-23";
//                                Date parse = new SimpleDateFormat("yyyy-MM-dd").parse(dat);
//                                Date date = new Date(l);
//                                if (date.after(parse)) {
//                                    continue;
//                                }
//                                String md5 = MD5.getMD5(f);
//                                String fileName = "";
//                                int lastIndex = f.getName().lastIndexOf(".");
//                                if (lastIndex <0) {
////                                fileName = f.getName().substring(0,f.getName().length()-1);
//                                    fileName = f.getName().replace("\uF029","");
//                                } else {
//                                    fileName = f.getName().substring(0, lastIndex);
//                                }
//                                if (!fileName.equals(md5)) {
//
//                                    System.out.println("原MD5:" + fileName);
//                                    System.out.println("现MD5:" + md5);
//                                    docFileService.update(new LambdaUpdateWrapper<DocFile>().set(DocFile::getFileStatus, 0).eq(DocFile::getIdentifier, fileName));
//                                    f.delete();
//                                }
 
                            }catch (Exception e) {
                                System.out.println("文件名:【" + f.getName()+"】");
                                e.printStackTrace();
                            }
 
 
                        }
                        System.out.println("完成!");
                    }
                }
    }
}