using System;
using System.Collections.Generic;
using System.IO;
using System.Web;
using System.Web.Helpers;
using System.Web.Mvc;
 
namespace BrandonCodeApi.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            ViewBag.Title = "Home Page";
            return Redirect("index.html");
        }
 
        [HttpPost]
        [AllowAnonymous]
        public ActionResult UploadFiles(HttpPostedFileBase file)
        {
            string path = "";
            string filePath;
            if (ModelState.IsValid)
            {
                try
                {
                    //Method 2 Get file details from HttpPostedFileBase class
                    if (file != null)
                    {
                        path = Path.Combine("C:\\BrandonFiles\\TestFolder\\" + Path.GetFileName(file.FileName));
                        filePath = "C:\\BrandonFiles\\TestFolder\\";
                        WebImage img = new WebImage(file.InputStream);
                        if (System.IO.Directory.Exists(filePath))
                        {
                            file.SaveAs(path);
                            if (img.Width > 600)
                                img.Resize(500, 600);
                            img.Save(path);
                        }
                        else
                        {
                            Directory.CreateDirectory("C:\\BrandonFiles\\TestFolder\\");
                            path = Path.Combine("C:\\BrandonFiles\\TestFolder\\" + Path.GetFileName(file.FileName));
                            if (img.Width > 600)
                                img.Resize(500, 500);
                            img.Save(path);
                        }
                    }
                    ViewBag.FileStatus = "Upload Successfully.";
                }
                catch (Exception)
                {
                    ViewBag.FileStatus = "Error while file uploading.";
                }
            }
            return RedirectToAction("Index""UploadPhoto");
        }
    }
}