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
package org.jeecg.modules.doc.component;
 
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOUtils;
import org.jeecg.common.constant.CacheConstant;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.modules.doc.constant.Constant;
import org.jeecg.modules.doc.entity.DocFile;
import org.jeecg.modules.doc.entity.DocFilePath;
import org.jeecg.modules.doc.mapper.DocFileMapper;
import org.jeecg.modules.doc.mapper.DocFilePathMapper;
import org.jeecg.modules.doc.service.IDocFilePathService;
import org.jeecg.modules.doc.service.IFiletransferService;
import org.jeecg.modules.doc.service.IRecoveryFileService;
import org.jeecg.modules.doc.vo.CopyFile;
import org.jeecg.modules.doc.vo.QiwenFile;
import org.jeecg.modules.doc.util.UFOPUtils;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Component;
 
import javax.annotation.Resource;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.Future;
 
/**
 * 功能描述:异步任务业务类(@Async也可添加在方法上)
 */
@Slf4j
@Component
@Async("asyncTaskExecutor")
public class AsyncTaskComp {
    @Resource
    IDocFilePathService userFileService;
 
    @Resource
    IRecoveryFileService recoveryFileService;
    @Resource
    IFiletransferService filetransferService;
    @Resource
    DocFilePathMapper userFileMapper;
    @Resource
    DocFileMapper fileMapper;
    @Resource
    DocFileDealComp fileDealComp;
 
    @Resource
    RedisUtil redisUtil;
 
 
 
    public Long getFilePointCount(String fileId) {
        LambdaQueryWrapper<DocFilePath> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(DocFilePath::getFileId, fileId);
        long count = userFileMapper.selectCount(lambdaQueryWrapper);
 
        return count;
    }
 
    public Future<String> deleteUserFile(String userFileId) {
        DocFilePath userFile = userFileService.getById(userFileId);
        if (userFile.getIsDir() == Constant.IS_DIR) {
            LambdaQueryWrapper<DocFilePath> userFileLambdaQueryWrapper = new LambdaQueryWrapper<>();
            userFileLambdaQueryWrapper.eq(DocFilePath::getDeleteBatchNum, userFile.getDeleteBatchNum()).eq(DocFilePath::getIsDir, Constant.IS_NOT_DIR);
            List<DocFilePath> list = userFileService.list(userFileLambdaQueryWrapper);
            recoveryFileService.deleteUserFileByDeleteBatchNum(userFile.getDeleteBatchNum());
            for (DocFilePath userFileItem : list) {
                Object o = redisUtil.get(CacheConstant.DOC_FILE_DELETE_CACHE + ":" + userFileItem.getFileId());
                if (o != null) continue;
                Long filePointCount = getFilePointCount(userFileItem.getFileId());
 
                if (filePointCount != null && filePointCount == 0 && userFileItem.getIsDir() == Constant.IS_NOT_DIR) {
                    DocFile fileBean = fileMapper.selectById(userFileItem.getFileId());
                    try {
                        filetransferService.deleteFile(fileBean);
                        fileMapper.deleteById(fileBean.getFileId());
                        redisUtil.set(CacheConstant.DOC_FILE_DELETE_CACHE +":" + userFileItem.getFileId(), userFileItem.getFileId(),60*60*24L);
                    } catch (Exception e) {
                        log.error("删除本地文件失败:" + JSON.toJSONString(fileBean));
                    }
                }
            }
        } else {
 
            recoveryFileService.deleteUserFileByDeleteBatchNum(userFile.getDeleteBatchNum());
            Long filePointCount = getFilePointCount(userFile.getFileId());
 
            if (filePointCount != null && filePointCount == 0 && userFile.getIsDir() == Constant.IS_NOT_DIR) {
                DocFile fileBean = fileMapper.selectById(userFile.getFileId());
                try {
                    filetransferService.deleteFile(fileBean);
                    fileMapper.deleteById(fileBean.getFileId());
                } catch (Exception e) {
                    log.error("删除本地文件失败:" + JSON.toJSONString(fileBean));
                }
            }
        }
 
        return new AsyncResult<String>("deleteUserFile");
    }
 
    public Future<String> checkESUserFileId(String userFileId) {
        DocFilePath userFile = userFileMapper.selectById(userFileId);
        if (userFile == null) {
           // fileDealComp.deleteESByUserFileId(userFileId);
        }
        return new AsyncResult<String>("checkUserFileId");
    }
 
 
    public Future<String> saveUnzipFile(DocFilePath userFile, DocFile fileBean, int unzipMode, String entryName, String filePath) {
        String unzipUrl = UFOPUtils.getTempFile(fileBean.getFileUrl()).getAbsolutePath().replace("." + userFile.getExtendName(), "");
        String totalFileUrl = unzipUrl + entryName;
        File currentFile = new File(totalFileUrl);
 
        String fileId = null;
        if (!currentFile.isDirectory()) {
 
            FileInputStream fis = null;
            String md5Str = UUID.randomUUID().toString();
            try {
                fis = new FileInputStream(currentFile);
                md5Str = DigestUtils.md5Hex(fis);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                IOUtils.closeQuietly(fis);
            }
 
            FileInputStream fileInputStream = null;
            try {
                Map<String, Object> param = new HashMap<>();
                param.put("identifier", md5Str);
                List<DocFile> list = fileMapper.selectByMap(param);
 
                if (list != null && !list.isEmpty()) { //文件已存在
                    fileId = list.get(0).getFileId();
                } else { //文件不存在
                    fileInputStream = new FileInputStream(currentFile);
                    CopyFile createFile = new CopyFile();
                    System.out.println("totalFileUrl::" + totalFileUrl);
                    createFile.setExtendName(FilenameUtils.getExtension(totalFileUrl));
                    String saveFileUrl =new LocalStorageCopier().copy(fileInputStream, createFile);
 
                    DocFile tempFileBean = new DocFile(saveFileUrl, currentFile.length(), md5Str, userFile.getUpdateBy());
;
                    fileMapper.insert(tempFileBean);
                    fileId = tempFileBean.getFileId();
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                IOUtils.closeQuietly(fileInputStream);
                System.gc();
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                currentFile.delete();
            }
 
 
        }
 
 
        QiwenFile qiwenFile = null;
        if (unzipMode == 0) {
            qiwenFile = new QiwenFile(userFile.getFilePath(), entryName, currentFile.isDirectory());
 
        } else if (unzipMode == 1) {
            qiwenFile = new QiwenFile(userFile.getFilePath() + "/" + userFile.getFileName(), entryName, currentFile.isDirectory());
        } else if (unzipMode == 2) {
            qiwenFile = new QiwenFile(filePath, entryName, currentFile.isDirectory());
        }
 
 
        System.out.println("qiwenFile:::" + qiwenFile.getPath());
        DocFilePath saveUserFile = new DocFilePath(qiwenFile, userFile.getUpdateBy(), fileId);
        System.out.println("saveUserFile:::" + saveUserFile.toString());
        DocFilePath parent = fileDealComp.getParent(saveUserFile.getFilePath());
        if (parent != null) {
            System.out.println("parent::"+parent.toString());
            saveUserFile.setParentId(parent.getPathId());
        }
        String fileName = fileDealComp.getRepeatFileName(saveUserFile, saveUserFile.getFilePath());
        saveUserFile.setFileName(fileName);
        if (saveUserFile.getIsDir() == Constant.IS_DIR && !fileName.equals(saveUserFile.getFileName())) {
            //如果是目录,而且重复,什么也不做
        } else {
 
            System.out.println(saveUserFile.toString());
            userFileMapper.insert(saveUserFile);
        }
 
        return new AsyncResult<String>("saveUnzipFile");
    }
 
 
}