IT-KIMI_SHI\SINOIT.KIMI
2018-06-04 b12dfac6e495d6cf38df6b7e5222191f59762900
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
package frame.file.repositoty;
 
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
 
import frame.file.UploadResult;
import frame.util.Util;
 
 
public class Repository {
 
    private String name;
    private String root;
    private Expression pathRule;
    private DataPool datapool;
    
    
    public Repository(String name, String root) throws Exception {
        this.name = name;
        this.root = parsePath(root);
    }
    
    public void setPath(String path) throws Exception {
        pathRule = new Expression(path);
    }
    
    public OutputStream getStream(String id) throws Exception {
        Entity entity = DataHandler.getLine("filerepository", id);
        
        if (entity == null) {
            throw new Exception("file not exists: " + id);
        }
        
        String internalPath = entity.getString("path");
        String path = Util.joinPath(root, internalPath);
        
        File file = new File(path);
        
        if (!file.exists()) {
            throw new Exception("file not exists: " + path);
        }
        
        return new FileOutputStream(file);
    }
    
    public String saveFileToAttachmentPath(List<FileItem> fileList, VariantRequestParams params) throws Exception {
        List<UploadResult> result = new ArrayList<UploadResult>();
        
        Expression expression = pathRule.newInstance();
        VariantContext context = new VariantContext();
        context.setParametersTo(expression, params);
        
        String internalPath = expression.getString();
        String path = Util.joinPath(root, internalPath);
        
        File pathfile = new File(path);
        if (!pathfile.exists()) {
            pathfile.mkdirs();
        }
        
        String code = Util.newShortGUID();
        
        for (FileItem item: fileList) {
            UploadResult uploadResult = doSaveOneFile(path, internalPath, code, item);
            result.add(uploadResult);
        }
        
        return path;
    }
    
    public List<UploadResult> saveFile(List<FileItem> fileList, VariantRequestParams params) throws Exception {
        List<UploadResult> result = new ArrayList<UploadResult>();
        
        Expression expression = pathRule.newInstance();
        VariantContext context = new VariantContext();
        context.setParametersTo(expression, params);
        
        String internalPath = expression.getString();
        String path = Util.joinPath(root, internalPath);
        
        File pathfile = new File(path);
        if (!pathfile.exists()) {
            pathfile.mkdirs();
        }
        
        String code = Util.newShortGUID();
        
        for (FileItem item: fileList) {
            UploadResult uploadResult = doSaveOneFile(path, internalPath, code, item);
            result.add(uploadResult);
        }
        
        return result;
    }
    
    public UploadResult saveFile(FileItem item) throws Exception {
        //1. path
        Expression expression = pathRule.newInstance();
        String internalPath = expression.toString();
        
        String path = Util.joinPath(root, internalPath);
        
        File pathfile = new File(path);
        if (!pathfile.exists()) {
            pathfile.mkdirs();
        }
        
        String code = Util.newShortGUID();
        
        UploadResult uploadResult = doSaveOneFile(path, internalPath, code, item);
        
        return uploadResult;
    }
    
    private UploadResult doSaveOneFile(String path, String internalPath, String filecode, FileItem item) throws Exception {
        String itemName = item.getName().substring(item.getName().lastIndexOf("\\") == -1 ? 0 : item.getName().lastIndexOf("\\") + 1);
        
        String internalFile = Util.joinPath(internalPath, itemName);
        File file = new File(root, internalFile);
        
        if (file.exists()) {
            file.delete();
        }
            
        file.createNewFile();
        item.write(file);
        
/*        Entity entity = new Entity("filerepository");
        entity.set("id", Util.newShortGUID());
        entity.set("typecode", "file");
        entity.set("filecode", filecode);
        entity.set("path", internalFile);
        entity.insert();*/
        
        UploadResult result = new UploadResult(filecode);
        result.setPath(path);
        result.setTempPath(path);
        
        EventListener.fireRepositoryEvent("aftersave", result, datapool);
        
        return result;
    }
 
    private String parsePath(String value) throws Exception {
        Expression expression = new Expression(value);
        
        for (VariantSegment variant: expression) {
            String name = variant.getName();
            int pos = value.indexOf("(");
            
            if (pos > 0) {
                String realname = name.substring(0, pos);
                String format = name.substring(pos + 1, name.length() - 1);
                
                VariantRequestParams params = new VariantRequestParams();
                params.addParam("format", format);
                
                GlobalVariant.getStringValue(realname, params);
            }
            else {
                GlobalVariant.getStringValue(name, null);
            }
        }
        
        return expression.toString();
    }
 
    public String getName() {
        return name;
    }
 
    public String getRoot() {
        return root;
    }
 
}