package com.imooc.utils;
import com.qcloud.vod.model.VodUploadResponse;
import com.qcloud.vod.VodUploadClient;
import com.qcloud.vod.model.VodUploadRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.File;

import java.nio.file.Files;



@Component
public class QcCloud {
    @Autowired
    private TencentCloudProperties tencentCloudProperties;
    public String getSecretId() {
        return tencentCloudProperties.getSecretId();
    }

    public String getSecretKey() {
        return tencentCloudProperties.getSecretKey();
    }

    public String getRegion() {
        return "ap-guangzhou";// 腾讯云 VOD 和 COS 使用相同区域
    }



    public String uploadViaTempFile(String fileName) throws Exception {
        //确保 vlogdata 目录存在
//        String storagePath = new File(System.getProperty("user.dir"), "vlogdata").getAbsolutePath();
//        String storagePath = "/Users/wuzhongjie/Desktop/vlog-1.0.0/vlogdata"; // 本地
        String storagePath = "/data/vlogdata"; // 生产

        System.out.println("腾讯云服务：绝对路径: " + storagePath);
        File storageDir = new File(storagePath);
        // 组装完整的文件路径
        File sourceFile = new File(storageDir, fileName);
        System.out.println("腾讯云服务：完整文件路径: " + sourceFile.toPath());

        if (!sourceFile.exists()) {
            throw new RuntimeException("腾讯云服务：文件不存在: " + sourceFile.getAbsolutePath());
        }
        try {
            VodUploadClient client = new VodUploadClient(getSecretId(), getSecretKey());
            VodUploadRequest request = new VodUploadRequest();
            request.setMediaFilePath(sourceFile.getAbsolutePath());// 指定本地文件路径
            request.setProcedure("视频处理");  // 智能审核+水印
            // 上传文件
            VodUploadResponse response = client.upload(getRegion(), request);
//            return response;
            return response.getFileId();
        } finally {
            // 删除本地文件
            Files.deleteIfExists(sourceFile.toPath());
        }
    }
}