void btnUpload_Click(object sender, EventArgs e)
        {
            // as long as javascript is available this code should never execute
            // because the standard file input ir replaced by javascript and the file upload happens
            // at the service url /ImageGallery/upload.ashx
            // this is fallback implementation

            Module module = GetModule(moduleId, Gallery.FeatureGuid);

            if (module == null)
            {
                SiteUtils.RedirectToAccessDeniedPage(this);
                return;
            }

            SiteUser siteUser = SiteUtils.GetCurrentSiteUser();

            try
            {
                if (uploader.HasFile)
                {
                    string ext = Path.GetExtension(uploader.FileName);
                    if (SiteUtils.IsAllowedUploadBrowseFile(ext, ".jpg|.gif|.png|.jpeg"))
                    {
                        GalleryImage galleryImage = new GalleryImage(this.moduleId);
                        galleryImage.ModuleGuid      = module.ModuleGuid;
                        galleryImage.WebImageHeight  = config.WebSizeHeight;
                        galleryImage.WebImageWidth   = config.WebSizeWidth;
                        galleryImage.ThumbNailHeight = config.ThumbnailHeight;
                        galleryImage.ThumbNailWidth  = config.ThumbnailWidth;
                        galleryImage.UploadUser      = Context.User.Identity.Name;

                        if (siteUser != null)
                        {
                            galleryImage.UserGuid = siteUser.UserGuid;
                        }

                        //string newFileName = Path.GetFileName(file.FileName).ToCleanFileName(WebConfigSettings.ForceLowerCaseForUploadedFiles);
                        string newFileName  = Path.GetFileName(uploader.FileName).ToCleanFileName(WebConfigSettings.ForceLowerCaseForUploadedFiles);
                        string newImagePath = VirtualPathUtility.Combine(fullSizeImageFolderPath, newFileName);

                        if (galleryImage.ImageFile == newFileName)
                        {
                            // an existing gallery image delete the old one
                            fileSystem.DeleteFile(newImagePath);
                        }
                        else
                        {
                            // this is a new galleryImage instance, make sure we don't use the same file name as any other instance
                            int i = 1;
                            while (fileSystem.FileExists(VirtualPathUtility.Combine(fullSizeImageFolderPath, newFileName)))
                            {
                                newFileName = i.ToInvariantString() + newFileName;
                                i          += 1;
                            }
                        }

                        newImagePath = VirtualPathUtility.Combine(fullSizeImageFolderPath, newFileName);


                        using (Stream s = uploader.FileContent)
                        {
                            //fileSystem.SaveFile(newImagePath, s, uploader.FileContentType, true);
                            fileSystem.SaveFile(newImagePath, s, IOHelper.GetMimeType(Path.GetExtension(ext).ToLower()), true);
                        }


                        galleryImage.ImageFile     = newFileName;
                        galleryImage.WebImageFile  = newFileName;
                        galleryImage.ThumbnailFile = newFileName;
                        galleryImage.Save();
                        GalleryHelper.ProcessImage(galleryImage, fileSystem, imageFolderPath, uploader.FileName, config.ResizeBackgroundColor);
                    }
                }

                WebUtils.SetupRedirect(this, SiteUtils.GetCurrentPageUrl());
            }
            catch (UnauthorizedAccessException ex)
            {
                lblError.Text = ex.Message;
            }
            catch (ArgumentException ex)
            {
                lblError.Text = ex.Message;
            }
        }
        void btnUpload_Click(object sender, EventArgs e)
        {
            Module   module   = new Module(moduleId);
            SiteUser siteUser = SiteUtils.GetCurrentSiteUser();

            try
            {
                if (multiFile.Files.Length > 0)
                {
                    foreach (UploadedFile file in multiFile.Files)
                    {
                        if (file != null && file.FileName != null && file.FileName.Trim().Length > 0)
                        {
                            string ext = Path.GetExtension(file.FileName);
                            if (SiteUtils.IsAllowedUploadBrowseFile(ext, ".jpg|.gif|.png|.jpeg"))
                            {
                                GalleryImage galleryImage = new GalleryImage(this.moduleId, this.imageFolderPath);
                                galleryImage.ModuleGuid = module.ModuleGuid;

                                if (webImageHeightSetting > -1)
                                {
                                    galleryImage.WebImageHeight = webImageHeightSetting;
                                }

                                if (webImageWidthSetting > -1)
                                {
                                    galleryImage.WebImageWidth = webImageWidthSetting;
                                }

                                if (thumbNailHeightSetting > -1)
                                {
                                    galleryImage.ThumbNailHeight = thumbNailHeightSetting;
                                }

                                if (thumbNailWidthSetting > -1)
                                {
                                    galleryImage.ThumbNailWidth = thumbNailWidthSetting;
                                }


                                galleryImage.UploadUser = Context.User.Identity.Name;

                                if (siteUser != null)
                                {
                                    galleryImage.UserGuid = siteUser.UserGuid;
                                }

                                //string destPath = this.fullSizeImageFolderPath + galleryImage.ImageFile;
                                //if (File.Exists(destPath))
                                //{
                                //    File.Delete(destPath);
                                //}
                                //file.MoveTo(destPath, MoveToOptions.Overwrite);

                                // 2010-02-12 change from previous implementation with ugly guid file names
                                string newFileName = Path.GetFileName(file.FileName).ToCleanFileName(WebConfigSettings.ForceLowerCaseForUploadedFiles);

                                if (galleryImage.ImageFile == newFileName)
                                {
                                    // an existing gallery image delete the old one
                                    if (File.Exists(fullSizeImageFolderPath + newFileName))
                                    {
                                        File.Delete(fullSizeImageFolderPath + newFileName);
                                    }
                                }
                                else
                                {
                                    // this is a new galleryImage instance, make sure we don't use the same file name as any other instance
                                    int i = 1;
                                    while (File.Exists(fullSizeImageFolderPath + newFileName))
                                    {
                                        newFileName = i.ToInvariantString() + newFileName;
                                        i          += 1;
                                    }
                                }

                                string destPath = fullSizeImageFolderPath + newFileName;
                                file.MoveTo(destPath, MoveToOptions.Overwrite);

                                galleryImage.ImageFile     = newFileName;
                                galleryImage.WebImageFile  = newFileName;
                                galleryImage.ThumbnailFile = newFileName;

                                GalleryHelper.ProcessImage(galleryImage, file.FileName);
                            }
                        }
                    }
                }

                WebUtils.SetupRedirect(this, SiteUtils.GetCurrentPageUrl());
            }
            catch (UnauthorizedAccessException ex)
            {
                lblError.Text = ex.Message;
            }
            catch (ArgumentException ex)
            {
                lblError.Text = ex.Message;
            }
        }
Beispiel #3
0
        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (!Page.IsValid)
            {
                return;
            }

            GalleryImage galleryImage;

            if (moduleId > -1)
            {
                if (itemId > -1)
                {
                    galleryImage = new GalleryImage(moduleId, itemId);
                }
                else
                {
                    galleryImage = new GalleryImage(moduleId);
                }

                if (galleryImage.ModuleId != moduleId)
                {
                    SiteUtils.RedirectToAccessDeniedPage(this);
                    return;
                }

                Module module = GetModule(moduleId, Gallery.FeatureGuid);
                galleryImage.ModuleGuid = module.ModuleGuid;

                galleryImage.ContentChanged += new ContentChangedEventHandler(galleryImage_ContentChanged);

                int displayOrder;
                if (!Int32.TryParse(txtDisplayOrder.Text, out displayOrder))
                {
                    displayOrder = -1;
                }

                if (displayOrder > -1)
                {
                    galleryImage.DisplayOrder = displayOrder;
                }

                galleryImage.WebImageHeight  = config.WebSizeHeight;
                galleryImage.WebImageWidth   = config.WebSizeWidth;
                galleryImage.ThumbNailHeight = config.ThumbnailHeight;
                galleryImage.ThumbNailWidth  = config.ThumbnailWidth;
                galleryImage.Description     = edDescription.Text;
                galleryImage.Caption         = txtCaption.Text;
                galleryImage.UploadUser      = Context.User.Identity.Name;
                SiteUser siteUser = SiteUtils.GetCurrentSiteUser();
                if (siteUser != null)
                {
                    galleryImage.UserGuid = siteUser.UserGuid;
                }

                // as long as javascript is available this code should never execute
                // because the standard file input ir replaced by javascript and the file upload happens
                // at the service url /ImageGallery/upload.ashx
                // this is fallback implementation

                if (uploader.HasFile)
                {
                    string ext = Path.GetExtension(uploader.FileName);
                    if (!SiteUtils.IsAllowedUploadBrowseFile(ext, ".jpg|.gif|.png|.jpeg"))
                    {
                        lblMessage.Text = GalleryResources.InvalidFile;

                        return;
                    }

                    string newFileName  = Path.GetFileName(uploader.FileName).ToCleanFileName(WebConfigSettings.ForceLowerCaseForUploadedFiles);
                    string newImagePath = VirtualPathUtility.Combine(fullSizeImageFolderPath, newFileName);
                    if (galleryImage.ImageFile == newFileName)
                    {
                        // an existing gallery image delete the old one
                        fileSystem.DeleteFile(newImagePath);
                    }
                    else
                    {
                        // this is a new galleryImage instance, make sure we don't use the same file name as any other instance
                        int i = 1;
                        while (fileSystem.FileExists(VirtualPathUtility.Combine(fullSizeImageFolderPath, newFileName)))
                        {
                            newFileName = i.ToInvariantString() + newFileName;
                            i          += 1;
                        }
                    }
                    newImagePath = VirtualPathUtility.Combine(fullSizeImageFolderPath, newFileName);

                    if (galleryImage.ItemId > -1)
                    {
                        //updating with a new image so delete the previous version
                        GalleryHelper.DeleteImages(galleryImage, fileSystem, imageFolderPath);
                    }


                    //using (Stream s = flImage.FileContent)
                    //{
                    //    fileSystem.SaveFile(newImagePath, s, flImage.ContentType, true);
                    //}
                    using (Stream s = uploader.FileContent)
                    {
                        fileSystem.SaveFile(newImagePath, s, IOHelper.GetMimeType(Path.GetExtension(ext).ToLower()), true);
                    }



                    galleryImage.ImageFile     = newFileName;
                    galleryImage.WebImageFile  = newFileName;
                    galleryImage.ThumbnailFile = newFileName;
                    galleryImage.Save();
                    GalleryHelper.ProcessImage(galleryImage, fileSystem, imageFolderPath, uploader.FileName, config.ResizeBackgroundColor);

                    CurrentPage.UpdateLastModifiedTime();
                    CacheHelper.ClearModuleCache(moduleId);

                    SiteUtils.QueueIndexing();
                    if (hdnReturnUrl.Value.Length > 0)
                    {
                        WebUtils.SetupRedirect(this, hdnReturnUrl.Value);
                        return;
                    }
                }
                else // not hasfile
                {       //updating a previously uploaded image
                    if (itemId > -1)
                    {
                        if (galleryImage.Save())
                        {
                            CurrentPage.UpdateLastModifiedTime();
                            CacheHelper.ClearModuleCache(moduleId);
                            SiteUtils.QueueIndexing();
                            if (newItem)
                            {
                                string thisUrl = SiteRoot + "/ImageGallery/EditImage.aspx?pageid="
                                                 + pageId.ToInvariantString()
                                                 + "&mid=" + moduleId.ToInvariantString()
                                                 + "&ItemID=" + galleryImage.ItemId.ToInvariantString();

                                WebUtils.SetupRedirect(this, thisUrl);
                                return;
                            }
                            else
                            {
                                if (hdnReturnUrl.Value.Length > 0)
                                {
                                    WebUtils.SetupRedirect(this, hdnReturnUrl.Value);
                                    return;
                                }

                                WebUtils.SetupRedirect(this, SiteUtils.GetCurrentPageUrl());
                            }
                        }
                    }
                }
            }
        }
        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (log.IsDebugEnabled)
            {
                log.Debug("in btnUpdate_Click");
            }

            if (!Page.IsValid)
            {
                return;
            }
            if (log.IsDebugEnabled)
            {
                log.Debug("Page.IsValid");
            }

            GalleryImage galleryImage;

            if (moduleId > -1)
            {
                if (itemId > -1)
                {
                    galleryImage = new GalleryImage(moduleId, itemId, imageFolderPath);
                }
                else
                {
                    galleryImage = new GalleryImage(moduleId, imageFolderPath);
                }

                Module module = new Module(moduleId);
                galleryImage.ModuleGuid = module.ModuleGuid;

                galleryImage.ContentChanged += new ContentChangedEventHandler(galleryImage_ContentChanged);

                int displayOrder;
                if (!Int32.TryParse(txtDisplayOrder.Text, out displayOrder))
                {
                    displayOrder = -1;
                }

                if (displayOrder > -1)
                {
                    galleryImage.DisplayOrder = displayOrder;
                }

                if (webImageHeightSetting > -1)
                {
                    galleryImage.WebImageHeight = webImageHeightSetting;
                }

                if (webImageWidthSetting > -1)
                {
                    galleryImage.WebImageWidth = webImageWidthSetting;
                }

                if (thumbNailHeightSetting > -1)
                {
                    galleryImage.ThumbNailHeight = thumbNailHeightSetting;
                }

                if (thumbNailWidthSetting > -1)
                {
                    galleryImage.ThumbNailWidth = thumbNailWidthSetting;
                }

                galleryImage.Description = edDescription.Text;
                galleryImage.Caption     = txtCaption.Text;
                galleryImage.UploadUser  = Context.User.Identity.Name;
                SiteUser siteUser = SiteUtils.GetCurrentSiteUser();
                if (siteUser != null)
                {
                    galleryImage.UserGuid = siteUser.UserGuid;
                }

                if (flImage.HasFile && flImage.FileName != null && flImage.FileName.Trim().Length > 0)
                {
                    string ext = Path.GetExtension(flImage.FileName);
                    if (!SiteUtils.IsAllowedUploadBrowseFile(ext, ".jpg|.gif|.png|.jpeg"))
                    {
                        lblMessage.Text = GalleryResources.InvalidFile;

                        return;
                    }
                    // 2010-02-12 change from previous implementation with ugly guid file names
                    string newFileName = Path.GetFileName(flImage.FileName).ToCleanFileName(WebConfigSettings.ForceLowerCaseForUploadedFiles);

                    if (galleryImage.ImageFile == newFileName)
                    {
                        // an existing gallery image delete the old one
                        if (File.Exists(fullSizeImageFolderPath + newFileName))
                        {
                            File.Delete(fullSizeImageFolderPath + newFileName);
                        }
                    }
                    else
                    {
                        // this is a new galleryImage instance, make sure we don't use the same file name as any other instance
                        int i = 1;
                        while (File.Exists(fullSizeImageFolderPath + newFileName))
                        {
                            newFileName = i.ToInvariantString() + newFileName;
                            i          += 1;
                        }
                    }

                    string destPath = fullSizeImageFolderPath + newFileName;
                    flImage.MoveTo(destPath, MoveToOptions.Overwrite);

                    galleryImage.ImageFile     = newFileName;
                    galleryImage.WebImageFile  = newFileName;
                    galleryImage.ThumbnailFile = newFileName;

                    //galleryImage.ProcessImage(flImage.FileName);
                    GalleryHelper.ProcessImage(galleryImage, flImage.FileName);

                    CurrentPage.UpdateLastModifiedTime();
                    CacheHelper.TouchCacheDependencyFile(cacheDependencyKey);
                    SiteUtils.QueueIndexing();
                    if (ViewState["UrlReferrer"] != null)
                    {
                        WebUtils.SetupRedirect(this, (string)ViewState["UrlReferrer"]);
                        return;
                    }
                }
                else
                {               //updating a previously uploaded image
                    if (itemId > -1)
                    {
                        if (galleryImage.Save())
                        {
                            CurrentPage.UpdateLastModifiedTime();
                            CacheHelper.TouchCacheDependencyFile(cacheDependencyKey);
                            SiteUtils.QueueIndexing();
                            if (hdnReturnUrl.Value.Length > 0)
                            {
                                WebUtils.SetupRedirect(this, hdnReturnUrl.Value);
                                return;
                            }

                            WebUtils.SetupRedirect(this, SiteUtils.GetCurrentPageUrl());
                        }
                    }
                }
            }
        }