shikeyin
2024-01-11 65da8373531677b1c37a98f53eaa30c892f35e5a
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
package com.iplatform.file.controller;
 
import com.iplatform.base.ArgumentsConstants;
import com.iplatform.base.Constants;
import com.iplatform.base.SystemController;
import com.iplatform.file.service.FileServiceImpl;
import com.iplatform.file.util.FileResultUtils;
import com.iplatform.file.util.FileStoreUtils;
import com.iplatform.file.util.ImageUtils;
import com.iplatform.model.po.SfAttachment;
import com.walker.db.page.GenericPager;
import com.walker.file.FileInfo;
import com.walker.file.FileStoreType;
import com.walker.infrastructure.utils.StringUtils;
import com.walker.web.ResponseValue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
 
import java.util.List;
 
@RestController
@RequestMapping("/platform/attachment")
public class AttachmentController extends SystemController {
 
    private FileServiceImpl fileService = null;
 
    @Autowired
    public AttachmentController(FileServiceImpl fileService){
        this.fileService = fileService;
    }
 
    @RequestMapping(value = "/upload/file", method = RequestMethod.POST)
    public ResponseValue uploadFile(MultipartFile multipart, String model, Integer pid){
        String owner = String.valueOf(this.getOwner());
        String fileStoreType = this.getArgumentVariable(ArgumentsConstants.CONFIG_UPLOAD_TYPE).getStringValue();
        try{
            FileInfo fileInfo = null;
            if(fileStoreType.equals(FileStoreType.INDEX_FS)){
                // 本地存储
                fileInfo = this.uploadFileToLocal(multipart, null, pid, owner, Constants.UPLOAD_AFTER_FILE_KEYWORD);
 
            } else {
                // 远程存储
                fileInfo = this.uploadFileToRemote(multipart, null, pid, owner, Constants.UPLOAD_AFTER_FILE_KEYWORD);
            }
            return ResponseValue.success(FileResultUtils.acquireFileResultVo(fileInfo, multipart.getContentType()));
 
        } catch (Exception ex){
            logger.error("文件上传错误:" + ex.getMessage(), ex);
            return ResponseValue.error(ex.getMessage());
        }
    }
 
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "model", value = "模块 用户user,商品product,微信wechat,文章article,系统system"),
//            @ApiImplicitParam(name = "pid", value = "分类ID 0编辑器,1商品图片,2拼团图片,3砍价图片,4秒杀图片,5文章图片,6组合数据图,7前台用户,8微信系列 ", allowableValues = "range[0,1,2,3,4,5,6,7,8]")
//    })
    @RequestMapping(value = "/upload/image", method = RequestMethod.POST)
    public ResponseValue uploadImage(MultipartFile multipart, String model, Integer pid){
        String owner = String.valueOf(this.getOwner());
        String fileStoreType = this.getArgumentVariable(ArgumentsConstants.CONFIG_UPLOAD_TYPE).getStringValue();
        try{
            FileInfo fileInfo = null;
            if(fileStoreType.equals(FileStoreType.INDEX_FS)){
                // 本地存储
                fileInfo = this.uploadFileToLocal(multipart, null, pid, owner, Constants.UPLOAD_AFTER_IMAGE_KEYWORD);
 
            } else {
                // 远程存储
                fileInfo = this.uploadFileToRemote(multipart, null, pid, owner, Constants.UPLOAD_AFTER_IMAGE_KEYWORD);
            }
            return ResponseValue.success(FileResultUtils.acquireFileResultVo(fileInfo, multipart.getContentType()));
 
        } catch (Exception ex){
            logger.error("图片上传错误:" + ex.getMessage(), ex);
            return ResponseValue.error(ex.getMessage());
        }
    }
 
    /**
     * 分页列出文件信息,在文件(图片)选择组件中使用。
     * @param pid
     * @param attType 文件类型,默认值:png,jpeg,jpg,audio/mpeg,text/plain,video/mp4,gif
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public ResponseValue list(Integer pid, String attType){
        List<String> attTypeList = null;
        if(StringUtils.isNotEmpty(attType)){
            attTypeList = ImageUtils.acquireFileTypeList(attType);
        }
        long owner = this.getOwner();
        logger.debug("owner = " + owner);
 
        GenericPager<SfAttachment> pager = this.fileService.queryPageAttachmentList((int)owner, pid, attTypeList);
        if(pager.getDatas() != null){
            String fileStoreType = null;
            for(SfAttachment attachment : pager.getDatas()){
                fileStoreType = attachment.getFileStoreType();
                if(StringUtils.isEmpty(attachment.getSattDir())){
                    continue;
                }
                attachment.setSattDir(FileStoreUtils.fileStoreTypeUrlPrefix.get(fileStoreType) + attachment.getSattDir());
            }
        }
        return ResponseValue.success(pager);
    }
 
//    /**
//     * 返回文件存储不同方式,对应的文件URL前缀。
//     * @param fileStoreType
//     * @return
//     */
//    private String getFileUrlPrefix(String fileStoreType){
//        String fileUrlKey = FileStoreUtils.getFileUrlPrefixKey(fileStoreType);
//        return this.getArgumentVariable(fileUrlKey).getStringValue();
//    }
}