示例#1
0
        public ActionResult Edit(News news)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    string new_image = Url.Content("/images/default-news-img.png");
                    // EDIT EXISTING IMAGE
                    string existingImages = managerForImages.FindNewsById(news.Id).Images;
                    if (!String.IsNullOrEmpty(existingImages))
                    {
                        string[] images = existingImages.Split('|');
                        foreach (string field in Request.Form.Keys)
                        {
                            if (field.Contains("check_"))
                            {
                                var    checkbox = Request.Form[field];
                                string src      = field.Split('_')[1];
                                if (checkbox == "false")
                                {
                                    GoogleDriveManager driveClient = new GoogleDriveManager(hosting);
                                    driveClient.DriveMoveFileToTrash(src);
                                }
                                else
                                {
                                    new_image = src;
                                }
                            }
                        }
                    }

                    // UPLOADING NEW IMAGE
                    for (int i = 0; i < Request.Form.Files.Count; i++)
                    {
                        var image = Request.Form.Files[i];
                        if (image != null && image.ContentType.Contains("image/"))
                        {
                            string fileName = Guid.NewGuid().ToString() + System.IO.Path.GetExtension(image.FileName);

                            GoogleDriveManager driveClient = new GoogleDriveManager(hosting);
                            new_image = driveClient.DriveUploadAndGetSrc(image, DRIVE_FOLDER_NAME);
                        }
                    }
                    news.Images = new_image;

                    newsManager.EditNews(news);
                    return(Redirect("/News/Details/" + news.Id));
                }
                catch
                {
                    return(View());
                }
            }
            return(View());
        }
示例#2
0
 public ActionResult Delete(int?id)
 {
     if (id != null)
     {
         Event e = eventsManager.FindEventById(id);
         if (e != null)
         {
             string[] images = e.Images.Split('|');
             foreach (string src in images)
             {
                 if (src.Contains("default"))    //don't delete default image
                 {
                     continue;
                 }
                 GoogleDriveManager driveClient = new GoogleDriveManager(hosting);
                 driveClient.DriveMoveFileToTrash(src);
             }
             eventsManager.RemoveEventById(id);
         }
     }
     return(RedirectToAction("Index"));
 }
示例#3
0
        public ActionResult Edit(Models.Event e)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    string new_image = Url.Content("/images/default-event-img.png");
                    // EDIT EXISTING IMAGES
                    string existingImages = managerForImages.FindEventById(e.Id).Images;
                    if (!String.IsNullOrEmpty(existingImages))
                    {
                        string[] images = existingImages.Split('|');
                        foreach (string field in Request.Form.Keys)
                        {
                            if (field.Contains("check_"))
                            {
                                var    checkbox = Request.Form[field];
                                string src      = field.Split('_')[1];
                                if (checkbox == "false" && !src.Contains("default"))
                                {
                                    GoogleDriveManager driveClient = new GoogleDriveManager(hosting);
                                    driveClient.DriveMoveFileToTrash(src);
                                }
                                else
                                {
                                    new_image = src;
                                }
                            }
                        }
                    }

                    // UPLOADING NEW IMAGES
                    for (int i = 0; i < Request.Form.Files.Count; i++)
                    {
                        var image = Request.Form.Files[i];
                        if (image != null && image.ContentType.Contains("image/"))
                        {
                            string fileName = Guid.NewGuid().ToString() + System.IO.Path.GetExtension(image.FileName);

                            GoogleDriveManager driveClient = new GoogleDriveManager(hosting);
                            new_image = driveClient.DriveUploadAndGetSrc(image, DRIVE_FOLDER_IMAGES);
                        }
                    }
                    e.Images = new_image;

                    // Зарузка и формирование файла
                    string file_name = Request.Form["file_name"];
                    string link      = Request.Form["link"];
                    var    file      = Request.Form.Files["download"];
                    if (file != null && file.Length > 0)
                    {
                        GoogleDriveManager driveClient = new GoogleDriveManager(hosting);
                        link = driveClient.DriveUploadAndGetSrc(file, DRIVE_FOLDER_FILES);
                    }
                    e.Files = file_name + "|" + link;

                    eventsManager.EditEvent(e);
                    return(Redirect("/Events/Details/" + e.Id));
                }
                catch
                {
                    return(View());
                }
            }
            return(View());
        }