刘光辉
3 小时以前 06f314545ac3514d7ce28939920866ae8188d31c
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
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.Locale;
import java.util.Map;
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;
 
    @jnpf.audit.sdk.annotation.AuditLog(action = "DMS_WORKFLOW_CONTENT_STORE", bizCodeExpr = "''")
    public DmsFileUploadRegisterForm storeWorkflowContent(UUID documentId, UUID fileVersionId, MultipartFile file) {
        UUID fileObjectId = UuidV7Generator.generate();
        String originalFilename = safeFilename(file.getOriginalFilename());
        String extension = UpUtil.getFileType(file);
        if (file.isEmpty() || !OptimizeUtil.fileType(configValueUtil.getAllowUploadFileType(), extension)) {
            throw new DataException("批准文件为空或类型不允许");
        }
        FileInfo stored = fileStorageService.of(file).setPlatform(STORAGE_PLATFORM)
                .setPath(objectPath(documentId))
                // Repeated delivery must never overwrite bytes already registered for this version.
                .setSaveFilename(fileObjectId + (StringUtil.isEmpty(extension) ? "" : "." + extension))
                .setOriginalFilename(originalFilename).setHashCalculatorSha256().upload();
        if (stored == null || stored.getHashInfo() == null || StringUtil.isEmpty(stored.getHashInfo().getSha256())) {
            deleteQuietly(stored);
            throw new DataException("SeaweedFS 上传失败或未生成 SHA-256");
        }
        MinioFileStorage storage = fileStorageService.getFileStorage(STORAGE_PLATFORM);
        DmsFileUploadRegisterForm result = new DmsFileUploadRegisterForm();
        result.setDocumentId(documentId);
        result.setFileVersionId(fileVersionId);
        result.setFileObjectId(fileObjectId);
        result.setFileName(originalFilename);
        result.setDocumentType(StringUtil.isEmpty(extension) ? "FILE" : extension.toUpperCase(Locale.ROOT));
        result.setMimeType(trimToNull(stored.getContentType()));
        result.setSizeBytes(stored.getSize());
        result.setChecksumSha256(stored.getHashInfo().getSha256());
        result.setStoragePlatform(stored.getPlatform());
        result.setBucketName(storage.getBucketName());
        result.setBasePath(trimToEmpty(stored.getBasePath()));
        result.setObjectPath(stored.getPath());
        result.setObjectName(stored.getFilename());
        return result;
    }
 
    public DmsFileUploadVO upload(UUID folderId, String sourceId, String documentNo,
                                  String documentName, String documentType,
                                  String responsibleDeptId, String versionLabel,
                                  String preparedBy, LocalDate preparedOn,
                                  Map<String, Object> auditSignParameters, String auditBizSign,
                                  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,
                auditSignParameters, stored);
        try {
            ActionResult<DmsFileUploadVO> result = dmsResourceApi.registerUpload(SERVICE_ID, auditBizSign, form);
            requireRegistrationResult(result, stored);
            if (!sameStoredObject(result.getData(), stored)) {
                deleteQuietly(stored);
            }
            return result.getData();
        } catch (RuntimeException exception) {
            logRegistrationFailure(stored, sourceId, exception);
            throw exception;
        }
    }
 
    public DmsFileUploadVO revise(UUID documentId, UUID baseFileVersionId, String sourceId,
                                  String versionLabel, String preparedBy, LocalDate preparedOn,
                                  String revisionReason, Map<String, Object> auditSignParameters,
                                  String auditBizSign, 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, auditSignParameters, stored);
        try {
            ActionResult<DmsFileUploadVO> result = dmsResourceApi.registerRevision(SERVICE_ID, auditBizSign, form);
            requireRegistrationResult(result, stored);
            if (!sameStoredObject(result.getData(), stored)) {
                deleteQuietly(stored);
            }
            return result.getData();
        } catch (RuntimeException exception) {
            logRegistrationFailure(stored, sourceId, exception);
            throw exception;
        }
    }
 
    private void requireRegistrationResult(ActionResult<DmsFileUploadVO> result, FileInfo stored) {
        if (result == null || result.getCode() == null) {
            throw new DataException("DMS登记结果未知,保留上传对象,请使用原请求重试");
        }
        if (!ActionResultCode.SUCCESS.getCode().equals(result.getCode())) {
            // 只对明确的请求拒绝补偿;服务端错误、超时和缺失数据不能证明事务未提交。
            if (result.getCode() >= 400 && result.getCode() < 500) {
                deleteQuietly(stored);
            }
            throw new DataException(result.getMsg());
        }
        if (result.getData() == null) {
            throw new DataException("DMS登记响应缺少数据,保留上传对象,请使用原请求重试");
        }
    }
 
    private void logRegistrationFailure(FileInfo stored, String sourceId, RuntimeException exception) {
        log.error("DMS登记未正常返回,需要核对登记与对象状态: sourceId={}, platform={}, path={}, filename={}",
                sourceId, stored.getPlatform(), stored.getPath(), stored.getFilename(), 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,
                                                   Map<String, Object> auditSignParameters,
                                                   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());
        form.setAuditSignParameters(auditSignParameters);
        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,
                                                              Map<String, Object> auditSignParameters,
                                                              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());
        form.setAuditSignParameters(auditSignParameters);
        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);
        }
    }
}