ny
昨天 b6f169fe43a2b13f351aefc152374fc7f0bc8cb7
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
package jnpf.controller;
 
import com.alibaba.fastjson.JSONObject;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.Operation;
import jnpf.base.ActionResult;
import jnpf.base.vo.PaginationVO;
import jnpf.config.ConfigValueUtil;
import jnpf.constant.FileTypeConstant;
import jnpf.constant.MsgCode;
import jnpf.entity.FileEntity;
import jnpf.exception.DataException;
import jnpf.file.FileApi;
import jnpf.model.YozoFileParams;
import jnpf.model.YozoParams;
import jnpf.service.YozoService;
import jnpf.util.FileUtil;
import jnpf.util.XSSEscape;
import jnpf.util.YozoUtils;
import jnpf.util.wxutil.HttpUtil;
import org.springframework.beans.factory.annotation.Autowired;
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 = "永中文件预览编辑")
public class YozoFileController {
 
    @Autowired
    private YozoService yozoService;
    @Autowired
    private YozoUtils yozoUtil;
    @Autowired
    private ConfigValueUtil configValueUtil;
    @Autowired
    private FileApi fileApi;
 
 
    @PostMapping("/getUrl")
    @Operation(summary = "在线预览")
    @Parameters({
            @Parameter(name = "params", description = "永中模型", required = true),
    })
    public ActionResult 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 upload(MultipartFile file) throws IOException, DataException {
        //获取签名
        Map<String, String[]> parameter = new HashMap<String, String[]>();
        parameter.put("appId", new String[]{"" });
        String sign = yozoUtil.generateSign(YozoParams.APP_ID, YozoParams.APP_KEY, parameter).getData();
        String resultString = "";
 
        String url = YozoParams.CLOUD_DOMAIN + "/api/file/upload";
 
        //统一上传到本地目录
        String fileName = file.getOriginalFilename();
        String filePath = fileApi.getPath(FileTypeConstant.DOCUMENTPREVIEW);
        String path = filePath + fileName;
        FileUtil.upFile(file, filePath, fileName);
 
        //文件流
        File fileUp = new File(jnpf.util.XSSEscape.escapePath(path));
        resultString = yozoUtil.uploadFile(url, fileUp, YozoParams.APP_ID, sign);
 
        JSONObject.parseObject(resultString);
        Map<String, Object> maps = JSONObject.parseObject(resultString, Map.class);
        Map<String, String> fileMap = (Map<String, String>) maps.get("data");
        String fileVersionId = fileMap.get("fileVersionId");
        String fileId = fileMap.get("fileId");
        ActionResult back = yozoService.saveFileIdByHttp(fileVersionId, fileId, path);
        return back;
    }
 
    @GetMapping("/newCreate")
    @Operation(summary = "新建文件")
    @Parameters({
            @Parameter(name = "fileName", description = "名称"),
            @Parameter(name = "templateType", description = "类型"),
    })
    public ActionResult 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<String, String[]>();
        params.put("templateType", new String[]{templateType});
        params.put("fileName", new String[]{fileName});
        String sign = yozoUtil.generateSign(YozoParams.APP_ID, YozoParams.APP_KEY, params).getData();
        String url = YozoParams.CLOUD_DOMAIN + "/api/file/template?templateType=" + templateType +
                "&fileName=" + fileName +
                "&appId=" + YozoParams.APP_ID +
                "&sign=" + sign;
        String s = HttpUtil.sendHttpPost(url);
        Map<String, Object> maps = JSONObject.parseObject(s, Map.class);
        Map<String, String> fileMap = (Map<String, String>) maps.get("data");
        String fileVersionId = fileMap.get("fileVersionId");
        String fileId = fileMap.get("fileId");
        ActionResult back = yozoService.saveFileId(fileVersionId, fileId, fileNa);
        //在本地新建文件
        FileUtil.createFile(fileApi.getPath(FileTypeConstant.DOCUMENTPREVIEW), fileNa);
        return back;
    }
 
    @GetMapping("/uploadByHttp")
    @Operation(summary = "http上传文件")
    @Parameters({
            @Parameter(name = "fileUrl", description = "路径"),
    })
    public ActionResult uploadByHttp(@RequestParam("fileUrl") String fileUrl) {
        //获取签名
        Map<String, String[]> params = new HashMap<String, String[]>();
        params.put("fileUrl", new String[]{fileUrl});
        String sign = yozoUtil.generateSign(YozoParams.APP_ID, YozoParams.APP_KEY, params).getData();
        String url = YozoParams.CLOUD_DOMAIN + "/api/file/http?fileUrl=" + fileUrl +
                "&appId=" + YozoParams.APP_ID +
                "&sign=" + sign;
        String s = HttpUtil.sendHttpPost(url);
        Map<String, Object> maps = JSONObject.parseObject(s, Map.class);
        Map<String, String> fileMap = (Map<String, String>) maps.get("data");
        String fileVersionId = fileMap.get("fileVersionId");
        String fileId = fileMap.get("fileId");
        ActionResult back = yozoService.saveFileIdByHttp(fileVersionId, fileId, fileUrl);
        return back;
    }
 
    @GetMapping("/downloadFile")
    @Operation(summary = "永中下载文件")
    @Parameters({
            @Parameter(name = "fileVersionId", description = "主键"),
    })
    public String downloadFile(@RequestParam("fileVersionId") String fileVersionId) {
        FileEntity fileEntity = yozoService.selectByVersionId(fileVersionId);
        if (fileEntity == null) {
            return MsgCode.FA044.get();
        }
        //获取签名
        Map<String, String[]> params = new HashMap<String, String[]>();
        params.put("fileVersionId", new String[]{fileVersionId});
        String sign = yozoUtil.generateSign(YozoParams.APP_ID, YozoParams.APP_KEY, params).getData();
        String url = YozoParams.CLOUD_DOMAIN + "/api/file/download?fileVersionId=" + fileVersionId +
                "&appId=" + YozoParams.APP_ID +
                "&sign=" + sign;
        url = XSSEscape.escape(url);
        return url;
    }
 
 
    @GetMapping("/deleteVersionFile")
    @Operation(summary = "删除文件版本")
    @Parameters({
            @Parameter(name = "fileVersionId", description = "主键"),
    })
    public ActionResult deleteVersion(@RequestParam("fileVersionId") String fileVersionId) {
        //获取签名
        Map<String, String[]> params = new HashMap<String, String[]>();
        params.put("fileVersionId", new String[]{fileVersionId});
        String sign = yozoUtil.generateSign(YozoParams.APP_ID, YozoParams.APP_KEY, params).getData();
        String url = YozoParams.CLOUD_DOMAIN + "/api/file/delete/version?fileVersionId=" + fileVersionId +
                "&appId=" + YozoParams.APP_ID +
                "&sign=" + sign;
        String s = HttpUtil.sendHttpGet(url);
        Map<String, Object> maps = JSONObject.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));
            file.delete();
        }
        String versionId = (String) maps.get("data");
        ActionResult back = yozoService.deleteFileByVersionId(versionId);
        return back;
    }
 
    @GetMapping("/batchDelete")
    @Operation(summary = "批量删除文件版本")
    @Parameters({
            @Parameter(name = "fileVersionIds", description = "主键"),
    })
    public ActionResult batchDelete(@RequestParam("fileVersionIds") String[] fileVersionIds) {
        //获取签名
        Map<String, String[]> params = new HashMap<String, String[]>();
        params.put("fileVersionIds", fileVersionIds);
        String sign = yozoUtil.generateSign(YozoParams.APP_ID, YozoParams.APP_KEY, params).getData();
 
        StringBuilder fileVersionIdList = new StringBuilder();
        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));
            file.delete();
            fileVersionIdList.append("fileVersionIds=" + s + "&");
        }
        String list = fileVersionIdList.toString();
        String url = YozoParams.CLOUD_DOMAIN + "/api/file/delete/versions?" + list +
                "appId=" + YozoParams.APP_ID +
                "&sign=" + sign;
        String s = HttpUtil.sendHttpGet(url);
        ActionResult back = yozoService.deleteBatch(fileVersionIds);
        return back;
    }
 
    @GetMapping("/editFile")
    @Operation(summary = "在线编辑")
    @Parameters({
            @Parameter(name = "fileVersionId", description = "主键"),
    })
    public ActionResult editFile(@RequestParam("fileVersionId") String fileVersionId) {
        //获取签名
        Map<String, String[]> params = new HashMap<String, String[]>();
        params.put("fileVersionId", new String[]{fileVersionId});
        String sign = yozoUtil.generateSign(YozoParams.APP_ID, YozoParams.APP_KEY, params).getData();
        String url = YozoParams.EDIT_DOMAIN + "/api/edit/file?fileVersionId=" + fileVersionId +
                "&appId=" + YozoParams.APP_ID +
                "&sign=" + sign;
        return ActionResult.success("success", url);
    }
 
    /**
     * 永中回调
     *
     * @param oldFileId
     * @param newFileId
     * @param message
     * @param errorCode
     * @return
     */
    @PostMapping("/3rd/edit/callBack")
    @Parameters({
            @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 = "文档列表")
    @Parameters({
            @Parameter(name = "pageModel", description = "分页模型", required = true),
    })
    public ActionResult 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 = "/同步文件版本到本地")
    @Parameters({
            @Parameter(name = "fileVersionId", description = "主键"),
    })
    public ActionResult 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));
            file.delete();
        }
        String fileUrl = this.downloadFile(fileVersionId);
        yozoUtil.downloadFile(fileUrl, path);
        return ActionResult.success(MsgCode.SU004.get());
    }
}