package org.sky.resource.rule.oss;

import com.qiniu.storage.BucketManager;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.Region;
import com.qiniu.storage.UploadManager;
import com.qiniu.util.Auth;
import org.sky.core.literule.annotation.LiteRuleComponent;
import org.sky.core.literule.core.RuleComponent;
import org.sky.core.oss.OssTemplate;
import org.sky.core.oss.QiniuTemplate;
import org.sky.core.oss.props.OssProperties;
import org.sky.core.oss.rule.OssRule;
import org.sky.resource.pojo.entity.Oss;
import org.sky.resource.rule.constant.OssRuleConstant;
import org.sky.resource.rule.context.OssContext;

/**
 * 七牛云存储构建类
 *
 */
@LiteRuleComponent(id = OssRuleConstant.QINIU_OSS_RULE, name = "七牛OSS构建")
public class QiniuOssRule extends RuleComponent {

	@Override
	public void process() {
		// 获取上下文
		OssContext contextBean = this.getContextBean(OssContext.class);
		Oss oss = contextBean.getOss();
		OssRule ossRule = contextBean.getOssRule();


		// 创建配置类
		OssProperties ossProperties = new OssProperties();
		ossProperties.setEndpoint(oss.getEndpoint());
		ossProperties.setTransformEndpoint(oss.getTransformEndpoint());
		ossProperties.setAccessKey(oss.getAccessKey());
		ossProperties.setSecretKey(oss.getSecretKey());
		ossProperties.setBucketName(oss.getBucketName());
		// 创建客户端
		Configuration cfg = new Configuration(Region.autoRegion());
		Auth auth = Auth.create(oss.getAccessKey(), oss.getSecretKey());
		UploadManager uploadManager = new UploadManager(cfg);
		BucketManager bucketManager = new BucketManager(auth, cfg);
		OssTemplate ossTemplate = new QiniuTemplate(auth, uploadManager, bucketManager, ossProperties, ossRule);

		// 设置上下文
		contextBean.setOssTemplate(ossTemplate);

	}
}
