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

namespace RMYY_CallCenter_Api.Utility
{
    public static class FileHelper
    {
        /// <summary>
        /// 上传文件
        /// </summary>
        /// <param name="hpFile">文件</param>
        /// <param name="path">路径</param>
        /// <returns></returns>
        public static string Upload(HttpPostedFileBase hpFile, string path)
        {
            path = HttpContext.Current.Server.MapPath("..") + path;
            string newName = "(" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ")" + hpFile.FileName;
            int iLen = hpFile.ContentLength;
            if (!Directory.Exists(path)) Directory.CreateDirectory(path);
            byte[] bData = new byte[iLen];
            hpFile.InputStream.Read(bData, 0, iLen);

            FileStream newFile = new FileStream(path + newName, FileMode.Create);
            newFile.Write(bData, 0, bData.Length);
            newFile.Flush();
            return newName;
        }

        /// <summary>
        /// 根据地址获取文件扩展名
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static string GetExt(string path)
        {
            return Path.GetExtension(path);
        }

        /// <summary>
        /// 根据地址获取文件扩展名
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static string GetPath()
        {
            return HttpContext.Current.Server.MapPath("..");
        }
        /// <summary>
        /// 判断附件是否存在
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static bool Exists(string path)
        {
            return File .Exists (path);
        }

        /// <summary>
        /// 获取文件MD5值
        /// </summary>
        /// <param name="fileName">文件绝对路径</param>
        /// <returns>MD5值</returns>
        public static string GetFileMD5(string fileName)
        {
            try
            {
                FileStream file = new FileStream(fileName, FileMode.Open);
                System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
                byte[] retVal = md5.ComputeHash(file);
                file.Close();

                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < retVal.Length; i++)
                {
                    sb.Append(retVal[i].ToString("x2"));
                }
                return sb.ToString();
            }
            catch
            {
                return null;
            }
        }

        public static string Upload(HttpPostedFileBase hpFile, string path, string names)
        {
            path = HttpContext.Current.Server.MapPath("..") + "/" + path;
            string fileType = hpFile.FileName.Substring(hpFile.FileName.LastIndexOf("."));
            string newName = DateTime.Now.ToString("yyyyMMdd") + names + fileType;
            int iLen = hpFile.ContentLength;
            if (!Directory.Exists(path)) Directory.CreateDirectory(path);
            byte[] bData = new byte[iLen];
            hpFile.InputStream.Read(bData, 0, iLen);

            FileStream newFile = new FileStream(path + newName, FileMode.Create);
            newFile.Write(bData, 0, bData.Length);
            newFile.Flush();

            return newName;
        }

        /// <summary>
        /// 获取文件文本
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static string GetFileText(string path)
        {
            string result = string.Empty;
            if (File.Exists(path))
            {
                result = File.ReadAllText(path, Encoding.UTF8);
            }
            return result;
        }

        /// <summary>
        /// 创建文本XML文件
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        public static bool CreateTextXMLText(string path,string name,string text)
        {
            try
            {
                if (!Directory.Exists(path)) Directory.CreateDirectory(path);
                string filepath = path + "\\" + name;

                FileStream fs = new FileStream(filepath, FileMode.Create, FileAccess.Write);
                StreamWriter sw = sw = new StreamWriter(fs);
                sw.WriteLine(text);
                sw.Close();
                fs.Close();

                return true;
            }
            catch(Exception ex)
            {
                return false;
            }
        }
    }
}
