UEBase64Uploader.java 1.73 KB
Newer Older
liqin's avatar
liqin committed
1 2 3 4 5 6
package com.baidu.ueditor.extend;

import com.baidu.ueditor.define.AppInfo;
import com.baidu.ueditor.define.BaseState;
import com.baidu.ueditor.define.FileType;
import com.baidu.ueditor.define.State;
liqin's avatar
liqin committed
7 8 9 10
import org.json.JSONObject;

import java.util.Base64;
import java.util.Map;
liqin's avatar
liqin committed
11 12 13 14 15 16 17 18

public final class UEBase64Uploader {

	public static State save(String fileName, Map<String, Object> conf, UeditorService ueditorService) {
	    // String filedName = (String) conf.get("fieldName");
		// String fileName = request.getParameter(filedName);
		byte[] data = decode(fileName);

liqin's avatar
liqin committed
19
		long maxSize = (Long) conf.get("maxSize");
liqin's avatar
liqin committed
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

		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"));
		State storageState = ueditorService.saveBinaryFile(data, conf.get("filename") + suffix);
		
		//savePath = savePath + suffix;
		//String rootPath = ConfigManager.getRootPath(request,conf);
		//String physicalPath = rootPath + savePath;
		//State storageState = StorageManager.saveBinaryFile(data, physicalPath);

		if (storageState.isSuccess()) {
			try {
				JSONObject jsonObj = new JSONObject(storageState.toJSONString());
				//storageState.putInfo("url", PathFormat.format(savePath));
				storageState.putInfo("url", jsonObj.getString("url"));
				storageState.putInfo("type", suffix);
				storageState.putInfo("original", "");
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return storageState;
	}

	private static byte[] decode(String content) {
liqin's avatar
liqin committed
50
		return Base64.getDecoder().decode(content);
liqin's avatar
liqin committed
51 52 53 54 55 56 57
	}

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