Helius
2020-12-23 a27143ea3ab9f215fcbddfd23eba530afbbc3c62
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
package com.matrix.component.ueditor.upload;
 
 
import java.util.Map;
import com.matrix.component.ueditor.define.State;
import com.matrix.component.ueditor.PathFormat;
import com.matrix.component.ueditor.define.AppInfo;
import com.matrix.component.ueditor.define.BaseState;
import com.matrix.component.ueditor.define.FileType;
import org.apache.commons.codec.binary.Base64;
 
public final class Base64Uploader {
 
    public static State save(String content, Map<String, Object> conf) {
        
        byte[] data = decode(content);
 
        long maxSize = ((Long) conf.get("maxSize")).longValue();
 
        if (!validSize(data, maxSize)) {
            return new BaseState(false, AppInfo.MAX_SIZE);
        }
 
        String suffix = FileType.getSuffix("JPG");
 
        String savePath = PathFormat.parse((String) conf.get("savePath"),
                (String) conf.get("filename"));
        
        savePath = savePath + suffix;
        String physicalPath = (String) conf.get("rootPath") + savePath;
 
        State storageState = StorageManager.saveBinaryFile(data, physicalPath);
 
        if (storageState.isSuccess()) {
            storageState.putInfo("url", PathFormat.format(savePath));
            storageState.putInfo("type", suffix);
            storageState.putInfo("original", "");
        }
 
        return storageState;
    }
 
    private static byte[] decode(String content) {
        return Base64.decodeBase64(content);
    }
 
    private static boolean validSize(byte[] data, long length) {
        return data.length <= length;
    }
    
}