using Blog.Common.OSS.UpYun;
using Blog.Common.OSS.WestYun;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Blog.Web.Controllers
{
    public class FileController : Controller
    {
        // GET: File
        public ActionResult Index()
        {
            return View();
        }


        [HttpPost]
        public  JsonResult Uploader(HttpPostedFileBase file)
        {

            string bicketName = "";
            // string userName = "";
            //string password = "";
            string userName = "";
            string password = "";
            //     string filePath = Server.MapPath("~/Uploads/");
            // if (!Directory.Exists(filePath))
            //  {
            //      Directory.CreateDirectory(filePath);
            //  }
            string fileName = Path.GetFileName(file.FileName);// 原始文件名称
            string fileExtension = Path.GetExtension(fileName); // 文件扩展名
            string saveName = "file/" +Guid.NewGuid().ToString()+ fileExtension; // 保存文件名称

            // file.SaveAs(filePath + saveName);

            //上传到又拍云
            string resUrl = "https://static.sicdn.com.cn"; //"https://img.statics.net.cn";
            //UpYunClient client = new UpYunClient(bicketName, userName, password);
            WestClient client = new WestClient(bicketName,userName, password);
            client.method = "PUT";
            BinaryReader br = new BinaryReader(file.InputStream);
            byte[] bytebuffer = br.ReadBytes((int)file.ContentLength);
            bool b = client.UploadFile(saveName, bytebuffer);
            if (b)
            {
                return Json(new { Success = true, FileName = fileName, SaveName = resUrl + saveName }, JsonRequestBehavior.DenyGet);
            }
            return Json(new { Success = false},JsonRequestBehavior.DenyGet);
        }
    }
}