刘光辉
6 小时以前 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
package jnpf.dmsEntity.preview.enums;
 
import java.util.Locale;
 
/** Selects watermark handling. IMAGE candidates may still be rejected by the image codec. */
public enum DmsWatermarkTargetType {
    DOCX,
    PDF,
    IMAGE,
    UNSUPPORTED;
 
    public static DmsWatermarkTargetType fromFileName(String fileName) {
        return from(fileName, null);
    }
 
    public static DmsWatermarkTargetType from(String fileName, String mimeType) {
        String normalized = fileName == null ? "" : fileName.trim().toLowerCase(Locale.ROOT);
        int extensionSeparator = normalized.lastIndexOf('.');
        String extension = extensionSeparator < 0 || extensionSeparator == normalized.length() - 1
                ? "" : normalized.substring(extensionSeparator + 1);
        if ("docx".equals(extension)) {
            return DOCX;
        }
        if ("pdf".equals(extension)) {
            return PDF;
        }
        switch (extension) {
            case "bmp":
            case "gif":
            case "jpeg":
            case "jpg":
            case "png":
            case "webp":
                return IMAGE;
            default:
                break;
        }
        String normalizedMime = mimeType == null ? "" : mimeType.trim().toLowerCase(Locale.ROOT);
        int parameters = normalizedMime.indexOf(';');
        if (parameters >= 0) {
            normalizedMime = normalizedMime.substring(0, parameters).trim();
        }
        if ("application/vnd.openxmlformats-officedocument.wordprocessingml.document"
                .equals(normalizedMime)) {
            return DOCX;
        }
        if ("application/pdf".equals(normalizedMime)) {
            return PDF;
        }
        return normalizedMime.startsWith("image/") ? IMAGE : UNSUPPORTED;
    }
}