From 025388cc86359c33fc05677274122aea92fced7b Mon Sep 17 00:00:00 2001 From: wanggeng <450292408@qq.com> Date: Wed, 8 Dec 2021 20:48:11 +0800 Subject: [PATCH] =?UTF-8?q?1.=E5=8F=96=E6=B6=88=E9=9F=B3=E8=A7=86=E9=A2=91?= =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E9=95=BF=E5=BA=A6=E9=99=90=E5=88=B6=202.?= =?UTF-8?q?=E6=9B=B4=E6=96=B0minio=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- module-file/pom.xml | 8 +--- .../file/controller/api/FileController.java | 10 ---- .../controller/app/api/FileAppController.java | 14 ------ .../module/file/service/IFileService.java | 15 ------ .../file/service/impl/FileServiceImpl.java | 47 ------------------- .../service/impl/MinIoFileServiceImpl.java | 27 ++++++----- module-file/src/test/java/MinIOTest.java | 35 +------------- pom.xml | 2 +- 8 files changed, 17 insertions(+), 141 deletions(-) diff --git a/module-file/pom.xml b/module-file/pom.xml index 80c48a20..58dc8620 100644 --- a/module-file/pom.xml +++ b/module-file/pom.xml @@ -19,13 +19,6 @@ 1.0-SNAPSHOT - - - it.sauronsoftware - jave - - - net.coobird @@ -39,6 +32,7 @@ minio + \ No newline at end of file diff --git a/module-file/src/main/java/ink/wgink/module/file/controller/api/FileController.java b/module-file/src/main/java/ink/wgink/module/file/controller/api/FileController.java index 0527bde7..058b10cc 100644 --- a/module-file/src/main/java/ink/wgink/module/file/controller/api/FileController.java +++ b/module-file/src/main/java/ink/wgink/module/file/controller/api/FileController.java @@ -3,7 +3,6 @@ package ink.wgink.module.file.controller.api; import com.alibaba.fastjson.JSONObject; import ink.wgink.common.base.DefaultBaseController; import ink.wgink.interfaces.consts.ISystemConstant; -import ink.wgink.properties.FileProperties; import ink.wgink.module.file.enums.UploadTypeEnum; import ink.wgink.module.file.pojo.dtos.FileDTO; import ink.wgink.module.file.pojo.dtos.FileInfoDTO; @@ -17,7 +16,6 @@ import io.swagger.annotations.*; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; -import org.springframework.web.servlet.mvc.method.annotation.SseEmitter; import javax.servlet.http.HttpServletRequest; import java.io.IOException; @@ -40,8 +38,6 @@ public class FileController extends DefaultBaseController { @Autowired private IFileService fileService; - @Autowired - private FileProperties fileProperties; @ApiOperation(value = "文件分页列表", notes = "文件分页列表接口") @ApiImplicitParams({ @@ -104,9 +100,6 @@ public class FileController extends DefaultBaseController { @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)}) @PostMapping("uploadvideo") public Callable> uploadVideo(@RequestParam("video") MultipartFile video) { - if (fileProperties.getMediaMaxDuration() != null && fileProperties.getMediaMaxDuration().getBackend() != null) { - fileService.checkVideoDurationAllow(video, fileProperties.getMediaMaxDuration().getBackend().getVideo()); - } Map params = requestParams(); return asyncUploadSingle(video, UploadTypeEnum.VIDEO, params); } @@ -118,9 +111,6 @@ public class FileController extends DefaultBaseController { @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)}) @PostMapping("uploadaudio") public Callable> uploadAudio(@RequestParam("audio") MultipartFile audio) { - if (fileProperties.getMediaMaxDuration() != null && fileProperties.getMediaMaxDuration().getBackend() != null) { - fileService.checkAudioDurationAllow(audio, fileProperties.getMediaMaxDuration().getBackend().getAudio()); - } Map params = requestParams(); return asyncUploadSingle(audio, UploadTypeEnum.AUDIO, params); } diff --git a/module-file/src/main/java/ink/wgink/module/file/controller/app/api/FileAppController.java b/module-file/src/main/java/ink/wgink/module/file/controller/app/api/FileAppController.java index e5f650c2..d86d5788 100644 --- a/module-file/src/main/java/ink/wgink/module/file/controller/app/api/FileAppController.java +++ b/module-file/src/main/java/ink/wgink/module/file/controller/app/api/FileAppController.java @@ -37,8 +37,6 @@ public class FileAppController extends DefaultBaseController { @Autowired private IFileService fileService; - @Autowired - private FileProperties fileProperties; @ApiOperation(value = "上传文件", notes = "上传文件接口") @ApiImplicitParams({ @@ -96,9 +94,6 @@ public class FileAppController extends DefaultBaseController { @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)}) @PostMapping("uploadvideo") public Callable> uploadVideo(@RequestHeader("token") String token, @RequestParam("video") MultipartFile video) { - if (fileProperties.getMediaMaxDuration() != null && fileProperties.getMediaMaxDuration().getApp() != null) { - fileService.checkVideoDurationAllow(video, fileProperties.getMediaMaxDuration().getApp().getVideo()); - } Map params = requestParams(); return asyncUploadSingle(token, video, UploadTypeEnum.VIDEO, params); } @@ -111,9 +106,6 @@ public class FileAppController extends DefaultBaseController { @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)}) @PostMapping("upload-video-release/{userId}") public Callable> uploadVideoByUserId(@PathVariable("userId") String userId, @RequestParam("video") MultipartFile video) { - if (fileProperties.getMediaMaxDuration() != null && fileProperties.getMediaMaxDuration().getApp() != null) { - fileService.checkVideoDurationAllow(video, fileProperties.getMediaMaxDuration().getApp().getVideo()); - } Map params = requestParams(); return asyncUploadSingleByUserId(userId, video, UploadTypeEnum.VIDEO, params); } @@ -126,9 +118,6 @@ public class FileAppController extends DefaultBaseController { @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)}) @PostMapping("uploadaudio") public Callable> uploadAudio(@RequestHeader("token") String token, @RequestParam("audio") MultipartFile audio) { - if (fileProperties.getMediaMaxDuration() != null && fileProperties.getMediaMaxDuration().getApp() != null) { - fileService.checkAudioDurationAllow(audio, fileProperties.getMediaMaxDuration().getApp().getAudio()); - } Map params = requestParams(); return asyncUploadSingle(token, audio, UploadTypeEnum.AUDIO, params); } @@ -141,9 +130,6 @@ public class FileAppController extends DefaultBaseController { @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)}) @PostMapping("upload-audio-release/{userId}") public Callable> uploadAudioByUserId(@PathVariable("userId") String userId, @RequestParam("audio") MultipartFile audio) { - if (fileProperties.getMediaMaxDuration() != null && fileProperties.getMediaMaxDuration().getApp() != null) { - fileService.checkAudioDurationAllow(audio, fileProperties.getMediaMaxDuration().getApp().getAudio()); - } Map params = requestParams(); return asyncUploadSingleByUserId(userId, audio, UploadTypeEnum.AUDIO, params); } diff --git a/module-file/src/main/java/ink/wgink/module/file/service/IFileService.java b/module-file/src/main/java/ink/wgink/module/file/service/IFileService.java index 792be513..a8312a6c 100644 --- a/module-file/src/main/java/ink/wgink/module/file/service/IFileService.java +++ b/module-file/src/main/java/ink/wgink/module/file/service/IFileService.java @@ -263,19 +263,4 @@ public interface IFileService { */ List listPO(List idList); - /** - * 校验视频长度是否符合 - * - * @param uploadFile - * @param maxDuration - */ - void checkVideoDurationAllow(MultipartFile uploadFile, long maxDuration); - - /** - * 校验音频长度是否符合 - * - * @param uploadFile - * @param maxDuration - */ - void checkAudioDurationAllow(MultipartFile uploadFile, long maxDuration); } diff --git a/module-file/src/main/java/ink/wgink/module/file/service/impl/FileServiceImpl.java b/module-file/src/main/java/ink/wgink/module/file/service/impl/FileServiceImpl.java index fa110558..25f76a5f 100644 --- a/module-file/src/main/java/ink/wgink/module/file/service/impl/FileServiceImpl.java +++ b/module-file/src/main/java/ink/wgink/module/file/service/impl/FileServiceImpl.java @@ -25,21 +25,15 @@ import ink.wgink.properties.FileProperties; import ink.wgink.util.ResourceUtil; import ink.wgink.util.UUIDUtil; import ink.wgink.util.map.HashMapUtil; -import it.sauronsoftware.jave.Encoder; -import it.sauronsoftware.jave.EncoderException; -import it.sauronsoftware.jave.MultimediaInfo; -import org.apache.commons.fileupload.disk.DiskFileItem; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; -import org.springframework.web.multipart.commons.CommonsMultipartFile; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; -import java.io.File; import java.io.IOException; import java.util.*; @@ -55,7 +49,6 @@ public class FileServiceImpl extends DefaultBaseService implements IFileService private static final Logger LOG = LoggerFactory.getLogger(FileServiceImpl.class); - @Autowired private FileProperties fileProperties; @Autowired @@ -280,46 +273,6 @@ public class FileServiceImpl extends DefaultBaseService implements IFileService return fileDao.getPO(params); } - @Override - public void checkVideoDurationAllow(MultipartFile uploadFile, long maxDuration) { - if (maxDuration <= 0) { - return; - } - checkDurationAllow(uploadFile, maxDuration, "视频"); - } - - @Override - public void checkAudioDurationAllow(MultipartFile uploadFile, long maxDuration) { - if (maxDuration <= 0) { - return; - } - checkDurationAllow(uploadFile, maxDuration, "音频"); - } - - /** - * 校验音、视频长度是否符合 - * - * @param uploadFile - * @param maxDuration - */ - private void checkDurationAllow(MultipartFile uploadFile, long maxDuration, String name) { - CommonsMultipartFile commonsMultipartFile = (CommonsMultipartFile) uploadFile; - DiskFileItem diskFileItem = (DiskFileItem) commonsMultipartFile.getFileItem(); - File source = diskFileItem.getStoreLocation(); - Encoder encoder = new Encoder(); - MultimediaInfo multimediaInfo; - try { - multimediaInfo = encoder.getInfo(source); - } catch (EncoderException e) { - e.printStackTrace(); - throw new SystemException("文件解析错误", e); - } - long fileDuration = multimediaInfo.getDuration() / 1000; - if (fileDuration > maxDuration) { - throw new FileException(name + "时间超过 " + maxDuration + " 秒"); - } - } - /** * uEditor文件列表 * diff --git a/module-file/src/main/java/ink/wgink/module/file/service/impl/MinIoFileServiceImpl.java b/module-file/src/main/java/ink/wgink/module/file/service/impl/MinIoFileServiceImpl.java index 2dfc1eb1..fee18923 100644 --- a/module-file/src/main/java/ink/wgink/module/file/service/impl/MinIoFileServiceImpl.java +++ b/module-file/src/main/java/ink/wgink/module/file/service/impl/MinIoFileServiceImpl.java @@ -12,8 +12,7 @@ import ink.wgink.properties.FileMinIoProperties; import ink.wgink.properties.FileProperties; import ink.wgink.util.UUIDUtil; import ink.wgink.util.request.StaticResourceRequestUtil; -import io.minio.MinioClient; -import io.minio.PutObjectOptions; +import io.minio.*; import io.minio.errors.*; import org.apache.commons.lang3.StringUtils; import org.joda.time.DateTime; @@ -51,14 +50,16 @@ public class MinIoFileServiceImpl extends DefaultBaseService implements IMinIoFi private FileMinIoProperties minIoProperties; private MinioClient minioClient; - @PostConstruct - public void init() throws InvalidPortException, InvalidEndpointException { + public void init() { if (!fileProperties.getUseMinIo()) { return; } minIoProperties = fileProperties.getMinIo(); - minioClient = new MinioClient(minIoProperties.getEndpoint(), minIoProperties.getAccessKey(), minIoProperties.getSecretKey(), minIoProperties.getSecure()); + minioClient = MinioClient.builder() + .endpoint(minIoProperties.getEndpoint()) + .credentials(minIoProperties.getAccessKey(), minIoProperties.getSecretKey()) + .build(); } @Override @@ -117,7 +118,8 @@ public class MinIoFileServiceImpl extends DefaultBaseService implements IMinIoFi if (!isOpen) { // 下载 response.setHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode(filePO.getFileName(), "UTF-8")); - inputStream = minioClient.getObject(filePO.getFileUrl(), objectName); + GetObjectArgs getObjectArgs = GetObjectArgs.builder().bucket(filePO.getFileUrl()).object(objectName).build(); + inputStream = minioClient.getObject(getObjectArgs); } else { // 直接打开 response.setHeader("Content-Disposition", "inline;fileName=" + URLEncoder.encode(filePO.getFileName(), "UTF-8")); @@ -153,9 +155,9 @@ public class MinIoFileServiceImpl extends DefaultBaseService implements IMinIoFi endRange = Long.valueOf(rangeArray[1]); } setRangeHeader(startRange, endRange, response, filePO.getFileId(), contentLength); - inputStream = minioClient.getObject(filePO.getFileUrl(), objectName, startRange, endRange); + inputStream = minioClient.getObject(GetObjectArgs.builder().bucket(filePO.getFileUrl()).object(objectName).offset(startRange).length(endRange - startRange).build()); } else { - inputStream = minioClient.getObject(filePO.getFileUrl(), objectName); + inputStream = minioClient.getObject(GetObjectArgs.builder().bucket(filePO.getFileUrl()).object(objectName).build()); } if (endRange == contentLength - 1) { isDownloadComplete = true; @@ -167,7 +169,7 @@ public class MinIoFileServiceImpl extends DefaultBaseService implements IMinIoFi } outputStream.flush(); inputStream.close(); - } catch (IOException | ErrorResponseException | InsufficientDataException | InternalException | InvalidBucketNameException | InvalidKeyException | InvalidResponseException | NoSuchAlgorithmException | XmlParserException e) { + } catch (ServerException | IOException | ErrorResponseException | InsufficientDataException | InternalException | InvalidKeyException | InvalidResponseException | NoSuchAlgorithmException | XmlParserException e) { throw new FileException("文件输出异常", e); } finally { if (inputStream != null) { @@ -193,11 +195,10 @@ public class MinIoFileServiceImpl extends DefaultBaseService implements IMinIoFi private void upload(String uploadFileName, long fileSize, String bucketName, MultipartFile uploadFile) { try (InputStream inputStream = uploadFile.getInputStream()) { // 创建桶 - if (!minioClient.bucketExists(bucketName)) { - minioClient.makeBucket(bucketName); + if (!minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build())) { + minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build()); } - PutObjectOptions putObjectOptions = new PutObjectOptions(fileSize, PutObjectOptions.MAX_PART_SIZE); - minioClient.putObject(bucketName, uploadFileName, inputStream, putObjectOptions); + minioClient.putObject(PutObjectArgs.builder().bucket(bucketName).object(uploadFileName).stream(inputStream, fileSize, -1).contentType(uploadFile.getContentType()).build()); } catch (MinioException | InvalidKeyException | IOException | NoSuchAlgorithmException e) { throw new SystemException(e); } diff --git a/module-file/src/test/java/MinIOTest.java b/module-file/src/test/java/MinIOTest.java index c00feb77..faa6dd4b 100644 --- a/module-file/src/test/java/MinIOTest.java +++ b/module-file/src/test/java/MinIOTest.java @@ -1,12 +1,3 @@ -import ink.wgink.util.request.StaticResourceRequestUtil; -import io.minio.MinioClient; -import io.minio.PutObjectOptions; -import io.minio.errors.MinioException; - -import java.io.File; -import java.io.FileInputStream; -import java.io.InputStream; - /** * @ClassName: MinIOTest * @Description: @@ -17,31 +8,7 @@ import java.io.InputStream; public class MinIOTest { public static void main(String[] args) throws Exception { - try { - // 使用MinIO服务的URL,端口,Access key和Secret key创建一个MinioClient对象 - MinioClient minioClient = new MinioClient("http://192.168.0.188:19001", "demotest", "demotest", false); - // 检查存储桶是否已经存在 - boolean isExist = minioClient.bucketExists("images"); - if (isExist) { - System.out.println("Bucket already exists."); - } else { - // 创建一个名为asiatrip的存储桶,用于存储照片的zip文件。 - minioClient.makeBucket("images"); - } - // 使用putObject上传一个文件到存储桶中。 - File file = new File("/Users/wanggeng/Desktop/UploadFiles/images/20210820/1.jpeg"); - InputStream inputStream = new FileInputStream(file); - PutObjectOptions putObjectOptions = new PutObjectOptions(file.length(), PutObjectOptions.MAX_PART_SIZE); - // 下载 - // putObjectOptions.setContentType("application/octet-stream"); - // 直接打开 - String contentType = StaticResourceRequestUtil.getContentType("jpeg"); - putObjectOptions.setContentType(contentType); - minioClient.putObject("images", "demo.jpeg", inputStream, putObjectOptions); - System.out.println("successfully uploaded"); - } catch (MinioException e) { - System.out.println("Error occurred: " + e); - } + } } diff --git a/pom.xml b/pom.xml index 2441d790..3a108736 100644 --- a/pom.xml +++ b/pom.xml @@ -91,7 +91,7 @@ 1.7.30 6.0.0 1.10 - 7.0.2 + 8.2.1 3.2.5 2.5.5 2.3.31