protected void changeImageNameBtn_Click(object sender, EventArgs e) { this.errorLabel.Visible = false; GigaGalleryWS ws = new GigaGalleryWS(); if (Session["selectedAlbumId"] == null) { // No album was selected. this.errorLabel.Text = "No album was selected! Please select an album!"; this.errorLabel.Visible = true; return; } Img[] images = ws.GetAlbumImages((int)Session["selectedAlbumId"]); if (images == null) { this.errorLabel.Text = "Album is empty, please insert some images!"; this.errorLabel.Visible = true; return; } Img selectedImg = null; foreach (Img img in images) { if (img.ImageName == Context.Server.HtmlDecode(this.imagesGridView.SelectedRow.Cells[2].Text)) { selectedImg = img; } } if (selectedImg == null) { this.errorLabel.Text = "The selected image does not exist in this album!"; this.errorLabel.Visible = true; return; } else { UserValidationWS validationWS = new UserValidationWS(); string newName = this.changeImageNameTB.Text.Trim(); if (validationWS.isImageNameValid(newName)) { selectedImg.ImageName = newName; bool res = false; try { res = ws.UpdateImage(selectedImg); } catch { this.errorLabel.Text = "Invalid Action!"; this.errorLabel.Visible = true; this.imagesGridView.DataBind(); this.imagesGridView.SelectedIndex = -1; return; } if (res) { this.imagesGridView.DataBind(); this.imagesGridView.SelectedIndex = -1; this.updateDropDownParams(sender, e); } else { this.errorLabel.Text = "Could not update the image name, try again later!"; this.errorLabel.Visible = true; this.imagesGridView.SelectedIndex = -1; } } else { this.errorLabel.Text = validationWS.imageNameLengthInvalidMessage(); this.errorLabel.Visible = true; this.imagesGridView.SelectedIndex = -1; 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; } }