using Blog.Common.Consts;
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)
        {
            if (file != null) { 
            string fileName = Path.GetFileName(file.FileName);// 原始文件名称
            string fileExtension = Path.GetExtension(fileName); // 文件扩展名
            string saveName = "file/" +Guid.NewGuid().ToString()+ fileExtension; // 保存文件名称
            WestClient client = new WestClient(Constr.Bucket_Name,Constr.Bucket_User,Constr.Bucket_Pwd);
            client.method = "PUT";
            BinaryReader br = new BinaryReader(file.InputStream);
            byte[] bytebuffer = br.ReadBytes((int)file.ContentLength);
            if (client.UploadFile(saveName, bytebuffer))
             {
                return Json(new { Success = true, FileName = fileName, url = Constr.Bucket_Res_Url+ saveName }, JsonRequestBehavior.DenyGet);
              }
            }
            return Json(new { Success = false},JsonRequestBehavior.DenyGet);
        }
    }
}