protected void changeAlbumNameBtn_Click(object sender, EventArgs e) { this.errorLabel.Visible = false; GigaGalleryWS ws = new GigaGalleryWS(); UserValidationWS validationWS = new UserValidationWS(); string newName = this.newAlbumName.Text.Trim(); if (Session["selectedAlbumId"] == null) { // No album was selected. this.errorLabel.Text = "No album was selected! Please select an album!"; this.errorLabel.Visible = true; return; } if (validationWS.isAlbumNameValid(newName)) { Album albmObj = ws.GetAlbumById((int)Session["selectedAlbumId"]); albmObj.AlbumName = newName; // Annoying but only works like this. bool res = false; try { res = ws.UpdateAlbum(albmObj.AlbumId, albmObj.AlbumOwnerId, albmObj.AlbumName, albmObj.AlbumCreationTime, albmObj.AlbumSize); } catch { this.errorLabel.Text = "Invalid Action!"; this.errorLabel.Visible = true; this.imagesGridView.DataBind(); this.imagesGridView.SelectedIndex = -1; return; } if (res) { this.updateDropDownParams(sender, e); this.imagesGridView.DataBind(); Session["selectedAlbumId"] = null; } else { this.errorLabel.Text = "Could not update album, try again later!"; this.errorLabel.Visible = true; } this.imagesGridView.SelectedIndex = -1; } else { this.errorLabel.Text = validationWS.albumNameLengthInvalidMessage(); this.errorLabel.Visible = true; this.imagesGridView.SelectedIndex = -1; this.imagesGridView.DataBind(); return; } }
protected void addImageBtn_Click(object sender, EventArgs e) { if (Session["selectedAlbumId"] == null) { // No album was selected. this.errorLabel.Text = "No album was selected! Please select an album!"; this.errorLabel.Visible = true; return; } GigaGalleryWS ws = new GigaGalleryWS(); Album selectedAlbum = ws.GetAlbumById((int)Session["selectedAlbumId"]); string beginPath = Request.PhysicalApplicationPath + "UserImages"; string userPath = string.Format("\\{0}\\{1}", CreateMD5(((User)Session["pUser"]).UserName), CreateMD5(selectedAlbum.AlbumName)); if (this.imageFileUpload.HasFile) { if (!System.IO.Directory.Exists(beginPath + userPath)) { // Need to create a directory for the album. System.IO.Directory.CreateDirectory(beginPath + userPath); } UserValidationWS validationWS = new UserValidationWS(); string newName = this.newImageNameTB.Text.Trim(); if (!validationWS.isImageNameValid(newName)) { this.errorLabel.Text = validationWS.imageNameLengthInvalidMessage(); this.errorLabel.Visible = true; return; } string fileExtention = System.IO.Path.GetExtension(this.imageFileUpload.FileName); if (fileExtention != ".png" && fileExtention != ".jpg" && fileExtention != ".jpeg") { this.errorLabel.Text = "File extention must be either of these types (.png, .jpg, .jpeg)!"; this.errorLabel.Visible = true; return; } if (this.imageFileUpload.PostedFile.ContentLength < 10000000) { if (doesUserImageExist(beginPath, ((User)Session["pUser"]).UserName, selectedAlbum.AlbumName, newName, fileExtention)) { this.errorLabel.Text = "Image with the same name alrady exists in this album!"; return; } else { // Successfully uploaded image. // Update sql database and save image. string fullPath = getHashedFilePath(beginPath, ((User)Session["pUser"]).UserName, selectedAlbum.AlbumName, newName, fileExtention); if (!ws.UpdateAlbum(selectedAlbum.AlbumId, selectedAlbum.AlbumOwnerId, selectedAlbum.AlbumName, selectedAlbum.AlbumCreationTime, selectedAlbum.AlbumSize + 1)) { // error! could not update album sql. return; } string imagePath = getHashedFilePath(".", ((User)Session["pUser"]).UserName, selectedAlbum.AlbumName, newName, fileExtention); bool res = false; try { res = ws.AddImage((int)Session["pUserId"], selectedAlbum.AlbumId, newName, imagePath); } catch { this.errorLabel.Text = "Invalid Action!"; this.errorLabel.Visible = true; this.imagesGridView.DataBind(); this.imagesGridView.SelectedIndex = -1; return; } if (!res) { // error! could not update image sql. return; } try { this.imageFileUpload.SaveAs(fullPath); } catch { this.errorLabel.Text = "Error!"; this.errorLabel.Visible = true; } this.updateDropDownParams(sender, e); this.imagesGridView.DataBind(); this.imagesGridView.SelectedIndex = -1; } } } else { this.errorLabel.Text = "Please make sure to select an image file!"; this.errorLabel.Visible = true; this.imagesGridView.SelectedIndex = -1; return; } }