package com.jsowell.common.util.file;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.ObjectMetadata;
import com.jsowell.common.config.AliyunOssConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.time.LocalDateTime;
import java.time.ZoneOffset;

@Component
public class AliyunOssUploadUtils {

	private static AliyunOssConfig aliyunOssConfig;


	/**
	 * 使用构造方法注入配置信息
	 */
	@Autowired
	public AliyunOssUploadUtils(AliyunOssConfig aliyunOssConfig) {
		AliyunOssUploadUtils.aliyunOssConfig = aliyunOssConfig;
	}

	/**
	 * 上传文件
	 *
	 * @param file
	 * @return
	 * @throws Exception
	 */
	public static String uploadFile(MultipartFile file) throws Exception {

		// 生成 OSSClient
		OSS ossClient = new OSSClientBuilder().build(aliyunOssConfig.getEndpoint(), aliyunOssConfig.getAccessKeyId(), aliyunOssConfig.getAccessKeySecret());
		// 原始文件名称
		// String originalFilename = file.getOriginalFilename();

		// 编码文件名
		String filePathName = FileUploadUtils.extractFilename(file);

		//获取当前时间
		LocalDateTime currentDate = LocalDateTime.now();
		String dir_name = currentDate.getYear() + "/" + currentDate.getMonthValue() + "/" + currentDate.getDayOfMonth() + "/";

		// 随机数
		String randomNumber = Long.toHexString(currentDate.toEpochSecond(ZoneOffset.UTC)) + "-";

		// 文件路径名称
		filePathName = aliyunOssConfig.getFilehost() + "/" + dir_name + randomNumber + filePathName;
		try {
			ossClient.putObject(aliyunOssConfig.getBucketName(), filePathName, file.getInputStream());
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (ossClient != null) {
				ossClient.shutdown();
			}
		}
		return aliyunOssConfig.getUrl() + "/" + filePathName;
	}

	/**
	 * 上传到阿里云(车位相机用)
	 * @param compressedImageBytes
	 * @param fileName 文件名 （车位号 + 时间戳）.jpg
	 * @return
	 */
	public static String upload2OSS(byte[] compressedImageBytes, String fileName) {
		// 生成 OSSClient
		OSS ossClient = new OSSClientBuilder().build(aliyunOssConfig.getEndpoint(), aliyunOssConfig.getAccessKeyId(), aliyunOssConfig.getAccessKeySecret());

		// 创建对象元数据，默认是可读的
		ObjectMetadata metadata = new ObjectMetadata();
		metadata.setContentLength(compressedImageBytes.length); // 设置内容长度

		// 创建输入流
		InputStream inputStream = new ByteArrayInputStream(compressedImageBytes);

		//获取当前时间
		LocalDateTime currentDate = LocalDateTime.now();
		String dir_name = currentDate.getYear() + "/" + currentDate.getMonthValue() + "/" + currentDate.getDayOfMonth() + "/";

		// 随机数
		String randomNumber = Long.toHexString(currentDate.toEpochSecond(ZoneOffset.UTC)) + "-";

		// 文件路径名称
		String filePathName = aliyunOssConfig.getFilehost() + "/" + dir_name + randomNumber + fileName;
		ossClient.putObject(aliyunOssConfig.getBucketName(), filePathName, inputStream, metadata);

		// 关闭 OSS 客户端
		ossClient.shutdown();
		return aliyunOssConfig.getUrl() + "/" + filePathName;
	}
}