刘光辉
昨天 bb638871a7fb692d80f1b7a758f991dc0879002c
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
package jnpf.fileStorage.dms;
 
import jnpf.base.ActionResult;
import jnpf.base.ActionResultCode;
import jnpf.config.ConfigValueUtil;
import jnpf.constant.MsgCode;
import jnpf.dms.DmsResourceApi;
import jnpf.dmsEntity.storage.form.DmsFileRevisionRegisterForm;
import jnpf.dmsEntity.storage.form.DmsFileUploadRegisterForm;
import jnpf.dmsEntity.storage.vo.DmsFileUploadVO;
import jnpf.exception.DataException;
import jnpf.util.OptimizeUtil;
import jnpf.util.StringUtil;
import jnpf.util.UpUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.x.file.storage.core.FileInfo;
import org.dromara.x.file.storage.core.FileStorageService;
import org.dromara.x.file.storage.core.hash.HashInfo;
import org.dromara.x.file.storage.core.platform.MinioFileStorage;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
 
import java.nio.file.Paths;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Objects;
import java.util.UUID;
 
@Slf4j
@Service
@RequiredArgsConstructor
public class DmsFileStorageService {
    static final String STORAGE_PLATFORM = "seaweedfs-1";
    static final String SERVICE_ID = "jnpf-file";
 
    private final FileStorageService fileStorageService;
    private final DmsResourceApi dmsResourceApi;
    private final ConfigValueUtil configValueUtil;
 
    public DmsFileUploadVO upload(UUID folderId, String sourceId, String documentNo,
                                  String documentName, String documentType,
                                  String responsibleDeptId, String versionLabel,
                                  String preparedBy, LocalDate preparedOn, MultipartFile file) {
        validate(sourceId, documentNo, documentName, documentType, responsibleDeptId,
                versionLabel, preparedBy, preparedOn, file);
        String originalFilename = safeFilename(file.getOriginalFilename());
        String extension = UpUtil.getFileType(file);
        if (!OptimizeUtil.fileType(configValueUtil.getAllowUploadFileType(), extension)) {
            throw new DataException(MsgCode.FA017.get());
        }
 
        UUID documentId = UuidV7Generator.generate();
        UUID fileVersionId = UuidV7Generator.generate();
        UUID fileObjectId = UuidV7Generator.generate();
        String objectPath = objectPath(documentId);
        String objectName = fileVersionId + (StringUtil.isEmpty(extension) ? "" : "." + extension);
 
        FileInfo stored = fileStorageService.of(file)
                .setPlatform(STORAGE_PLATFORM)
                .setPath(objectPath)
                .setSaveFilename(objectName)
                .setOriginalFilename(originalFilename)
                .setHashCalculatorSha256()
                .upload();
        if (stored == null || stored.getHashInfo() == null
                || StringUtil.isEmpty(stored.getHashInfo().getSha256())) {
            deleteQuietly(stored);
            throw new DataException("SeaweedFS 上传失败或未生成 SHA-256");
        }
 
        DmsFileUploadRegisterForm form = registration(folderId, sourceId, documentNo,
                documentName, documentType, responsibleDeptId, versionLabel, preparedBy,
                preparedOn, originalFilename, documentId, fileVersionId, fileObjectId, stored);
        try {
            ActionResult<DmsFileUploadVO> result = dmsResourceApi.registerUpload(SERVICE_ID, form);
            if (result == null || !ActionResultCode.SUCCESS.getCode().equals(result.getCode())
                    || result.getData() == null) {
                throw new DataException(result == null ? "DMS 上传登记无响应" : result.getMsg());
            }
            if (!sameStoredObject(result.getData(), stored)) {
                deleteQuietly(stored);
            }
            return result.getData();
        } catch (RuntimeException exception) {
            deleteQuietly(stored);
            throw exception;
        }
    }
 
    public DmsFileUploadVO revise(UUID documentId, UUID baseFileVersionId, String sourceId,
                                  String versionLabel, String preparedBy, LocalDate preparedOn,
                                  String revisionReason, MultipartFile file) {
        validateRevision(documentId, baseFileVersionId, sourceId, versionLabel, preparedBy,
                preparedOn, revisionReason, file);
        String originalFilename = safeFilename(file.getOriginalFilename());
        String extension = UpUtil.getFileType(file);
        if (!OptimizeUtil.fileType(configValueUtil.getAllowUploadFileType(), extension)) {
            throw new DataException(MsgCode.FA017.get());
        }
 
        UUID fileVersionId = UuidV7Generator.generate();
        UUID fileObjectId = UuidV7Generator.generate();
        String objectPath = objectPath(documentId);
        String objectName = fileVersionId + (StringUtil.isEmpty(extension) ? "" : "." + extension);
 
        FileInfo stored = fileStorageService.of(file)
                .setPlatform(STORAGE_PLATFORM)
                .setPath(objectPath)
                .setSaveFilename(objectName)
                .setOriginalFilename(originalFilename)
                .setHashCalculatorSha256()
                .upload();
        if (stored == null || stored.getHashInfo() == null
                || StringUtil.isEmpty(stored.getHashInfo().getSha256())) {
            deleteQuietly(stored);
            throw new DataException("SeaweedFS 上传失败或未生成 SHA-256");
        }
 
        DmsFileRevisionRegisterForm form = revisionRegistration(documentId, baseFileVersionId,
                fileVersionId, fileObjectId, sourceId, versionLabel, preparedBy, preparedOn,
                revisionReason, originalFilename, stored);
        try {
            ActionResult<DmsFileUploadVO> result = dmsResourceApi.registerRevision(SERVICE_ID, form);
            if (result == null || !ActionResultCode.SUCCESS.getCode().equals(result.getCode())
                    || result.getData() == null) {
                throw new DataException(result == null ? "DMS 修订登记无响应" : result.getMsg());
            }
            if (!sameStoredObject(result.getData(), stored)) {
                deleteQuietly(stored);
            }
            return result.getData();
        } catch (RuntimeException exception) {
            deleteQuietly(stored);
            throw exception;
        }
    }
 
    private DmsFileUploadRegisterForm registration(UUID folderId, String sourceId,
                                                   String documentNo, String documentName,
                                                   String documentType, String responsibleDeptId,
                                                   String versionLabel, String preparedBy,
                                                   LocalDate preparedOn, String originalFilename,
                                                   UUID documentId,
                                                   UUID fileVersionId, UUID fileObjectId,
                                                   FileInfo stored) {
        MinioFileStorage storage = fileStorageService.getFileStorage(STORAGE_PLATFORM);
        HashInfo hashInfo = stored.getHashInfo();
        DmsFileUploadRegisterForm form = new DmsFileUploadRegisterForm();
        form.setDocumentId(documentId);
        form.setFileVersionId(fileVersionId);
        form.setFileObjectId(fileObjectId);
        form.setFolderId(folderId);
        form.setDocumentNo(documentNo.trim());
        form.setDocumentName(documentName.trim());
        form.setDocumentType(documentType.trim());
        form.setResponsibleDeptId(responsibleDeptId.trim());
        form.setVersionLabel(versionLabel.trim());
        form.setFileName(originalFilename);
        form.setMimeType(trimToNull(stored.getContentType()));
        form.setSizeBytes(stored.getSize());
        form.setChecksumSha256(hashInfo.getSha256());
        form.setPreparedBy(preparedBy.trim());
        form.setPreparedOn(preparedOn);
        form.setStoragePlatform(stored.getPlatform());
        form.setBucketName(storage.getBucketName());
        form.setBasePath(trimToEmpty(stored.getBasePath()));
        form.setObjectPath(stored.getPath());
        form.setObjectName(stored.getFilename());
        form.setSourceId(sourceId.trim());
        return form;
    }
 
    private DmsFileRevisionRegisterForm revisionRegistration(UUID documentId,
                                                              UUID baseFileVersionId,
                                                              UUID fileVersionId,
                                                              UUID fileObjectId,
                                                              String sourceId,
                                                              String versionLabel,
                                                              String preparedBy,
                                                              LocalDate preparedOn,
                                                              String revisionReason,
                                                              String originalFilename,
                                                              FileInfo stored) {
        MinioFileStorage storage = fileStorageService.getFileStorage(STORAGE_PLATFORM);
        HashInfo hashInfo = stored.getHashInfo();
        DmsFileRevisionRegisterForm form = new DmsFileRevisionRegisterForm();
        form.setDocumentId(documentId);
        form.setBaseFileVersionId(baseFileVersionId);
        form.setFileVersionId(fileVersionId);
        form.setFileObjectId(fileObjectId);
        form.setVersionLabel(versionLabel.trim());
        form.setFileName(originalFilename);
        form.setMimeType(trimToNull(stored.getContentType()));
        form.setSizeBytes(stored.getSize());
        form.setChecksumSha256(hashInfo.getSha256());
        form.setPreparedBy(preparedBy.trim());
        form.setPreparedOn(preparedOn);
        form.setRevisionReason(revisionReason.trim());
        form.setStoragePlatform(stored.getPlatform());
        form.setBucketName(storage.getBucketName());
        form.setBasePath(trimToEmpty(stored.getBasePath()));
        form.setObjectPath(stored.getPath());
        form.setObjectName(stored.getFilename());
        form.setSourceId(sourceId.trim());
        return form;
    }
 
    private boolean sameStoredObject(DmsFileUploadVO registered, FileInfo stored) {
        return Objects.equals(registered.getStoragePlatform(), stored.getPlatform())
                && Objects.equals(registered.getBasePath(), trimToEmpty(stored.getBasePath()))
                && Objects.equals(registered.getObjectPath(), stored.getPath())
                && Objects.equals(registered.getObjectName(), stored.getFilename());
    }
 
    private String objectPath(UUID documentId) {
        return LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy/MM/dd/"))
                + documentId + "/";
    }
 
    private String safeFilename(String value) {
        if (value == null || value.trim().isEmpty()) {
            throw new DataException("文件名不能为空");
        }
        String normalized = value.replace('\\', '/');
        String filename = Paths.get(normalized).getFileName().toString().trim();
        if (filename.isEmpty() || filename.length() > 300) {
            throw new DataException("文件名长度必须在 1 到 300 之间");
        }
        return filename;
    }
 
    private void validate(String sourceId, String documentNo, String documentName,
                          String documentType, String responsibleDeptId, String versionLabel,
                          String preparedBy, LocalDate preparedOn, MultipartFile file) {
        if (file == null || file.isEmpty()) {
            throw new DataException("上传文件不能为空");
        }
        requireText(sourceId, "sourceId", 160);
        requireText(documentNo, "文件编号", 200);
        requireText(documentName, "文件名称", 300);
        requireText(documentType, "文件类型", 100);
        requireText(responsibleDeptId, "归属部门", 100);
        requireText(versionLabel, "版本号", 50);
        requireText(preparedBy, "制定人", 100);
        if (preparedOn == null) {
            throw new DataException("制定日期不能为空");
        }
        fileStorageService.getFileStorageVerify(STORAGE_PLATFORM);
    }
 
    private void validateRevision(UUID documentId, UUID baseFileVersionId, String sourceId,
                                  String versionLabel, String preparedBy, LocalDate preparedOn,
                                  String revisionReason, MultipartFile file) {
        if (documentId == null || baseFileVersionId == null) {
            throw new DataException("文件及当前版本不能为空");
        }
        if (file == null || file.isEmpty()) {
            throw new DataException("修订文件不能为空");
        }
        requireText(sourceId, "sourceId", 120);
        requireText(versionLabel, "新版本号", 50);
        requireText(preparedBy, "制定人", 100);
        requireText(revisionReason, "修订原因", 1000);
        if (preparedOn == null) {
            throw new DataException("制定日期不能为空");
        }
        fileStorageService.getFileStorageVerify(STORAGE_PLATFORM);
    }
 
    private void requireText(String value, String label, int maxLength) {
        if (value == null || value.trim().isEmpty() || value.trim().length() > maxLength) {
            throw new DataException(label + "长度必须在 1 到 " + maxLength + " 之间");
        }
    }
 
    private String trimToEmpty(String value) {
        return value == null ? "" : value.trim();
    }
 
    private String trimToNull(String value) {
        return value == null || value.trim().isEmpty() ? null : value.trim();
    }
 
    private void deleteQuietly(FileInfo stored) {
        if (stored == null) {
            return;
        }
        try {
            if (!fileStorageService.delete(stored)) {
                log.error("DMS 上传补偿删除失败: platform={}, path={}, filename={}",
                        stored.getPlatform(), stored.getPath(), stored.getFilename());
            }
        } catch (Exception exception) {
            log.error("DMS 上传补偿删除异常: platform={}, path={}, filename={}",
                    stored.getPlatform(), stored.getPath(), stored.getFilename(), exception);
        }
    }
}