package com.bonus.common.houqin.oss.service;

import com.amazonaws.ClientConfiguration;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.AWSCredentialsProvider;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.client.builder.AwsClientBuilder;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.amazonaws.services.s3.model.*;
import com.amazonaws.util.IOUtils;
import com.bonus.common.houqin.oss.OssProperties;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Service;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.net.URL;
import java.util.*;


public class OssTemplate implements InitializingBean {
   private final OssProperties ossProperties;
   private AmazonS3 amazonS3;

   public void createBucket(String bucketName) {
      try {
         if (!this.amazonS3.doesBucketExistV2(bucketName)) {
            this.amazonS3.createBucket(bucketName);
         }

      } catch (Throwable var3) {
         throw var3;
      }
   }

   public List<Bucket> getAllBuckets() {
      try {
         return this.amazonS3.listBuckets();
      } catch (Throwable var2) {
         throw var2;
      }
   }

   public Optional<Bucket> getBucket(String bucketName) {
      try {
         return this.amazonS3.listBuckets().stream().filter((b) -> {
            return b.getName().equals(bucketName);
         }).findFirst();
      } catch (Throwable var3) {
         throw var3;
      }
   }

   public void removeBucket(String bucketName) {
      try {
         this.amazonS3.deleteBucket(bucketName);
      } catch (Throwable var3) {
         throw var3;
      }
   }

   public List<S3ObjectSummary> getAllObjectsByPrefix(String bucketName, String prefix, boolean recursive) {
      try {
         ObjectListing objectListing = this.amazonS3.listObjects(bucketName, prefix);
         return new ArrayList(objectListing.getObjectSummaries());
      } catch (Throwable var5) {
         throw var5;
      }
   }

   public CopyObjectResult copyObject(String bucketName, String objectName, String destbucketName, String destobjectName) {
      try {
         CopyObjectResult copyObjectResult = this.amazonS3.copyObject(bucketName, objectName, destbucketName, destobjectName);
         return copyObjectResult;
      } catch (Throwable var6) {
         throw var6;
      }
   }

   public String getObjectURL(String bucketName, String objectName, Integer expires) {
      try {
         Date date = new Date();
         Calendar calendar = new GregorianCalendar();
         calendar.setTime(date);
         calendar.add(13, expires);
         URL url = this.amazonS3.generatePresignedUrl(bucketName, objectName, calendar.getTime());
         return url.toString();
      } catch (Throwable var7) {
         throw var7;
      }
   }

   public InputStream getObject(String bucketName, String objectName) {
      try {
         return this.amazonS3.getObject(bucketName, objectName).getObjectContent();
      } catch (Throwable var4) {
         throw var4;
      }
   }

   public void putObject(String bucketName, String objectName, InputStream stream) throws Exception {
      this.putObject(bucketName, objectName, stream, (long) stream.available(), "application/octet-stream");
   }

   public PutObjectResult putObject(String bucketName, String objectName, InputStream stream, long size, String contextType) throws Exception {
      byte[] bytes = IOUtils.toByteArray(stream);
      ObjectMetadata objectMetadata = new ObjectMetadata();
      objectMetadata.setContentLength(size);
      objectMetadata.setContentType(contextType);
      ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
      return this.amazonS3.putObject(bucketName, objectName, byteArrayInputStream, objectMetadata);
   }

   public S3Object getObjectInfo(String bucketName, String objectName) throws Exception {
      return this.amazonS3.getObject(bucketName, objectName);
   }

   public void removeObject(String bucketName, String objectName) throws Exception {
      this.amazonS3.deleteObject(bucketName, objectName);
   }

   @Override
   public void afterPropertiesSet() throws Exception {
      ClientConfiguration clientConfiguration = new ClientConfiguration();
      AwsClientBuilder.EndpointConfiguration endpointConfiguration = new AwsClientBuilder.EndpointConfiguration(this.ossProperties.getEndpoint(), this.ossProperties.getRegion());
      AWSCredentials awsCredentials = new BasicAWSCredentials(this.ossProperties.getAccessKey(), this.ossProperties.getSecretKey());
      AWSCredentialsProvider awsCredentialsProvider = new AWSStaticCredentialsProvider(awsCredentials);
      if (this.ossProperties.getUseHttp()) {
         System.setProperty("com.amazonaws.sdk.disableCertChecking", "true");
      }

      this.amazonS3 = (AmazonS3) ((AmazonS3ClientBuilder) ((AmazonS3ClientBuilder) ((AmazonS3ClientBuilder) ((AmazonS3ClientBuilder) ((AmazonS3ClientBuilder) AmazonS3Client.builder().withEndpointConfiguration(endpointConfiguration)).withClientConfiguration(clientConfiguration)).withCredentials(awsCredentialsProvider)).disableChunkedEncoding()).withPathStyleAccessEnabled(this.ossProperties.getPathStyleAccess())).build();
   }

   public OssTemplate(final OssProperties ossProperties) {
      this.ossProperties = ossProperties;
   }

}