diff --git a/src/main/java/au/com/royalpay/payment/manage/support/ueditor/core/UEditorApi.java b/src/main/java/au/com/royalpay/payment/manage/support/ueditor/core/UEditorApi.java new file mode 100644 index 000000000..b0c82d0b1 --- /dev/null +++ b/src/main/java/au/com/royalpay/payment/manage/support/ueditor/core/UEditorApi.java @@ -0,0 +1,21 @@ +package au.com.royalpay.payment.manage.support.ueditor.core; + +import com.alibaba.fastjson.JSONObject; +import org.springframework.web.multipart.MultipartFile; + +/** + * Created by yixian on 2016-10-11. + */ +public interface UEditorApi { + JSONObject getAllConfigs(); + + JSONObject uploadImage(MultipartFile file); + + JSONObject uploadFile(MultipartFile file); + + JSONObject uploadScrawl(String base64String); + + JSONObject listImages(int start); + + JSONObject listFiles(int start); +} diff --git a/src/main/java/au/com/royalpay/payment/manage/support/ueditor/core/impls/UEditorApiImpl.java b/src/main/java/au/com/royalpay/payment/manage/support/ueditor/core/impls/UEditorApiImpl.java new file mode 100644 index 000000000..5ff303ba6 --- /dev/null +++ b/src/main/java/au/com/royalpay/payment/manage/support/ueditor/core/impls/UEditorApiImpl.java @@ -0,0 +1,134 @@ +package au.com.royalpay.payment.manage.support.ueditor.core.impls; + +import au.com.royalpay.payment.manage.support.ueditor.core.UEditorApi; +import au.com.royalpay.payment.tools.connections.attachment.core.AttachmentClient; +import au.com.royalpay.payment.tools.exceptions.ServerErrorException; +import com.alibaba.fastjson.JSONObject; +import org.apache.commons.codec.binary.Base64; +import org.springframework.stereotype.Service; +import org.springframework.web.multipart.MultipartFile; + +import javax.annotation.Resource; +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +/** + * Created by yixian on 2016-10-11. + */ +@Service +public class UEditorApiImpl implements UEditorApi { + @Resource + private AttachmentClient attachmentClient; + + private final int maxUploadSize = 5_000_000; + + @Override + public JSONObject getAllConfigs() { + List imageTypes = Arrays.asList(".png", ".jpg", ".jpeg", ".gif"); + List zipTypes = Arrays.asList(".zip", ".rar", ".gz", ".tar", ".7z"); + List documentTypes = Arrays.asList(".pdf", ".doc", ".docx", ".xls", ".xlsx"); + + JSONObject config = new JSONObject(); + //图片上传 + config.put("imageActionName", "uploadimage"); + config.put("imageFieldName", "file"); + config.put("imageMaxSize", maxUploadSize); + config.put("imageAllowFiles", imageTypes); + config.put("imageCompressEnable", true); + config.put("imageCompressBorder", 1600); + config.put("imageInsertAlign", "none"); + config.put("imageUrlPrefix", ""); + + //涂鸦 + config.put("scrawlActionName", "uploadscrawl"); + config.put("scrawlFieldname", "file"); + config.put("scrawlMaxSize", maxUploadSize); + config.put("scrawlUrlPrefix", ""); + config.put("scrawlInsertAlign", "none"); + + //上传文件 + config.put("fileActionName", "uploadfile"); + config.put("fileFieldName", "file"); + config.put("fileUrlPrefix", ""); + config.put("fileMaxSize", maxUploadSize); + List allTypes = new ArrayList<>(); + allTypes.addAll(imageTypes); + allTypes.addAll(zipTypes); + allTypes.addAll(documentTypes); + config.put("fileAllowFiles", allTypes); + + config.put("imageManagerActionName", "listimage"); + config.put("imageManagerListSize", 20); + config.put("imageManagerUrlPrefix", ""); + config.put("imageManagerInsertAlign", "none"); + config.put("imageManagerAllowFiles", imageTypes); + + config.put("fileManagerActionName", "listfile"); + config.put("fileManagerUrlPrefix", ""); + config.put("fileManagerListSize", 20); + config.put("fileManagerAllowFiles", allTypes); + return config; + } + + @Override + public JSONObject uploadImage(MultipartFile file) { + return upload(file); + } + + private JSONObject upload(MultipartFile file) { + try { + JSONObject uploadResult = attachmentClient.uploadFile(file, false); + return buildResult(uploadResult); + } catch (IOException e) { + throw new ServerErrorException(e); + } + } + + private JSONObject buildResult(JSONObject uploadResult) { + JSONObject res = new JSONObject(); + res.put("state", "SUCCESS"); + res.put("size", uploadResult.getLongValue("length")); + res.put("name", uploadResult.getString("original_filename")); + res.put("original", uploadResult.getString("original_filename")); + res.put("url", uploadResult.getString("url")); + res.put("type", "." + uploadResult.getString("filetype")); + return res; + } + + @Override + public JSONObject uploadFile(MultipartFile file) { + return upload(file); + } + + @Override + public JSONObject uploadScrawl(String base64String) { + byte[] bytes = Base64.decodeBase64(base64String); + InputStream ins = new ByteArrayInputStream(bytes); + JSONObject uploadResult = attachmentClient.uploadFile(ins, "scrawl.jpg", false); + return buildResult(uploadResult); + } + + @Override + public JSONObject listImages(int start) { + return emptyResult(start); + } + + @Override + public JSONObject listFiles(int start) { + return emptyResult(start); + } + + private JSONObject emptyResult(int start) { + JSONObject res = new JSONObject(); + res.put("state","SUCCESS"); + res.put("start",start); + res.put("total",0); + res.put("list",new ArrayList()); + //{url:...,mtime:seconds stamp} + return res; + } +} diff --git a/src/main/java/au/com/royalpay/payment/manage/support/ueditor/web/UEditorApiController.java b/src/main/java/au/com/royalpay/payment/manage/support/ueditor/web/UEditorApiController.java new file mode 100644 index 000000000..e1fc5550d --- /dev/null +++ b/src/main/java/au/com/royalpay/payment/manage/support/ueditor/web/UEditorApiController.java @@ -0,0 +1,51 @@ +package au.com.royalpay.payment.manage.support.ueditor.web; + +import au.com.royalpay.payment.manage.support.ueditor.core.UEditorApi; +import com.alibaba.fastjson.JSONObject; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.multipart.MultipartFile; + +import javax.annotation.Resource; + +/** + * Created by yixian on 2016-10-11. + */ +@RestController +@RequestMapping("/api/ueditor_api") +public class UEditorApiController { + @Resource + private UEditorApi uEditorApi; + + @RequestMapping(method = RequestMethod.GET, params = "action=config") + public JSONObject getActionConfig() { + return uEditorApi.getAllConfigs(); + } + + @RequestMapping(method = RequestMethod.POST, params = "action=uploadimage") + public JSONObject uploadImage(@RequestParam MultipartFile file) { + return uEditorApi.uploadImage(file); + } + + @RequestMapping(method = RequestMethod.POST, params = "action=uploadfile") + public JSONObject uploadFile(@RequestParam MultipartFile file) { + return uEditorApi.uploadFile(file); + } + + @RequestMapping(method = RequestMethod.POST, params = "action=uploadscrawl") + public JSONObject uploadScrawl(@RequestParam String file) { + return uEditorApi.uploadScrawl(file); + } + + @RequestMapping(method = RequestMethod.POST, params = "action=listimage") + public JSONObject listImages(@RequestParam(defaultValue = "0") int start) { + return uEditorApi.listImages(start); + } + + @RequestMapping(method = RequestMethod.POST, params = "action=listfile") + public JSONObject listFiles(@RequestParam(defaultValue = "0") int start) { + return uEditorApi.listFiles(start); + } +}