package com.gkhy.safePlatform.safeCheck.service.impl; import com.gkhy.safePlatform.commons.config.file.MinioConfig; import com.gkhy.safePlatform.commons.utils.StringUtils; import com.gkhy.safePlatform.safeCheck.service.SafeCheckMinioAccessService; import com.gkhy.safePlatform.safeCheck.util.UUIDUtil; import io.minio.GetPresignedObjectUrlArgs; import io.minio.MinioClient; import io.minio.RemoveObjectArgs; import io.minio.errors.*; import io.minio.http.Method; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import javax.annotation.Resource; import javax.management.ObjectName; import java.io.IOException; import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.HashMap; import java.util.Map; @Service public class SafeCheckMinioAccessServiceImpl implements SafeCheckMinioAccessService { // @Value("${safecheckminio.urlPrefix}") // private String externalNetworkPort; @Value("${safecheckminio.floders.safecheck}") private String minioFloders; @Value("${safecheckminio.floders.exceorderimage}") private String minioExceOrderImageFloders; @Autowired private MinioConfig minioConfig; @Autowired private MinioClient minioClient; /** * @description 获取文件下载地址 */ public String viewFile(String objectName){ return viewFileDown(minioFloders+objectName); } /** * @description 获取异常文件的下载地址 */ public String viewExceFile(String objectName){ return viewFileDown(minioExceOrderImageFloders+objectName); } private String viewFileDown(String objectName){ String url = null; // HashMap map = new HashMap<>(); // map.put("id","123"); try { url = minioClient.getPresignedObjectUrl(GetPresignedObjectUrlArgs.builder() .method(Method.GET) .bucket(minioConfig.getBucketName()) .object(objectName) //.object(minioExceOrderImageFloders+objectName) // .extraHeaders(map) .expiry(60*60*5).build() ); } catch (ErrorResponseException e) { e.printStackTrace(); } catch (InsufficientDataException e) { e.printStackTrace(); } catch (InternalException e) { e.printStackTrace(); } catch (InvalidKeyException e) { e.printStackTrace(); } catch (InvalidResponseException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (XmlParserException e) { e.printStackTrace(); } catch (ServerException e) { e.printStackTrace(); } url = minioConfig.getUrlPrefix() + url.substring((url.indexOf("//") + 2)).substring(url.substring((url.indexOf("//") + 2)).indexOf( "/")); return url; } /** * @description 获取文件上传地址 */ public Map getPresignUrl(String prefixName ,String suffixName){ String uuid= UUIDUtil.initUUID(); SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); String nowTime = formatter.format(new Date()); String objName = nowTime+"_"+uuid+"."+suffixName; String url = null; Map resMap = new HashMap<>(); resMap.put("rfidImage",objName); try { Map reqParams = new HashMap<>(); reqParams.put("response-content-type", "application/json"); url = minioClient.getPresignedObjectUrl(GetPresignedObjectUrlArgs.builder() .method(Method.PUT) .bucket(minioConfig.getBucketName()) .object(minioFloders+objName) .expiry(60*60*2) .extraQueryParams(reqParams) .build()); resMap.put("uploadUrl",url); } catch (ErrorResponseException e) { e.printStackTrace(); } catch (InsufficientDataException e) { e.printStackTrace(); } catch (InternalException e) { e.printStackTrace(); } catch (InvalidKeyException e) { e.printStackTrace(); } catch (InvalidResponseException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (XmlParserException e) { e.printStackTrace(); } catch (ServerException e) { e.printStackTrace(); } return resMap; } /** * @description 获取巡检异常图片上传地址 */ public Map getExcPresignUrl(String prefixName ,String suffixName){ String uuid= UUIDUtil.initUUID(); SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); String nowTime = formatter.format(new Date()); String objName = nowTime+"_"+uuid+"."+suffixName; String url = null; Map resMap = new HashMap<>(); resMap.put("image",objName); try { Map reqParams = new HashMap<>(); reqParams.put("response-content-type", "application/json"); url = minioClient.getPresignedObjectUrl(GetPresignedObjectUrlArgs.builder() .method(Method.PUT) .bucket(minioConfig.getBucketName()) .object(minioExceOrderImageFloders+objName) .expiry(60*60*2) .extraQueryParams(reqParams) .build()); resMap.put("uploadUrl",url); } catch (ErrorResponseException e) { e.printStackTrace(); } catch (InsufficientDataException e) { e.printStackTrace(); } catch (InternalException e) { e.printStackTrace(); } catch (InvalidKeyException e) { e.printStackTrace(); } catch (InvalidResponseException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (XmlParserException e) { e.printStackTrace(); } catch (ServerException e) { e.printStackTrace(); } return resMap; } /** * @description 删除文件 */ @Override public String deleteFile(String name) { try { minioClient.removeObject(RemoveObjectArgs.builder() .bucket(minioConfig.getBucketName()) .object(minioFloders+name) .build()); } catch (ErrorResponseException e) { e.printStackTrace(); } catch (InsufficientDataException e) { e.printStackTrace(); } catch (InternalException e) { e.printStackTrace(); } catch (InvalidKeyException e) { e.printStackTrace(); } catch (InvalidResponseException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (XmlParserException e) { e.printStackTrace(); } catch (ServerException e) { e.printStackTrace(); } return "删除成功"; } }