You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
116 lines
3.7 KiB
116 lines
3.7 KiB
package com.qs.serve.controller;
|
|
|
|
import com.qs.serve.common.config.properties.UploadProperties;
|
|
import com.qs.serve.common.model.annotation.SysLog;
|
|
import com.qs.serve.common.model.dto.R;
|
|
import com.qs.serve.common.model.enums.BizType;
|
|
import com.qs.serve.common.util.*;
|
|
import com.qs.serve.modules.oss.service.OssService;
|
|
import lombok.AllArgsConstructor;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.springframework.web.bind.annotation.*;
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
import org.springframework.web.multipart.MultipartHttpServletRequest;
|
|
import org.springframework.web.util.WebUtils;
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
import java.io.File;
|
|
import java.io.IOException;
|
|
import java.time.LocalDate;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
/**
|
|
* 门户:后台通用接口
|
|
* @author YenHex
|
|
* @since 2022/3/14
|
|
*/
|
|
@Slf4j
|
|
@AllArgsConstructor
|
|
@RestController
|
|
@RequestMapping("/common")
|
|
public class CommonController {
|
|
|
|
private UploadProperties uploadProperties;
|
|
|
|
private OssService ossService;
|
|
|
|
/**
|
|
* 获取签名
|
|
* @param request
|
|
* @return
|
|
*/
|
|
@SysLog(title = "文件",biz = BizType.UPLOAD)
|
|
@GetMapping("/getSign")
|
|
public R getSign(HttpServletRequest request) {
|
|
return R.ok(ossService.getPolicySign());
|
|
}
|
|
|
|
|
|
/**
|
|
* 单图上传
|
|
* @apiNote file不能为空
|
|
* @param request
|
|
* @return
|
|
*/
|
|
@SysLog(title = "文件",biz = BizType.UPLOAD)
|
|
@PostMapping("/upload")
|
|
public R upload(HttpServletRequest request) {
|
|
String relativePath = UploadUtil.put(uploadProperties.getLogicalPath(),IdUtil.timeStampId(),request);
|
|
if(relativePath!=null){
|
|
return R.ok(uploadProperties.getProxyUrl()+relativePath,"上传成功");
|
|
}
|
|
return R.error("上传失败");
|
|
}
|
|
|
|
/**
|
|
* 验证token
|
|
* @param token
|
|
* @return
|
|
*/
|
|
@PostMapping("/validToken")
|
|
public R validToken(String token) {
|
|
return R.isOk(JwtUtils.verify(token),"无效token");
|
|
}
|
|
|
|
/**
|
|
* 多图上传
|
|
* @apiNote files不能为空
|
|
* @param request
|
|
* @return
|
|
*/
|
|
@SysLog(title = "文件",biz = BizType.UPLOAD)
|
|
@PostMapping( "/uploadMulti")
|
|
public R<?> multiUpload(HttpServletRequest request) {
|
|
String contentType = request.getContentType();
|
|
List<MultipartFile> files = null;
|
|
if (contentType != null && contentType.toLowerCase().startsWith("multipart")) {
|
|
MultipartHttpServletRequest multipartHttpServletRequest = WebUtils.getNativeRequest(request, MultipartHttpServletRequest.class);
|
|
files = multipartHttpServletRequest.getFiles("files");
|
|
}
|
|
if (files==null||files.isEmpty()) {
|
|
return R.error("请选择文件");
|
|
}
|
|
List<String> list = new ArrayList<>();
|
|
for (int i = 0; i < files.size(); i++) {
|
|
MultipartFile file = files.get(i);
|
|
if (file.isEmpty()) {
|
|
return R.error("上传第" + (i++) + "个文件失败");
|
|
}
|
|
String fileOrgName = file.getOriginalFilename();
|
|
String mimeType = fileOrgName.substring(fileOrgName.lastIndexOf("."));
|
|
String filePath = LocalDate.now().toString().replace("-","")+"/"+ IdUtil.timeStampId()+mimeType;
|
|
String uploadPath = uploadProperties.getLogicalPath();
|
|
File dest = new File(uploadPath + filePath);
|
|
try {
|
|
file.transferTo(dest);
|
|
list.add(uploadProperties.getProxyUrl()+filePath);
|
|
} catch (IOException e) {
|
|
log.error(e.toString(), e);
|
|
return R.error("上传第" + (i++) + "个文件失败");
|
|
}
|
|
}
|
|
return R.ok(list);
|
|
}
|
|
|
|
}
|
|
|