package com.bonus.oss.http;

import com.amazonaws.services.s3.model.Bucket;
import com.amazonaws.services.s3.model.S3Object;
import com.amazonaws.services.s3.model.S3ObjectSummary;
import com.bonus.oss.service.OssTemplate;
import lombok.SneakyThrows;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping({"/oss"})
public class OssEndpoint {
   private final OssTemplate template;

   @PostMapping({"/bucket/{bucketName}"})
   public Bucket createBucker(@PathVariable String bucketName) {
      try {
         this.template.createBucket(bucketName);
         return (Bucket)this.template.getBucket(bucketName).get();
      } catch (Throwable var3) {
         throw var3;
      }
   }
   @GetMapping({"/bucket"})
   public List<Bucket> getBuckets() {
      try {
         return this.template.getAllBuckets();
      } catch (Throwable var2) {
         throw var2;
      }
   }

   @GetMapping({"/bucket/{bucketName}"})
   public Bucket getBucket(@PathVariable String bucketName) {
      try {
         return (Bucket)this.template.getBucket(bucketName).orElseThrow(() -> {
            return new IllegalArgumentException("Bucket Name not found!");
         });
      } catch (Throwable var3) {
         throw var3;
      }
   }

   @DeleteMapping({"/bucket/{bucketName}"})
   @ResponseStatus(HttpStatus.ACCEPTED)
   public void deleteBucket(@PathVariable String bucketName) {
      try {
         this.template.removeBucket(bucketName);
      } catch (Throwable var3) {
         throw var3;
      }
   }

   @SneakyThrows
   @PostMapping({"/object/{bucketName}"})
   public S3Object createObject(@RequestBody MultipartFile object, @PathVariable String bucketName) {
      try {
         String name = object.getOriginalFilename();
         this.template.putObject(bucketName, name, object.getInputStream(), object.getSize(), object.getContentType());
         return this.template.getObjectInfo(bucketName, name);
      } catch (Throwable var4) {
         throw var4;
      }
   }

   @SneakyThrows
   @PostMapping({"/object/{bucketName}/{objectName}"})
   public S3Object createObject(@RequestBody MultipartFile object, @PathVariable String bucketName, @PathVariable String objectName) {
      try {
         this.template.putObject(bucketName, objectName, object.getInputStream(), object.getSize(), object.getContentType());
         return this.template.getObjectInfo(bucketName, objectName);
      } catch (Throwable var5) {
         throw var5;
      }
   }

   @GetMapping({"/object/{bucketName}/{objectName}"})
   public List<S3ObjectSummary> filterObject(@PathVariable String bucketName, @PathVariable String objectName) {
      try {
         return this.template.getAllObjectsByPrefix(bucketName, objectName, true);
      } catch (Throwable var4) {
         throw var4;
      }
   }

   @GetMapping({"/object/{bucketName}/{objectName}/{expires}"})
   public Map<String, Object> getObject(@PathVariable String bucketName, @PathVariable String objectName, @PathVariable Integer expires) {
      try {
         Map<String, Object> responseBody = new HashMap(8);
         responseBody.put("bucket", bucketName);
         responseBody.put("object", objectName);
         responseBody.put("url", this.template.getObjectURL(bucketName, objectName, expires));
         responseBody.put("expires", expires);
         return responseBody;
      } catch (Throwable var5) {
         throw var5;
      }
   }

   @SneakyThrows
   @ResponseStatus(HttpStatus.ACCEPTED)
   @DeleteMapping({"/object/{bucketName}/{objectName}/"})
   public void deleteObject(@PathVariable String bucketName, @PathVariable String objectName) {
      try {
         this.template.removeObject(bucketName, objectName);
      } catch (Throwable var4) {
         throw var4;
      }
   }

   public OssEndpoint(final OssTemplate template) {
      this.template = template;
   }
}
