|
@@ -0,0 +1,183 @@
|
|
|
+package com.webchat.common.service;
|
|
|
+
|
|
|
+import com.webchat.common.config.properties.MinioProperties;
|
|
|
+import com.webchat.common.enums.FileTypeEnum;
|
|
|
+import com.webchat.domain.dto.UploadResultDTO;
|
|
|
+import com.webchat.domain.vo.response.UploadResultVO;
|
|
|
+import io.minio.GetObjectArgs;
|
|
|
+import io.minio.MinioClient;
|
|
|
+import io.minio.PutObjectArgs;
|
|
|
+import io.minio.UploadObjectArgs;
|
|
|
+import jakarta.servlet.ServletOutputStream;
|
|
|
+import jakarta.servlet.http.HttpServletResponse;
|
|
|
+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 java.io.File;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.net.URLEncoder;
|
|
|
+import java.util.Arrays;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+@Service
|
|
|
+public class MinioService {
|
|
|
+
|
|
|
+ private static final Logger LOGGER = LoggerFactory.getLogger(MinioService.class);
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private MinioClient minioClient;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private MinioProperties properties;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 定义图片和视频的MIME类型列表
|
|
|
+ */
|
|
|
+ private static final List<String> IMAGE_MIME_TYPES = Arrays.asList("image/jpeg", "image/png", "image/gif", "image/bmp");
|
|
|
+ private static final List<String> VIDEO_MIME_TYPES = Arrays.asList("video/mp4", "video/quicktime", "video/x-msvideo", "video/3gpp", "video/3gpp2", "video/webm", "video/ogg");
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 上传文件到Minio服务器
|
|
|
+ *
|
|
|
+ * @param file
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public boolean upload(File file) {
|
|
|
+ String filename = file.getName();
|
|
|
+ try {
|
|
|
+ minioClient.uploadObject(UploadObjectArgs.builder()
|
|
|
+ .bucket(properties.getBucketName())
|
|
|
+ .object(filename)
|
|
|
+ .filename(file.getAbsolutePath())
|
|
|
+ .build());
|
|
|
+ return true;
|
|
|
+ } catch (Exception e) {
|
|
|
+ LOGGER.error("file upload minio exception, file name: {}", filename, e);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 前端的文件上传
|
|
|
+ *
|
|
|
+ * @param file
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public boolean upload(MultipartFile file) {
|
|
|
+ String filename = file.getOriginalFilename();
|
|
|
+ try {
|
|
|
+ InputStream inputStream = file.getInputStream();
|
|
|
+ minioClient.putObject(PutObjectArgs.builder()
|
|
|
+ .bucket(properties.getBucketName())
|
|
|
+ .object(filename)
|
|
|
+ .stream(inputStream, inputStream.available(), -1)
|
|
|
+ .contentType(file.getContentType())
|
|
|
+ .build());
|
|
|
+ return true;
|
|
|
+ } catch (Exception e) {
|
|
|
+ LOGGER.error("file upload minio exception, file name: {}", filename, e);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 上传到Minio指定路径
|
|
|
+ *
|
|
|
+ * @param path
|
|
|
+ * @param file
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public boolean upload(String path, MultipartFile file) {
|
|
|
+ String filename = file.getOriginalFilename();
|
|
|
+ try {
|
|
|
+ filename = path + filename;
|
|
|
+ InputStream inputStream = file.getInputStream();
|
|
|
+ minioClient.putObject(PutObjectArgs.builder()
|
|
|
+ .bucket(properties.getBucketName())
|
|
|
+ .object(filename)
|
|
|
+ .stream(inputStream, inputStream.available(), -1)
|
|
|
+ .contentType(file.getContentType())
|
|
|
+ .build());
|
|
|
+ return true;
|
|
|
+ } catch (Exception e) {
|
|
|
+ LOGGER.error("file upload minio exception, file name: {}", filename, e);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 上传到Minio指定目录,可选择自定义随机文件名
|
|
|
+ *
|
|
|
+ * @param path
|
|
|
+ * @param file
|
|
|
+ * @param isRandom
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public UploadResultVO upload(String path, MultipartFile file, Boolean isRandom) {
|
|
|
+ String filename = file.getOriginalFilename();
|
|
|
+ if (Boolean.TRUE.equals(isRandom)) {
|
|
|
+ long millis = System.currentTimeMillis();
|
|
|
+ String extension = filename.substring(filename.lastIndexOf(".") + 1);
|
|
|
+ filename = millis + "." + extension;
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ filename = path + "/" + filename;
|
|
|
+ InputStream inputStream = file.getInputStream();
|
|
|
+ minioClient.putObject(PutObjectArgs.builder()
|
|
|
+ .bucket(properties.getBucketName())
|
|
|
+ .object(filename)
|
|
|
+ .stream(inputStream, inputStream.available(), -1)
|
|
|
+ .contentType(file.getContentType())
|
|
|
+ .build());
|
|
|
+ String resourceUrl = properties.getEndpoint() + "/" + properties.getBucketName() + "/" + filename;
|
|
|
+ return UploadResultVO.of(resourceUrl, fileType(file), filename);
|
|
|
+ } catch (Exception e) {
|
|
|
+ LOGGER.error("file upload minio exception, file name: {}, source file name: {}", filename, file.getOriginalFilename(), e);
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private String fileType(MultipartFile file) {
|
|
|
+ // 获取文件的MIME类型
|
|
|
+ String mimeType = file.getContentType();
|
|
|
+ // 发布动态上传文件仅支持图片和视频
|
|
|
+ if (IMAGE_MIME_TYPES.contains(mimeType)) {
|
|
|
+ return FileTypeEnum.IMAGE.name();
|
|
|
+ }
|
|
|
+ if (VIDEO_MIME_TYPES.contains(mimeType)) {
|
|
|
+ return FileTypeEnum.VIDEO.name();
|
|
|
+ }
|
|
|
+ return FileTypeEnum.OTHER.name();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 文件下载
|
|
|
+ *
|
|
|
+ * @param filename
|
|
|
+ * @param response
|
|
|
+ */
|
|
|
+ public void download(String filename, HttpServletResponse response) {
|
|
|
+ response.reset();
|
|
|
+ try(InputStream inputStream = minioClient.getObject(GetObjectArgs.builder()
|
|
|
+ .bucket(properties.getBucketName())
|
|
|
+ .object(filename)
|
|
|
+ .build())) {
|
|
|
+ filename = filename.substring(filename.lastIndexOf("/") + 1);
|
|
|
+ ServletOutputStream outputStream = response.getOutputStream();
|
|
|
+ response.setContentType("application/octet-stream");
|
|
|
+ response.setCharacterEncoding("utf-8");
|
|
|
+ response.addHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(filename, "UTF-8"));
|
|
|
+ byte[] bytes = new byte[1024];
|
|
|
+ int len;
|
|
|
+ while ((len = inputStream.read(bytes)) > 0) {
|
|
|
+ outputStream.write(bytes, 0, len);
|
|
|
+ }
|
|
|
+ outputStream.close();
|
|
|
+ } catch (Exception e) {
|
|
|
+ LOGGER.error("file download from minio exception, file name: {}", filename, e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|