package com.izouma.awesomeadmin.util;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.vod.model.v20170321.CreateUploadVideoRequest;
import com.aliyuncs.vod.model.v20170321.CreateUploadVideoResponse;
import com.aliyuncs.vod.model.v20170321.GetPlayInfoRequest;
import com.aliyuncs.vod.model.v20170321.GetPlayInfoResponse;

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


public class AliyunVodUtil {
    private static String accessKeyId = "LTAI8YwFszgs6QHJ";
    private static String accessKeySecret = "DKNuEv4dwegZcZLU1yNFRGou0E9WW2";

    public static Map<String, String> createUploadVideo(String fileName, String title) {
        DefaultAcsClient aliyunClient = new DefaultAcsClient(
                DefaultProfile.getProfile("cn-shanghai", accessKeyId, accessKeySecret));
        CreateUploadVideoRequest request = new CreateUploadVideoRequest();
        CreateUploadVideoResponse response = null;
        try {
            request.setFileName(fileName);
            //必选，视频标题
            request.setTitle(title);
            /*request.setCateId(0);//可选，分类ID
            request.setTags("标签1,标签2");//可选，视频标签，多个用逗号分隔
            request.setDescription("视频描述");//可选，视频描述
            request.setFileSize(0L);//可选，视频源文件字节数*/
            response = aliyunClient.getAcsResponse(request);
            Map<String, String> map = new HashMap<>();
            map.put("videoId", response.getVideoId());
            map.put("requestId", response.getRequestId());
            map.put("uploadAuth", response.getUploadAuth());
            map.put("uploadAddress", response.getUploadAddress());
            System.out.println(map);
            return map;
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static GetPlayInfoResponse getVideoAddress(String videoId) {
        DefaultAcsClient aliyunClient = new DefaultAcsClient(
                DefaultProfile.getProfile("cn-shanghai", accessKeyId, accessKeySecret));
        GetPlayInfoRequest request = new GetPlayInfoRequest();
        request.setVideoId(videoId);
        request.setFormats("mp4");
        request.setAuthTimeout(1800L);
        GetPlayInfoResponse response = null;
        try {
            response = aliyunClient.getAcsResponse(request);
            return response;
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return null;
    }

}
