public static void DeleteAllFiles(SharedFileFolder folder, IFileSystem fileSystem, string fileVirtualBasePath, SharedFilesConfiguration config)
        {
            // method implemented by Jean-Michel 2008-07-31

            // TODO: implement check whether versioning is enabled before calling this method
            // if we are keeping versions we should not delete the files

            if (folder == null) { return; }
            if (fileSystem == null) { return; }
            if (string.IsNullOrEmpty(fileVirtualBasePath)) { return; }
            if (folder.FolderId == -1) { return; }

            ArrayList folders = new ArrayList();
            ArrayList files = new ArrayList();
            using (IDataReader reader = SharedFile.GetSharedFiles(folder.ModuleId, folder.FolderId))
            {
                while (reader.Read())
                {
                    files.Add(Convert.ToInt32(reader["ItemID"]));
                }
            }

            using (IDataReader reader = SharedFileFolder.GetSharedFolders(folder.ModuleId, folder.FolderId))
            {
                while (reader.Read())
                {
                    folders.Add(Convert.ToInt32(reader["FolderID"]));
                }
            }

            foreach (int id in files)
            {
                SharedFile sharedFile = new SharedFile(folder.ModuleId, id);
                sharedFile.Delete();

                if (!config.EnableVersioning)
                {
                    fileSystem.DeleteFile(VirtualPathUtility.Combine(fileVirtualBasePath, sharedFile.ServerFileName));

                }
            }

            foreach (int id in folders)
            {
                SharedFileFolder subFolder = new SharedFileFolder(folder.ModuleId, id);

                DeleteAllFiles(subFolder, fileSystem, fileVirtualBasePath, config);

                SharedFileFolder.DeleteSharedFileFolder(id);
            }
        }
Example #2
0
        public void ProcessRequest(HttpContext context)
        {
            base.Initialize(context);

            if (!UserCanEditModule(ModuleId, SharedFile.FeatureGuid))
            {
                log.Info("User has no edit permission so returning 404");
                Response.StatusCode = 404;
                return;
            }

            if (CurrentSite == null)
            {
                log.Info("CurrentSite is null so returning 404");
                Response.StatusCode = 404;
                return;
            }

            if (CurrentUser == null)
            {
                log.Info("CurrentUser is null so returning 404");
                Response.StatusCode = 404;
                return;
            }

            if (FileSystem == null)
            {
                log.Info("FileSystem is null so returning 404");
                Response.StatusCode = 404;
                return;
            }

            if (Request.Files.Count == 0)
            {
                log.Info("Posted File Count is zero so returning 404");
                Response.StatusCode = 404;
                return;
            }

            if (Request.Files.Count > SharedFilesConfiguration.MaxFilesToUploadAtOnce)
            {
                log.Info("Posted File Count is greater than allowed amount so returning 404");
                Response.StatusCode = 404;
                return;
            }

            module = GetModule(ModuleId, SharedFile.FeatureGuid);

            if (module == null)
            {
                log.Info("Module is null so returning 404");
                Response.StatusCode = 404;
                return;
            }

            itemId          = WebUtils.ParseInt32FromQueryString("ItemID", itemId);
            currentFolderId = WebUtils.ParseInt32FromQueryString("frmData", currentFolderId);


            virtualSourcePath  = "~/Data/Sites/" + CurrentSite.SiteId.ToInvariantString() + "/SharedFiles/";
            virtualHistoryPath = "~/Data/Sites/" + CurrentSite.SiteId.ToInvariantString() + "/SharedFiles/History/";

            Hashtable moduleSettings = ModuleSettings.GetModuleSettings(ModuleId);

            config = new SharedFilesConfiguration(moduleSettings);


            context.Response.ContentType = "text/plain";//"application/json";
            var r = new System.Collections.Generic.List <UploadFilesResult>();
            JavaScriptSerializer js = new JavaScriptSerializer();


            if (!FileSystem.FolderExists(virtualSourcePath))
            {
                FileSystem.CreateFolder(virtualSourcePath);
            }

            for (int f = 0; f < Request.Files.Count; f++)
            {
                HttpPostedFile file = Request.Files[f];

                string fileName = Path.GetFileName(file.FileName);

                SharedFile sharedFile;
                if ((itemId > -1) && (Request.Files.Count == 1))
                {
                    // updating an existing file
                    sharedFile = new SharedFile(ModuleId, itemId);

                    if (config.EnableVersioning)
                    {
                        bool historyCreated = SharedFilesHelper.CreateHistory(sharedFile, FileSystem, virtualSourcePath, virtualHistoryPath);
                        if (historyCreated)
                        {
                            sharedFile.ServerFileName = System.Guid.NewGuid().ToString() + ".config";
                        }
                    }
                }
                else
                {   // new file
                    sharedFile = new SharedFile();
                }
                sharedFile.ModuleId         = ModuleId;
                sharedFile.ModuleGuid       = module.ModuleGuid;
                sharedFile.OriginalFileName = fileName;
                sharedFile.FriendlyName     = fileName;
                sharedFile.SizeInKB         = (file.ContentLength / 1024);
                sharedFile.FolderId         = currentFolderId;
                if (currentFolderId > -1)
                {
                    SharedFileFolder folder = new SharedFileFolder(ModuleId, currentFolderId);
                    sharedFile.FolderGuid = folder.FolderGuid;
                }
                sharedFile.UploadUserId = CurrentUser.UserId;
                sharedFile.UserGuid     = CurrentUser.UserGuid;
                sharedFile.UploadDate   = DateTime.UtcNow;

                sharedFile.ContentChanged += new ContentChangedEventHandler(sharedFile_ContentChanged);


                //file.SaveAs(Server.MapPath("~/Files/" + fileName));
                if (sharedFile.Save())
                {
                    string destPath = VirtualPathUtility.Combine(virtualSourcePath, sharedFile.ServerFileName);

                    using (Stream s = file.InputStream)
                    {
                        FileSystem.SaveFile(destPath, s, IOHelper.GetMimeType(Path.GetExtension(sharedFile.FriendlyName).ToLower()), true);
                    }
                }


                r.Add(new UploadFilesResult()
                {
                    //Thumbnail_url = savedFileName,
                    Name   = fileName,
                    Length = file.ContentLength,
                    Type   = file.ContentType
                });
            }

            CurrentPage.UpdateLastModifiedTime();
            CacheHelper.ClearModuleCache(ModuleId);
            SiteUtils.QueueIndexing();

            var uploadedFiles = new
            {
                files = r.ToArray()
            };
            var jsonObj = js.Serialize(uploadedFiles);

            context.Response.Write(jsonObj.ToString());
        }
Example #3
0
        private void LoadSettings()
        {
            timeZone = SiteUtils.GetUserTimeZone();
            virtualSourcePath = "~/Data/Sites/" + siteSettings.SiteId.ToInvariantString() + "/SharedFiles/";
            virtualHistoryPath = "~/Data/Sites/" + siteSettings.SiteId.ToInvariantString() + "/SharedFiles/History/";

            lnkCancelFile.NavigateUrl = SiteUtils.GetCurrentPageUrl();
            lnkCancelFolder.NavigateUrl = lnkCancelFile.NavigateUrl;

            //if (BrowserHelper.IsIE())
            //{
            //    //this is a needed hack because IE 8 doesn't work correctly with window.open
            //    // a "security feature" of IE 8
            //    // unfortunately this is not valid xhtml to use target but it works in IE
            //    newWindowMarkup = " target='_blank' ";
            //}

            //this page handles both folders and files
            //expected strItem examples are 23~folder and 13~file
            //the number portion is the ItemID of the folder or file
            if (strItem.IndexOf("~") > -1)
            {
                try
                {
                    char[] separator = { '~' };
                    string[] args = strItem.Split(separator);
                    this.itemId = int.Parse(args[0]);
                    type = args[1];
                }
                catch { };

            }

            Hashtable moduleSettings = ModuleSettings.GetModuleSettings(moduleId);
            config = new SharedFilesConfiguration(moduleSettings);

            divHistory.Visible = config.EnableVersioning;

            uploader.UploadButtonClientId = btnUpload.ClientID;
            uploader.ServiceUrl = SiteRoot + "/SharedFiles/upload.ashx?pageid=" + pageId.ToInvariantString()
                + "&mid=" + moduleId.ToInvariantString()
                + "&ItemID=" + itemId.ToInvariantString();

            uploader.FormFieldClientId = hdnCurrentFolderId.ClientID;

            string refreshFunction = "function refresh" + moduleId.ToInvariantString()
                    + " () { window.location.reload(true); } ";

            uploader.UploadCompleteCallback = "refresh" + moduleId.ToInvariantString();

            ScriptManager.RegisterClientScriptBlock(
                this,
                this.GetType(), "refresh" + moduleId.ToInvariantString(),
                refreshFunction,
                true);

            AddClassToBody("sharedfilesedit");

            FileSystemProvider p = FileSystemManager.Providers[WebConfigSettings.FileSystemProvider];
            if (p == null) { return; }

            fileSystem = p.GetFileSystem();
        }
Example #4
0
        public void ProcessRequest(HttpContext context)
        {
            base.Initialize(context);

            if (!UserCanEditModule(ModuleId, SharedFile.FeatureGuid))
            {
                log.Info("User has no edit permission so returning 404");
                Response.StatusCode = 404;
                return;
            }

            if (CurrentSite == null)
            {
                log.Info("CurrentSite is null so returning 404");
                Response.StatusCode = 404;
                return;
            }

            if (CurrentUser == null)
            {
                log.Info("CurrentUser is null so returning 404");
                Response.StatusCode = 404;
                return;
            }

            if (FileSystem == null)
            {
                log.Info("FileSystem is null so returning 404");
                Response.StatusCode = 404;
                return;
            }

            if (Request.Files.Count == 0)
            {
                log.Info("Posted File Count is zero so returning 404");
                Response.StatusCode = 404;
                return;
            }

            if (Request.Files.Count > SharedFilesConfiguration.MaxFilesToUploadAtOnce)
            {
                log.Info("Posted File Count is greater than allowed amount so returning 404");
                Response.StatusCode = 404;
                return;
            }

            module = GetModule(ModuleId, SharedFile.FeatureGuid);

            if (module == null)
            {
                log.Info("Module is null so returning 404");
                Response.StatusCode = 404;
                return;
            }

            itemId = WebUtils.ParseInt32FromQueryString("ItemID", itemId);
            currentFolderId = WebUtils.ParseInt32FromQueryString("frmData", currentFolderId);

            virtualSourcePath = "~/Data/Sites/" + CurrentSite.SiteId.ToInvariantString() + "/SharedFiles/";
            virtualHistoryPath = "~/Data/Sites/" + CurrentSite.SiteId.ToInvariantString() + "/SharedFiles/History/";

            Hashtable moduleSettings = ModuleSettings.GetModuleSettings(ModuleId);
            config = new SharedFilesConfiguration(moduleSettings);

            context.Response.ContentType = "text/plain";//"application/json";
            var r = new System.Collections.Generic.List<UploadFilesResult>();
            JavaScriptSerializer js = new JavaScriptSerializer();

            if (!FileSystem.FolderExists(virtualSourcePath))
            {
                FileSystem.CreateFolder(virtualSourcePath);
            }

            for (int f = 0; f < Request.Files.Count; f++)
            {
                HttpPostedFile file = Request.Files[f];

                string fileName = Path.GetFileName(file.FileName);

                SharedFile sharedFile;
                if ((itemId > -1) && (Request.Files.Count == 1))
                {
                    // updating an existing file
                    sharedFile = new SharedFile(ModuleId, itemId);

                    if (config.EnableVersioning)
                    {
                        bool historyCreated = SharedFilesHelper.CreateHistory(sharedFile, FileSystem, virtualSourcePath, virtualHistoryPath);
                        if (historyCreated)
                        {
                            sharedFile.ServerFileName = System.Guid.NewGuid().ToString() + ".config";
                        }

                    }
                }
                else
                {   // new file
                    sharedFile = new SharedFile();
                }
                sharedFile.ModuleId = ModuleId;
                sharedFile.ModuleGuid = module.ModuleGuid;
                sharedFile.OriginalFileName = fileName;
                sharedFile.FriendlyName = fileName;
                sharedFile.SizeInKB = (file.ContentLength / 1024);
                sharedFile.FolderId = currentFolderId;
                if (currentFolderId > -1)
                {
                    SharedFileFolder folder = new SharedFileFolder(ModuleId, currentFolderId);
                    sharedFile.FolderGuid = folder.FolderGuid;
                }
                sharedFile.UploadUserId = CurrentUser.UserId;
                sharedFile.UserGuid = CurrentUser.UserGuid;
                sharedFile.UploadDate = DateTime.UtcNow;

                sharedFile.ContentChanged += new ContentChangedEventHandler(sharedFile_ContentChanged);

                //file.SaveAs(Server.MapPath("~/Files/" + fileName));
                if (sharedFile.Save())
                {
                    string destPath = VirtualPathUtility.Combine(virtualSourcePath, sharedFile.ServerFileName);

                    using (Stream s = file.InputStream)
                    {
                        FileSystem.SaveFile(destPath, s, IOHelper.GetMimeType(Path.GetExtension(sharedFile.FriendlyName).ToLower()), true);
                    }

                }

                r.Add(new UploadFilesResult()
                {
                    //Thumbnail_url = savedFileName,
                    Name = fileName,
                    Length = file.ContentLength,
                    Type = file.ContentType
                });

            }

            CurrentPage.UpdateLastModifiedTime();
            CacheHelper.ClearModuleCache(ModuleId);
            SiteUtils.QueueIndexing();

            var uploadedFiles = new
            {
                files = r.ToArray()
            };
            var jsonObj = js.Serialize(uploadedFiles);
            context.Response.Write(jsonObj.ToString());
        }
        private void LoadSettings()
        {
            config = new SharedFilesConfiguration(Settings);
            EditContentImage = WebConfigSettings.EditContentImage;
            lblError.Text = String.Empty;

            FileSystemProvider p = FileSystemManager.Providers[WebConfigSettings.FileSystemProvider];
            if (p == null) { return; }

            fileSystem = p.GetFileSystem();
            if (fileSystem == null) { return; }

            newWindowMarkup = displaySettings.NewWindowLinkMarkup;
            if (BrowserHelper.IsIE())
            {
                //this is a needed hack because IE 8 doesn't work correctly with window.open
                // a "security feature" of IE 8
                // unfortunately this is not valid xhtml to use target but it works in IE
                newWindowMarkup = displaySettings.IeNewWindowLinkMarkup;
            }

            if (!SharedFilesConfiguration.DownloadLinksOpenNewWindow)
            {
                newWindowMarkup = string.Empty;
            }

            TimeOffset = SiteUtils.GetUserTimeOffset();
            timeZone = SiteUtils.GetUserTimeZone();
            fileVirtualBasePath = "~/Data/Sites/" + siteSettings.SiteId.ToInvariantString() + "/SharedFiles/";

            try
            {
                // this keeps the action from changing during ajax postback in folder based sites
                SiteUtils.SetFormAction(Page, Request.RawUrl);
            }
            catch (MissingMethodException)
            {
                //this method was introduced in .NET 3.5 SP1
            }

            btnUpload2.Visible = IsEditable;
            uploader.Visible = IsEditable;
            uploader.MaxFilesAllowed = SharedFilesConfiguration.MaxFilesToUploadAtOnce;
            uploader.ServiceUrl = SiteRoot + "/SharedFiles/upload.ashx?pageid=" + PageId.ToInvariantString()
                + "&mid=" + ModuleId.ToInvariantString();
            uploader.FormFieldClientId = hdnCurrentFolderId.ClientID;
            uploader.UploadButtonClientId = btnUpload2.ClientID;

            if(IsEditable)
            {
                string refreshFunction = "function refresh" + ModuleId.ToInvariantString()
                    + " () { $('#" + btnRefresh.ClientID + "').click(); } ";

                uploader.UploadCompleteCallback = "refresh" + ModuleId.ToInvariantString();

                ScriptManager.RegisterClientScriptBlock(
                    this,
                    this.GetType(), "refresh" + ModuleId.ToInvariantString(),
                    refreshFunction,
                    true);

            }

            if ((dgFile.TableCssClass.Contains("jqtable")) && (!WebConfigSettings.DisablejQuery))
            {

                StringBuilder script = new StringBuilder();

                script.Append("function setupJTable" + ModuleId.ToInvariantString() + "() {");

                script.Append("$('#" + dgFile.ClientID + " th').each(function(){ ");

                script.Append("$(this).addClass('ui-state-default'); ");
                script.Append("}); ");
                script.Append("$('table.jqtable td').each(function(){ ");
                script.Append("$(this).addClass('ui-widget-content'); ");
                script.Append("}); ");
                script.Append("$('table.jqtable tr').hover( ");
                script.Append("function() {");
                script.Append("$(this).children('td').addClass('ui-state-hover'); ");
                script.Append("},function() {");
                script.Append("$(this).children('td').removeClass('ui-state-hover'); ");
                script.Append("} ");
                script.Append("); ");
                script.Append("$('table.jqtable tr').click(function() { ");
                script.Append("$(this).children('td').toggleClass('ui-state-highlight'); ");
                script.Append("}); ");
                script.Append("} "); // end function

                script.Append("Sys.WebForms.PageRequestManager.getInstance().add_endRequest(setupJTable" + ModuleId.ToInvariantString() + "); ");

                ScriptManager.RegisterStartupScript(
                        this,
                        this.GetType(), "jTable" + ModuleId.ToInvariantString(),
                        script.ToString(),
                        true);
            }

            trObjectCount.Visible = config.ShowObjectCount;

            if (config.InstanceCssClass.Length > 0) { pnlOuterWrap.SetOrAppendCss(config.InstanceCssClass); }

            if(WebConfigSettings.ForceLegacyFileUpload)
            {
                ScriptManager.GetCurrent(Page).RegisterPostBackControl(btnUpload2);
            }
        }