using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using JuSNS.Config;
using System.IO;
using System.Drawing;

namespace JuSNS.Common.PicUpload
{
    /// <summary>
    /// 照片上传
    /// </summary>
    public class PhotoUpload : PicUpload
    {
        public PhotoUpload(HttpFileCollection hfc)
            : base(hfc)
        {
            server = PicConfig.PhotoServer;
        }
        public PhotoUpload()
            : base()
        {
            server = PicConfig.PhotoServer;
        }

        public override void Start()
        {
            string dir=string.Empty;
            dir = PicConfig.PhotoRoot;
            string path = RootDir + "/" + dir;
            path = HttpContext.Current.Server.MapPath(path);
            Dictionary<string, PicConfigInfo> pci = PicConfig.Photo;
            int j = 0;
            for (int i = 0; i < hfc.Count; i++)
            {
               
                string fileMainName = GetNewName();
                string fileExtName = GetExt(hfc[i].FileName);
                string filename = fileMainName + "." + fileExtName;
                if (i > 0 && hfc[i].ContentLength == 0)
                {
                    _errmsg = "图片文件太小";
                    continue;
                }
                if (hfc[i].ContentLength > size)
                {
                    _errmsg = "图片文件占用内存超过" + (size / 1000) + "KB";
                    continue;
                }
                if (!AllowExt(fileExtName))
                {
                    _errmsg = "图片格式不正确";
                    continue;
                }
                bool issuc = false;
                if (string.IsNullOrEmpty(server) || server == "~")
                {
                    #region 上传for本地
                    Upload up = new Upload();
                    up.PostedFile = hfc[i];
                    up.Extension = fileExtName;
                    up.FileLength = up.PostedFile.ContentLength;
                    up.FileName = filename;
                    up.SavePath = path;
                    string err = up.UploadStart();
                    if (err.IndexOf('$') != -1)
                    {
                        Thumbnail.MakeAllThumbnail(path, filename);
                        _SuccessNum++;
                        issuc = true;
                    }
                    #endregion
                }
                else
                {
                    #region 上传for远程
                    byte[] data = new byte[hfc[i].ContentLength];
                    hfc[i].InputStream.Write(data, 0, hfc[i].ContentLength);
                    int n = UploadService(data, RootDir + "/" + dir + "/" + filename);
                    foreach (string k in pci.Keys)
                    {
                        if (k != string.Empty)
                        {
                            string tmDir = RootDir + "/" + pci[k].Directory;
                            byte[] tmData;
                            Thumbnail.CreateThumbnail(data, out tmData, pci[k].X, pci[k].Y);
                            UploadService(tmData, tmDir + "/" + filename);
                        }
                    }
                    if (n > 0)
                    {
                        _SuccessNum++;
                        issuc = true;
                    }
                    #endregion
                }
                if (issuc)
                {
                    Array.Resize<int>(ref _Width, j + 1);
                    Array.Resize<int>(ref _Height, j + 1);
                    Array.Resize<string>(ref _FileName, j + 1);
                    Array.Resize<int>(ref _ContentLength, j + 1);
                    System.Drawing.Image image = Bitmap.FromStream(hfc[i].InputStream);
                    _Width[j] = image.Width;
                    _Height[j] = image.Height;
                    _FileName[j] = filename;
                    _ContentLength[j] = hfc[i].ContentLength;
                    j++;
                }
            }
        }
    }
}
