package com.search.supersearch.controller;

import com.aliyun.oss.OSS;
import com.aliyun.oss.model.OSSObject;
import com.aliyun.oss.model.OSSObjectSummary;
import com.aliyun.oss.model.ObjectListing;
import com.search.supersearch.common.R;
import com.search.supersearch.config.OSSConfig;
import com.search.supersearch.service.EsService;
import com.search.supersearch.service.OssService;
import com.search.supersearch.service.TikaService;
import com.search.supersearch.util.FileUtil;
import io.swagger.annotations.Api;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

@RestController
@RequestMapping("/oss")
@Slf4j
@Api(value = "oss 接口测试", tags = "oss 接口测试")
public class OssController {
    @Resource
    OSSConfig ossConfig;
    @Value("${oss.bucketName}")
    private String bucket_name;
    @Resource
    private EsService esService;

    @Resource
    private OssService ossService;
    @Resource
    private TikaService tikaService;

    @GetMapping("/test")
    @SneakyThrows
    public R<?> test() {

     InputStream inputStream = FileUtil.class.getClassLoader().getResourceAsStream("tmp.txt");
        byte[] buff = new byte[inputStream.available()];
        String json = null;
        int len;
        while ((len = inputStream.read(buff)) != -1) {
            json = new String(buff, 0, len);
        }

        System.out.println(json);
        return R.success(json) ;
    }

    @GetMapping("/list")
    @SneakyThrows
    public R<?> list(String prefix) {
        OSS oss = ossConfig.restOSSClient();
        ObjectListing objectListing = oss.listObjects(bucket_name, prefix);
        for (OSSObjectSummary objectSummary : objectListing.getObjectSummaries()) {
            log.info(" - " + objectSummary.getKey() + "  " +
                    "(size = " + objectSummary.getSize() + ")");
        }
        return R.data(objectListing);
    }

    @GetMapping("/get/content")
    @SneakyThrows
    public R<?> get(String objectName) {
        OSS oss = ossConfig.restOSSClient();
        // 调用ossClient.getObject返回一个OSSObject实例，该实例包含文件内容及文件元信息。
        OSSObject ossObject = oss.getObject(bucket_name, objectName);
        // 调用ossObject.getObjectContent获取文件输入流，可读取此输入流获取其内容。
        InputStream content = ossObject.getObjectContent();
        if (content != null) {
            BufferedReader reader = new BufferedReader(new InputStreamReader(content));
            while (true) {
                String line = reader.readLine();
                if (line == null) {
                    break;
                }
                log.info("\n" + line);
            }
            // 数据读取完成后，获取的流必须关闭，否则会造成连接泄漏，导致请求无连接可用，程序无法正常工作。
            content.close();
        }
        return R.data(ossObject);
    }
}
