package com.mcwl.common.utils.obs;

import com.obs.services.ObsClient;
import com.obs.services.model.ObsObject;
import com.obs.services.model.PutObjectResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.PreDestroy;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * 华为云obs上传文件工具类
 *
 * @author DaiZibo
 * @date 2025/2/18
 * @apiNote
 */


@Component
public class ObsUtils {

    @Autowired
    private ObsClient obsClient;

    @Value("${huawei.obs.bucketName}")
    private String bucketName;

    //上传文件，multipartFile就是你要的文件，
    //objectKey就是文件名，如果桶中有文件夹的话，如往test文件上传test.txt文件，那么objectKey就是test/test.txt
    public Map<String,String> uploadFile(MultipartFile multipartFile){

        HashMap<String, String> map = new HashMap<>();

        try {
            InputStream inputStream = multipartFile.getInputStream();
            String uuid = UUID.randomUUID().toString();
            String ossDefaultPath = getOssDefaultPath(multipartFile.getOriginalFilename(),uuid);
            PutObjectResult putObjectResult = obsClient.putObject(bucketName, ossDefaultPath, inputStream);
            inputStream.close();
            map.put("path",putObjectResult.getObjectUrl());
            map.put("objectKey",putObjectResult.getObjectKey());
            map.put("size",multipartFile.getSize()+"");
            map.put("fileName",multipartFile.getOriginalFilename());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        return map;
    }

    public void deleteFile(String objectKey) throws Exception {
        obsClient.deleteObject(bucketName, objectKey);
        obsClient.close();
    }

    public String getOssDefaultPath(String name,String uuid) {
        LocalDateTime now = LocalDateTime.now();
        String url =
                now.getYear() + "/" +
                        now.getMonthValue() + "/" +
                        now.getDayOfMonth() + "/"
                        + uuid + "/"
                        + name;
        return url;
    }

    public void download(String name) throws IOException {

        ObsObject obsObject = obsClient.getObject(bucketName, name);
        InputStream content = obsObject.getObjectContent();
        if (content != null) {
            BufferedReader reader = new BufferedReader(new InputStreamReader(content));
            while (true) {
                String line = reader.readLine();
                if (line == null){
                    break;
                }
                System.out.println("\n" + line);
            }
            reader.close();
        }
    }


    @PreDestroy
    public void cleanup() {
        if (obsClient != null) {
            try {
                obsClient.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
