|
@@ -0,0 +1,93 @@
|
|
|
+package edu.math.diagnosis.file;
|
|
|
+
|
|
|
+import edu.math.diagnosis.config.MinioProperties;
|
|
|
+import edu.math.diagnosis.util.CommonUtil;
|
|
|
+import io.minio.MinioClient;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
+import org.springframework.http.ResponseEntity;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.util.Assert;
|
|
|
+
|
|
|
+import javax.annotation.Resource;
|
|
|
+import java.io.ByteArrayInputStream;
|
|
|
+
|
|
|
+public class MinioFileService implements FileService {
|
|
|
+
|
|
|
+ private static final Logger logger = LoggerFactory.getLogger(MinioFileService.class);
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private MinioProperties properties;
|
|
|
+ @Resource
|
|
|
+ private MinioClient minioClient;
|
|
|
+
|
|
|
+ public String upload(String filename) {
|
|
|
+ return "";
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ private String upload(String filename, String bucket) throws Exception {
|
|
|
+ checkAndMake(bucket);
|
|
|
+
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+
|
|
|
+ private void checkAndMake(String bucket) throws Exception {
|
|
|
+ Assert.hasText(bucket, "bucket is blank");
|
|
|
+ if (!minioClient.bucketExists(bucket)) {
|
|
|
+ logger.info("bucket {} doesn't exists.", bucket);
|
|
|
+ minioClient.makeBucket(bucket);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public String uploadWithOriginName(byte[] data, String filename) {
|
|
|
+ try {
|
|
|
+ return uploadWithOriginName(data, filename, properties.getDefaultBucket());
|
|
|
+ } catch (Exception e) {
|
|
|
+ logger.error("upload file error", e);
|
|
|
+ }
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+
|
|
|
+ public String uploadWithOriginName(byte[] data, String filename, String bucket) throws Exception {
|
|
|
+ ByteArrayInputStream bais = new ByteArrayInputStream(data);
|
|
|
+ checkAndMake(bucket);
|
|
|
+ String suffix = filename.substring(filename.lastIndexOf("."));
|
|
|
+ filename = CommonUtil.randomUUID() + suffix;
|
|
|
+ String url = buildUrl(bucket, filename);
|
|
|
+ logger.info("putting object {} to {}", filename, url);
|
|
|
+ minioClient.putObject(bucket, filename, bais, "application/octet-stream");
|
|
|
+ return url;
|
|
|
+ }
|
|
|
+
|
|
|
+ public String upload(byte[] data, String filename) {
|
|
|
+ try {
|
|
|
+ return upload(data, filename, properties.getDefaultBucket());
|
|
|
+ } catch (Exception e) {
|
|
|
+ logger.error("upload file error", e);
|
|
|
+ }
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+
|
|
|
+ private String upload(byte[] data, String filename, String bucket) throws Exception {
|
|
|
+ ByteArrayInputStream bais = new ByteArrayInputStream(data);
|
|
|
+ checkAndMake(bucket);
|
|
|
+ String url = buildUrl(bucket, filename);
|
|
|
+ logger.info("putting object {} to {}", filename, url);
|
|
|
+ minioClient.putObject(bucket, filename, bais, "application/octet-stream");
|
|
|
+
|
|
|
+ return url;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ private String buildUrl(String bucket, String filename) {
|
|
|
+ return properties.getUrl() + bucket + "/" + filename;
|
|
|
+ }
|
|
|
+
|
|
|
+ public ResponseEntity<byte[]> download(String url) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+}
|