const env = {
	aliyunServerURL: 'https://czg-oss.oss-cn-beijing.aliyuncs.com',
	accessid: 'LTAI5tMLHwnM1zYYAFuRa1fK',
	AccessKeySecret: 'jS1h2STq1vcODczDNFQkjcU6ODyYAj',
	timeout: 100000
}

const Base64 = require('./Base64.js');

require('./hmac.js');
require('./sha1.js');
const Crypto = require('./crypto.js');


const uploadFile = function(filePath, key = new Date().getTime()) {
	return new Promise((resolve, reject) => {
		// const aliyunFileKey = `${new Date().getTime()}_${userId}`;
		const aliyunServerURL = env.aliyunServerURL;
		const accessid = env.accessid;
		const policyBase64 = getPolicyBase64();
		const signature = getSignature(policyBase64);

		wx.uploadFile({
			url: aliyunServerURL, //仅为示例，非真实的接口地址
			filePath: filePath,
			name: 'file',
			formData: {
				'key': 'headportrait/' + key,
				'OSSAccessKeyId': accessid,
				'policy': policyBase64,
				'Signature': signature,
				'success_action_status': '200',
			},
			success: function(res) {
				if (res.statusCode != 200) {
					reject('上传错误:' + JSON.stringify(res))
					return;
				}
				resolve(`https://czh5.sxczgkj.cn/headportrait/${key}`)
			},
			fail: function(err) {
				reject('上传错误:' + JSON.stringify(err))
			}
		})
	})
}

const getPolicyBase64 = function() {
	let date = new Date();
	date.setHours(date.getHours() + env.timeout);
	let srcT = date.toISOString();
	const policyText = {
		"expiration": srcT, //设置该Policy的失效时间，超过这个失效时间之后，就没有办法通过这个policy上传文件了 指定了Post请求必须发生在2020年01月01日12点之前("2020-01-01T12:00:00.000Z")。
		"conditions": [
			["content-length-range", 0, 20 * 1024 * 1024] // 设置上传文件的大小限制,1048576000=1000mb
		]
	};

	const policyBase64 = Base64.encode(JSON.stringify(policyText));
	return policyBase64;
}

const getSignature = function(policyBase64) {
	const accesskey = env.AccessKeySecret;

	const bytes = Crypto.HMAC(Crypto.SHA1, policyBase64, accesskey, {
		asBytes: true
	});
	const signature = Crypto.util.bytesToBase64(bytes);

	return signature;
}

module.exports = uploadFile;
