Explorar el Código

--commit editor service

jiapeng %!s(int64=8) %!d(string=hace) años
padre
commit
4a395f5180

+ 273 - 0
src/main/java/com/ekexiu/portal/editor/Controller.java

@ -0,0 +1,273 @@
1
package com.ekexiu.portal.editor;
2

3
import java.io.BufferedInputStream;
4
import java.io.BufferedOutputStream;
5
import java.io.File;
6
import java.io.FileOutputStream;
7
import java.io.IOException;
8
import java.io.InputStream;
9
import java.io.Writer;
10
import java.util.HashMap;
11
import java.util.List;
12
import java.util.Map;
13

14
import javax.servlet.http.HttpServletRequest;
15
import javax.servlet.http.HttpServletResponse;
16

17
import org.jfw.apt.annotation.Autowrie;
18
import org.jfw.apt.annotation.Bean;
19
import org.jfw.util.ConstData;
20
import org.jfw.util.json.JsonService;
21
import org.jfw.util.web.fileupload.Item;
22
import org.jfw.util.web.fileupload.UploadItemIterator;
23
import org.jfw.util.web.fileupload.impl.UploadItemIteratorImpl;
24

25
import com.ekexiu.portal.editor.define.BaseState;
26
import com.ekexiu.portal.editor.define.State;
27

28
@Bean
29
public class Controller {
30

31
    @Autowrie
32
    private EditorConfig config;
33

34
    public EditorConfig getConfig() {
35
        return config;
36
    }
37

38
    public void setConfig(EditorConfig config) {
39
        this.config = config;
40
    }
41

42
    public void service(HttpServletRequest req, HttpServletResponse res, int _viewType) throws javax.servlet.ServletException, java.io.IOException {
43
        req.setCharacterEncoding("utf-8");
44
        res.setHeader("Content-Type", "text/html");
45
        String callbackName = req.getParameter("callback");
46
        String ret = null;
47
        if (callbackName != null) {
48
            if (!validCallbackName(callbackName)) {
49
                ret = new BaseState(false, 401).toJSONString();
50
            } else {
51
                ret = callbackName + "(" + invoke(req) + ");";
52
            }
53
        } else {
54
            ret = invoke(req);
55
        }
56
        Writer out = res.getWriter();
57
        try {
58
            out.write(ret);
59
        } finally {
60
            out.close();
61
        }
62
    }
63

64
    public String invoke(HttpServletRequest req) {
65
        String actionType = req.getParameter("action");
66
        if ((actionType == null) || (!mapping.containsKey(actionType))) {
67
            return new BaseState(false, 101).toJSONString();
68
        }
69

70
        State state = new BaseState(false, 101);
71

72
        int actionCode = getType(actionType);
73

74
        switch (actionCode) {
75
        case 0:
76

77
            return JsonService.toJson(this.config.getAllConfig());
78
        case 1:
79
        case 2:
80
        case 3:
81
        case 4:
82
            state = this.upload(req, actionCode);
83
            break;
84

85
        }
86
        return state.toJSONString();
87
    }
88

89
    public boolean validCallbackName(String name) {
90
        if (name.matches("^[a-zA-Z_]+[\\w0-9_]*$")) {
91
            return true;
92
        }
93
        return false;
94
    }
95

96
    private State upload(HttpServletRequest req, int actionCode) {
97
        if (2 == actionCode) {
98
            return this.uploadBase64(req);
99
        }
100
        boolean isAjaxUpload = req.getHeader("X_Requested_With") != null;
101
        if (!UploadItemIteratorImpl.isMultipartContent(req)) {
102
            return new BaseState(false, 5);
103
        }
104
        try {
105
            if (isAjaxUpload) {
106
                req.setCharacterEncoding("UTF-8");
107
            }
108
            String savePath = this.config.getImagePathFormat();
109
            long maxSize = this.config.getImageMaxSize();
110
            String afs = this.config.getImageAllowFiles();
111
            if (actionCode == 3) {
112
                savePath = this.config.getVideoPathFormat();
113
                maxSize = this.config.getVideoMaxSize();
114
                afs = this.config.getVideoAllowFiles();
115
            }
116
            if (actionCode == 4) {
117
                savePath = this.config.getFilePathFormat();
118
                maxSize = this.config.getFileMaxSize();
119
                afs = this.config.getFileAllowFiles();
120
            }
121
            List<String> afList = this.config.addDot(this.config.from(afs));
122

123
            UploadItemIterator it = UploadItemIteratorImpl.build(req);
124
            try {
125
                while (it.hasNext()) {
126
                    Item item = it.next();
127
                    if (!item.isFormField()) {
128
                        String filename = item.getName();
129
                        String suffix = filename.substring(filename.lastIndexOf(".")).toLowerCase();
130
                        if (!afList.contains(suffix)) {
131
                            return new BaseState(false, 8);
132
                        }
133
                        filename = filename.substring(0, filename.length() - suffix.length());
134
                        savePath = savePath + suffix;
135
                        savePath = PathFormat.parse(savePath, filename);
136
                        State storageState = this.saveFileByInputStream(item.getInputStream(), savePath, maxSize);
137
                        if (storageState.isSuccess()) {
138
                            storageState.putInfo("url", PathFormat.format(savePath));
139
                            storageState.putInfo("type", suffix);
140
                            storageState.putInfo("original", filename + suffix);
141
                        }
142
                        return storageState;
143
                    }
144
                }
145
                return new BaseState(false, 7);
146
            } finally {
147
                it.clean();
148
            }
149
        } catch (IOException e) {
150
        }
151

152
        return new BaseState(false, 4);
153
    }
154

155
    private static boolean validSize(byte[] data, long length) {
156
        return data.length <= length;
157
    }
158

159
    private State uploadBase64(HttpServletRequest req) {
160
        String fn = this.config.getScrawlFieldName();
161
        String content = req.getParameter(fn);
162
        byte[] data = org.jfw.apt.util.Base64.decodeBase64(content.getBytes(ConstData.UTF8));
163

164
        long maxSize = this.config.getScrawlMaxSize();
165
        if (!validSize(data, maxSize)) {
166
            return new BaseState(false, 1);
167
        }
168
        String suffix = ".jpg";
169

170
        String savePath = PathFormat.parse(this.config.getScrawlPathFormat(), "scrawl");
171

172
        savePath = savePath + suffix;
173
        State storageState = saveBinaryFile(data, savePath);
174
        if (storageState.isSuccess()) {
175
            storageState.putInfo("url", PathFormat.format(savePath));
176
            storageState.putInfo("type", suffix);
177
            storageState.putInfo("original", "");
178
        }
179
        return storageState;
180

181
    }
182

183
    public static int getType(String key) {
184
        return ((Integer) mapping.get(key)).intValue();
185
    }
186

187
    public static final int BUFFER_SIZE = 8192;
188

189
    public State saveBinaryFile(byte[] data, String path) {
190
        File file = new File(this.config.getRootPath(), path);
191

192
        State state = valid(file);
193
        if (!state.isSuccess()) {
194
            return state;
195
        }
196
        try {
197
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
198
            bos.write(data);
199
            bos.flush();
200
            bos.close();
201
        } catch (IOException ioe) {
202
            return new BaseState(false, 4);
203
        }
204
        state = new BaseState(true, file.getAbsolutePath());
205
        state.putInfo("size", data.length);
206
        state.putInfo("title", file.getName());
207
        return state;
208
    }
209

210
    public State saveFileByInputStream(InputStream is, String path, long maxSize) {
211
        State state = null;
212
        File file = new File(this.config.getRootPath(), path);
213
        if (file.exists()) {
214
            return new BaseState(false, 2);
215
        }
216
        File parent = file.getParentFile();
217
        if(!parent.exists()){
218
            if(!parent.mkdirs())return new BaseState(false, 3);
219
        }
220

221
        byte[] dataBuf = new byte[4096];
222
        BufferedInputStream bis = new BufferedInputStream(is, 8192);
223
        try {
224
            try {
225
                BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file), 8192);
226

227
                int count = 0;
228
                while ((count = bis.read(dataBuf)) != -1) {
229
                    bos.write(dataBuf, 0, count);
230
                }
231
                bos.flush();
232
                bos.close();
233
                if (file.length() > maxSize) {
234
                    file.delete();
235
                    return new BaseState(false, 1);
236
                }
237
                state = new BaseState(true);
238
                state.putInfo("size", file.length());
239
                state.putInfo("title", file.getName());
240
                return state;
241
            } finally {
242
                is.close();
243
            }
244
        } catch (IOException localIOException) {
245
        }
246
        return new BaseState(false, 4);
247
    }
248

249
    private static State valid(File file) {
250
        File parentPath = file.getParentFile();
251
        if ((!parentPath.exists()) && (!parentPath.mkdirs())) {
252
            return new BaseState(false, 3);
253
        }
254
        if (!parentPath.canWrite()) {
255
            return new BaseState(false, 2);
256
        }
257
        return new BaseState(true);
258
    }
259

260
    public static final Map<String, Integer> mapping = new HashMap<String, Integer>();
261
    public static final int CONFIG = 0;
262
    public static final int UPLOAD_IMAGE = 1;
263
    public static final int UPLOAD_SCRAWL = 2;
264
    public static final int UPLOAD_VIDEO = 3;
265
    public static final int UPLOAD_FILE = 4;
266
    static {
267
        mapping.put("config", CONFIG);
268
        mapping.put("uploadimage", UPLOAD_IMAGE);
269
        mapping.put("uploadscrawl", UPLOAD_SCRAWL);
270
        mapping.put("uploadvideo", UPLOAD_VIDEO);
271
        mapping.put("uploadfile", UPLOAD_FILE);
272
    }
273
}

+ 656 - 0
src/main/java/com/ekexiu/portal/editor/EditorConfig.java

@ -0,0 +1,656 @@
1
package com.ekexiu.portal.editor;
2

3
import java.io.File;
4
import java.util.HashMap;
5
import java.util.LinkedList;
6
import java.util.List;
7
import java.util.Map;
8

9
import org.jfw.apt.annotation.Bean;
10
import org.jfw.util.ListUtil;
11
@Bean
12
public class EditorConfig {
13
    private File rootPath;
14
    
15
    /* 执行上传图片的action名称 */
16
    private String imageActionName = "uploadimage";
17
    /* 提交的图片表单名称 */
18
    private String imageFieldName = "upfile";
19
    /* 上传大小限制,单位B */
20
    private long imageMaxSize = 2048000;
21
    /* 上传图片格式显示 */
22
    private String imageAllowFiles = "png,jpg,jpeg,gif,bmp";
23
    /* 是否压缩图片,默认是true */
24
    private boolean imageCompressEnable = true;
25
    /* 图片压缩最长边限制 */
26
    private long imageCompressBorder = 1600;
27
    /* 插入的图片浮动方式 */
28
    private String imageInsertAlign = "none";
29
    /* 图片访问路径前缀 */
30
    private String imageUrlPrefix = "";
31
    /* 上传保存路径,可以自定义保存路径和文件名格式 */
32
    /* {filename} 会替换成原文件名,配置这项需要注意中文乱码问题 */
33
    /* {rand:6} 会替换成随机数,后面的数字是随机数的位数 */
34
    /* {time} 会替换成时间戳 */
35
    /* {yyyy} 会替换成四位年份 */
36
    /* {yy} 会替换成两位年份 */
37
    /* {mm} 会替换成两位月份 */
38
    /* {dd} 会替换成两位日期 */
39
    /* {hh} 会替换成两位小时 */
40
    /* {ii} 会替换成两位分钟 */
41
    /* {ss} 会替换成两位秒 */
42
    /* 非法字符 \ : * ? " < > | */
43
    /* 具请体看线上文档: fexbaiducom/ueditor/#use-format_upload_filename */
44

45
    private String imagePathFormat = "/data/editor/image/{yyyy}{mm}{dd}/{time}{rand:6}";
46
    /* 涂鸦图片上传配置项 */
47
    /* 执行上传涂鸦的action名称 */
48
    private String scrawlActionName = "uploadscrawl";
49
    /* 提交的图片表单名称 */
50
    private String scrawlFieldName = "upfile";
51
    /* 上传保存路径,可以自定义保存路径和文件名格式 */
52
    private String scrawlPathFormat = "/data/editor/image/{yyyy}{mm}{dd}/{time}{rand:6}";
53
    /* 上传大小限制,单位B */
54
    private long scrawlMaxSize = 2048000;
55
    private String scrawlUrlPrefix = "";
56
    /* 图片访问路径前缀 */
57
    private String scrawlInsertAlign = "none";
58

59
    /* 截图工具上传 */
60
    /* 执行上传截图的action名称 */
61
    private String snapscreenActionName = "uploadimage";
62
    /* 上传保存路径,可以自定义保存路径和文件名格式 */
63
    private String snapscreenPathFormat = "/data/editor/image/{yyyy}{mm}{dd}/{time}{rand:6}";
64
    /* 图片访问路径前缀 */
65
    private String snapscreenUrlPrefix = "";
66
    /* 插入的图片浮动方式 */
67
    private String snapscreenInsertAlign = "none";
68

69
    /* 抓取远程图片配置 */
70
    private String catcherLocalDomain = "127001,localhost,imgbaiducom";
71
    /* 执行抓取远程图片的action名称 */
72
    private String catcherActionName = "catchimage";
73
    /* 提交的图片列表表单名称 */
74
    private String catcherFieldName = "source";
75
    /* 上传保存路径,可以自定义保存路径和文件名格式 */
76
    private String catcherPathFormat = "/data/editor/image/{yyyy}{mm}{dd}/{time}{rand:6}";
77
    /* 图片访问路径前缀 */
78
    private String catcherUrlPrefix = "";
79
    /* 上传大小限制,单位B */
80
    private long catcherMaxSize = 2048000;
81
    /* 抓取图片格式显示 */
82
    private String catcherAllowFiles = "png,jpg,jpeg,gif,bmp";
83

84
    /* 上传视频配置 */
85
    /* 执行上传视频的action名称 */
86
    private String videoActionName = "uploadvideo";
87
    /* 提交的视频表单名称 */
88
    private String videoFieldName = "upfile";
89
    /* 上传保存路径,可以自定义保存路径和文件名格式 */
90
    private String videoPathFormat = "/data/editor/video/{yyyy}{mm}{dd}/{time}{rand:6}";
91
    /* 视频访问路径前缀 */
92
    private String videoUrlPrefix = "";
93
    /* 上传大小限制,单位B,默认100MB */
94
    private long videoMaxSize = 102400000;
95
    /* 上传视频格式显示 */
96
    private String videoAllowFiles = "flv,swf,mkv,avi,rm,rmvb,mpeg,mpg,ogg,ogv,mov,wmv,mp4,webm,mp3,wav,mid";
97

98
    /* 上传文件配置 */
99
    /* controller里,执行上传视频的action名称 */
100
    private String fileActionName = "uploadfile";
101
    /* 提交的文件表单名称 */
102
    private String fileFieldName = "upfile";
103
    /* 上传保存路径,可以自定义保存路径和文件名格式 */
104
    private String filePathFormat = "/data/editor/file/{yyyy}{mm}{dd}/{time}{rand:6}";
105
    /* 文件访问路径前缀 */
106
    private String fileUrlPrefix = "";
107
    /* 上传大小限制,单位B,默认50MB */
108
    private long fileMaxSize = 51200000;
109
    /* 上传文件格式显示 */
110
    private String fileAllowFiles = "png,jpg,jpeg,gif,bmp,flv,swf,mkv,avi,rm,rmvb,mpeg,mpg,ogg,ogv,mov,wmv,mp4,webm,mp3,wav,mid,rar,zip,tar,gz,7z,bz2,cab,iso,doc,docx,xls,xlsx,ppt,pptx,pdf,txt,md,xml";
111

112
    /* 列出指定目录下的图片 */
113
    /* 执行图片管理的action名称 */
114
    private String imageManagerActionName = "listimage";
115
    /* 指定要列出图片的目录 */
116
    private String imageManagerListPath = "/data/editor/image/";
117
    /* 每次列出文件数量 */
118
    private long imageManagerListSize = 20;
119
    /* 图片访问路径前缀 */
120
    private String imageManagerUrlPrefix = "";
121
    /* 插入的图片浮动方式 */
122
    private String imageManagerInsertAlign = "none";
123
    /* 列出的文件类型 */
124
    private String imageManagerAllowFiles = "png,jpg,jpeg,gif,bmp";
125

126
    /* 列出指定目录下的文件 */
127
    /* 执行文件管理的action名称 */
128
    private String fileManagerActionName = "listfile";
129
    /* 指定要列出文件的目录 */
130
    private String fileManagerListPath = "/data/editor/upload/file/";
131
    /* 文件访问路径前缀 */
132
    private String fileManagerUrlPrefix = "";
133
    /* 每次列出文件数量 */
134

135
    private long fileManagerListSize = 20;
136
    /* 列出的文件类型 */
137
    private String fileManagerAllowFiles = "png,jpg,jpeg,gif,bmp,flv,swf,mkv,avi,rm,rmvb,mpeg,mpg,ogg,ogv,mov,wmv,mp4,webm,mp3,wav,mid,rar,zip,tar,gz,7z,bz2,cab,iso,doc,docx,xls,xlsx,ppt,pptx,pdf,txt,md,xml";
138

139
    public String getImageActionName() {
140
        return imageActionName;
141
    }
142

143
    public void setImageActionName(String imageActionName) {
144
        this.imageActionName = imageActionName;
145
    }
146

147
    public String getImageFieldName() {
148
        return imageFieldName;
149
    }
150

151
    public void setImageFieldName(String imageFieldName) {
152
        this.imageFieldName = imageFieldName;
153
    }
154

155
    public long getImageMaxSize() {
156
        return imageMaxSize;
157
    }
158

159
    public void setImageMaxSize(long imageMaxSize) {
160
        this.imageMaxSize = imageMaxSize;
161
    }
162

163
    public String getImageAllowFiles() {
164
        return imageAllowFiles;
165
    }
166

167
    public void setImageAllowFiles(String imageAllowFiles) {
168
        this.imageAllowFiles = imageAllowFiles;
169
    }
170

171
    public boolean isImageCompressEnable() {
172
        return imageCompressEnable;
173
    }
174

175
    public void setImageCompressEnable(boolean imageCompressEnable) {
176
        this.imageCompressEnable = imageCompressEnable;
177
    }
178

179
    public long getImageCompressBorder() {
180
        return imageCompressBorder;
181
    }
182

183
    public void setImageCompressBorder(long imageCompressBorder) {
184
        this.imageCompressBorder = imageCompressBorder;
185
    }
186

187
    public String getImageInsertAlign() {
188
        return imageInsertAlign;
189
    }
190

191
    public void setImageInsertAlign(String imageInsertAlign) {
192
        this.imageInsertAlign = imageInsertAlign;
193
    }
194

195
    public String getImageUrlPrefix() {
196
        return imageUrlPrefix;
197
    }
198

199
    public void setImageUrlPrefix(String imageUrlPrefix) {
200
        this.imageUrlPrefix = imageUrlPrefix;
201
    }
202

203
    public String getImagePathFormat() {
204
        return imagePathFormat;
205
    }
206

207
    public void setImagePathFormat(String imagePathFormat) {
208
        this.imagePathFormat = imagePathFormat;
209
    }
210

211
    public String getScrawlActionName() {
212
        return scrawlActionName;
213
    }
214

215
    public void setScrawlActionName(String scrawlActionName) {
216
        this.scrawlActionName = scrawlActionName;
217
    }
218

219
    public String getScrawlFieldName() {
220
        return scrawlFieldName;
221
    }
222

223
    public void setScrawlFieldName(String scrawlFieldName) {
224
        this.scrawlFieldName = scrawlFieldName;
225
    }
226

227
    public String getScrawlPathFormat() {
228
        return scrawlPathFormat;
229
    }
230

231
    public void setScrawlPathFormat(String scrawlPathFormat) {
232
        this.scrawlPathFormat = scrawlPathFormat;
233
    }
234

235
    public long getScrawlMaxSize() {
236
        return scrawlMaxSize;
237
    }
238

239
    public void setScrawlMaxSize(long scrawlMaxSize) {
240
        this.scrawlMaxSize = scrawlMaxSize;
241
    }
242

243
    public String getScrawlUrlPrefix() {
244
        return scrawlUrlPrefix;
245
    }
246

247
    public void setScrawlUrlPrefix(String scrawlUrlPrefix) {
248
        this.scrawlUrlPrefix = scrawlUrlPrefix;
249
    }
250

251
    public String getScrawlInsertAlign() {
252
        return scrawlInsertAlign;
253
    }
254

255
    public void setScrawlInsertAlign(String scrawlInsertAlign) {
256
        this.scrawlInsertAlign = scrawlInsertAlign;
257
    }
258

259
    public String getSnapscreenActionName() {
260
        return snapscreenActionName;
261
    }
262

263
    public void setSnapscreenActionName(String snapscreenActionName) {
264
        this.snapscreenActionName = snapscreenActionName;
265
    }
266

267
    public String getSnapscreenPathFormat() {
268
        return snapscreenPathFormat;
269
    }
270

271
    public void setSnapscreenPathFormat(String snapscreenPathFormat) {
272
        this.snapscreenPathFormat = snapscreenPathFormat;
273
    }
274

275
    public String getSnapscreenUrlPrefix() {
276
        return snapscreenUrlPrefix;
277
    }
278

279
    public void setSnapscreenUrlPrefix(String snapscreenUrlPrefix) {
280
        this.snapscreenUrlPrefix = snapscreenUrlPrefix;
281
    }
282

283
    public String getSnapscreenInsertAlign() {
284
        return snapscreenInsertAlign;
285
    }
286

287
    public void setSnapscreenInsertAlign(String snapscreenInsertAlign) {
288
        this.snapscreenInsertAlign = snapscreenInsertAlign;
289
    }
290

291
    public String getCatcherLocalDomain() {
292
        return catcherLocalDomain;
293
    }
294

295
    public void setCatcherLocalDomain(String catcherLocalDomain) {
296
        this.catcherLocalDomain = catcherLocalDomain;
297
    }
298

299
    public String getCatcherActionName() {
300
        return catcherActionName;
301
    }
302

303
    public void setCatcherActionName(String catcherActionName) {
304
        this.catcherActionName = catcherActionName;
305
    }
306

307
    public String getCatcherFieldName() {
308
        return catcherFieldName;
309
    }
310

311
    public void setCatcherFieldName(String catcherFieldName) {
312
        this.catcherFieldName = catcherFieldName;
313
    }
314

315
    public String getCatcherPathFormat() {
316
        return catcherPathFormat;
317
    }
318

319
    public void setCatcherPathFormat(String catcherPathFormat) {
320
        this.catcherPathFormat = catcherPathFormat;
321
    }
322

323
    public String getCatcherUrlPrefix() {
324
        return catcherUrlPrefix;
325
    }
326

327
    public void setCatcherUrlPrefix(String catcherUrlPrefix) {
328
        this.catcherUrlPrefix = catcherUrlPrefix;
329
    }
330

331
    public long getCatcherMaxSize() {
332
        return catcherMaxSize;
333
    }
334

335
    public void setCatcherMaxSize(long catcherMaxSize) {
336
        this.catcherMaxSize = catcherMaxSize;
337
    }
338

339
    public String getCatcherAllowFiles() {
340
        return catcherAllowFiles;
341
    }
342

343
    public void setCatcherAllowFiles(String catcherAllowFiles) {
344
        this.catcherAllowFiles = catcherAllowFiles;
345
    }
346

347
    public String getVideoActionName() {
348
        return videoActionName;
349
    }
350

351
    public void setVideoActionName(String videoActionName) {
352
        this.videoActionName = videoActionName;
353
    }
354

355
    public String getVideoFieldName() {
356
        return videoFieldName;
357
    }
358

359
    public void setVideoFieldName(String videoFieldName) {
360
        this.videoFieldName = videoFieldName;
361
    }
362

363
    public String getVideoPathFormat() {
364
        return videoPathFormat;
365
    }
366

367
    public void setVideoPathFormat(String videoPathFormat) {
368
        this.videoPathFormat = videoPathFormat;
369
    }
370

371
    public String getVideoUrlPrefix() {
372
        return videoUrlPrefix;
373
    }
374

375
    public void setVideoUrlPrefix(String videoUrlPrefix) {
376
        this.videoUrlPrefix = videoUrlPrefix;
377
    }
378

379
    public long getVideoMaxSize() {
380
        return videoMaxSize;
381
    }
382

383
    public void setVideoMaxSize(long videoMaxSize) {
384
        this.videoMaxSize = videoMaxSize;
385
    }
386

387
    public String getVideoAllowFiles() {
388
        return videoAllowFiles;
389
    }
390

391
    public void setVideoAllowFiles(String videoAllowFiles) {
392
        this.videoAllowFiles = videoAllowFiles;
393
    }
394

395
    public String getFileActionName() {
396
        return fileActionName;
397
    }
398

399
    public void setFileActionName(String fileActionName) {
400
        this.fileActionName = fileActionName;
401
    }
402

403
    public String getFileFieldName() {
404
        return fileFieldName;
405
    }
406

407
    public void setFileFieldName(String fileFieldName) {
408
        this.fileFieldName = fileFieldName;
409
    }
410

411
    public String getFilePathFormat() {
412
        return filePathFormat;
413
    }
414

415
    public void setFilePathFormat(String filePathFormat) {
416
        this.filePathFormat = filePathFormat;
417
    }
418

419
    public String getFileUrlPrefix() {
420
        return fileUrlPrefix;
421
    }
422

423
    public void setFileUrlPrefix(String fileUrlPrefix) {
424
        this.fileUrlPrefix = fileUrlPrefix;
425
    }
426

427
    public long getFileMaxSize() {
428
        return fileMaxSize;
429
    }
430

431
    public void setFileMaxSize(long fileMaxSize) {
432
        this.fileMaxSize = fileMaxSize;
433
    }
434

435
    public String getFileAllowFiles() {
436
        return fileAllowFiles;
437
    }
438

439
    public void setFileAllowFiles(String fileAllowFiles) {
440
        this.fileAllowFiles = fileAllowFiles;
441
    }
442

443
    public String getImageManagerActionName() {
444
        return imageManagerActionName;
445
    }
446

447
    public void setImageManagerActionName(String imageManagerActionName) {
448
        this.imageManagerActionName = imageManagerActionName;
449
    }
450

451
    public String getImageManagerListPath() {
452
        return imageManagerListPath;
453
    }
454

455
    public void setImageManagerListPath(String imageManagerListPath) {
456
        this.imageManagerListPath = imageManagerListPath;
457
    }
458

459
    public long getImageManagerListSize() {
460
        return imageManagerListSize;
461
    }
462

463
    public void setImageManagerListSize(long imageManagerListSize) {
464
        this.imageManagerListSize = imageManagerListSize;
465
    }
466

467
    public String getImageManagerUrlPrefix() {
468
        return imageManagerUrlPrefix;
469
    }
470

471
    public void setImageManagerUrlPrefix(String imageManagerUrlPrefix) {
472
        this.imageManagerUrlPrefix = imageManagerUrlPrefix;
473
    }
474

475
    public String getImageManagerInsertAlign() {
476
        return imageManagerInsertAlign;
477
    }
478

479
    public void setImageManagerInsertAlign(String imageManagerInsertAlign) {
480
        this.imageManagerInsertAlign = imageManagerInsertAlign;
481
    }
482

483
    public String getImageManagerAllowFiles() {
484
        return imageManagerAllowFiles;
485
    }
486

487
    public void setImageManagerAllowFiles(String imageManagerAllowFiles) {
488
        this.imageManagerAllowFiles = imageManagerAllowFiles;
489
    }
490

491
    public String getFileManagerActionName() {
492
        return fileManagerActionName;
493
    }
494

495
    public void setFileManagerActionName(String fileManagerActionName) {
496
        this.fileManagerActionName = fileManagerActionName;
497
    }
498

499
    public String getFileManagerListPath() {
500
        return fileManagerListPath;
501
    }
502

503
    public void setFileManagerListPath(String fileManagerListPath) {
504
        this.fileManagerListPath = fileManagerListPath;
505
    }
506

507
    public String getFileManagerUrlPrefix() {
508
        return fileManagerUrlPrefix;
509
    }
510

511
    public void setFileManagerUrlPrefix(String fileManagerUrlPrefix) {
512
        this.fileManagerUrlPrefix = fileManagerUrlPrefix;
513
    }
514

515
    public long getFileManagerListSize() {
516
        return fileManagerListSize;
517
    }
518

519
    public void setFileManagerListSize(long fileManagerListSize) {
520
        this.fileManagerListSize = fileManagerListSize;
521
    }
522

523
    public String getFileManagerAllowFiles() {
524
        return fileManagerAllowFiles;
525
    }
526

527
    public void setFileManagerAllowFiles(String fileManagerAllowFiles) {
528
        this.fileManagerAllowFiles = fileManagerAllowFiles;
529
    }
530

531
    public File getRootPath() {
532
        return rootPath;
533
    }
534

535
    public void setRootPath(File rootPath) {
536
        this.rootPath = rootPath;
537
    }
538

539
    public List<String> from(String src) {
540
        if (src == null)
541
            return null;
542
        return ListUtil.splitTrimExcludeEmpty(src, ',');
543
    }
544

545
    public List<String> addDot(List<String> list) {
546
        if (null == list)
547
            return null;
548
        List<String> ret = new LinkedList<String>();
549
        for (String s : list) {
550
            if (s.length() > 0)
551
                ret.add("." + s);
552
        }
553
        return ret;
554
    }
555

556
    public Map<String, Object> getAllConfig() {
557
        Map<String, Object> ret = new HashMap<String, Object>();
558
        ret.put("imageActionName", this.imageActionName);
559
        ret.put("imageFieldName", this.imageFieldName);
560
        ret.put("imageMaxSize", this.imageMaxSize);
561
        ret.put("imageAllowFiles", this.addDot(this.from(this.imageAllowFiles)));
562
        ret.put("imageCompressEnable", this.imageCompressEnable);
563
        ret.put("imageCompressBorder", this.imageCompressBorder);
564
        ;
565
        ret.put("imageInsertAlign", this.imageInsertAlign);
566
        ret.put("imageUrlPrefix", this.imageUrlPrefix);
567

568
        ret.put("imagePathFormat", this.imagePathFormat);
569

570
        ret.put("scrawlActionName", this.scrawlActionName);
571

572
        ret.put("scrawlFieldName", this.scrawlFieldName);
573
        ret.put("scrawlPathFormat", this.scrawlPathFormat);
574
        ret.put("scrawlMaxSize", this.scrawlMaxSize);
575
        ret.put("scrawlUrlPrefix", this.scrawlUrlPrefix);
576
        ret.put("scrawlInsertAlign", this.scrawlInsertAlign);
577

578
        ret.put("snapscreenActionName", this.snapscreenActionName);
579
        /* 上传保存路径,可以自定义保存路径和文件名格式 */
580
        ret.put("snapscreenPathFormat", this.snapscreenPathFormat);
581
        /* 图片访问路径前缀 */
582
        ret.put("snapscreenUrlPrefix", this.snapscreenUrlPrefix);
583
        /* 插入的图片浮动方式 */
584
        ret.put("snapscreenInsertAlign", this.snapscreenInsertAlign);
585

586
        /* 抓取远程图片配置 */
587
        ret.put("catcherLocalDomain", this.from(this.catcherLocalDomain));
588
        /* 执行抓取远程图片的action名称 */
589
        ret.put("catcherActionName", this.catcherActionName);
590
        /* 提交的图片列表表单名称 */
591
        ret.put("catcherFieldName", this.catcherFieldName);
592
        /* 上传保存路径,可以自定义保存路径和文件名格式 */
593
        ret.put("catcherPathFormat", this.catcherPathFormat);
594
        /* 图片访问路径前缀 */
595
        ret.put("catcherUrlPrefix", this.catcherUrlPrefix);
596
        /* 上传大小限制,单位B */
597
        ret.put("catcherMaxSize", this.catcherMaxSize);
598
        ret.put("catcherAllowFiles", this.addDot(this.from(this.catcherAllowFiles)));
599

600
        /* 上传视频配置 */
601
        /* 执行上传视频的action名称 */
602
        ret.put("videoActionName", this.videoActionName);
603
        /* 提交的视频表单名称 */
604
        ret.put("videoFieldName", this.videoFieldName);
605
        /* 上传保存路径,可以自定义保存路径和文件名格式 */
606
        ret.put("videoPathFormat", this.videoPathFormat);
607
        /* 视频访问路径前缀 */
608
        ret.put("videoUrlPrefix", this.videoUrlPrefix);
609
        ret.put("videoMaxSize", this.videoMaxSize);
610
        /* 上传视频格式显示 */
611
        ret.put("videoAllowFiles", this.addDot(this.from(this.videoAllowFiles)));
612

613
        /* 上传文件配置 */
614
        /* controller里,执行上传视频的action名称 */
615
        ret.put("fileActionName", this.fileActionName);
616
        /* 提交的文件表单名称 */
617
        ret.put("fileFieldName", this.fileFieldName);
618
        /* 上传保存路径,可以自定义保存路径和文件名格式 */
619
        ret.put("filePathFormat", this.filePathFormat);
620
        /* 文件访问路径前缀 */
621
        ret.put("fileUrlPrefix", this.fileUrlPrefix);
622
        /* 上传大小限制,单位B,默认50MB */
623
        ret.put("fileMaxSize", this.fileMaxSize);
624
        /* 上传文件格式显示 */
625
        ret.put("fileAllowFiles", this.addDot(this.from(this.fileAllowFiles)));
626

627
        /* 列出指定目录下的图片 */
628
        /* 执行图片管理的action名称 */
629
        ret.put("imageManagerActionName", this.imageManagerActionName);
630
        /* 指定要列出图片的目录 */
631
        ret.put("imageManagerListPath", this.imageManagerListPath);
632
        /* 每次列出文件数量 */
633
        ret.put("imageManagerListSize", this.imageManagerListSize);
634
        /* 图片访问路径前缀 */
635
        ret.put("imageManagerUrlPrefix", this.imageManagerUrlPrefix);
636
        /* 插入的图片浮动方式 */
637
        ret.put("imageManagerInsertAlign", this.imageManagerInsertAlign);
638
        /* 列出的文件类型 */
639
        ret.put("imageManagerAllowFiles", this.addDot(this.from(this.imageManagerAllowFiles)));
640

641
        /* 列出指定目录下的文件 */
642
        /* 执行文件管理的action名称 */
643
        ret.put("fileManagerActionName", this.fileManagerActionName);
644
        /* 指定要列出文件的目录 */
645
        ret.put("fileManagerListPath", this.fileManagerListPath);
646
        /* 文件访问路径前缀 */
647
        ret.put("fileManagerUrlPrefix", this.fileManagerUrlPrefix);
648
        /* 每次列出文件数量 */
649

650
        ret.put("fileManagerListSize", this.fileManagerListSize);
651
        /* 列出的文件类型 */
652
        ret.put("fileManagerAllowFiles", this.addDot(this.from(this.fileManagerAllowFiles)));
653
        return ret;
654

655
    }
656
}

+ 128 - 0
src/main/java/com/ekexiu/portal/editor/PathFormat.java

@ -0,0 +1,128 @@
1
package com.ekexiu.portal.editor;
2

3
import java.text.SimpleDateFormat;
4
import java.util.Date;
5
import java.util.regex.Matcher;
6
import java.util.regex.Pattern;
7

8
public class PathFormat {
9

10
    private static Date currentDate = null;
11

12
    public static String parse(String input) {
13
        Pattern pattern = Pattern.compile("\\{([^\\}]+)\\}", 2);
14
        Matcher matcher = pattern.matcher(input);
15

16
        currentDate = new Date();
17

18
        StringBuffer sb = new StringBuffer();
19
        while (matcher.find()) {
20
            matcher.appendReplacement(sb, getString(matcher.group(1)));
21
        }
22
        matcher.appendTail(sb);
23

24
        return sb.toString();
25
    }
26

27
    public static String format(String input) {
28
        return input.replace("\\", "/");
29
    }
30

31
    public static String parse(String input, String filename) {
32
        Pattern pattern = Pattern.compile("\\{([^\\}]+)\\}", 2);
33
        Matcher matcher = pattern.matcher(input);
34
        String matchStr = null;
35

36
        currentDate = new Date();
37

38
        StringBuffer sb = new StringBuffer();
39
        while (matcher.find()) {
40
            matchStr = matcher.group(1);
41
            if (matchStr.indexOf("filename") != -1) {
42
                filename = filename.replace("$", "\\$").replaceAll("[\\/:*?\"<>|]", "");
43
                matcher.appendReplacement(sb, filename);
44
            } else {
45
                matcher.appendReplacement(sb, getString(matchStr));
46
            }
47
        }
48
        matcher.appendTail(sb);
49

50
        return sb.toString();
51
    }
52

53
    private static String getString(String pattern) {
54
        pattern = pattern.toLowerCase();
55
        if (pattern.indexOf("time") != -1) {
56
            return "" + getTimestamp();
57
        }
58
        if (pattern.indexOf("yyyy") != -1) {
59
            return getFullYear();
60
        }
61
        if (pattern.indexOf("yy") != -1) {
62
            return getYear();
63
        }
64
        if (pattern.indexOf("mm") != -1) {
65
            return getMonth();
66
        }
67
        if (pattern.indexOf("dd") != -1) {
68
            return getDay();
69
        }
70
        if (pattern.indexOf("hh") != -1) {
71
            return getHour();
72
        }
73
        if (pattern.indexOf("ii") != -1) {
74
            return getMinute();
75
        }
76
        if (pattern.indexOf("ss") != -1) {
77
            return getSecond();
78
        }
79
        if (pattern.indexOf("rand") != -1) {
80
            return getRandom(pattern);
81
        }
82
        return pattern;
83
    }
84

85
    private static long getTimestamp() {
86
        return System.currentTimeMillis();
87
    }
88

89
    private static String getFullYear() {
90
        return new SimpleDateFormat("yyyy").format(currentDate);
91
    }
92

93
    private static String getYear() {
94
        return new SimpleDateFormat("yy").format(currentDate);
95
    }
96

97
    private static String getMonth() {
98
        return new SimpleDateFormat("MM").format(currentDate);
99
    }
100

101
    private static String getDay() {
102
        return new SimpleDateFormat("dd").format(currentDate);
103
    }
104

105
    private static String getHour() {
106
        return new SimpleDateFormat("HH").format(currentDate);
107
    }
108

109
    private static String getMinute() {
110
        return new SimpleDateFormat("mm").format(currentDate);
111
    }
112

113
    private static String getSecond() {
114
        return new SimpleDateFormat("ss").format(currentDate);
115
    }
116

117
    private static String getRandom(String pattern) {
118
        int length = 0;
119
        pattern = pattern.split(":")[1].trim();
120

121
        length = Integer.parseInt(pattern);
122

123
        return ("" + Math.random()).replace(".", "").substring(0, length);
124
    }
125

126
    public static void main(String[] args) {
127
    }
128
}

+ 65 - 0
src/main/java/com/ekexiu/portal/editor/define/AppInfo.java

@ -0,0 +1,65 @@
1
package com.ekexiu.portal.editor.define;
2

3
import java.util.HashMap;
4
import java.util.Map;
5

6
public class AppInfo {
7
    public static final int SUCCESS = 0;
8
    public static final int MAX_SIZE = 1;
9
    public static final int PERMISSION_DENIED = 2;
10
    public static final int FAILED_CREATE_FILE = 3;
11
    public static final int IO_ERROR = 4;
12
    public static final int NOT_MULTIPART_CONTENT = 5;
13
    public static final int PARSE_REQUEST_ERROR = 6;
14
    public static final int NOTFOUND_UPLOAD_DATA = 7;
15
    public static final int NOT_ALLOW_FILE_TYPE = 8;
16
    public static final int INVALID_ACTION = 101;
17
    public static final int CONFIG_ERROR = 102;
18
    public static final int PREVENT_HOST = 201;
19
    public static final int CONNECTION_ERROR = 202;
20
    public static final int REMOTE_FAIL = 203;
21
    public static final int NOT_DIRECTORY = 301;
22
    public static final int NOT_EXIST = 302;
23
    public static final int ILLEGAL = 401;
24
    public static Map<Integer, String> info = new HashMap<Integer, String>();
25

26
    public static String getStateInfo(int key) {
27
        return info.get(Integer.valueOf(key));
28
    }
29

30
    static {
31
        info.put(SUCCESS, "SUCCESS");
32

33
        info.put(INVALID_ACTION, "无效的Action");
34

35
        info.put(CONFIG_ERROR, "配置文件初始化失败");
36

37
        info.put(REMOTE_FAIL, "抓取远程图片失败");
38

39
        info.put(PREVENT_HOST, "被阻止的远程主机");
40

41
        info.put(CONNECTION_ERROR, "远程连接出错");
42

43
        info.put(MAX_SIZE, "文件大小超出限制");
44

45
        info.put(PERMISSION_DENIED, "权限不足");
46

47
        info.put(FAILED_CREATE_FILE, "创建文件失败");
48

49
        info.put(IO_ERROR, "IO错误");
50

51
        info.put(NOT_MULTIPART_CONTENT, "上传表单不是multipart/form-data类型");
52

53
        info.put(PARSE_REQUEST_ERROR, "解析上传表单错误");
54

55
        info.put(NOTFOUND_UPLOAD_DATA, "未找到上传数据");
56

57
        info.put(NOT_ALLOW_FILE_TYPE, "不允许的文件类型");
58

59
        info.put(NOT_DIRECTORY, "指定路径不是目录");
60

61
        info.put(NOT_EXIST, "指定路径并不存在");
62

63
        info.put(ILLEGAL, "Callback参数名不合法");
64
    }
65
}

+ 89 - 0
src/main/java/com/ekexiu/portal/editor/define/BaseState.java

@ -0,0 +1,89 @@
1
package com.ekexiu.portal.editor.define;
2

3
import java.util.HashMap;
4
import java.util.Iterator;
5
import java.util.Map;
6

7
public class BaseState implements State {
8
    private boolean state = false;
9
    private String info = null;
10
    private Map<String, String> infoMap = new HashMap<String, String>();
11

12
    public BaseState() {
13
        this.state = true;
14
    }
15

16
    public BaseState(boolean state) {
17
        setState(state);
18
    }
19

20
    public BaseState(boolean state, String info) {
21
        setState(state);
22
        this.info = info;
23
    }
24

25
    public BaseState(boolean state, int infoCode) {
26
        setState(state);
27
        this.info = AppInfo.getStateInfo(infoCode);
28
    }
29

30
    public boolean isSuccess() {
31
        return this.state;
32
    }
33

34
    public void setState(boolean state) {
35
        this.state = state;
36
    }
37

38
    public void setInfo(String info) {
39
        this.info = info;
40
    }
41

42
    public void setInfo(int infoCode) {
43
        this.info = AppInfo.getStateInfo(infoCode);
44
    }
45

46
    public String toJSONString() {
47
        return toString();
48
    }
49

50
    public String toString() {
51
        String key = null;
52
        String stateVal = isSuccess() ? AppInfo.getStateInfo(0) : this.info;
53

54
        StringBuilder builder = new StringBuilder();
55

56
        builder.append("{\"state\": \"" + stateVal + "\"");
57

58
        Iterator<String> iterator = this.infoMap.keySet().iterator();
59
        while (iterator.hasNext()) {
60
            key = (String) iterator.next();
61

62
            builder.append(",\"" + key + "\": \"" + (String) this.infoMap.get(key) + "\"");
63
        }
64
        builder.append("}");
65

66
        return toUnicode(builder.toString());
67
    }
68

69
    public void putInfo(String name, String val) {
70
        this.infoMap.put(name, val);
71
    }
72

73
    public void putInfo(String name, long val) {
74
        putInfo(name,""+val);
75
    }
76

77
    public static String toUnicode(String input) {
78
        StringBuilder builder = new StringBuilder();
79
        char[] chars = input.toCharArray();
80
        for (char ch : chars) {
81
            if (ch < '\u0100') {
82
                builder.append(ch);
83
            } else {
84
                builder.append("\\u" + Integer.toHexString(ch & 0xFFFF));
85
            }
86
        }
87
        return builder.toString();
88
    }
89
}

+ 11 - 0
src/main/java/com/ekexiu/portal/editor/define/State.java

@ -0,0 +1,11 @@
1
package com.ekexiu.portal.editor.define;
2

3
public interface State {
4
   boolean isSuccess();
5

6
   void putInfo(String name, String val);
7

8
   void putInfo(String name, long val);
9

10
    String toJSONString();
11
}