stream_srs/src/main/java/com/tuoheng/steam/schedule/Scheduler.java

232 lines
7.4 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.tuoheng.steam.schedule;
import com.tuoheng.steam.service.IRecordService;
import com.tuoheng.steam.service.TaskService;
import com.tuoheng.steam.service.dos.DayRecord;
import com.tuoheng.steam.service.dos.FlvRecord;
import com.tuoheng.steam.service.dos.Mp4Record;
import com.tuoheng.steam.service.dos.StreamRecord;
import com.tuoheng.steam.util.TimeUtils;
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import io.minio.credentials.AssumeRoleProvider;
import io.minio.credentials.Credentials;
import io.minio.errors.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.io.*;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.*;
@Component
public class Scheduler {
@Value("${minio.oss.endpoint}")
private String endpoint;
@Value("${minio.oss.accessKeyId}")
private String accessKeyId;
@Value("${minio.oss.accessKeySecret}")
private String accessKeySecret;
@Value("${minio.oss.bucketName}")
private String bucketName;
//文件存储目录
@Value("${minio.oss.filedir}")
private String filedir;
@Value("${minio.oss.dajiangName}")
private String dajiangName;
@Value("${minio.oss.dajiangPassword}")
private String dajiangPassword;
//授权策略允许访问名为bucket的桶的目录
public static final String ROLE_ARN = "arn:aws:s3:::";
private static final Logger logger = LoggerFactory.getLogger(Scheduler.class);
@Autowired
IRecordService iRecordService;
@Value("${livedates}")
private Integer livedates;
/**
* 初次执行延迟6秒执行
* 每隔 60 分钟执行一次 60*60*1000
*/
@Scheduled(fixedRate = 3600000, initialDelay = 10)
public void mergeTask() throws Exception {
// File file = new File("/Users/sunpeng/workspace/text.txt");
// InputStream inputStream = new FileInputStream(file);
// try {
// uploadFile2OSS(inputStream,"text.txt",file.length());
// }catch (Exception e) {
// logger.error(e.getMessage());
// }
//
//
// getCredentials();
}
/**
* 得到 临时凭据
*/
private Credentials getCredentials() throws Exception {
String POLICY_GET_AND_PUT = "{\n" +
" \"Version\": \"2012-10-17\",\n" +
" \"Statement\": [\n" +
" {\n" +
" \"Effect\": \"Allow\",\n" +
" \"Action\": [\n" +
" \"s3:GetObject\",\n" +
" \"s3:GetBucketLocation\",\n" +
" \"s3:PutObject\"\n" +
" ],\n" +
" \"Resource\": [\n" +
" \"arn:aws:s3:::"+bucketName+"/*\"\n" +
" ]\n" +
" }\n" +
" ]\n" + "}";
int durationSeconds = 360000;//秒
//创建签名对象
AssumeRoleProvider provider = new AssumeRoleProvider(
endpoint,
dajiangName,
dajiangPassword,
durationSeconds,//默认3600秒失效设置小于这个就是3600大于3600就实际值
POLICY_GET_AND_PUT,
"us-east-1",
ROLE_ARN+bucketName+"/*",
"anysession",
null,
null);
Credentials credentials = provider.fetch();
/**
* 下面的值按照大疆的要求传给大疆
*/
// System.out.println("sessionToken=" + credentials.sessionToken());
// System.out.println("accessKey=" + credentials.accessKey());
// System.out.println("secretKey=" + credentials.secretKey());
// System.out.println("isExpired=" + credentials.isExpired());
return credentials;
}
private void uploadFile2OSS(InputStream inputStream, String fileName, long streamSize) throws Exception{
// 初始化 MinioClient
MinioClient minioClient = MinioClient.builder()
.endpoint(endpoint) // MinIO 服务器地址
.credentials(accessKeyId, accessKeySecret) // 访问密钥和秘密密钥
.build();
String contentType = getContentType(fileName.substring(fileName.lastIndexOf("."))); // 获取文件类型
try {
minioClient.putObject(
PutObjectArgs.builder()
.bucket(bucketName) // 存储桶名称
.object(filedir + "/" + fileName) // 对象名称(路径)
.stream(inputStream, streamSize, -1) // 输入流、文件大小(-1 表示未知大小)
.contentType(contentType) // 文件类型
.build()
);
}catch (Exception e) {
logger.error(e.getMessage());
}
}
private static String getContentType(String FilenameExtension) {
if (FilenameExtension.equalsIgnoreCase(".bmp")) {
return "image/bmp";
}
if (FilenameExtension.equalsIgnoreCase(".gif")) {
return "image/gif";
}
if (FilenameExtension.equalsIgnoreCase(".jpeg") ||
FilenameExtension.equalsIgnoreCase(".jpg") ||
FilenameExtension.equalsIgnoreCase(".png")) {
return "image/jpeg";
}
if (FilenameExtension.equalsIgnoreCase(".html")) {
return "text/html";
}
if (FilenameExtension.equalsIgnoreCase(".txt")) {
return "text/plain";
}
if (FilenameExtension.equalsIgnoreCase(".vsd")) {
return "application/vnd.visio";
}
if (FilenameExtension.equalsIgnoreCase(".pptx") ||
FilenameExtension.equalsIgnoreCase(".ppt")) {
return "application/vnd.ms-powerpoint";
}
if (FilenameExtension.equalsIgnoreCase(".docx") ||
FilenameExtension.equalsIgnoreCase(".doc")) {
return "application/msword";
}
if (FilenameExtension.equalsIgnoreCase(".xml")) {
return "text/xml";
}
//PDF
if (FilenameExtension.equalsIgnoreCase(".pdf")) {
return "application/pdf";
}
//excel
if (FilenameExtension.equalsIgnoreCase(".xls") ||
FilenameExtension.equalsIgnoreCase(".xlsx")) {
return "application/octet-stream";
}
//waypoints 拓恒+大疆的航线文件类型
if (FilenameExtension.equalsIgnoreCase(".waypoints") ||
FilenameExtension.equalsIgnoreCase(".kmz")) {
return "application/octet-stream";
}
return "image/jpeg";
}
// public static boolean isWithin15Minutes(String timestamp1, String timestamp2) {
// // 将字符串转换为 long 类型
// long time1 = Long.parseLong(timestamp1);
// long time2 = Long.parseLong(timestamp2);
// // 计算时间差的绝对值
// long diff = Math.abs(time2 - time1);
// // 15 分钟 = 15 * 60 * 1000 毫秒
// long fifteenMinutesInMillis = 15 * 60 * 1000;
// // 判断是否小于或等于 15 分钟
// return diff <= fifteenMinutesInMillis;
// }
}