刘光辉
13 小时以前 34981c30a78e8bbd7791131059a9210f9928b62c
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
package jnpf.controller;
 
import com.alibaba.fastjson.JSON;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jnpf.base.ActionResult;
import jnpf.base.vo.PageListVO;
import jnpf.base.vo.PaginationVO;
import jnpf.config.ConfigValueUtil;
import jnpf.constant.FileTypeConstant;
import jnpf.constant.MsgCode;
import jnpf.entity.FileEntity;
import jnpf.file.FileApi;
import jnpf.model.UploaderVO;
import jnpf.model.YozoFileParams;
import jnpf.model.YozoParams;
import jnpf.service.YozoService;
import jnpf.util.FileUtil;
import jnpf.util.JsonUtil;
import jnpf.util.XSSEscape;
import jnpf.util.YozoUtils;
import jnpf.util.wxutil.HttpUtil;
import lombok.RequiredArgsConstructor;
import org.apache.commons.io.FileUtils;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
 
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
/**
 * @author JNPF
 */
@RestController
@RequestMapping
@Tag(name = "永中文件预览编辑")
@RequiredArgsConstructor
public class YozoFileController {
 
 
    private final YozoService yozoService;
    private final YozoUtils yozoUtil;
    private final ConfigValueUtil configValueUtil;
    private final FileApi fileApi;
    private final YozoParams yozoParams;
 
    public static final String APPID = "&appId=";
    public static final String FILE_VERSION_ID = "fileVersionId";
 
 
    @PostMapping("/getUrl")
    @Operation(summary = "在线预览")
    @Parameter(name = "params", description = "永中模型", required = true)
    public ActionResult<Object> getUrl(@RequestBody YozoFileParams params) {
        String previewUrl = yozoService.getPreviewUrl(params);
        return ActionResult.success("success", previewUrl);
    }
 
    @PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    @Operation(summary = "上传本地文件")
    public ActionResult<Object> upload(@RequestPart("multipartFile") MultipartFile file) throws IOException {
        String result = yozoUtil.uploadFileInPreview(file.getInputStream(), file.getOriginalFilename());
        String fileName = file.getOriginalFilename();
        UploaderVO vo = UploaderVO.builder().name(fileName).build();
        Map<String, Object> map = JsonUtil.stringToMap(result);
        if ("操作成功".equals(map.get("message"))) {
            Map<String, Object> dataMap = JsonUtil.stringToMap(String.valueOf(map.get("data")));
            String verId = String.valueOf(dataMap.get(FILE_VERSION_ID));
            vo.setFileVersionId(verId);
            return ActionResult.success("Success", vo);
        }
 
        return ActionResult.fail(MsgCode.FA033.get());
    }
 
    @GetMapping("/newCreate")
    @Operation(summary = "新建文件")
    @Parameter(name = "fileName", description = "名称")
    @Parameter(name = "templateType", description = "类型")
    public ActionResult<Object> newCreate(@RequestParam("fileName") String fileName, @RequestParam("templateType") String templateType) {
        String fileNa = yozoUtil.getFileName(fileName, templateType);
        if (fileNa == null) {
            return ActionResult.fail(MsgCode.FA042.get());
        }
        //判断文件是否创建过
        FileEntity fileEntity = yozoService.selectByName(fileNa);
        if (fileEntity != null) {
            return ActionResult.fail(MsgCode.FA043.get());
        }
        Map<String, String[]> params = new HashMap<>();
        params.put("templateType", new String[]{templateType});
        params.put("fileName", new String[]{fileName});
        String sign = yozoUtil.generateSign(yozoParams.getAppId(), yozoParams.getAppKey(), params).getData();
        String url = yozoParams.getCloudDomain() + "/api/file/template?templateType=" + templateType +
                "&fileName=" + fileName +
                APPID + yozoParams.getAppId() +
                FILE_VERSION_ID + sign;
        String s = HttpUtil.sendHttpPost(url);
        Map<String, Object> maps = JSON.parseObject(s, Map.class);
        Map<String, String> fileMap = (Map<String, String>) maps.get("data");
        String fileVersionId = fileMap.get(FILE_VERSION_ID);
        String fileId = fileMap.get("fileId");
        ActionResult<Object> back = yozoService.saveFileId(fileVersionId, fileId, fileNa);
        //在本地新建文件
        FileUtil.createFile(configValueUtil.getDocumentPreviewPath(), fileNa);
        return back;
    }
 
    @GetMapping("/uploadByHttp")
    @Operation(summary = "http上传文件")
    @Parameter(name = "fileUrl", description = "路径")
    public ActionResult<Object> uploadByHttp(@RequestParam("fileUrl") String fileUrl) {
        //获取签名
        Map<String, String[]> params = new HashMap<>();
        params.put("fileUrl", new String[]{fileUrl});
        String sign = yozoUtil.generateSign(yozoParams.getAppId(), yozoParams.getAppKey(), params).getData();
        String url = yozoParams.getCloudDomain() + "/api/file/http?fileUrl=" + fileUrl +
                APPID + yozoParams.getAppId() +
                FILE_VERSION_ID + sign;
        String s = HttpUtil.sendHttpPost(url);
        Map<String, Object> maps = JSON.parseObject(s, Map.class);
        Map<String, String> fileMap = (Map<String, String>) maps.get("data");
        String fileVersionId = fileMap.get(FILE_VERSION_ID);
        String fileId = fileMap.get("fileId");
        return yozoService.saveFileIdByHttp(fileVersionId, fileId, fileUrl);
    }
 
    @GetMapping("/downloadFile")
    @Operation(summary = "永中下载文件")
    @Parameter(name = FILE_VERSION_ID, description = "主键")
    public String downloadFile(@RequestParam("fileVersionId") String fileVersionId) {
        String newFileVersionId = XSSEscape.escape(fileVersionId);
        FileEntity fileEntity = yozoService.selectByVersionId(newFileVersionId);
        if (fileEntity == null) {
            return MsgCode.FA044.get();
        }
        //获取签名
        Map<String, String[]> params = new HashMap<>();
        params.put(FILE_VERSION_ID, new String[]{newFileVersionId});
        String sign = yozoUtil.generateSign(yozoParams.getAppId(), yozoParams.getAppKey(), params).getData();
        return yozoParams.getCloudDomain() + "/api/file/download?fileVersionId=" + newFileVersionId +
                APPID + yozoParams.getAppId() +
                FILE_VERSION_ID + sign;
    }
 
 
    @GetMapping("/deleteVersionFile")
    @Operation(summary = "删除文件版本")
    @Parameter(name = "fileVersionId", description = "主键")
    public ActionResult<Object> deleteVersion(@RequestParam("fileVersionId") String fileVersionId) {
        //获取签名
        Map<String, String[]> params = new HashMap<>();
        params.put(FILE_VERSION_ID, new String[]{fileVersionId});
        String sign = yozoUtil.generateSign(yozoParams.getAppId(), yozoParams.getAppKey(), params).getData();
        String url = yozoParams.getCloudDomain() + "/api/file/delete/version?fileVersionId=" + fileVersionId +
                APPID + yozoParams.getAppId() +
                FILE_VERSION_ID + sign;
        String s = HttpUtil.sendHttpGet(url);
        Map<String, Object> maps = JSON.parseObject(s, Map.class);
        String fileName = yozoService.selectByVersionId(fileVersionId).getFileName();
        String path = fileApi.getPath(FileTypeConstant.DOCUMENTPREVIEW) + fileName;
        if (FileUtil.fileIsFile(path)) {
            File file = new File(jnpf.util.XSSEscape.escapePath(path));
            FileUtils.deleteQuietly(file);
        }
        String versionId = (String) maps.get("data");
        return yozoService.deleteFileByVersionId(versionId);
    }
 
    @GetMapping("/batchDelete")
    @Operation(summary = "批量删除文件版本")
    @Parameter(name = "fileVersionIds", description = "主键")
    public ActionResult<Object> batchDelete(@RequestParam("fileVersionIds") String[] fileVersionIds) {
        //获取签名
        Map<String, String[]> params = new HashMap<>();
        params.put("fileVersionIds", fileVersionIds);
 
 
        for (String s : fileVersionIds) {
            String fileName = yozoService.selectByVersionId(s).getFileName();
            String path = fileApi.getPath(FileTypeConstant.DOCUMENTPREVIEW) + fileName;
            File file = new File(jnpf.util.XSSEscape.escapePath(path));
            FileUtils.deleteQuietly(file);
        }
 
 
        return yozoService.deleteBatch(fileVersionIds);
    }
 
    @GetMapping("/editFile")
    @Operation(summary = "在线编辑")
    @Parameter(name = "fileVersionId", description = "主键")
    public ActionResult<Object> editFile(@RequestParam("fileVersionId") String fileVersionId) {
        String newFileVersionId = XSSEscape.escape(fileVersionId);
        //获取签名
        Map<String, String[]> params = new HashMap<>();
        params.put(FILE_VERSION_ID, new String[]{fileVersionId});
        String sign = yozoUtil.generateSign(yozoParams.getAppId(), yozoParams.getAppKey(), params).getData();
        String url = yozoParams.getEditDomain() + "/api/edit/file?fileVersionId=" + newFileVersionId +
                APPID + yozoParams.getAppId() +
                FILE_VERSION_ID + sign;
        return ActionResult.success("success", url);
    }
 
    /**
     * 永中回调
     *
     * @param oldFileId
     * @param newFileId
     * @param message
     * @param errorCode
     * @return
     */
    @PostMapping("/3rd/edit/callBack")
    @Parameter(name = "oldFileId", description = "主键")
    @Parameter(name = "newFileId", description = "主键")
    @Parameter(name = "message", description = "消息")
    @Parameter(name = "errorCode", description = "编码")
    public Map<String, Object> editCallBack(@RequestParam("oldFileId") String oldFileId, @RequestParam("newFileId") String newFileId, @RequestParam("message") String message, @RequestParam("errorCode") Integer errorCode) {
 
        yozoService.editFileVersion(oldFileId, newFileId);
 
        Map<String, Object> result = new HashMap<>();
        result.put("oldFileId", oldFileId);
        result.put("newFileId", newFileId);
        result.put("message", message);
        result.put("errorCode", errorCode);
        result = XSSEscape.escapeObj(result);
        return result;
    }
 
    @PostMapping("/documentList")
    @Operation(summary = "文档列表")
    @Parameter(name = "pageModel", description = "分页模型", required = true)
    public ActionResult<PageListVO<FileEntity>> documentList(@RequestBody PaginationVO pageModel) {
        List<FileEntity> list = yozoService.getAllList(pageModel);
        return ActionResult.page(list, pageModel);
    }
 
    /**
     * 传入新的fileVersionId同步
     *
     * @param fileVersionId
     * @return
     * @throws Exception
     */
    @GetMapping("/updateFile")
    @Operation(summary = "/同步文件版本到本地")
    @Parameter(name = "fileVersionId", description = "主键")
    public ActionResult<Object> updateFile(@RequestParam("fileVersionId") String fileVersionId) throws Exception {
        FileEntity fileEntity = yozoService.selectByVersionId(fileVersionId);
        String fileName = fileEntity.getFileName();
        String path = fileApi.getPath(FileTypeConstant.DOCUMENTPREVIEW) + fileName;
        if (FileUtil.fileIsFile(path)) {
            File file = new File(jnpf.util.XSSEscape.escapePath(path));
            FileUtils.deleteQuietly(file);
        }
        String fileUrl = this.downloadFile(fileVersionId);
        yozoUtil.downloadFile(fileUrl, path);
        return ActionResult.success(MsgCode.SU004.get());
    }
}