|
|
@@ -0,0 +1,100 @@
|
|
|
+package com.fdage.controller.app;
|
|
|
+
|
|
|
+import fdage.back.sdk.base.entity.Result;
|
|
|
+import fdage.back.sdk.core.alibabaUtils.AlibabaOssHelper;
|
|
|
+import io.swagger.annotations.Api;
|
|
|
+import io.swagger.annotations.ApiOperation;
|
|
|
+import lombok.extern.log4j.Log4j2;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.web.bind.annotation.PostMapping;
|
|
|
+import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
+import org.springframework.web.bind.annotation.RequestParam;
|
|
|
+import org.springframework.web.bind.annotation.RestController;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
+
|
|
|
+import java.io.File;
|
|
|
+import java.io.IOException;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.regex.Matcher;
|
|
|
+import java.util.regex.Pattern;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 2 * @Author: Abner
|
|
|
+ * 3 * @Date: 2021/3/2 12:15
|
|
|
+ * 4
|
|
|
+ */
|
|
|
+@Api(tags = "小程序公共接口")
|
|
|
+@RestController
|
|
|
+@RequestMapping("app/common")
|
|
|
+@Log4j2
|
|
|
+public class AppCommonController {
|
|
|
+
|
|
|
+
|
|
|
+ @Value("${image.local.path}")
|
|
|
+ private String imageLocalPath;
|
|
|
+
|
|
|
+ @Value("${image.oss.path}")
|
|
|
+ private String ossPath;
|
|
|
+
|
|
|
+ @Value("${oss.query.url}")
|
|
|
+ private String ossQueryUrl;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private AlibabaOssHelper alibabaOssHelper;
|
|
|
+
|
|
|
+ public static String IMAGE_PATH = ".+(.JPEG|.jpeg|.JPG|.jpg|.GIF|.gif|.BMP|.bmp|.PNG|.png)";
|
|
|
+
|
|
|
+ @ApiOperation(value = "上传照片")
|
|
|
+ @PostMapping("/uploadImage")
|
|
|
+ public Result uploadImage(@RequestParam("file") MultipartFile file) {
|
|
|
+ if (null == file) {
|
|
|
+ return Result.failure("文件缺失");
|
|
|
+ }
|
|
|
+ Pattern pattern = Pattern.compile(IMAGE_PATH);
|
|
|
+ Matcher matcher = pattern.matcher(file.getOriginalFilename());
|
|
|
+ //TODO:这里限制照片大小,如果很大,则缩略
|
|
|
+ if(!matcher.find()){
|
|
|
+ return Result.failure("只能上传照片格式的文件");
|
|
|
+ }
|
|
|
+ int index = file.getOriginalFilename().indexOf(".");
|
|
|
+ String newFileName = System.currentTimeMillis() + "";
|
|
|
+ if(index != -1){
|
|
|
+ newFileName = newFileName + file.getOriginalFilename().substring(index);
|
|
|
+ }
|
|
|
+ if(StringUtils.isBlank(newFileName)){
|
|
|
+ return Result.failure("获取文件后缀名失败");
|
|
|
+ }
|
|
|
+ String fullLocalDirect = imageLocalPath;
|
|
|
+ File direct = new File(fullLocalDirect);
|
|
|
+ if(!direct.exists()){
|
|
|
+ direct.mkdir();
|
|
|
+ }
|
|
|
+ String fullLocalImagePath = imageLocalPath.concat(File.separator).concat(newFileName);
|
|
|
+ log.info("照片文件下载到本地路径为:{}", fullLocalImagePath);
|
|
|
+ File totalFile = new File(fullLocalImagePath);
|
|
|
+ if(totalFile.exists()){
|
|
|
+ totalFile.delete();
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ file.transferTo(totalFile);
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ log.info("照片文件已经下载到本地出现异常:{}", e);
|
|
|
+ return Result.failure("下载文件出现异常");
|
|
|
+ }
|
|
|
+ if(!totalFile.exists()){
|
|
|
+ log.info("缓存照片到本地失败:{}" , fullLocalImagePath);
|
|
|
+ return Result.failure("上传照片失败");
|
|
|
+ }
|
|
|
+ String totalOssQueryPath = "";
|
|
|
+ String resultOssPath = ossPath + newFileName;
|
|
|
+ alibabaOssHelper.doUploadThenDelete(fullLocalImagePath , resultOssPath);
|
|
|
+ totalOssQueryPath = ossQueryUrl + resultOssPath;
|
|
|
+ Map<String , Object> result = new HashMap<>();
|
|
|
+ result.put("link" , totalOssQueryPath);
|
|
|
+ return Result.success(result);
|
|
|
+ }
|
|
|
+}
|