using Aliyun.OSS;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace XYY.Core.Standard.AliYun
{

    public class AliYunConfig
    {
         

        public string AliYunEndPoint
        {
            get; set;
        }

        public string AliYunKeyId
        {
            get; set;
        }

        public string AliYunKeySecret
        {
            get; set;
        }

        public string AliYunBucketName
        {
            get; set;
        }

        public string BasePath
        {
            get; set;
        }

        public string CDNEndPoint
        {
            get; set;
        }
    }

    public class AliYunPostFileSerivce : IAliYunPostFileSerivce
    {

        private AliYunConfig _aliYunConfig;
        public AliYunPostFileSerivce(AliYunConfig aliYunConfig)
        {
            _aliYunConfig = aliYunConfig;
        }

        /// <summary>
        /// 上传文件到阿里云
        /// </summary> 
        /// <param name="fileName"></param>
        /// <param name="fileUpdate"></param>
        /// <returns></returns>
        public string PostFile(string fileName, string fileToUpdate)
        {
            string endPoint = _aliYunConfig.AliYunEndPoint;
            string keyId = _aliYunConfig.AliYunKeyId;
            string keySecret = _aliYunConfig.AliYunKeySecret;
            string bucketName = _aliYunConfig.AliYunBucketName;
            string basePath = _aliYunConfig.BasePath;

            fileName = System.IO.Path.Combine(basePath, fileName).Replace("\\", "/");
            try
            {
                PostFile(endPoint, keyId, keySecret, bucketName, fileName, fileToUpdate);
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return fileName;
        }

        /// <summary>
        /// 上传文件到阿里云
        /// </summary>
        /// <param name="endPoint"></param>
        /// <param name="keyId"></param>
        /// <param name="keySecret"></param>
        /// <param name="bucketName"></param>
        /// <param name="fileName"></param>
        /// <param name="fileUpdate"></param>
        /// <returns></returns>
        public PutObjectResult PostFile(string endPoint, string keyId, string keySecret, string bucketName, string fileName, string fileToUpdate)
        {
            var client = new OssClient(endPoint, keyId, keySecret, new Aliyun.OSS.Common.ClientConfiguration { ConnectionTimeout = 60000 });
            return client.PutObject(bucketName, fileName, fileToUpdate);
        }

        /// <summary>
        /// 上传文件到阿里云
        /// </summary>
        /// <param name="endPoint"></param>
        /// <param name="keyId"></param>
        /// <param name="keySecret"></param>
        /// <param name="bucketName"></param>
        /// <param name="fileName"></param>
        /// <param name="fileUpdate"></param>
        /// <returns></returns>
        public string PostMd5File(string fileToUpdate, string key)
        {
            string fileName = System.IO.Path.Combine(GetNowPathName(key), System.IO.Path.GetFileName(fileToUpdate));
            return PostFile(fileName, fileToUpdate);
        }

        public string GetNowPathName(string key)
        {
            return DateTime.Now.ToString_yyyyMMdd() + "\\" + key;
        }

        /// <summary>
        /// 上传文件到阿里云
        /// </summary>
        /// <param name="endPoint"></param>
        /// <param name="keyId"></param>
        /// <param name="keySecret"></param>
        /// <param name="bucketName"></param>
        /// <param name="fileName"></param>
        /// <param name="fileUpdate"></param>
        /// <returns></returns>
        public string PostMd5File2(string fileToUpdate, string key)
        {
            string fileName = System.IO.Path.Combine(GetNowPathName2(key), System.IO.Path.GetFileName(fileToUpdate));
            return PostFile(fileName, fileToUpdate);
        }


        public string GetNowPathName2(string key)
        {
            return key;
        }


        /// <summary>
        /// 获取已上传文件的CDN地址 
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public string GetCDN(string fileName)
        {
            string endPoint = _aliYunConfig.CDNEndPoint;
            return GetCDN(fileName, endPoint);
        }
        /// <summary>
        /// 获取已上传文件的CDN地址 
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public string GetCDN(string fileName, string endPoint)
        {
            return string.Concat(endPoint, fileName);
        }
        /// <summary>
        /// 获取已上传文件的CDN地址 
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public string GetPublicCDN(string fileName)
        {
            string endPoint = _aliYunConfig.CDNEndPoint;
            return GetCDN(fileName, endPoint).Replace("-internal", "");

        }

        public string PostHttpFile(System.IO.Stream file, string name)
        {
            string basePath = System.IO.Path.Combine(System.AppContext.BaseDirectory, "temp");
            if (!System.IO.Directory.Exists(basePath))
                System.IO.Directory.CreateDirectory(basePath);
            string path = System.IO.Path.Combine(basePath, name);

            byte[] data = new byte[file.Length];
            file.Read(data, 0, data.Count());
           
            System.IO.File.WriteAllBytes(path, data);
            string url = GetPublicCDN(PostMd5File(path, "temp"));
            System.IO.File.Delete(path);
            return url;
        }
         
    }
}
