add
yj
2024-12-05 b9900893177c78fc559223521fe839aa21000017
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
package com.dobbinsoft.fw.launcher.controller;
 
import cn.hutool.core.io.file.FileNameUtil;
import com.alibaba.fastjson.JSONObject;
import com.dobbinsoft.fw.core.util.GeneratorUtil;
import com.dobbinsoft.fw.launcher.inter.BeforeFileUploadPath;
import com.dobbinsoft.fw.launcher.inter.BeforeUEditorFileUpload;
import com.dobbinsoft.fw.support.image.ImageManager;
import com.dobbinsoft.fw.support.image.ImageModel;
import com.dobbinsoft.fw.support.model.Page;
import com.dobbinsoft.fw.support.storage.StorageClient;
import com.dobbinsoft.fw.support.storage.StorageRequest;
import com.dobbinsoft.fw.support.storage.StorageResult;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
 
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.stream.Collectors;
 
/**
 * 为 UEditor 提供API
 */
@RequestMapping("/ueditor")
@RestController
public class UEditorController {
 
    @Autowired(required = false)
    private ImageManager imageManager;
 
    @Autowired(required = false)
    private StorageClient storageClient;
 
    @Autowired(required = false)
    private BeforeUEditorFileUpload beforeUEditorFileUpload;
 
    @Autowired(required = false)
    private BeforeFileUploadPath beforeFileUploadPath;
 
    private static final String SUCCESS = "SUCCESS";
 
    private static final String FAILED = "FAILED";
 
    @GetMapping("/cos")
    public String cosGet(String action, String callback, Integer start, Integer size, HttpServletRequest request) throws Exception {
        if (this.beforeUEditorFileUpload != null) {
            this.beforeUEditorFileUpload.before(request);
        }
        String res = "";
        if ("config".equals(action)) {
            // 客户端侧配置
            res = "";
        } else if ("listimage".equals(action)) {
            Page<ImageModel> imageList = imageManager.getImageList(null, null, start / size + 1, size);
            ListPage listPage = new ListPage();
            listPage.setStart(start);
            listPage.setTotal((int) imageList.getTotal());
            listPage.setState(SUCCESS);
            listPage.setList(imageList.getItems().stream().map(item -> {
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("url", item.getUrl());
                return jsonObject;
            }).collect(Collectors.toList()));
            res = callback(callback, JSONObject.toJSONString(listPage));
        }
        return callback(callback, res);
    }
 
    @PostMapping("/cos")
    public String cosPost(String action, MultipartFile upfile, HttpServletRequest request) throws Exception{
        if (this.beforeUEditorFileUpload != null) {
            this.beforeUEditorFileUpload.before(request);
        }
        if ("uploadimage".equals(action)) {
            InputStream inputStream = null;
            try {
                String ext = FileNameUtil.getSuffix(upfile.getOriginalFilename());
                String uuid = GeneratorUtil.genFileName();
                StorageRequest storageRequest = new StorageRequest();
                storageRequest.setContentType(upfile.getContentType());
                storageRequest.setFilename(uuid + "." + ext);
                storageRequest.setSize(upfile.getSize());
                inputStream = upfile.getInputStream();
                storageRequest.setIs(inputStream);
                if (this.beforeFileUploadPath != null) {
                    storageRequest.setPath(this.beforeFileUploadPath.setPath("commons"));
                } else {
                    storageRequest.setPath("commons");
                }
                StorageResult result = storageClient.save(storageRequest);
                if (result.isSuc()) {
                    ImageModel imageModel = new ImageModel();
                    imageModel.setBizType(0);
                    imageModel.setBizTypeTitle("富文本");
                    imageModel.setContentLength(upfile.getSize());
                    imageModel.setBizId(0L);
                    imageModel.setTitle(upfile.getOriginalFilename());
                    imageModel.setUrl(result.getUrl());
                    imageManager.upsertImg(imageModel);
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("state", SUCCESS);
                    jsonObject.put("url", result.getUrl());
                    jsonObject.put("title", upfile.getOriginalFilename());
                    jsonObject.put("original", upfile.getOriginalFilename());
                    return jsonObject.toJSONString();
                }
            } catch (IOException e) {
                throw new RuntimeException("网络错误");
            } finally {
                if (inputStream != null) {
                    try {
                        inputStream.close();
                    } catch (IOException e) {
                    }
                }
            }
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("state", FAILED);
        return jsonObject.toJSONString();
    }
 
    private String callback(String callback, String text) {
        return callback + "(" + text + ")";
    }
 
    @Data
    private static class ListPage {
 
        private Integer start;
 
        private Integer total;
 
        // SUCCESS
        private String state;
 
        private List<JSONObject> list;
 
    }
 
}