public ActionResult UploadPhoto([Bind(Include = "Name,Description")]  PhotoFile file)
        {
            SaveObject obj;
            try
            {
                obj = PhotoHelper.SavePhoto(Request, Server, "Product", "FileUpload");
                if (obj.isValid)
                {
                    if (ModelState.IsValid)
                    {
                        file.FilePath = obj.FilePath;

                        db.Photos.AddOrUpdate(file);
                        db.SaveChanges();
                        Session.Add("FileID", file.PhotoId);

                    }
                    else
                    {
                        return View(file);
                    }
                    //  ViewBag.ProdCategories = new SelectList(db.ProdCategories, "CategoryId", "Name");

                    string path = "";
                    PhotoEdit check = PhotoEdit.def;
                    if (Session["EditUrl"] != null)
                    {
                        check = (PhotoEdit)Enum.Parse(typeof(PhotoEdit), Session["EditUrl"].ToString());
                        Session.Remove("EditUrl");
                    }
                    int EditId = 0;
                    if (Session["EditID"] != null)
                    {
                        EditId = Regex.IsMatch(Session["EditID"].ToString(), Variables.RegexForNumbers) ?
                            Int32.Parse(Session["EditID"].ToString()) : -1;
                        Session.Remove("EditID");
                        if (EditId == -1)
                        {

                            obj.Error.Add("Edit ID Problem or Something!");
                            ViewBag.Errors = obj;

                            return View();
                        }
                    }
                    string prod = null;
                    dynamic model = null;
                    switch (check)
                    {
                        case PhotoEdit.Create:
                            ViewBag.FileName = db.Photos.Find(file.PhotoId);

                            ViewBag.ProdCategories = new SelectList(db.ProdCategories, "CategoryId", "Name");
                            path = "Create";
                            break;
                        case PhotoEdit.EditProduct:
                            TempData["FileName"] = db.Photos.Find(file.PhotoId);
                            // model = db.Products.Find();
                            prod = "Products";
                            path = "Edit";
                            break;
                        case PhotoEdit.Edit:

                            model = db.Photos.Find(file.PhotoId);
                            path = "EditPhoto";
                            break;
                        default:
                            return RedirectToAction("Index");

                    }
                    System.Web.Routing.RouteValueDictionary routeValues = new System.Web.Routing.RouteValueDictionary();
                    routeValues.Add("id",EditId);

                    return RedirectToAction(path,prod,routeValues);

                    // return RedirectToAction("Create");

                }
                else
                {
                    ViewBag.Errors = obj.Error;
                    return View();
                }
            }
            catch (HttpException)
            {
                obj = new SaveObject();
                obj.isValid = false;
                obj.Error.Add("The size of the file must be below 6mb");
                return View();
            }
        }
        public ActionResult Create([Bind(Include = "FileName,Description")] PdfFile pdfFile)
        {
            int? PdfCatId = null;
            var req = Request["PDFCategories"];
            if (req != null)
            {
                if (req == "")
                {
                    req = "0";
                }

                if (Regex.IsMatch(req, Variables.RegexForNumbers))
                {
                    PdfCatId = Int32.Parse(req);
                    if (PdfCatId == 0 || PdfCatId == null)
                    {
                        ViewBag.PDFCategories = new SelectList(db.PdfCategories, "CategoryId", "Name");
                        ViewBag.PdfCatError = "Please Select a Product Category!";

                    }
                }
            }
            SaveObject obj;
            try
            {
                obj = PdfHelper.SavePDF(Request, Server, "PDF", "FileUpload");
                if (obj.isValid)
                {
                    pdfFile.Category = db.PdfCategories.Find(PdfCatId);
                    ModelState.Clear();
                    if (TryValidateModel(pdfFile))
                    {

                        pdfFile.FilePath = obj.FilePath;

                        db.PdfFiles.AddOrUpdate(pdfFile);
                        db.SaveChanges();
                        Session.Add("FileID", pdfFile.PdfId);
                        return RedirectToAction("Index");

                    }
                    else
                    {
                        return View(pdfFile);
                    }
                    //  ViewBag.ProdCategories = new SelectList(db.ProdCategories, "CategoryId", "Name");

                }
                else
                {
                    ViewBag.Errors = obj.Error;
                    return View();
                }
            }
            catch (HttpException)
            {
                obj = new SaveObject();
                obj.isValid = false;
                obj.Error.Add("The size of the file must be below 6mb");
                return View();
            }
        }
        public ActionResult EditPhoto([Bind(Include = "Name,Description")] PhotoFile editfile, int? id)
        {
            string htmlattr = "FileUpload";
            SaveObject Edit = new SaveObject();

            if (!Regex.IsMatch(id.ToString(), Variables.RegexForNumbers))
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }

            if (Request.Files[htmlattr].ContentLength > 0)
            {
                Edit = PhotoHelper.EditPhoto((int)id, editfile, Request, Server, "Product", htmlattr);
            }
            else
            {
                PhotoFile file = db.Photos.Find(id);
                if (file == null)
                {
                    return HttpNotFound();
                }
                file.Description = editfile.Description;
                file.Name = editfile.Name;
                try
                {
                    db.Photos.AddOrUpdate(file);
                    db.SaveChanges();
                    Edit.isValid = true;
                }
                catch (Exception)
                {
                    Edit.Error.Add("Something happened!");
                    Edit.isValid = false;
                }
            }
            if (Edit.isValid)
            {
                string path = "";
                PhotoEdit check = PhotoEdit.def;
                if (Session["EditUrl"] != null)
                {
                    check = (PhotoEdit)Enum.Parse(typeof(PhotoEdit), Session["EditUrl"].ToString());
                    Session.Remove("EditUrl");
                }
                int EditId = 0;
                if(Session["EditID"] != null)
                {
                    EditId = Regex.IsMatch(Session["EditID"].ToString(), Variables.RegexForNumbers) ?
                        Int32.Parse(Session["EditID"].ToString()) : -1;
                    Session.Remove("EditID");
                    if(EditId == -1)
                    {
                        Edit.Error.Add("Edit ID Problem or Something!");
                        ViewBag.Edit = Edit;

                        return View();
                    }
                }

                string prod = null;
                dynamic model = null;
                switch (check)
                {
                    case PhotoEdit.Create:
                        ViewBag.FileName = db.Photos.Find(id);

                        ViewBag.ProdCategories = new SelectList(db.ProdCategories, "CategoryId", "Name");
                        path = "Create";
                        break;
                    case PhotoEdit.EditProduct:
                        TempData["FileName"] = db.Photos.Find(id);
                        // model = db.Products.Find();
                        prod = "Products";
                        path = "Edit";
                        break;
                    case PhotoEdit.Edit:

                        model = db.Photos.Find(id);
                        path = "EditPhoto";
                        break;
                    default:
                        return RedirectToAction("Index");

                }
                if(Session["FromEditProduct"] !=null)
                {
                    bool ep = bool.Parse(Session["FromEditProduct"].ToString());
                    Session.Remove("FromEditProduct");
                    if(ep)
                    {
                        path = "Edit";
                        prod = "Products";
                        TempData["FileName"] = db.Photos.Find(id);
                    }
                }
                System.Web.Routing.RouteValueDictionary routeValues = new System.Web.Routing.RouteValueDictionary();
                routeValues.Add("id", EditId);

                return RedirectToAction(path, prod, routeValues);

            }
            ViewBag.Edit = Edit;
            return View();
        }
        public static SaveObject SavePhoto(HttpRequestBase Request, HttpServerUtilityBase Server, string prefix, string htmlattr)
        {
            SaveObject obj = new SaveObject();
            try
            {
                var file = Request.Files[htmlattr];
                string pathToSave = Server.MapPath(SynMetal_MVC.Models.Variables.pathToSavePhotos);
                string extension = Path.GetExtension(file.FileName);
                if (extension == ".jpg" || extension == ".png" || extension == ".JPG" || extension == ".PNG")
                {
                    if (file.ContentLength > 0 && file.ContentLength <= 6000000)
                    {

                        string filename = prefix + "_photo_"
                            + Guid.NewGuid().ToString().Substring(0, 8) + extension;
                        string url = Path.Combine(pathToSave, filename);
                        file.SaveAs(url);
                        obj.Name = filename;
                        obj.FilePath = Path.Combine(Variables.pathToSavePhotos, filename);
                        obj.isValid = true;

                    }
                    else
                    {
                        obj.isValid = false;
                        obj.Error.Add("The size of the file must be below 6mb");
                    }
                }
                else
                {
                    obj.isValid = false;
                    obj.Error.Add("Not valid extension!! Use .jpg or .png");

                }
            }
            catch (HttpException)
            {
                obj.isValid = false;
                obj.Error.Add("The size of the file must be below 6mb");
                // return obj;
            }

            return obj;
        }
        public static SaveObject EditPhoto(int id, PhotoFile editfile, HttpRequestBase Request, HttpServerUtilityBase Server, string prefix, string htmlattr)
        {
            SaveObject obj = new SaveObject();
            obj = DeletePhoto(id, Server, true);
            if (obj.isValid)
            {
                obj = SavePhoto(Request, Server, prefix, htmlattr);
                if (obj.isValid)
                {
                    PhotoFile file = StoreDB.Photos.Find(id);
                    file.Description = editfile.Description;
                    file.Name = editfile.Name;
                    file.FilePath = obj.FilePath;
                    editfile = null;
                    StoreDB.Photos.AddOrUpdate(file);
                    StoreDB.SaveChanges();
                }

            }
            return obj;
        }
        public static SaveObject DeletePhoto(int id, HttpServerUtilityBase Server, bool IsEditable)
        {
            SaveObject obj = new SaveObject();
            PhotoFile file = StoreDB.Photos.Find(id);

            if (file == null)
            {
                obj.isValid = false;
                obj.Error.Add("The File you trying to delete doesnt exist!");
                return obj;
            }
            try
            {
                File.Delete(Server.MapPath(file.FilePath));
                if (!IsEditable)
                {
                    var trry = from prod in StoreDB.Products
                               where prod.Photo.PhotoId == file.PhotoId
                               select prod;
                    foreach(var tr in trry)
                    {
                        tr.Photo = null;
                    }

                    StoreDB.Photos.Remove(file);
                    StoreDB.SaveChanges();
                }
                obj.Name = file.Name;
                obj.isValid = true;

            }
            catch (FileNotFoundException)
            {
                obj.isValid = false;
                obj.Error.Add("The File:" + file.Name + " could not be deleted");
                return obj;
            }

            return obj;
        }
        public static SaveObject EditPDF(int id, PdfFile editfile, HttpRequestBase Request, HttpServerUtilityBase Server, string prefix, string htmlattr)
        {
            SaveObject obj = new SaveObject();
            obj = DeletePDF(id, Server, true);
            if (obj.isValid)
            {
                obj = SavePDF(Request, Server, prefix, htmlattr);

            }
            return obj;
        }
        public static SaveObject DeletePDF(int id, HttpServerUtilityBase Server, bool IsEditable)
        {
            SaveObject obj = new SaveObject();
            PdfFile file = DB.PdfFiles.Find(id);

            if (file == null)
            {
                obj.isValid = false;
                obj.Error.Add("The File you trying to delete doesnt exist!");
                return obj;
            }
            try
            {
                File.Delete(Server.MapPath(file.FilePath));
                if (!IsEditable)
                {

                    DB.PdfFiles.Remove(file);
                    DB.SaveChanges();
                }
                obj.Name = file.FileName;
                obj.isValid = true;

            }
            catch (FileNotFoundException)
            {
                obj.isValid = false;
                obj.Error.Add("The File:" + file.FileName + " could not be deleted");
                return obj;
            }

            return obj;
        }