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
package org.jeecg.modules.doc.vo;
 
 
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.modules.doc.exception.QiwenException;
import org.jeecg.modules.doc.util.UFOPUtils;
 
/**
 * @author MAC
 * @version 1.0
 * @description: TODO
 * @date 2022/4/21 12:08
 */
public class QiwenFile {
 
    private final String path;
    public static final String separator = "/";
    private boolean isDirectory;
 
    public QiwenFile(String pathname, boolean isDirectory) {
        if (StringUtils.isEmpty(pathname)) {
            throw new QiwenException("file name format error,pathname:" + pathname);
        }
        this.path = formatPath(pathname);
        this.isDirectory = isDirectory;
    }
 
    public QiwenFile(String parent, String child, boolean isDirectory) {
        if (StringUtils.isEmpty(child)) {
            throw new QiwenException("file name format error,parent:" + parent +", child:" + child);
        }
        if (parent != null) {
            String parentPath = separator.equals(formatPath(parent)) ? "" : formatPath(parent);
            String childPath = formatPath(child);
            if (childPath.startsWith(separator)) {
                childPath = childPath.replaceFirst(separator, "");
            }
            this.path = parentPath + separator + childPath;
        } else {
            this.path = formatPath(child);
        }
        this.isDirectory = isDirectory;
    }
 
    public static String formatPath(String path) {
        path = UFOPUtils.pathSplitFormat(path);
        if ("/".equals(path)) {
            return path;
        }
        if (path.endsWith("/")) {
            int length = path.length();
            return path.substring(0, length - 1);
        }
 
        return path;
    }
 
    public String getParent() {
        if (separator.equals(this.path)) {
            return null;
        }
        if (!this.path.contains("/")) {
            return null;
        }
        int index = path.lastIndexOf(separator);
        if (index == 0) {
            return separator;
        }
        return path.substring(0, index);
    }
 
    public QiwenFile getParentFile() {
        String parentPath = this.getParent();
        return new QiwenFile(parentPath, true);
    }
 
    public String getName() {
        int index = path.lastIndexOf(separator);
        if (!path.contains(separator)) {
            return path;
        }
        return path.substring(index + 1);
    }
 
    public String getExtendName() {
        if (isDirectory) {
            return null;
        }
        return FilenameUtils.getExtension(getName());
    }
 
    public String getNameNotExtend() {
        if (isDirectory) {
            return getName();
        } else {
            return FilenameUtils.removeExtension(getName());
        }
 
    }
 
    public String getPath() {
        return path;
    }
 
    public boolean isDirectory() {
       return isDirectory;
    }
 
    public boolean isFile() {
        return !isDirectory;
    }
 
 
}