刘光辉
4 小时以前 06f314545ac3514d7ce28939920866ae8188d31c
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
package jnpf.controller;
 
import cn.dev33.satoken.same.SaSameUtil;
import jnpf.base.ActionResult;
import jnpf.base.ActionResultCode;
import jnpf.exception.DataException;
import jnpf.file.WorkflowFileTicketApi;
import jnpf.fileStorage.WorkflowFileTicketService;
import jnpf.model.PreviewParams;
import jnpf.model.fileaccess.WorkflowFileGrant;
import jnpf.model.fileaccess.WorkflowFileRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import java.util.*;
 
/** 流程附件入口只增加动作准入,执行委托原公共接口,不另建文件传输路径。 */
@RestController
@RequiredArgsConstructor
public class WorkflowFileController implements WorkflowFileTicketApi {
    private final WorkflowFileTicketService tickets;
    // 原打包与下载签发实现在该控制器;直接复用,避免改动公共接口或复制实现。
    private final UtilsController files;
 
    @PostMapping("/WorkflowFile/download")
    public ActionResult<Map<String, Object>> download(@RequestBody List<WorkflowFileRequest> sources) throws Exception {
        List<WorkflowFileGrant> grants = tickets.authorizeDownloads(sources);
        Map<String, Object> result = new LinkedHashMap<>();
        if (grants.size() == 1) {
            WorkflowFileGrant grant = grants.get(0);
            result.put("downloadVo", data(files.downloadUrl("annex", grant.getContext().getFileId())));
            result.put("downloadName", grant.getFile().get("name"));
        } else {
            List<Map<String, String>> entries = new ArrayList<>();
            for (WorkflowFileGrant grant : grants) {
                Map<String, String> entry = new LinkedHashMap<>();
                entry.put("fileId", grant.getContext().getFileId());
                entry.put("fileName", Objects.toString(grant.getFile().get("name"), "附件"));
                entries.add(entry);
            }
            Object packed = data(files.packDownloadUrl("annex", entries));
            if (!(packed instanceof Map)) throw new DataException("获取附件下载地址失败");
            result.putAll((Map<String, Object>) packed);
        }
        return ActionResult.success(result);
    }
 
    @PostMapping("/WorkflowFile/preview")
    public ActionResult<Map<String, Object>> preview(@RequestBody WorkflowFileRequest request,
            @RequestParam(value = "mode", defaultValue = "office") String mode) {
        if (!Arrays.asList("office", "platform").contains(mode)) throw new DataException("预览方式无效");
        WorkflowFileGrant grant = tickets.authorizePreview(request);
        Map<String, Object> file = grant.getFile();
        String name = Objects.toString(file.get("name"), "附件");
        String extension = name.substring(name.lastIndexOf('.') + 1).toLowerCase(Locale.ROOT);
        Map<String, Object> result = new LinkedHashMap<>();
        result.put("file", file);
        result.put("fileName", name);
        if (Arrays.asList("png", "jpg", "jpeg", "bmp", "gif").contains(extension)) {
            result.put("kind", "image");
            result.put("url", file.get("url"));
        } else if ("office".equals(mode)) {
            result.put("kind", "office");
            result.put("ticket", tickets.previewTicket(grant));
        } else {
            PreviewParams params = new PreviewParams();
            params.setFileName(grant.getContext().getFileId());
            params.setFileDownloadUrl(Objects.toString(file.get("url"), ""));
            params.setFileVersionId((String) file.get("fileVersionId"));
            result.put("kind", "platform");
            result.put("url", data(files.preview(params)));
        }
        return ActionResult.success(result);
    }
 
    @Override @PostMapping("/WorkflowFile/preview-access")
    public ActionResult<WorkflowFileGrant> previewAccess(@RequestParam("ticket") String ticket) {
        SaSameUtil.checkCurrentRequestToken();
        return ActionResult.success(tickets.previewAccess(ticket));
    }
 
    private static Object data(ActionResult<?> response) {
        if (response == null || !ActionResultCode.SUCCESS.getCode().equals(response.getCode()) || response.getData() == null)
            throw new DataException(response == null ? "文件服务暂不可用" : response.getMsg());
        return response.getData();
    }
}