示例#1
0
        public async Task<ActionResult> Edit(GalleryEditViewModel model)
        {
            string result = "";
            var data = db.Galleries.Find(model.Id);
            if (data == null) return Json(LanguageDB.NotFound.GetError());
            if (!string.IsNullOrEmpty(model.ImageData))
            {
                var image = db.AppFiles.Find(model.AppFileId);
                if (image != null)
                {
                    image.FullPath.DeleteFile();
                    db.AppFiles.Remove(image);
                }
                var saveFile = await db.SaveImage(model.ImageData, "galleries", null);
                if (!saveFile.OK) return Json(Js.Error(saveFile.Message));
                data.AppFiles.Add(saveFile.Image);

                db.Entry(image).State = System.Data.Entity.EntityState.Modified;
                result += saveFile.Message;
            }
            data.Name = model.Name;
            data.Subtitle = model.Subtitle;
            data.Link = model.Link;
            data.VideoLink = model.VideoLink;
            data.InHome = model.InHome;
            data.ButtonText = model.ButtonText;
            db.Entry(data).State = System.Data.Entity.EntityState.Modified;
            var str = await db.SaveMessageAsync();
            if (str != null) return Json(Js.Error(str));
            return Json(Js.SuccessRedirect("Đã cập nhật Gallery: " + result, "/admin/home/config", 10));
        }
        public ActionResult Edit(int Page_ID, int Gallery_ID)
        {
            Gallery gallery = db.Galleries.Find(Gallery_ID);

            if (gallery == null)
            {
                return(HttpNotFound());
            }
            GalleryEditViewModel galViewMod = new GalleryEditViewModel(Page_ID, gallery);

            cleanUpUploads(gallery);
            ViewBag.Success_Uploads = "";
            ViewBag.Failed_Uploads  = "";
            ViewBag.Upload_Counts   = "";
            return(View(galViewMod));
        }
示例#3
0
        public ActionResult Index(HttpPostedFileBase[] files, string Gallery_Name, int Gallery_ID, int Page_ID)
        {
            Gallery gal = db.Galleries.Find(Gallery_ID);

            try
            {
                /*Lopp for multiple files*/
                string retView = "";
                foreach (HttpPostedFileBase ImgFile in files)
                {
                    Regex  r             = new Regex("[^a-zA-Z0-9]");
                    string dirName       = r.Replace(Gallery_Name, "");
                    string pathRoot      = "~/" + IMAGE_BASE_ROOT + dirName + "/";
                    string pathRootTemp  = "~/" + IMAGE_BASE_ROOT + dirName + "/uploadTemp/";
                    string dbRoot        = IMAGE_BASE_ROOT + dirName + "/";
                    string displayRoot   = "/" + IMAGE_BASE_ROOT + dirName + "/";
                    string fileNameNoExt = System.IO.Path.GetFileNameWithoutExtension(ImgFile.FileName);
                    string ext           = System.IO.Path.GetExtension(ImgFile.FileName).ToLower();
                    if (ext == ".jpg" || ext == ".png")
                    {
                        if (!System.IO.Directory.Exists(Server.MapPath(pathRoot)))
                        {
                            System.IO.Directory.CreateDirectory(Server.MapPath(pathRoot));
                        }
                        if (!System.IO.Directory.Exists(Server.MapPath(pathRootTemp)))
                        {
                            System.IO.Directory.CreateDirectory(Server.MapPath(pathRootTemp));
                        }

                        string filePath     = Server.MapPath(pathRoot) + ImgFile.FileName;
                        string filePathTemp = Server.MapPath(pathRootTemp) + ImgFile.FileName;
                        string displayPath  = displayRoot + ImgFile.FileName;
                        string retName      = "";
                        try
                        {
                            retName       = UploadFile(ImgFile, pathRootTemp, fileNameNoExt, ext);
                            filePathTemp  = Server.MapPath(pathRootTemp) + retName + ext;
                            filePath      = Server.MapPath(pathRoot) + retName + ext;
                            displayPath   = displayRoot + retName + ext;
                            fileNameNoExt = retName;
                        }
                        catch (Exception ex)
                        {
                            return(Json(new { error = "Upload Failed", message = ex.Message }));
                        }

                        //scaling image
                        Image origImg = Bitmap.FromFile(filePathTemp);
                        int   width   = origImg.Width;
                        int   height  = origImg.Height;

                        if (width > height)
                        {
                            if (width > GALLERY_IMG_WIDTH)
                            {
                                double origDouble = Convert.ToDouble(width);
                                double maxDouble  = Convert.ToDouble(GALLERY_IMG_WIDTH);
                                double perc       = 1 - ((origDouble - maxDouble) / origDouble);
                                width  = Convert.ToInt32(Convert.ToDouble(width) * perc);
                                height = Convert.ToInt32(Convert.ToDouble(height) * perc);
                                Size  newSize  = new Size(width, height);
                                Image newImage = (Image)(new Bitmap(origImg, newSize));
                                //System.IO.File.Delete(filePath);
                                if (System.IO.File.Exists(filePath))
                                {
                                    int i = 1;
                                    filePath = Server.MapPath(pathRoot) + fileNameNoExt + i.ToString() + ext;
                                    retName  = fileNameNoExt + i.ToString();

                                    while (System.IO.File.Exists(filePath))
                                    {
                                        i++;
                                        filePath = Server.MapPath(pathRoot) + fileNameNoExt + i.ToString() + ext;
                                        retName  = fileNameNoExt + i.ToString();
                                    }
                                    fileNameNoExt = retName;
                                    displayPath   = displayRoot + retName + ext;
                                }
                                if (ext == ".jpg")
                                {
                                    newImage.Save(filePath, ImageFormat.Jpeg);
                                }
                                else if (ext == ".png")
                                {
                                    newImage.Save(filePath, ImageFormat.Png);
                                }
                                newImage.Dispose();
                            }
                            else
                            {
                                retName       = UploadFile(ImgFile, pathRoot, fileNameNoExt, ext);
                                filePath      = Server.MapPath(pathRoot) + retName + ext;
                                displayPath   = displayRoot + retName + ext;
                                fileNameNoExt = retName;
                            }
                        }
                        else
                        {
                            if (height > GALLERY_IMG_HEIGHT)
                            {
                                double origDouble = Convert.ToDouble(height);
                                double maxDouble  = Convert.ToDouble(GALLERY_IMG_HEIGHT);
                                double perc       = 1 - ((origDouble - maxDouble) / origDouble);
                                width  = Convert.ToInt32(Convert.ToDouble(width) * perc);
                                height = Convert.ToInt32(Convert.ToDouble(height) * perc);
                                Size  newSize  = new Size(width, height);
                                Image newImage = (Image)(new Bitmap(origImg, newSize));

                                if (System.IO.File.Exists(filePath))
                                {
                                    int i = 1;
                                    filePath = Server.MapPath(pathRoot) + fileNameNoExt + i.ToString() + ext;
                                    retName  = fileNameNoExt + i.ToString();
                                    while (System.IO.File.Exists(filePath))
                                    {
                                        i++;
                                        filePath = Server.MapPath(pathRoot) + fileNameNoExt + i.ToString() + ext;
                                        retName  = fileNameNoExt + i.ToString();
                                    }
                                    fileNameNoExt = retName;
                                    displayPath   = displayRoot + retName + ext;
                                }

                                if (ext == ".jpg")
                                {
                                    newImage.Save(filePath, ImageFormat.Jpeg);
                                }
                                else if (ext == ".png")
                                {
                                    newImage.Save(filePath, ImageFormat.Png);
                                }
                                newImage.Dispose();
                            }
                            else
                            {
                                retName       = UploadFile(ImgFile, pathRoot, fileNameNoExt, ext);
                                filePath      = Server.MapPath(pathRoot) + retName + ext;
                                displayPath   = displayRoot + retName + ext;
                                fileNameNoExt = retName;
                            }
                        }
                        origImg.Dispose();

                        Photo photo = new Photo();
                        photo.File_Path  = dbRoot + fileNameNoExt + ext;
                        photo.Thumb_Path = dbRoot + fileNameNoExt + ext;
                        photo.Archived   = false;
                        photo.Added_By   = User.Identity.Name.ToString();///TODO:auth
                        photo.Date_Added = DateTime.Now;
                        photo.Gallery    = gal;
                        db.Photos.Add(photo);
                        db.SaveChanges();

                        //var files = new List<object>();
                        //files.Add(new { url = displayPath.Replace("\\", "\\/"), thumbnailUrl = displayPath.Replace("\\", "\\/"), name = ImgFile.FileName, type = "image/jpeg", size = ImgFile.ContentLength, deleteUrl = displayPath.Replace("\\", "\\/"), deleteType = "DELETE" });
                        //return Json(files, JsonRequestBehavior.AllowGet);
                    }
                    else
                    {
                        return(Json(new { error = "Upload Failed", message = "Not a  supported image format" }));
                    }

                    ///*Geting the file name*/
                    //string filename = System.IO.Path.GetFileName(file.FileName);
                    ///*Saving the file in server folder*/
                    //retView += "<img src=\"images/"+filename+"\" alt=\"Image Not Found\"/>";
                    //file.SaveAs(Server.MapPath("~/images/" + filename));
                    //string filepathtosave = "Images/" + filename;
                    ///*HERE WILL BE YOUR CODE TO SAVE THE FILE DETAIL IN DATA BASE*/
                    retView += "<img src=\"/" + dbRoot + fileNameNoExt + ext + "\" alt=\"Image Not Found\"/>";
                }

                ViewBag.Message = retView;
            }
            catch
            {
                ViewBag.Message = "Error while uploading the files.";
            }
            GalleryEditViewModel vm = new GalleryEditViewModel(Page_ID, gal);

            return(View());
        }
        public ActionResult AddImages(HttpPostedFileBase[] files, int Page_ID, int Gallery_ID, string Gallery_Name)
        {
            string  successUploads = "<h3>Successful Uploads</h3><hr /><table role=\"presentation\" class=\"table table-striped\"><tbody class=\"files\">";
            string  failedUploads  = "<h3>Failed Uploads</h3><hr /><table role=\"presentation\" class=\"table table-striped\"><tbody class=\"files\">";
            int     success        = 0;
            int     failed         = 0;
            Gallery gal            = db.Galleries.Find(Gallery_ID);

            foreach (HttpPostedFileBase ImgFile in files)
            {
                if (ImgFile != null)
                {
                    Regex  r             = new Regex("[^a-zA-Z0-9]");
                    string dirName       = r.Replace(Gallery_Name, "");
                    string pathRoot      = "~/" + IMAGE_BASE_ROOT + dirName + "/";
                    string pathRootTemp  = "~/" + IMAGE_BASE_ROOT + dirName + "/uploadTemp/";
                    string dbRoot        = IMAGE_BASE_ROOT + dirName + "/";
                    string displayRoot   = "/" + IMAGE_BASE_ROOT + dirName + "/";
                    string fileNameNoExt = System.IO.Path.GetFileNameWithoutExtension(ImgFile.FileName);
                    string ext           = System.IO.Path.GetExtension(ImgFile.FileName).ToLower();
                    if (ext == ".jpg" || ext == ".png")
                    {
                        try
                        {
                            if (!System.IO.Directory.Exists(Server.MapPath(pathRoot)))
                            {
                                System.IO.Directory.CreateDirectory(Server.MapPath(pathRoot));
                            }
                            if (!System.IO.Directory.Exists(Server.MapPath(pathRootTemp)))
                            {
                                System.IO.Directory.CreateDirectory(Server.MapPath(pathRootTemp));
                            }

                            string filePath     = Server.MapPath(pathRoot) + ImgFile.FileName;
                            string filePathTemp = Server.MapPath(pathRootTemp) + ImgFile.FileName;
                            string displayPath  = displayRoot + ImgFile.FileName;
                            string retName      = "";
                            try
                            {
                                retName       = UploadFile(ImgFile, pathRootTemp, fileNameNoExt, ext);
                                filePathTemp  = Server.MapPath(pathRootTemp) + retName + ext;
                                filePath      = Server.MapPath(pathRoot) + retName + ext;
                                displayPath   = displayRoot + retName + ext;
                                fileNameNoExt = retName;
                            }
                            catch (Exception ex)
                            {
                                return(Json(new { error = "Upload Failed", message = ex.Message }));
                            }

                            //scaling image
                            Image origImg = Bitmap.FromFile(filePathTemp);
                            int   width   = origImg.Width;
                            int   height  = origImg.Height;

                            if (width > height)
                            {
                                if (width > GALLERY_IMG_WIDTH)
                                {
                                    double origDouble = Convert.ToDouble(width);
                                    double maxDouble  = Convert.ToDouble(GALLERY_IMG_WIDTH);
                                    double perc       = 1 - ((origDouble - maxDouble) / origDouble);
                                    width  = Convert.ToInt32(Convert.ToDouble(width) * perc);
                                    height = Convert.ToInt32(Convert.ToDouble(height) * perc);
                                    Size  newSize  = new Size(width, height);
                                    Image newImage = (Image)(new Bitmap(origImg, newSize));
                                    //System.IO.File.Delete(filePath);
                                    if (System.IO.File.Exists(filePath))
                                    {
                                        int i = 1;
                                        filePath = Server.MapPath(pathRoot) + fileNameNoExt + i.ToString() + ext;
                                        retName  = fileNameNoExt + i.ToString();

                                        while (System.IO.File.Exists(filePath))
                                        {
                                            i++;
                                            filePath = Server.MapPath(pathRoot) + fileNameNoExt + i.ToString() + ext;
                                            retName  = fileNameNoExt + i.ToString();
                                        }
                                        fileNameNoExt = retName;
                                        displayPath   = displayRoot + retName + ext;
                                    }
                                    if (ext == ".jpg")
                                    {
                                        newImage.Save(filePath, ImageFormat.Jpeg);
                                    }
                                    else if (ext == ".png")
                                    {
                                        newImage.Save(filePath, ImageFormat.Png);
                                    }
                                    newImage.Dispose();
                                }
                                else
                                {
                                    retName       = UploadFile(ImgFile, pathRoot, fileNameNoExt, ext);
                                    filePath      = Server.MapPath(pathRoot) + retName + ext;
                                    displayPath   = displayRoot + retName + ext;
                                    fileNameNoExt = retName;
                                }
                            }
                            else
                            {
                                if (height > GALLERY_IMG_HEIGHT)
                                {
                                    double origDouble = Convert.ToDouble(height);
                                    double maxDouble  = Convert.ToDouble(GALLERY_IMG_HEIGHT);
                                    double perc       = 1 - ((origDouble - maxDouble) / origDouble);
                                    width  = Convert.ToInt32(Convert.ToDouble(width) * perc);
                                    height = Convert.ToInt32(Convert.ToDouble(height) * perc);
                                    Size  newSize  = new Size(width, height);
                                    Image newImage = (Image)(new Bitmap(origImg, newSize));

                                    if (System.IO.File.Exists(filePath))
                                    {
                                        int i = 1;
                                        filePath = Server.MapPath(pathRoot) + fileNameNoExt + i.ToString() + ext;
                                        retName  = fileNameNoExt + i.ToString();
                                        while (System.IO.File.Exists(filePath))
                                        {
                                            i++;
                                            filePath = Server.MapPath(pathRoot) + fileNameNoExt + i.ToString() + ext;
                                            retName  = fileNameNoExt + i.ToString();
                                        }
                                        fileNameNoExt = retName;
                                        displayPath   = displayRoot + retName + ext;
                                    }

                                    if (ext == ".jpg")
                                    {
                                        newImage.Save(filePath, ImageFormat.Jpeg);
                                    }
                                    else if (ext == ".png")
                                    {
                                        newImage.Save(filePath, ImageFormat.Png);
                                    }
                                    newImage.Dispose();
                                }
                                else
                                {
                                    retName       = UploadFile(ImgFile, pathRoot, fileNameNoExt, ext);
                                    filePath      = Server.MapPath(pathRoot) + retName + ext;
                                    displayPath   = displayRoot + retName + ext;
                                    fileNameNoExt = retName;
                                }
                            }
                            origImg.Dispose();

                            Photo photo = new Photo();
                            photo.File_Path  = dbRoot + fileNameNoExt + ext;
                            photo.Thumb_Path = dbRoot + fileNameNoExt + ext;
                            photo.Archived   = false;
                            photo.Added_By   = User.Identity.Name.ToString();///TODO:auth
                            photo.Date_Added = DateTime.Now;
                            photo.Gallery    = gal;
                            db.Photos.Add(photo);

                            successUploads += "<tr class=\"table table-striped\"><td class=\"col-md-6\"><span class=\"preview\"><div class=\"col-sm-6 col-md-4\"><div class=\"thumbnail\"><a rel=\"lightbox-cats\" href=\"" + "/" + dbRoot + fileNameNoExt + ext + "\" title=\"" + ImgFile.FileName + "\" target=\"_blank\"><img class=\"img-responsive\" alt=\"No Image Found\" src=\"" + "/" + dbRoot + fileNameNoExt + ext + "\"></img></a></div></div></span></td><td class=\"col-md-2\"><p class=\"name\">" + ImgFile.FileName + "</p></td><td class=\"col-md-2\"><img class=\"img-responsive col-md-6\" src=\"../images/green_tick.png\" alt\"No Image Found\"></img></td></tr>";
                            success++;
                            //var files = new List<object>();
                            //files.Add(new { url = displayPath.Replace("\\", "\\/"), thumbnailUrl = displayPath.Replace("\\", "\\/"), name = ImgFile.FileName, type = "image/jpeg", size = ImgFile.ContentLength, deleteUrl = displayPath.Replace("\\", "\\/"), deleteType = "DELETE" });
                            //return Json(files, JsonRequestBehavior.AllowGet);
                        }
                        catch (Exception EX)
                        {
                            failedUploads += "<tr class=\"table table-striped\"><td class=\"col-md-6\"><span class=\"label label-danger\">Error:</span>&nbsp;" + EX.Message + "</td><td class=\"col-md-2\"><p class=\"name\">" + ImgFile.FileName + "</p></td><td class=\"col-md-2\"><img class=\"img-responsive col-md-6\" src=\"../images/red_x.png\" alt\"No Image Found\"></img></td></tr>";
                            failed++;
                        }
                    }
                    else
                    {
                        failedUploads += "<tr class=\"table table-striped\"><td class=\"col-md-6\"><span class=\"label label-danger\">Error:</span>&nbsp;" + ext + " is not a supported image format</td><td class=\"col-md-2\"><p class=\"name\">" + ImgFile.FileName + "</p></td><td class=\"col-md-2\"><img class=\"img-responsive col-md-6\" src=\"../images/red_x.png\" alt\"No Image Found\"></img></td></tr>";
                        failed++;
                    }
                }
            }
            db.SaveChanges();
            if (success > 0)
            {
                successUploads += "</tbody></table>";
            }
            else
            {
                successUploads = "";
            }
            if (failed > 0)
            {
                failedUploads += "</tbody></table>";
            }
            else
            {
                failedUploads = "";
            }

            ViewBag.Success_Uploads = successUploads;
            ViewBag.Failed_Uploads  = failedUploads;
            string cssClass = "warning";

            if (failed == 0)
            {
                cssClass = "success";
            }
            else if (success == 0)
            {
                cssClass = "danger";
            }
            if (success > 0 || failed > 0)
            {
                ViewBag.Upload_Counts = "<div class=\"alert alert-" + cssClass + " alert-dismissible\" role=\"alert\"><button type=\"button\" class=\"close\" data-dismiss=\"alert\" aria-label=\"Close\"><span aria-hidden=\"true\">&times;</span></button><strong>Upload Complete:</strong> " + success + " file(s) uploaded successfully, " + failed + " failed.</div>";
            }
            else
            {
                ViewBag.Upload_Counts = "";
            }
            GalleryEditViewModel vm = new GalleryEditViewModel(Page_ID, gal);

            return(View("Edit", vm));
        }