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
package org.jeecg.modules.doc.service.impl;
 
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOUtils;
import org.jeecg.common.util.DateUtils;
import org.jeecg.modules.doc.component.*;
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.IDocFileService;
import org.jeecg.modules.doc.vo.CopyFile;
import org.jeecg.modules.doc.vo.DownloadFile;
import org.jeecg.modules.doc.exception.QiwenException;
import org.jeecg.modules.doc.util.UFOPUtils;
import org.jeecg.modules.doc.vo.QiwenFile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.multipart.MultipartFile;
 
import javax.annotation.Resource;
import java.io.*;
import java.util.*;
 
/**
 * @Description: doc_file
 * @Author: jeecg-boot
 * @Date:   2022-07-04
 * @Version: V1.0
 */
@Service
public class DocFileServiceImpl extends ServiceImpl<DocFileMapper, DocFile> implements IDocFileService {
 
 
    @Resource
    DocFilePathMapper docFilePathMapper;
    @Resource
    AsyncTaskComp asyncTaskComp;
 
    @Autowired
    DocFileDealComp fileDealComp;
 
 
 
    /**
     * 上传文件到目的根路径,先计算文件MD5值,查询已上传过同一文件,有相同文件返回文件id,否则上传新文件并返回id
     *
     * @param f          待上传文件
     * @param identifier
     * @return
     */
    @Override
    public DocFile saveFile(MultipartFile f, String identifier) throws IOException {
        String dfId;
        DocFile docFile = new DocFile();
 
            dfId = IdUtil.getSnowflakeNextIdStr();
            docFile.setFileId(dfId);
 
            String date = DateUtils.getDate("yyyyMMdd");
            File savepath = new File(UFOPUtils.getStaticPath() + date);
            if (!savepath.exists()) {
                savepath.mkdirs();// 创建文件根目录
            }
            int index = f.getOriginalFilename().lastIndexOf(".");
            String extend = index == -1?"":f.getOriginalFilename().substring(index+1);
            String fileUrl = UFOPUtils.getUploadFileUrl(identifier,extend);
            File saveFile = new File(UFOPUtils.getStaticPath() + fileUrl);
            try {
               f.transferTo(saveFile);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
 
            docFile.setFileUrl(fileUrl);
            docFile.setFileStatus(1);
            docFile.setStorageType(0);
            docFile.setFileSize(f.getSize());
            docFile.setIdentifier(identifier);
            baseMapper.insert(docFile);
//        }
 
        return docFile;
    }
 
 
    @Override
    public DocFile saveFile(File f, String identifier) throws IOException {
        String dfId = null;
        String fileUrl = null;
        DocFile docFile = new DocFile();
        List<DocFile> fileList = baseMapper.selectList(new LambdaQueryWrapper<DocFile>().eq(DocFile::getIdentifier,identifier));
 
        if (fileList!=null && fileList.size()>0) {
            docFile = fileList.get(0);
            fileUrl = docFile.getFileUrl();
 
        } else {
 
            dfId = IdUtil.getSnowflakeNextIdStr();
            docFile.setFileId(dfId);
 
            String date = DateUtils.getDate("yyyyMMdd");
            File savepath = new File(UFOPUtils.getStaticPath() + "/研发技术文档/" + date);
            if (!savepath.exists()) {
                savepath.mkdirs();// 创建文件根目录
            }
            int index = f.getName().lastIndexOf(".");
            String extend = index == -1?"":f.getName().substring(index+1);
            fileUrl = UFOPUtils.getUploadFileUrl(identifier,extend);
        }
 
 
            String saveUrl = fileUrl.replace("研发技术文档", "//192.168.0.9/研发技术文档");
            File saveFile = new File(saveUrl);
            try {
                FileCopyUtils.copy(f, saveFile);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
 
            docFile.setFileUrl(fileUrl.replace("研发技术文档", "upload"));
            docFile.setFileStatus(1);
            docFile.setStorageType(0);
            docFile.setFileSize(f.length());
            docFile.setIdentifier(identifier);
            if (dfId != null) {
                baseMapper.insert(docFile);
            } else if (docFile.getFileId() != null) {
                baseMapper.update(null, new LambdaUpdateWrapper<DocFile>().set(DocFile::getFileStatus, 1).eq(DocFile::getFileId, docFile.getFileId()));
            }
 
//        }
 
        return docFile;
    }
    @Override
    public Long getFilePointCount(String fileId) {
        LambdaQueryWrapper<DocFilePath> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(DocFilePath::getFileId, fileId);
        long count = docFilePathMapper.selectCount(lambdaQueryWrapper);
        return count;
    }
    @Override
    public void updateFileDetail(String userFileId, String identifier, long fileSize, String modifyUserId) {
        DocFilePath userFile = docFilePathMapper.selectById(userFileId);
        DocFile fileBean = new DocFile();
        fileBean.setIdentifier(identifier);
        fileBean.setFileSize(fileSize);
        fileBean.setUpdateTime(DateUtils.getDate());
        fileBean.setUpdateBy(modifyUserId);
        fileBean.setFileId(userFile.getFileId());
        baseMapper.updateById(fileBean);
        userFile.setCreateTime(DateUtils.getDate());
        docFilePathMapper.updateById(userFile);
    }
 
 
    /**
     * 解压文件
     * @param userFileId
     * @param unzipMode
     * @param filePath
     */
    @Override
    public void unzipFile(String userFileId, int unzipMode, String filePath) {
        DocFilePath userFile = docFilePathMapper.selectById(userFileId);
        DocFile fileBean = baseMapper.selectById(userFile.getFileId());
        File destFile = new File(UFOPUtils.getStaticPath() + "temp" + File.separator + fileBean.getFileUrl());
 
 
        Downloader downloader = new LocalStorageDownloader();
        DownloadFile downloadFile = new DownloadFile();
        downloadFile.setFileUrl(fileBean.getFileUrl());
        InputStream inputStream = downloader.getInputStream(downloadFile);
 
        try {
            FileUtils.copyInputStreamToFile(inputStream, destFile);
        } catch (IOException e) {
            e.printStackTrace();
        }
 
 
        String extendName = userFile.getExtendName();
 
        String unzipUrl = UFOPUtils.getTempFile(fileBean.getFileUrl()).getAbsolutePath().replace("." + extendName, "");
 
        List<String> fileEntryNameList = new ArrayList<>();
 
        try {
            fileEntryNameList = FileOperation.unzip(destFile, unzipUrl);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("解压失败" + e);
            throw new QiwenException(500001, "解压异常:" + e.getMessage());
        }
 
        if (destFile.exists()) {
            destFile.delete();
        }
 
        if (!fileEntryNameList.isEmpty() && unzipMode == 1) {
            DocFilePath parent = fileDealComp.getParent(userFile.getFilePath());
            DocFilePath qiwenDir = PathFileUtil.getDir( userFile.getFilePath(), userFile.getFileName(), parent);
            docFilePathMapper.insert(qiwenDir);
        }
 
        fileEntryNameList.sort((a,b)->{
            return a.length() - b.length();
        });
 
        for (int i = 0; i < fileEntryNameList.size(); i++){
            String entryName = fileEntryNameList.get(i);
            //asyncTaskComp.saveUnzipFile(userFile, fileBean, unzipMode, entryName, filePath);
            this.saveUnzipFile(userFile, fileBean, unzipMode, entryName, filePath);
 
        }
    }
 
    void 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 = baseMapper.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());
                    ;
                    baseMapper.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());
            docFilePathMapper.insert(saveUserFile);
        }
 
    }
}