wuxw
2022-11-09 a74c647c3151f23f735bb27d826fea08faf834fa
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
package com.java110.boot.smo.file.impl;
 
import com.alibaba.fastjson.JSONObject;
import com.java110.boot.smo.DefaultAbstractComponentSMO;
import com.java110.boot.smo.file.IAddFileSMO;
import com.java110.core.context.IPageData;
import com.java110.core.factory.GenerateCodeFactory;
import com.java110.dto.file.FileDto;
import com.java110.entity.component.ComponentValidateResult;
import com.java110.intf.common.IFileInnerServiceSMO;
import com.java110.utils.cache.MappingCache;
import com.java110.utils.constant.PrivilegeCodeConstant;
import com.java110.utils.util.Assert;
import com.java110.utils.util.Base64Convert;
import com.java110.utils.util.BeanConvertUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;
 
import java.io.IOException;
import java.io.InputStream;
 
/**
 * 添加小区服务实现类
 * add by wuxw 2019-06-30
 */
@Service("addFileSMOImpl")
public class AddFileSMOImpl extends DefaultAbstractComponentSMO implements IAddFileSMO {
 
    @Autowired
    private RestTemplate restTemplate;
 
    @Autowired
    private IFileInnerServiceSMO fileInnerServiceSMOImpl;
 
 
 
    @Override
    public ResponseEntity<String> saveFile(IPageData pd, MultipartFile uploadFile) throws IOException {
 
        JSONObject paramIn = JSONObject.parseObject(pd.getReqData());
        if (uploadFile.getSize() > 2 * 1024 * 1024) {
            throw new IllegalArgumentException("上传文件超过两兆");
        }
 
        Assert.hasKeyAndValue(paramIn, "communityId", "必填,请填写小区ID");
        Assert.hasKeyAndValue(paramIn, "suffix", "必填,请填写文件类型");
        super.checkUserHasPrivilege(pd, restTemplate, PrivilegeCodeConstant.SAVE_FILE);
 
        ComponentValidateResult result = this.validateStoreStaffCommunityRelationship(pd, restTemplate);
        InputStream is = uploadFile.getInputStream();
        String fileContext = Base64Convert.ioToBase64(is);
        paramIn.put("context", fileContext);
        paramIn.put("fileName", uploadFile.getOriginalFilename());
 
        FileDto fileDto = BeanConvertUtil.covertBean(paramIn, FileDto.class);
 
        fileDto.setFileId(GenerateCodeFactory.getGeneratorId(GenerateCodeFactory.CODE_PREFIX_file_id));
 
        String fileName = fileInnerServiceSMOImpl.saveFile(fileDto);
 
 
 
        JSONObject outParam = new JSONObject();
        outParam.put("fileId", fileName);
        String imgUrl = MappingCache.getValue("IMG_PATH");
        outParam.put("url",imgUrl+fileName);
 
        ResponseEntity<String> responseEntity = new ResponseEntity<String>(outParam.toJSONString(), HttpStatus.OK);
 
//
//        String apiUrl = "file.saveFile" ;
//
//
//        ResponseEntity<String> responseEntity = this.callCenterService(restTemplate, pd, paramIn.toJSONString(),
//                apiUrl,
//                HttpMethod.POST);
        return responseEntity;
 
    }
 
    @Override
    public ResponseEntity<String> savePhotoFile(IPageData pd) {
 
        String images = pd.getReqData();
        JSONObject imageObj=JSONObject.parseObject(images);
        JSONObject paramIn = new JSONObject();
        paramIn.put("context", imageObj.getString("uploadFile"));
        paramIn.put("fileName", "upload.jpg");
 
        FileDto fileDto = BeanConvertUtil.covertBean(paramIn, FileDto.class);
 
        fileDto.setFileId(GenerateCodeFactory.getGeneratorId(GenerateCodeFactory.CODE_PREFIX_file_id));
//        String file = paramIn.getString("fileName");
//        if (file.contains("mp4") || file.contains("MP4") || file.contains("AVI") || file.contains("avi")
//                || file.contains("WMV") || file.contains("wmv")) {
//            fileDto.setFlag("");
//        } else {
//            fileDto.setFlag("0");
//        }
 
        String fileName = fileInnerServiceSMOImpl.saveFile(fileDto);
 
        JSONObject outParam = new JSONObject();
        outParam.put("fileId", fileName);
        String imgUrl = MappingCache.getValue("IMG_PATH");
        outParam.put("url", imgUrl + fileName);
 
        ResponseEntity<String> responseEntity = new ResponseEntity<String>(outParam.toJSONString(), HttpStatus.OK);
 
//        String apiUrl = "file.saveFile" ;
 
//        ResponseEntity<String> responseEntity = this.callCenterService(restTemplate, pd, paramIn.toJSONString(),
//                apiUrl,
//                HttpMethod.POST);
        return responseEntity;
    }
 
 
 
    public RestTemplate getRestTemplate() {
        return restTemplate;
    }
 
    public void setRestTemplate(RestTemplate restTemplate) {
        this.restTemplate = restTemplate;
    }
}