|
@ -11,9 +11,12 @@ package com.epmet.controller; |
|
|
import com.epmet.cloud.CloudStorageConfig; |
|
|
import com.epmet.cloud.CloudStorageConfig; |
|
|
import com.epmet.cloud.OssFactory; |
|
|
import com.epmet.cloud.OssFactory; |
|
|
import com.epmet.commons.tools.constant.NumConstant; |
|
|
import com.epmet.commons.tools.constant.NumConstant; |
|
|
|
|
|
import com.epmet.commons.tools.constant.StrConstant; |
|
|
import com.epmet.commons.tools.exception.EpmetErrorCode; |
|
|
import com.epmet.commons.tools.exception.EpmetErrorCode; |
|
|
import com.epmet.commons.tools.exception.RenException; |
|
|
import com.epmet.commons.tools.exception.RenException; |
|
|
import com.epmet.commons.tools.page.PageData; |
|
|
import com.epmet.commons.tools.page.PageData; |
|
|
|
|
|
import com.epmet.commons.tools.redis.RedisKeys; |
|
|
|
|
|
import com.epmet.commons.tools.redis.RedisUtils; |
|
|
import com.epmet.commons.tools.utils.Result; |
|
|
import com.epmet.commons.tools.utils.Result; |
|
|
import com.epmet.commons.tools.validator.ValidatorUtils; |
|
|
import com.epmet.commons.tools.validator.ValidatorUtils; |
|
|
import com.epmet.commons.tools.validator.group.AliyunGroup; |
|
|
import com.epmet.commons.tools.validator.group.AliyunGroup; |
|
@ -32,6 +35,7 @@ import io.swagger.annotations.Api; |
|
|
import io.swagger.annotations.ApiOperation; |
|
|
import io.swagger.annotations.ApiOperation; |
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
import org.apache.commons.io.FilenameUtils; |
|
|
import org.apache.commons.io.FilenameUtils; |
|
|
|
|
|
import org.apache.commons.lang3.StringUtils; |
|
|
import org.apache.http.entity.ContentType; |
|
|
import org.apache.http.entity.ContentType; |
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
import org.springframework.http.MediaType; |
|
|
import org.springframework.http.MediaType; |
|
@ -62,6 +66,8 @@ public class OssController { |
|
|
private OssService ossService; |
|
|
private OssService ossService; |
|
|
@Autowired |
|
|
@Autowired |
|
|
private ParamsRemoteService paramsRemoteService; |
|
|
private ParamsRemoteService paramsRemoteService; |
|
|
|
|
|
@Autowired |
|
|
|
|
|
private RedisUtils redisUtils; |
|
|
|
|
|
|
|
|
private final static String KEY = ModuleConstant.CLOUD_STORAGE_CONFIG_KEY; |
|
|
private final static String KEY = ModuleConstant.CLOUD_STORAGE_CONFIG_KEY; |
|
|
|
|
|
|
|
@ -205,25 +211,52 @@ public class OssController { |
|
|
return ossService.uploadImg(file, null); |
|
|
return ossService.uploadImg(file, null); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* desc: 党建声音模块 上传图片(2M)或附件(5M) |
|
|
|
|
|
* |
|
|
|
|
|
* @param file |
|
|
|
|
|
* @return com.epmet.commons.tools.utils.Result<com.epmet.dto.result.UploadImgResultDTO> |
|
|
|
|
|
* @author LiuJanJun |
|
|
|
|
|
* @date 2021/3/29 4:56 下午 |
|
|
|
|
|
*/ |
|
|
@PostMapping("article/upload") |
|
|
@PostMapping("article/upload") |
|
|
public Result<UploadImgResultDTO> uploadArticleImg(@RequestParam("file") MultipartFile file) { |
|
|
public Result<UploadImgResultDTO> uploadArticleImg(@RequestParam("file") MultipartFile file) { |
|
|
|
|
|
|
|
|
// 校验文件类型
|
|
|
// 校验文件类型 图片或pdf
|
|
|
if (!MediaType.IMAGE_PNG_VALUE.equals(file.getContentType()) && !MediaType.IMAGE_JPEG_VALUE.equals(file.getContentType()) |
|
|
if (!MediaType.IMAGE_PNG_VALUE.equals(file.getContentType()) |
|
|
&& !ModuleConstant.FILE_CONTENT_TYPE_JPG.equals(file.getContentType()) ) { |
|
|
&& !MediaType.IMAGE_JPEG_VALUE.equals(file.getContentType()) |
|
|
log.error("uploadArticleImg file type:{} is not support 2 upload",file.getContentType() == null? null:file.getContentType()); |
|
|
&& !ModuleConstant.FILE_CONTENT_TYPE_JPG.equals(file.getContentType()) |
|
|
|
|
|
&& !ModuleConstant.FILE_CONTENT_TYPE_PDF.equals(file.getContentType())) { |
|
|
|
|
|
log.error("uploadArticleImg file type:{} is not support 2 upload", file.getContentType()); |
|
|
throw new RenException(EpmetErrorCode.OPER_UPLOAD_FILE_TYPE_ERROR.getCode() |
|
|
throw new RenException(EpmetErrorCode.OPER_UPLOAD_FILE_TYPE_ERROR.getCode() |
|
|
, EpmetErrorCode.OPER_UPLOAD_FILE_TYPE_ERROR.getMsg()); |
|
|
, EpmetErrorCode.OPER_UPLOAD_FILE_TYPE_ERROR.getMsg()); |
|
|
} |
|
|
} |
|
|
|
|
|
if (ModuleConstant.FILE_CONTENT_TYPE_PDF.equals(file.getContentType())) { |
|
|
// 校验文件体积,不超过2m
|
|
|
// 校验文件体积,不超过5m
|
|
|
long maxSize = 2 * 1024 * 1024; |
|
|
long maxSize = 5 * 1024 * 1024; |
|
|
long size = file.getSize(); |
|
|
long size = file.getSize(); |
|
|
if (size > maxSize) { |
|
|
if (size > maxSize) { |
|
|
throw new RenException(EpmetErrorCode.OPER_UPLOAD_FILE_OVER_SIZE.getCode() |
|
|
throw new RenException(EpmetErrorCode.OPER_UPLOAD_FILE_OVER_SIZE.getCode() |
|
|
, EpmetErrorCode.OPER_UPLOAD_FILE_OVER_SIZE.getMsg()); |
|
|
, EpmetErrorCode.OPER_UPLOAD_FILE_OVER_SIZE.getMsg()); |
|
|
|
|
|
} |
|
|
|
|
|
//todo 获取url 缓存半小时 删除时获取并调用删除方法
|
|
|
|
|
|
} else { |
|
|
|
|
|
// 校验文件体积,不超过2m
|
|
|
|
|
|
long maxSize = 2 * 1024 * 1024; |
|
|
|
|
|
long size = file.getSize(); |
|
|
|
|
|
if (size > maxSize) { |
|
|
|
|
|
throw new RenException(EpmetErrorCode.OPER_UPLOAD_FILE_OVER_SIZE.getCode() |
|
|
|
|
|
, EpmetErrorCode.OPER_UPLOAD_FILE_OVER_SIZE.getMsg()); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
Result<UploadImgResultDTO> result = ossService.uploadImg(file, null); |
|
|
|
|
|
if (result != null && result.success() ){ |
|
|
|
|
|
UploadImgResultDTO data = result.getData(); |
|
|
|
|
|
if ( data!= null && StringUtils.isNotBlank(data.getUrl())){ |
|
|
|
|
|
String path = data.getUrl().replace(data.getUrl(), StrConstant.EPMETY_STR); |
|
|
|
|
|
redisUtils.set(RedisKeys.getOssFileKey(path),path,RedisUtils.MINUTE_THIRTY_EXPIRE); |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
return ossService.uploadImg(file, null); |
|
|
return ossService.uploadImg(file, null); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|