|
@@ -10,6 +10,8 @@ import cn.kdan.pdf.backend.core.pojo.app.UpTokenVo;
|
|
|
import cn.kdan.pdf.backend.core.properties.OssProperties;
|
|
|
import cn.kdan.pdf.backend.core.service.AvatarService;
|
|
|
import cn.kdan.pdf.backend.core.service.MemberService;
|
|
|
+import cn.kdan.pdf.backend.core.utils.CommonBusinessUtils;
|
|
|
+import exception.BackendRuntimeException;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.beans.BeanUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
@@ -18,6 +20,7 @@ import org.springframework.util.CollectionUtils;
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
import utils.CommonUtils;
|
|
|
|
|
|
+import javax.activation.MimetypesFileTypeMap;
|
|
|
import javax.annotation.Resource;
|
|
|
import java.io.IOException;
|
|
|
import java.util.Date;
|
|
@@ -40,6 +43,14 @@ public class AvatarServiceImpl implements AvatarService {
|
|
|
@Autowired
|
|
|
private OssProperties ossProperties;
|
|
|
|
|
|
+ // 图片文件类型
|
|
|
+ public static String IMG_TYPE_PNG = "PNG";
|
|
|
+ public static String IMG_TYPE_JPG = "JPG";
|
|
|
+ public static String IMG_TYPE_JPEG = "JPEG";
|
|
|
+ public static String IMG_TYPE_DMG = "BMP";
|
|
|
+ public static String IMG_TYPE_GIF = "GIF";
|
|
|
+ public static String IMG_TYPE_SVG = "SVG";
|
|
|
+
|
|
|
|
|
|
@Override
|
|
|
public List<Avatars> list() {
|
|
@@ -68,6 +79,7 @@ public class AvatarServiceImpl implements AvatarService {
|
|
|
@Override
|
|
|
public UpTokenVo picUpload(MultipartFile file) {
|
|
|
UpTokenVo upTokenVo = new UpTokenVo();
|
|
|
+ checkFile(file);
|
|
|
Members currentUser = memberService.getCurrentUser();
|
|
|
String memberId = currentUser.getId();
|
|
|
|
|
@@ -117,4 +129,25 @@ public class AvatarServiceImpl implements AvatarService {
|
|
|
return upTokenVo;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 检查文件类型大小等
|
|
|
+ * @param file
|
|
|
+ */
|
|
|
+ private void checkFile(MultipartFile file) {
|
|
|
+ long size = file.getSize();
|
|
|
+ String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".") + 1);
|
|
|
+ if(!(IMG_TYPE_DMG.equals(suffix.toUpperCase()) ||
|
|
|
+ IMG_TYPE_GIF.equals(suffix.toUpperCase()) ||
|
|
|
+ IMG_TYPE_JPEG.equals(suffix.toUpperCase()) ||
|
|
|
+ IMG_TYPE_JPG.equals(suffix.toUpperCase()) ||
|
|
|
+ IMG_TYPE_PNG.equals(suffix.toUpperCase()) ||
|
|
|
+ IMG_TYPE_SVG.equals(suffix.toUpperCase()))){
|
|
|
+ throw new BackendRuntimeException("请上传图片类型的文件!");
|
|
|
+ }
|
|
|
+ boolean flag = CommonBusinessUtils.checkFileSize(size, 2, "M");
|
|
|
+ if(!flag){
|
|
|
+ throw new BackendRuntimeException("上传图片太大!");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
}
|