Ejemplo n.º 1
0
        protected void Page_Load(object sender, EventArgs e)
        {
            var tagParam     = Request.QueryString["tag"];
            var userTagParam = Request.QueryString["user"];
            var Search       = Request.QueryString["search"];

            Login();

            GetMedia(); //CheckPermissions() is dependent on this for CreatedByUserId

            NewMenu.LoggedInUser = myUser;
            NewMenu.HighlightButtonsForPage(PageIndex.Tag, "Edit");

            MediaTag OriginalTag = string.IsNullOrEmpty(tagParam) ? null : new MediaTag(Convert.ToInt32(tagParam));

            GetOriginalRedirect();

            CheckPermissions();

            string id = Request.QueryString["id"];

            if (!string.IsNullOrEmpty(id))
            {
                //REQUIRED TO VIEW POSTBACK
                form1.Action = Request.RawUrl;

                if (Request.QueryString["delete"] != null)
                {
                    DoDeleteMedia();

                    Response.Redirect(OriginalAlbumRedirect);
                }

                CheckForThumbnailUpload();

                PopulateTags();

                PopulateTagDropDowns();

                DisplayMediaAttributes();

                if (!IsPostBack)
                {
                    InputTitleTwo.Focus();
                }
            }
            else
            {
                Response.Redirect("home");
            }

            void Login()
            {
                myUser = Account.SecurePage(this, Data.DeviceType);

                NewMenu.SelectedPage = PageIndex.EditMedia;
                NewMenu.LoggedInUser = myUser;
                NewMenu.Share        = true;

                if (Request.QueryString["removetag"] == "true")
                {
                    MediaTagPair.Delete(new MediaId(Request.QueryString["id"]), Convert.ToInt32(Request.QueryString["tag"]));
                    if (Request.QueryString["tag"] == null)
                    {
                        Response.Redirect($"edit?id={Request.QueryString["id"]}");
                    }
                    else
                    {
                        Response.Redirect($"edit?id={Request.QueryString["id"]}&tag={Request.QueryString["tag"]}");
                    }
                }
                else if (Request.QueryString["removeusertag"] == "true")
                {
                    MediaUserPair.Delete(new MediaId(Request.QueryString["id"]), Convert.ToInt32(Request.QueryString["userid"]));
                    if (Request.QueryString["userid"] == null)
                    {
                        Response.Redirect($"edit?id={Request.QueryString["id"]}");
                    }
                    else
                    {
                        Response.Redirect($"edit?id={Request.QueryString["id"]}&userid={Request.QueryString["userid"]}");
                    }
                }
            }

            void GetMedia()
            {
                if (!string.IsNullOrWhiteSpace(Request.QueryString["id"]))
                {
                    MyYoutubeVideo = ParsnipData.Media.Youtube.Select(new MediaId(Request.QueryString["id"]), myUser.Id);
                    if (MyYoutubeVideo == null)
                    {
                        MyVideo = ParsnipData.Media.Video.Select(new MediaId(Request.QueryString["id"]), myUser.Id);
                    }

                    if (MyYoutubeVideo == null && MyVideo == null)
                    {
                        MyImage = ParsnipData.Media.Image.Select(new MediaId(Request.QueryString["id"]), myUser.Id);
                    }
                }

                if (MyYoutubeVideo != null)
                {
                    MyYoutubeVideo = Youtube.Select(new MediaId(Request.QueryString["id"]), myUser.Id);
                    MediaShare myMediaShare = MyYoutubeVideo.MyMediaShare;
                    if (myMediaShare == null)
                    {
                        myMediaShare = new MediaShare(MyYoutubeVideo.Id, myUser.Id);
                        myMediaShare.Insert();
                    }
                    ShareLink.Value = Request.Url.GetLeftPart(UriPartial.Authority) + "/view?share=" +
                                      myMediaShare.Id;
                    thumbnail.Src = MyYoutubeVideo.Compressed;
                    input_date_media_captured.Value = MyYoutubeVideo.DateTimeCaptured.ToString();
                    a_play_video.HRef    = string.Format("../../view?id={0}", MyYoutubeVideo.Id);
                    a_play_video.Visible = true;
                    Page.Title           = "Edit Youtube Video";
                }
                else if (MyVideo != null)
                {
                    MyVideo = Video.Select(new MediaId(Request.QueryString["id"]), myUser.Id);
                    MediaShare myMediaShare = MyVideo.MyMediaShare;
                    if (myMediaShare == null)
                    {
                        myMediaShare = new MediaShare(MyVideo.Id, myUser.Id);
                        myMediaShare.Insert();
                    }
                    ShareLink.Value = Request.Url.GetLeftPart(UriPartial.Authority) + "/view?share=" +
                                      myMediaShare.Id;
                    thumbnail.Src = MyVideo.Compressed;
                    input_date_media_captured.Value = MyVideo.DateTimeCaptured.ToString();
                    a_play_video.HRef    = string.Format("../../view?id={0}", MyVideo.Id);
                    a_play_video.Visible = true;
                    Page.Title           = "Edit Video";
                    if (MyVideo.Thumbnails.Count() > 0)
                    {
                        ThumbnailSelectorContainer.Visible = true;
                        foreach (var control in VideoThumbnailControl.GetVideoAsVideoThumbnailControls(MyVideo))
                        {
                            control.VideoThumbnailClick += new EventHandler(VideoThumbnail_ButtonClick);
                            ThumbnailSelector.Controls.Add(control);
                        }
                    }
                }
                else if (MyImage != null)
                {
                    myMediaShare = MyImage.MyMediaShare;
                    if (myMediaShare == null)
                    {
                        myMediaShare = new MediaShare(MyImage.Id, myUser.Id);
                        myMediaShare.Insert();
                    }
                    ShareLink.Value = Request.Url.GetLeftPart(UriPartial.Authority) + "/view?share=" +
                                      myMediaShare.Id;
                    ImagePreview.ImageUrl           = MyImage.Compressed;
                    input_date_media_captured.Value = MyImage.DateTimeCaptured.ToString();
                    ImagePreviewContainer.HRef      = string.Format("../../view?id={0}", MyImage.Id);
                    ImagePreviewContainer.Visible   = true;
                    Page.Title = "Edit Image";
                }
                else
                {
                    Response.Redirect("myuploads");
                }
            }

            void PopulateTags()
            {
                Page httpHandler = (Page)HttpContext.Current.Handler;

                foreach (MediaTagPair mediaTagPair in MyMedia.MediaTagPairs)
                {
                    MediaTagPairControl mediaTagPairControl = (MediaTagPairControl)httpHandler.LoadControl("~/Custom_Controls/Media/MediaTagPairControl.ascx");
                    mediaTagPairControl.MyMedia = MyMedia;
                    mediaTagPairControl.MyPair  = mediaTagPair;
                    MediaTagContainer.Controls.Add(mediaTagPairControl);
                }
                foreach (MediaUserPair mediaUserPair in MyMedia.MediaUserPairs)
                {
                    MediaUserPairControl mediaUserPairControl = (MediaUserPairControl)httpHandler.LoadControl("~/Custom_Controls/Media/MediaUserPairControl.ascx");
                    mediaUserPairControl.MyMedia = MyMedia;
                    mediaUserPairControl.MyPair  = mediaUserPair;
                    UserTagContainer.Controls.Add(mediaUserPairControl);
                }
            }

            void GetOriginalRedirect()
            {
                if (OriginalTag == null && tagParam == null && userTagParam == null && MyMedia.AlbumId == default && MyMedia.MediaTagPairs.Count == default && !string.IsNullOrEmpty(OriginalAlbumRedirect))
                {
                    OriginalAlbumRedirect = "myuploads?focus=" + MyMedia.Id.ToString();
                }
                else if (!string.IsNullOrEmpty(Search))
                {
                    OriginalAlbumRedirect = $"search?text={Search}&focus={MyMedia.Id}";
                }
                else if (OriginalTag != null)
                {
                    switch (OriginalTag.Id)
                    {
                    case (int)Data.MediaTagIds.Photos:
                        OriginalAlbumRedirect = "photos?focus=" + MyMedia.Id.ToString();
                        break;

                    case (int)Data.MediaTagIds.Memes:
                        OriginalAlbumRedirect = "memes?focus=" + MyMedia.Id.ToString();
                        break;

                    case (int)Data.MediaTagIds.Krakow:
                        OriginalAlbumRedirect = "krakow?focus=" + MyMedia.Id.ToString();
                        break;

                    case (int)Data.MediaTagIds.Videos:
                        OriginalAlbumRedirect = "videos?focus=" + MyMedia.Id.ToString();
                        break;

                    case (int)Data.MediaTagIds.Portugal:
                        OriginalAlbumRedirect = "portugal?focus=" + MyMedia.Id.ToString();
                        break;

                    case (int)Data.MediaTagIds.Amsterdam:
                        OriginalAlbumRedirect = "amsterdam?focus=" + MyMedia.Id.ToString();
                        break;

                    case default(int):
                        Debug.WriteLine(string.Format("The album id {0} was not recognised!",
                                                      MyMedia.AlbumId));
                        OriginalAlbumRedirect = "home?error=nomediaalbum4";
                        break;

                    default:
                        OriginalAlbumRedirect = $"tag?id={OriginalTag.Id}&focus={MyMedia.Id}";
                        break;
                    }
                }
                else
                {
                    if (userTagParam != null)
                    {
                        OriginalAlbumRedirect = $"tag?user={userTagParam}&focus={MyMedia.Id}";
                    }
                    else if (tagParam != null)
                    {
                        OriginalAlbumRedirect = $"tag?id={tagParam}&focus={MyMedia.Id}";
                    }
                    else
                    {
                        OriginalAlbumRedirect = $"myuploads?focus={MyMedia.Id}";
                    }
                    //else if (MyMedia.MediaTagPairs.Count != default)
                    //    OriginalAlbumRedirect = $"tag?id={MyMedia.MediaTagPairs[0].MediaTag.Id}&focus={MyMedia.Id}";
                }
            }

            void PopulateTagDropDowns()
            {
                NewAlbumsDropDown.Items.Clear();
                NewAlbumsDropDown.Items.Add(new ListItem()
                {
                    Value = "0", Text = "(No tag selected)"
                });
                foreach (MediaTag tempMediaTag in MediaTag.GetAllTags())
                {
                    NewAlbumsDropDown.Items.Add(new ListItem()
                    {
                        Value = Convert.ToString(tempMediaTag.Id),
                        Text  = tempMediaTag.Name
                    });
                }

                DropDown_SelectUser.Items.Clear();
                foreach (User user in ParsnipData.Accounts.User.GetAllUsers())
                {
                    DropDown_SelectUser.Items.Add(new ListItem()
                    {
                        Value = Convert.ToString(user.Id),
                        Text  = user.FullName
                    });
                }
            }

            void DoDeleteMedia()
            {
                bool deleteSuccess;

                if (myUser.AccountType == "admin")
                {
                    MyMedia.Delete();
                    deleteSuccess = true;
                }
                else
                {
                    new LogEntry(Log.General)
                    {
                        Text = string.Format("{0} tried to delete media called \"{1}\", but {2} was not allowed " +
                                             "because {2} is not an admin", myUser.FullName, MyMedia.Title,
                                             myUser.SubjectiveGenderPronoun)
                    };
                    deleteSuccess = false;
                }
            }

            void CheckPermissions()
            {
                if (MyMedia.CreatedById.ToString() != myUser.Id.ToString())
                {
                    if (myUser.AccountType == "admin" || myUser.AccountType == "media")
                    {
                        string accountType = myUser.AccountType == "admin" ? "admin" : "approved media editor";
                        new LogEntry(Log.General)
                        {
                            Text = string.Format("{0} started editing media called \"{1}\". {2} does not own the " +
                                                 "media but {3} is allowed since {3} is an {4}", myUser.FullName, MyMedia.Title,
                                                 myUser.SubjectiveGenderPronoun.First().ToString().ToUpper() +
                                                 myUser.SubjectiveGenderPronoun.Substring(1), myUser.SubjectiveGenderPronoun, accountType)
                        };
                    }
                    else
                    {
                        new LogEntry(Log.General)
                        {
                            Text = string.Format("{0} attempted to edit media called \"{1}\" which {2} " +
                                                 "did not own. Access was DENIED!", myUser.FullName, MyMedia.Title, myUser.SubjectiveGenderPronoun)
                        };

                        Response.Redirect($"{OriginalAlbumRedirect}&alert=P100");
                    }
                }
            }

            void DisplayMediaAttributes()
            {
                if (MyMedia.Title != null && !string.IsNullOrEmpty(MyMedia.Title) && !string.IsNullOrWhiteSpace(MyMedia.Title))
                {
                    InputTitleTwo.Text = MyMedia.Title;
                }

                if (myUser.AccountType == "admin")
                {
                    btn_AdminDelete.Visible = true;
                }

                if (MyMedia.SearchTerms != null && !string.IsNullOrEmpty(MyMedia.SearchTerms) && !string.IsNullOrWhiteSpace(MyMedia.SearchTerms))
                {
                    SearchTerms_Input.Text = MyMedia.SearchTerms;
                }
            }

            void CheckForThumbnailUpload()
            {
                if (MyVideo != null)
                {
                    if (ThumbnailUpload.PostedFile != null && ThumbnailUpload.PostedFile.ContentLength > 0)
                    {
                        var thumbnail = new VideoThumbnail(MyVideo, myUser, ThumbnailUpload.PostedFile);
                        thumbnail.Insert();
                        Response.Redirect(Page.Request.Url.ToString(), true);
                    }

                    if (MyVideo.Thumbnails.Count > 0 || !MyVideo.Status.Equals(MediaStatus.Complete))
                    {
                        ThumbnailUploadControl.Visible = true;

                        if (MyVideo.Thumbnails.Count == 0)
                        {
                            ThumbnailsAreProcessing.Visible = true;
                        }
                    }
                }
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            //REQUIRED TO VIEW POSTBACK
            form1.Action = Request.RawUrl;

            if (Request.QueryString["id"] == null)
            {
                myUser = Account.SecurePage("edit_media", this, Data.DeviceType);
            }
            else if (Request.QueryString["tag"] == null)
            {
                myUser = Account.SecurePage($"edit_media?id={Request.QueryString["id"]}", this, Data.DeviceType);
            }
            else
            {
                myUser = Account.SecurePage($"edit_media?id={Request.QueryString["id"]}&tag={Request.QueryString["tag"]}", this, Data.DeviceType);
            }

            if (Request.QueryString["removetag"] == "true")
            {
                MediaTagPair.Delete(new MediaId(Request.QueryString["id"]), Convert.ToInt32(Request.QueryString["tag"]));
                if (Request.QueryString["tag"] == null)
                {
                    Response.Redirect($"edit_media?id={Request.QueryString["id"]}");
                }
                else
                {
                    Response.Redirect($"edit_media?id={Request.QueryString["id"]}&tag={Request.QueryString["tag"]}");
                }
            }
            else if (Request.QueryString["removeusertag"] == "true")
            {
                MediaUserPair.Delete(new MediaId(Request.QueryString["id"]), Convert.ToInt32(Request.QueryString["userid"]));
                if (Request.QueryString["userid"] == null)
                {
                    Response.Redirect($"edit_media?id={Request.QueryString["id"]}");
                }
                else
                {
                    Response.Redirect($"edit_media?id={Request.QueryString["id"]}&userid={Request.QueryString["userid"]}");
                }
            }

            if (!IsPostBack)
            {
                InputTitleTwo.Focus();
            }

            if (Request.QueryString["id"] != null)
            {
                string id = Request.QueryString["id"];

                MyYoutubeVideo = ParsnipData.Media.Youtube.Select(new MediaId(Request.QueryString["id"]), myUser.Id);
                if (MyYoutubeVideo == null)
                {
                    MyVideo = ParsnipData.Media.Video.Select(new MediaId(Request.QueryString["id"]), myUser.Id);
                }

                if (MyYoutubeVideo == null && MyVideo == null)
                {
                    MyImage = ParsnipData.Media.Image.Select(new MediaId(Request.QueryString["id"]), myUser.Id);
                }

                if (MyYoutubeVideo != null)
                {
                    MyYoutubeVideo = Youtube.Select(new MediaId(Request.QueryString["id"]), myUser.Id);
                    MediaShare myMediaShare = MyYoutubeVideo.MyMediaShare;
                    if (myMediaShare == null)
                    {
                        myMediaShare = new MediaShare(MyYoutubeVideo.Id, myUser.Id);
                        myMediaShare.Insert();
                    }
                    ShareLink.Value = Request.Url.GetLeftPart(UriPartial.Authority) + "/view?share=" +
                                      myMediaShare.Id;
                    input_date_media_captured.Value = MyYoutubeVideo.DateTimeCaptured.ToString();
                    youtube_video.Attributes.Add("data-id", MyYoutubeVideo.DataId);
                    youtube_video_container.Visible = true;
                    Page.Title = "Edit Youtube Video";
                }
                else if (MyVideo != null)
                {
                    MyVideo = Video.Select(new MediaId(Request.QueryString["id"]), myUser.Id);
                    MediaShare myMediaShare = MyVideo.MyMediaShare;
                    if (myMediaShare == null)
                    {
                        myMediaShare = new MediaShare(MyVideo.Id, myUser.Id);
                        myMediaShare.Insert();
                    }
                    ShareLink.Value = Request.Url.GetLeftPart(UriPartial.Authority) + "/view?share=" +
                                      myMediaShare.Id;
                    thumbnail.Src = MyVideo.Compressed;
                    input_date_media_captured.Value = MyVideo.DateTimeCaptured.ToString();
                    a_play_video.HRef    = string.Format("../../view?id={0}", MyVideo.Id);
                    a_play_video.Visible = true;
                    Page.Title           = "Edit Video";
                }
                else if (MyImage != null)
                {
                    myMediaShare = MyImage.MyMediaShare;
                    if (myMediaShare == null)
                    {
                        myMediaShare = new MediaShare(MyImage.Id, myUser.Id);
                        myMediaShare.Insert();
                    }
                    ShareLink.Value = Request.Url.GetLeftPart(UriPartial.Authority) + "/view?share=" +
                                      myMediaShare.Id;
                    ImagePreview.ImageUrl           = MyImage.Compressed;
                    input_date_media_captured.Value = MyImage.DateTimeCaptured.ToString();
                    ImagePreview.Visible            = true;
                    Page.Title = "Edit Image";
                }
                else
                {
                    Response.Redirect("home");
                }

                Page httpHandler = (Page)HttpContext.Current.Handler;
                foreach (MediaTagPair mediaTagPair in MyMedia.MediaTagPairs)
                {
                    MediaTagPairControl mediaTagPairControl = (MediaTagPairControl)httpHandler.LoadControl("~/Custom_Controls/Media/MediaTagPairControl.ascx");
                    mediaTagPairControl.MyMedia = MyMedia;
                    mediaTagPairControl.MyPair  = mediaTagPair;
                    MediaTagContainer.Controls.Add(mediaTagPairControl);
                }

                var      tagParam     = Request.QueryString["tag"];
                var      userTagParam = Request.QueryString["user"];
                MediaTag OriginalTag  = string.IsNullOrEmpty(tagParam) ? null : new MediaTag(Convert.ToInt32(tagParam));

                if (OriginalTag == null && userTagParam == null)
                {
                    OriginalAlbumRedirect = "home?focus=" + MyMedia.Id.ToString();
                }
                else if (userTagParam == null)
                {
                    switch (OriginalTag.Id)
                    {
                    case default(int):
                        Debug.WriteLine(string.Format("The album id {0} was not recognised!",
                                                      MyMedia.AlbumId));
                        OriginalAlbumRedirect = "home?error=nomediaalbum4";
                        break;

                    default:
                        OriginalAlbumRedirect = $"tag?id={OriginalTag.Id}&focus={MyMedia.Id}";
                        break;
                    }
                }
                else
                {
                    OriginalAlbumRedirect = $"tag?user={userTagParam}&focus={MyMedia.Id}";
                }
                NewAlbumsDropDown.Items.Clear();
                if (myUser.AccountType == "admin")
                {
                    NewAlbumsDropDown.Items.Add(new ListItem()
                    {
                        Value = "0", Text = "(No tag selected)"
                    });
                }
                foreach (MediaTag tempMediaTag in MediaTag.GetAllTags())
                {
                    NewAlbumsDropDown.Items.Add(new ListItem()
                    {
                        Value = Convert.ToString(tempMediaTag.Id),
                        Text  = tempMediaTag.Name
                    });
                }

                var AlbumIds = MyMedia.SelectMediaTagIds();

                if (Request.QueryString["delete"] != null)
                {
                    bool deleteSuccess;

                    if (myUser.AccountType == "admin")
                    {
                        MyMedia.Delete();
                        deleteSuccess = true;
                    }
                    else
                    {
                        new LogEntry(Log.General)
                        {
                            text = string.Format("{0} tried to delete media called \"{1}\", but {2} was not allowed " +
                                                 "because {2} is not an admin", myUser.FullName, MyMedia.Title,
                                                 myUser.SubjectiveGenderPronoun)
                        };
                        deleteSuccess = false;
                    }

                    string Redirect;

                    switch (Convert.ToInt16(NewAlbumsDropDown.SelectedValue))
                    {
                    case default(int):
                        Debug.WriteLine("No album selected. Must be none! Redirecting to manage photos...");
                        Redirect = "home";
                        break;

                    default:
                        Redirect = $"tag?id={OriginalTag.Id}&focus={MyMedia.Id}";
                        break;
                    }
                    if (deleteSuccess)
                    {
                        new LogEntry(Log.General)
                        {
                            text = string.Format("{0} deleted media called \"{1}\"",
                                                 myUser.FullName, MyMedia.Title)
                        };
                        Response.Redirect(Redirect);
                    }
                    else
                    {
                        if (Redirect.Contains("?"))
                        {
                            Response.Redirect(Redirect + "&error=access");
                        }
                        else
                        {
                            Response.Redirect(Redirect + "?error=access");
                        }
                    }
                }

                if (MyMedia.Title != null && !string.IsNullOrEmpty(MyMedia.Title) &&
                    !string.IsNullOrWhiteSpace(MyMedia.Title))
                {
                    Debug.WriteLine("Updating title from media object: " + MyMedia.Title);
                    InputTitleTwo.Text = MyMedia.Title;
                }

                if (myUser.AccountType == "admin")
                {
                    DateCapturedDiv.Visible = true;
                    btn_AdminDelete.Visible = true;
                }

                if (MyMedia.CreatedById.ToString() != myUser.Id.ToString())
                {
                    if (myUser.AccountType == "admin" || myUser.AccountType == "media")
                    {
                        string accountType = myUser.AccountType == "admin" ? "admin" : "approved media editor";
                        new LogEntry(Log.General)
                        {
                            text = string.Format("{0} started editing media called \"{1}\". {2} does not own the " +
                                                 "media but {3} is allowed since {3} is an {4}", myUser.FullName, MyMedia.Title,
                                                 myUser.SubjectiveGenderPronoun.First().ToString().ToUpper() +
                                                 myUser.SubjectiveGenderPronoun.Substring(1), myUser.SubjectiveGenderPronoun, accountType)
                        };
                    }
                    else
                    {
                        new LogEntry(Log.General)
                        {
                            text = string.Format("{0} attempted to edit media called \"{1}\" which {2} " +
                                                 "did not own. Access was DENIED!", myUser.FullName, MyMedia.Title, myUser.SubjectiveGenderPronoun)
                        };

                        Response.Redirect(OriginalAlbumRedirect + "&error=0");
                    }
                }
                Debug.WriteLine("Setting media directory to: " + MyMedia.Compressed);
            }
            else
            {
                Response.Redirect("home");
            }
        }