using System.IO;

namespace Com.Bocom.OpenApi
{
    namespace Utils
    {
        public class FileItem
        {
            private string fileName;
            private string mimeType;
            private byte[] content;
            private FileStream fileStream;


            /**
             * 基于本地文件的构造器。
             * 
             * @param file
             *            本地文件
             */
            public FileItem(FileStream fileStream)
            {
                this.fileStream = fileStream;
                content = ToByteArray(fileStream);
                fileName = GetFileName();
            }

            /**
             * 基于文件绝对路径的构造器。
             * 
             * @param filePath
             *            文件绝对路径
             */
            public FileItem(string filePath)
            {
                FileStream fileStream = new FileStream(filePath, FileMode.OpenOrCreate);
                fileName = filePath;
                content = ToByteArray(fileStream);
            }


            public string GetFileName()
            {
                if (fileName == null && fileStream != null)
                {
                    fileName = fileStream.Name;
                }

                return fileName;
            }

            public string GetMimeType()
            {
                if (mimeType == null)
                {
                    mimeType = GetMimeType(GetContent());
                }

                return mimeType;
            }

            public static byte[] ToByteArray(Stream stream)
            {
                byte[] bytes = new byte[stream.Length];
                stream.Read(bytes, 0, bytes.Length);
                stream.Seek(0, SeekOrigin.Begin);
                stream.Close();
                return bytes;
            }

            /**
             * @param filePath
             *            文件路径
             * @return file bytes
             * @throws IOException
             *             读取文件错误
             */
            public byte[] GetContent()
            {
                /*  if (this.fileName == null && this.fileStream != null )
                  {
                      byte[] content = toByteArray(this.fileStream);
                      this.content = content;
                  }*/
                return content;
            }

            public static byte[] ToByteArray(FileStream fi)
            {
                byte[] bytes = new byte[fi.Length];
                fi.Read(bytes, 0, bytes.Length);
                fi.Close();
                return bytes;
            }

            /**
             * 获取文件的真实媒体类型。目前只支持JPG, GIF, PNG, BMP四种图片文件。
             * 
             * @param bytes
             *            文件字节流
             * @return 媒体类型(MEME-TYPE)
             */
            public static string GetMimeType(byte[] bytes)
            {
                string suffix = GetFileSuffix(bytes);
                string mimeType;

                if ("JPG".Equals(suffix))
                {
                    mimeType = "image/jpeg";
                }
                else if ("GIF".Equals(suffix))
                {
                    mimeType = "image/gif";
                }
                else if ("PNG".Equals(suffix))
                {
                    mimeType = "image/png";
                }
                else if ("BMP".Equals(suffix))
                {
                    mimeType = "image/bmp";
                }
                else
                {
                    mimeType = "application/octet-stream";
                }

                return mimeType;
            }

            /**
             * 获取文件的真实后缀名。目前只支持JPG, GIF, PNG, BMP四种图片文件。
             * 
             * @param bytes
             *            文件字节流
             * @return JPG, GIF, PNG or null
             */
            public static string GetFileSuffix(byte[] bytes)
            {
                if (bytes == null || bytes.Length < 10)
                {
                    return null;
                }

                if (bytes[0] == 'G' && bytes[1] == 'I' && bytes[2] == 'F')
                {
                    return "GIF";
                }

                if (bytes[1] == 'P' && bytes[2] == 'N' && bytes[3] == 'G')
                {
                    return "PNG";
                }

                if (bytes[6] == 'J' && bytes[7] == 'F' && bytes[8] == 'I' && bytes[9] == 'F')
                {
                    return "JPG";
                }

                if (bytes[0] == 'B' && bytes[1] == 'M')
                {
                    return "BMP";
                }

                return null;
            }

            public FileStream GetFileStream()
            {
                // TODO Auto-generated method stub
                return fileStream;
            }

            public void SetContent(byte[] encryptContent)
            {
                content = encryptContent;
            }
        }
    }
}