huanghongfa
2021-09-02 177249c76aeea0b4bf8d8816d4994e3b445b45ce
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
package com.panzhihua.grid_app.api;
 
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicBoolean;
 
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
 
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
 
import com.panzhihua.common.constants.FtpConstants;
import com.panzhihua.common.model.vos.R;
import com.panzhihua.common.service.community.CommunityService;
import com.panzhihua.common.utlis.SFTPUtil;
 
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
 
/**
 * @program: springcloud_k8s_panzhihuazhihuishequ
 * @description: 公共接口
 * @author: huang.hongfa weixin hhf9596 qq 959656820
 * @create: 2020-12-10 15:41
 **/
@Slf4j
@RestController
@RequestMapping("/common/")
@Api(tags = {"公共接口"})
public class CommonApi {
 
    /**
     * 允许的图片文件后缀
     */
    private static List<String> fileExtensionAllow = Arrays.asList("gif", "jpg", "png", "jpeg");
    /**
     * 允许的视频文件后缀
     */
    private static List<String> videoExtensionAllow = Arrays.asList("mp4", "mov");
    // FTP 登录用户名
    @Value("${ftp.username}")
    private String userName;
    // FTP 登录密码
    @Value("${ftp.password}")
    private String password;
    // FTP 服务器地址IP地址
    @Value("${ftp.host}")
    private String host;
    // FTP 端口
    @Value("${ftp.port}")
    private int port;
    @Value("${ftp.url}")
    private String url;
    @Resource
    private CommunityService communityService;
 
    @ApiOperation(value = "上传照片/视频 (jpg/jpeg/png/mp4/mov)")
    @PostMapping(value = "uploadimage", consumes = "multipart/*", headers = "content-type=multipart/form-date")
    public R uploadImage(@RequestParam MultipartFile file, HttpServletRequest request) throws IOException {
        // 微信图片内容校验
        // WxMaSecCheckService wxMaSecCheckService = wxMaConfiguration.getMaService().getSecCheckService();
        String property = System.getProperty("user.dir");
 
        String fileExtension = ".jpg";
 
        String originName = file.getOriginalFilename();
        AtomicBoolean isVideo = new AtomicBoolean(false);
        videoExtensionAllow.forEach(ext -> {
            String originNameLowerCase = originName.toLowerCase();
            if (originNameLowerCase.endsWith("." + ext)) {
                isVideo.set(true);
            }
        });
        if (isVideo.get()) {
            fileExtension = ".mp4";
        }
 
        if (originName.toLowerCase().endsWith(".mp3")) {
            fileExtension = ".mp3";
        }
 
        String fileName = property + File.separator + UUID.randomUUID().toString().replace("-", "") + fileExtension;
        File file1 = new File(fileName);
 
        String name = file.getOriginalFilename();
        name = UUID.randomUUID().toString().replaceAll("-", "") + fileExtension;
        try {
            SFTPUtil sftp = new SFTPUtil(userName, password, host, port);
            sftp.login();
            InputStream is = file.getInputStream();
            file.transferTo(file1);
            boolean delete = file1.delete();
            log.info("临时文件删除【{}】", delete);
            sftp.uploadMore(FtpConstants.FTPFILEPATH_IDCARD, name, is);
            sftp.logout();
            return R.ok(url + "idcard/" + name);
        } catch (Exception e) {
            log.error("上传照片失败【{}】", e.getMessage());
            return R.fail();
        }
 
    }
 
    @ApiOperation(value = "批量上传照片/视频 (jpg/jpeg/png/mp4/mov)")
    @PostMapping(value = "uploads", consumes = "multipart/*", headers = "content-type=multipart/form-date")
    public R uploadImages(@RequestParam MultipartFile[] files, HttpServletRequest request) throws IOException {
        // 微信图片内容校验
        // WxMaSecCheckService wxMaSecCheckService = wxMaConfiguration.getMaService().getSecCheckService();
        String property = System.getProperty("user.dir");
        String fileExtension = ".jpg";
 
        List<String> urlList = new ArrayList<>();
        SFTPUtil sftp = new SFTPUtil(userName, password, host, port);
        sftp.login();
 
        for (MultipartFile file : files) {
            String originName = file.getOriginalFilename();
            AtomicBoolean isVideo = new AtomicBoolean(false);
            videoExtensionAllow.forEach(ext -> {
                String originNameLowerCase = originName.toLowerCase();
                if (originNameLowerCase.endsWith("." + ext)) {
                    isVideo.set(true);
                }
            });
            if (isVideo.get()) {
                fileExtension = ".mp4";
            }
 
            if (originName.toLowerCase().endsWith(".mp3")) {
                fileExtension = ".mp3";
            }
 
            String name = UUID.randomUUID().toString().replaceAll("-", "") + fileExtension;
            try {
                InputStream is = file.getInputStream();
                String fileName =
                    property + File.separator + UUID.randomUUID().toString().replace("-", "") + fileExtension;
                File file1 = new File(fileName);
                file.transferTo(file1);
                boolean delete = file1.delete();
                log.info("临时文件删除【{}】", delete);
                sftp.uploadMore(FtpConstants.FTPFILEPATH_IDCARD, name, is);
                urlList.add(url + "idcard/" + name);
            } catch (Exception e) {
                log.error("上传文件失败【{}】", e.getMessage());
                return R.fail();
            }
        }
        sftp.logout();
        return R.ok(urlList);
    }
 
    @GetMapping("/getConf/noToken")
    @ApiOperation(value = "根据code查询系统配置")
    public R getConf(@RequestParam("code") String code) {
        return communityService.getConf(code);
    }
}