刘光辉
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
package jnpf.file;
 
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import jnpf.base.ActionResultCode;
import jnpf.exception.DataException;
import jnpf.model.filecontent.FileContentComparisonForm;
import java.util.HashSet;
import java.util.Set;
 
/** Compare canonical, authorized attachments. Never treats a client-supplied checksum as evidence. */
public final class FileContentEquality {
    private static final ObjectMapper JSON = new ObjectMapper();
    private FileContentEquality() {}
 
    public static boolean same(String original, Object candidate, FileContentApi api) {
        JsonNode before = files(original), after = files(candidate);
        if (before == null || after == null || before.size() != after.size()) return false;
        boolean identical = true;
        for (int i = 0; i < before.size(); i++) {
            if (!before.get(i).path("fileId").equals(after.get(i).path("fileId"))
                    || !before.get(i).path("url").equals(after.get(i).path("url"))) identical = false;
        }
        if (identical) return true; // Same immutable server object; display metadata is not content.
        if (api == null) throw new DataException("文件内容校验服务不可用,请稍后重试");
        FileContentComparisonForm form = new FileContentComparisonForm();
        form.setOriginal(before.toString()); form.setCandidate(after.toString());
        var result = api.sameContent("jnpf-dms", form);
        if (result == null || !ActionResultCode.SUCCESS.getCode().equals(result.getCode()) || result.getData() == null)
            throw new DataException("文件内容校验失败,请稍后重试");
        return result.getData();
    }
 
    private static JsonNode files(Object value) {
        try {
            JsonNode files = value instanceof String text ? JSON.readTree(text) : JSON.valueToTree(value);
            if (files == null || !files.isArray() || files.isEmpty() || files.size() > 100) return null;
            Set<String> ids = new HashSet<>();
            for (JsonNode file : files) {
                String id = file.path("fileId").asText("");
                if (id.isBlank() || file.path("url").asText("").isBlank() || !ids.add(id)) return null;
            }
            return files;
        } catch (Exception invalid) { return null; }
    }
}