浏览代码

代码优化

tongfeng 2 年之前
父节点
当前提交
ca070f897b
共有 1 个文件被更改,包括 174 次插入0 次删除
  1. 174 0
      eladmin-system/src/main/java/me/zhengjie/base/file/FaceMinioFileHandle.java

+ 174 - 0
eladmin-system/src/main/java/me/zhengjie/base/file/FaceMinioFileHandle.java

@@ -0,0 +1,174 @@
+package me.zhengjie.base.file;
+
+import cn.hutool.core.io.IoUtil;
+import io.minio.MinioClient;
+import io.minio.ObjectStat;
+import io.minio.PutObjectOptions;
+import lombok.extern.slf4j.Slf4j;
+import me.zhengjie.base.config.AppConfigInfo;
+import me.zhengjie.dao.mybatis.entity.FileInfoEntity;
+import me.zhengjie.dao.mybatis.mapper.FileInfoMapper;
+import org.apache.commons.io.FileUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.context.annotation.Primary;
+import org.springframework.stereotype.Component;
+
+import javax.annotation.PostConstruct;
+import java.io.*;
+
+@Slf4j
+@Primary
+@Component
+public class FaceMinioFileHandle extends AbstractFileHandle {
+	// minio的连接bucketName
+	@Value("mytest")
+	private String bucketName;
+	// 设置连接minio的url
+	@Value("http://124.222.192.60:9000")
+	public String appMinioUrl;
+	// minio的连接accesskey
+	@Value("minioadmin")
+	public String appMinioAccesskey;
+	// minio的连接secretkey
+	@Value("minioadmin")
+	public String appMinioSecretkey;
+
+	@Autowired
+	public FileInfoMapper fileInfoMapper;
+
+	private MinioClient minioClient;
+
+	@PostConstruct
+	private void init() throws Exception {
+		try {
+			if (minioClient == null) {
+				log.info("minioClient create start");
+				minioClient = new MinioClient(appMinioUrl, appMinioAccesskey, appMinioSecretkey);
+				log.info("minioClient create end");
+				// 判断桶是否已经存在
+				log.info("开始创建minio的:");
+				createBucket();
+			}
+		} catch (Exception e) {
+			log.error("连接MinIO服务器异常:{}", e);
+			throw e;
+		}
+	}
+
+	/**
+	 * @Title: createBucket
+	 * @Description: (创建bucket)
+	 * @author humuyu [bucketName] 桶名
+	 */
+	public void createBucket() {
+		try {
+			if (!minioClient.bucketExists(bucketName)) {
+				minioClient.makeBucket(bucketName);
+			}
+		} catch (Exception e) {
+			log.error("创建桶失败:{}", e);
+		}
+	}
+
+	/**
+	 * 通过流上传文件
+	 *
+	 * @param bucketName  存储桶
+	 * @param objectName  文件对象
+	 * @param inputStream 文件流
+	 */
+	public void uploadFileStream(FileHandleVo file) throws Exception {
+		int fileSize = file.getInputStream().available();
+		PutObjectOptions options = new PutObjectOptions(fileSize, -1);
+		options.setContentType(file.getContentType());
+		minioClient.putObject(bucketName, file.getFilePath(), file.getInputStream(), options);
+	}
+
+	public void uploadfilePath(FileHandleVo file) throws Exception {
+		InputStream is = new FileInputStream(file.getFilePath());
+		long size = is.available();
+		PutObjectOptions options = new PutObjectOptions(size, -1);
+		options.setContentType(file.getContentType());
+		minioClient.putObject(bucketName, file.getSourceFilePath(), is, options);
+		if (is != null) {
+			is.close();
+		}
+//
+	}
+
+	public InputStream getObject(String objectName) throws Exception {
+		return minioClient.getObject(bucketName, objectName);
+	}
+
+	/**
+	 * @param bucketName bucket名称
+	 * @param objectName ⽂件名称
+	 * @param expires    过期时间 <=7
+	 * @return url
+	 * @Title: getObjectURL
+	 * @Description: (获取 ⽂ 件外链) [bucketName 桶名, objectName 文件名, expires 时间<=1小时]
+	 */
+	public String getObjectUrl(String keyName) throws Exception {
+		return minioClient.presignedGetObject(bucketName, keyName, 3600);
+	}
+
+	/**
+	 * 自定义url
+	 * 
+	 * @param fileKey 文件的
+	 * @return
+	 */
+	public String getCustomUrl(String fileKey, String filePath) {
+		try {
+			// return fileHandle.getPreviewUrl(fileKey);
+			InputStream input = minioClient.getObject(bucketName, fileKey);
+			byte[] b = IoUtil.readBytes(input);
+			String uploadPath = AppConfigInfo.APP_UPLOAD_PATH;
+			if (!uploadPath.endsWith("/")) {
+				uploadPath = uploadPath + "/";
+			}
+			String transferFilePath = uploadPath + filePath;
+			FileUtils.writeByteArrayToFile(new File(transferFilePath), b);
+			return appMinioPreview +"/image/"+ filePath;
+		} catch (Exception e) {
+			log.error("图片预览失败:" + e.getLocalizedMessage());
+			return null;
+		}
+	}
+
+	/**
+	 * 根据文件路径从minio获取文件
+	 * @param filePath
+	 * @return
+	 * @throws Exception
+	 */
+	public File getFileWithPath(String filePath) throws Exception {
+		InputStream inputStream = minioClient.getObject(bucketName, filePath);
+		File file = File.createTempFile("compare",".jpeg");
+		file.deleteOnExit();
+		OutputStream outputStream = new FileOutputStream(file);
+		int bytesRead = 0;
+		byte[] buffer = new byte[8192];
+		while ((bytesRead = inputStream.read(buffer, 0, 8192)) != -1) {
+			outputStream.write(buffer, 0, bytesRead);
+		}
+		inputStream.close();
+		outputStream.close();
+		return file;
+	}
+
+	public byte[] getByte(String fileId) throws Exception {
+		FileInfoEntity fileInfo = fileInfoMapper.selectById(fileId);
+		String path = fileInfo.getPath();
+		InputStream inputStream = this.getObject(path);
+		// 这里已经关闭流,不需要手动关闭
+		return IoUtil.readBytes(inputStream);
+	}
+
+	public long getSize(String keyName) throws Exception {
+		ObjectStat stat = minioClient.statObject(bucketName, keyName);
+		long length = stat.length();
+		return length;
+	}
+}