public static string GetFileList(string filePath, int folderId, int UserID, int IsSort, int CurrentPage, int PageSize, int portalID, string userName, int userModuleID, string secureToken)
    {
        StringBuilder       sb         = new StringBuilder();
        AuthenticateService objService = new AuthenticateService();

        if (objService.IsPostAuthenticatedView(portalID, userModuleID, userName, secureToken))
        {
            System.IO.DirectoryInfo di       = new System.IO.DirectoryInfo(filePath);
            List <ATTFile>          lstFiles = new List <ATTFile>();
            if (filePath == "/")
            {
                filePath = HttpContext.Current.Server.MapPath("~/");
            }
            try
            {
                if (Directory.Exists(filePath))
                {
                    DirectoryInfo dirInfo = new DirectoryInfo(filePath);
                    foreach (FileInfo file in dirInfo.GetFiles())
                    {
                        ATTFile obj = new ATTFile();
                        obj.FileName  = file.Name;
                        obj.Folder    = file.Directory.ToString();
                        obj.Size      = int.Parse(file.Length.ToString());
                        obj.Extension = file.Extension;
                        lstFiles.Add(obj);
                    }
                }
            }
            catch (Exception ex)
            {
                fb.ProcessException(ex);
            }
            if (IsSort == 1)
            {
                SortList(ref lstFiles);
            }
            Dictionary <string, string> dictImages = GetImages();
            if (lstFiles.Count > 0)
            {
                lstFiles = lstFiles.GetRange(GetStartRange(CurrentPage, PageSize), GetEndRange(CurrentPage, PageSize, lstFiles.Count));
                ///Get the dictionary of images used in buttons

                sb.Append("<div class='sfGridwrapper'><table  width='100%' cellspacing='0' cellpadding='0' class=\"jqueryFileTree\" id=\"fileList\">\n");
                if (lstFiles.Count > 0 && HttpContext.Current.Session["SortDir"] == null || (string)HttpContext.Current.Session["SortDir"] == "asc")
                {
                    sb.Append("<tr><th><span class='selectAll'><input type='checkbox' id='chkSelectAll'/></span></th><th><span class='fileName'>FileName &nbsp;&nbsp;<i class='icon-ascending-order' id='imgSort'></i></span></th><th><span class='fileInfo'>FileInfo</span></th><th class='sfEdit'></th><th class='sfEdit'></th><th class='sfEdit'></th></tr>");
                }
                else if (lstFiles.Count > 0 && (string)HttpContext.Current.Session["SortDir"] == "desc")
                {
                    sb.Append("<tr><th><span class='selectAll'><input type='checkbox' id='chkSelectAll'/></span></th><th><span class='fileName'>FileName &nbsp;&nbsp;<i class='icon-descending-order' id='imgSort' ></i></span></th><th><span class='fileInfo'>FileInfo</span></th><th class='sfEdit'></th><th class='sfEdit'></th><th class='sfEdit'></th></tr>");
                }
            }
            if (lstFiles.Count == 0)
            {
                sb.Append("<div class='sfEmptyrow'>No Files</div>");
            }
            string downloadPath = FileManagerHelper.ReplaceBackSlash(Path.Combine(HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority), GetRelativePath("Modules/FileManager/DownloadHandler.ashx?")));
            // string urlPath = GetUrlPath(filePath);
            string urlPath      = GetPreviewUrlPath(filePath);
            string absolutePath = FileManagerHelper.ReplaceBackSlash(Path.Combine(HttpContext.Current.Request.PhysicalApplicationPath.ToString(), filePath));
            int    index        = 0;
            foreach (ATTFile fi in lstFiles)
            {
                string ext   = "";
                bool   IsZip = false;
                bool   IsImg = false;

                if (fi.Extension.Length > 1)
                {
                    ext = fi.Extension.Substring(1).ToLower();
                }
                if (ext == "zip")
                {
                    IsZip = true;
                }
                if (ext == "png" || ext == "gif" || ext == "jpg" || ext == "jpeg")
                {
                    IsImg = true;
                }
                string checkId = "chk_" + fi.FileId;
                try
                {
                    FileManagerHelper.ConstructHTMLString(IsZip, IsImg, fi.StorageLocation, ext, urlPath + fi.FileName, Path.Combine(absolutePath, fi.FileName), downloadPath, checkId, folderId, fi, ref sb, "edit", dictImages, index, secureToken);
                }
                catch (Exception ex)
                {
                    fb.ProcessException(ex);
                }
                index++;
            }
            sb.Append("</table></div>");
            sb.Append("<div id='divBottomControl'>");
            sb.Append("</div>");
        }
        return(sb.ToString());
    }
    public static string SearchFiles(string SearchQuery, int UserModuleID, string UserName, int CurrentPage, int PageSize)
    {
        StringBuilder  sb       = new StringBuilder();
        bool           IsZip    = false;
        bool           IsImg    = false;
        List <ATTFile> lstFiles = FileManagerController.SearchFiles(SearchQuery);
        Dictionary <string, string> dictImages = GetImages();

        List <string> lstPermissions    = FileManagerController.GetModulePermission(UserModuleID, UserName);
        int           UserPermissionKey = lstPermissions.Contains("EDIT") ? 1 : 0;

        if (lstFiles.Count > 0)
        {
            lstFiles = lstFiles.GetRange(GetStartRange(CurrentPage, PageSize), GetEndRange(CurrentPage, PageSize, lstFiles.Count));
        }

        if (lstFiles.Count > 0)
        {
            sb.Append("<div id='divFileHeader'><span class='selectAll'><input type='checkbox' id='chkSelectAll'/></span><span class='fileName'>FileName</span><span class='fileInfo'>FileInfo</span></div>");
        }

        sb.Append("<ul class=\"jqueryFileTree\" id=\"fileList\">\n");
        if (lstFiles.Count == 0)
        {
            sb.Append("<div class='cssClassNoFilesDiv'>No Files</div>");
        }
        string downloadPath = FileManagerHelper.ReplaceBackSlash(Path.Combine(HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority), GetRelativePath("Modules/FileManager/DownloadHandler.ashx?")));



        if (UserPermissionKey == 1)
        {
            foreach (ATTFile fi in lstFiles)
            {
                string ext = "";
                if (fi.Extension.Length > 1)
                {
                    ext = fi.Extension.Substring(1).ToLower();
                }
                if (ext == "zip")
                {
                    IsZip = true;
                }
                if (ext == "png" || ext == "gif" || ext == "jpg" || ext == "jpeg")
                {
                    IsImg = true;
                }
                string checkId = "chk_" + fi.FileId;
                try
                {
                    FileManagerHelper.ConstructHTMLString(false, IsImg, fi.StorageLocation, ext, Path.Combine(GetUrlPath(fi.Folder), fi.FileName), Path.Combine(GetAbsolutePath(fi.Folder), fi.FileName), downloadPath, checkId, 0, fi, ref sb, "edit", dictImages);
                }
                catch (Exception ex)
                {
                    fb.ProcessException(ex);
                }
            }
        }
        else
        {
            foreach (ATTFile fi in lstFiles)
            {
                string ext = "";
                if (fi.Extension.Length > 1)
                {
                    ext = fi.Extension.Substring(1).ToLower();
                }
                if (ext == "zip")
                {
                    IsZip = true;
                }
                if (ext == "png" || ext == "gif" || ext == "jpg" || ext == "jpeg")
                {
                    IsImg = true;
                }
                string checkId = "chk_" + fi.FileId;
                try
                {
                    FileManagerHelper.ConstructHTMLString(false, IsImg, fi.StorageLocation, ext, Path.Combine(GetUrlPath(fi.Folder), fi.FileName), Path.Combine(GetAbsolutePath(fi.Folder), fi.FileName), downloadPath, checkId, 0, fi, ref sb, "view", dictImages);
                }
                catch (Exception ex)
                {
                    fb.ProcessException(ex);
                }
            }
        }
        sb.Append("</ul>");
        sb.Append("<div id='divBottomControl'>");
        sb.Append("<div id='divPagerNav'></div>");
        sb.Append("</div>");


        return(sb.ToString());
    }
    public static string SearchFiles(string SearchQuery, int CurrentPage, int PageSize, string FilePath, int portalID, string userName, int userModuleID, string secureToken)
    {
        StringBuilder       sb         = new StringBuilder();
        AuthenticateService objService = new AuthenticateService();

        if (objService.IsPostAuthenticatedView(portalID, userModuleID, userName, secureToken))
        {
            if (FilePath == "/")
            {
                FilePath = HttpContext.Current.Server.MapPath("~/");
            }

            List <ATTFile> lstFiles = new List <ATTFile>();
            if (Directory.Exists(FilePath))
            {
                DirectoryInfo dirInfo = new DirectoryInfo(FilePath);
                foreach (FileInfo file in dirInfo.GetFiles(SearchQuery + "*"))
                {
                    ATTFile obj = new ATTFile();
                    obj.FileName  = file.Name;
                    obj.Folder    = file.Directory.ToString();
                    obj.Size      = int.Parse(file.Length.ToString());
                    obj.Extension = file.Extension;
                    lstFiles.Add(obj);
                }
            }

            Dictionary <string, string> dictImages = GetImages();
            List <string> lstPermissions           = FileManagerController.GetModulePermission(userModuleID, userName);
            int           UserPermissionKey        = lstPermissions.Contains("EDIT") ? 1 : 0;
            if (lstFiles.Count > 0)
            {
                lstFiles = lstFiles.GetRange(GetStartRange(CurrentPage, PageSize), GetEndRange(CurrentPage, PageSize, lstFiles.Count));
            }

            sb.Append("<div class='sfGridwrapper'><table  width='100%' cellspacing='0' cellpadding='0' class=\"jqueryFileTree\" id=\"fileList\">\n");
            if (lstFiles.Count > 0)
            {
                sb.Append("<tr><th><span class='selectAll'><input type='checkbox' id='chkSelectAll'/></span></th><th><span class='fileName'>FileName<img src=" + dictImages["Sort"].ToString() + "></span></th><th><span class='fileInfo'>FileInfo</span></th><th class='sfEdit'></th><th class='sfEdit'></th><th class='sfEdit'></th></tr>");
            }
            if (lstFiles.Count == 0)
            {
                sb.Append("<div class='sfEmptyrow'>No Files</div>");
            }
            string downloadPath = FileManagerHelper.ReplaceBackSlash(Path.Combine(HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority), GetRelativePath("Modules/FileManager/DownloadHandler.ashx?")));



            if (UserPermissionKey == 1)
            {
                int index = 0;
                foreach (ATTFile fi in lstFiles)
                {
                    string ext = "";
                    //bool IsZip = false;
                    bool IsImg = false;
                    if (fi.Extension.Length > 1)
                    {
                        ext = fi.Extension.Substring(1).ToLower();
                    }
                    // if (ext == "zip")
                    //     IsZip = true;
                    if (ext == "png" || ext == "gif" || ext == "jpg" || ext == "jpeg")
                    {
                        IsImg = true;
                    }
                    string checkId = "chk_" + fi.FileId;
                    try
                    {
                        FileManagerHelper.ConstructHTMLString(false, IsImg, fi.StorageLocation, ext, Path.Combine(GetUrlPath(fi.Folder), fi.FileName), Path.Combine(GetAbsolutePath(fi.Folder), fi.FileName), downloadPath, checkId, 0, fi, ref sb, "edit", dictImages, index, secureToken);
                    }
                    catch (Exception ex)
                    {
                        fb.ProcessException(ex);
                    }
                    index++;
                }
            }
            else
            {
                int index = 0;
                foreach (ATTFile fi in lstFiles)
                {
                    string ext = "";
                    //  bool IsZip = false;
                    bool IsImg = false;
                    if (fi.Extension.Length > 1)
                    {
                        ext = fi.Extension.Substring(1).ToLower();
                    }
                    //if (ext == "zip")
                    //   IsZip = true;
                    if (ext == "png" || ext == "gif" || ext == "jpg" || ext == "jpeg")
                    {
                        IsImg = true;
                    }
                    string checkId = "chk_" + fi.FileId;
                    try
                    {
                        FileManagerHelper.ConstructHTMLString(false, IsImg, fi.StorageLocation, ext, Path.Combine(GetUrlPath(fi.Folder), fi.FileName), Path.Combine(GetAbsolutePath(fi.Folder), fi.FileName), downloadPath, checkId, 0, fi, ref sb, "view", dictImages, index, secureToken);
                    }
                    catch (Exception ex)
                    {
                        fb.ProcessException(ex);
                    }
                    index++;
                }
            }
            sb.Append("</table>");
            sb.Append("<div id='divPagerNav'></div>");
            sb.Append("</div>");
        }
        return(sb.ToString());
    }
    public static string GetFileList(string filePath, int folderId, int UserID, int IsSort, int UserModuleID, int CurrentPage, int PageSize)
    {
        List <string> lstPermissionKeys = FileMangerDataProvider.GetPermissionKeys(folderId, UserID, UserModuleID, "superuser");
        StringBuilder sb = new StringBuilder();

        System.IO.DirectoryInfo di         = new System.IO.DirectoryInfo(filePath);
        List <Folder>           lstFolders = new List <Folder>();

        if (!CacheHelper.Get("FileManagerFolders", out lstFolders))
        {
            lstFolders = FileManagerController.GetFolders();
            CacheHelper.Add(lstFolders, "FileManagerFolders");
        }


        List <ATTFile> lstFiles = new List <ATTFile>();

        List <FileCacheInfo> lstCache = new List <FileCacheInfo>();


        if (!CacheHelper.Get("FileManagerFileList", out lstCache))    //if the cache list does not exist,then create on
        {
            try
            {
                lstFiles = FileManagerController.GetFiles(folderId);
                List <FileCacheInfo> lstFCI   = new List <FileCacheInfo>();
                FileCacheInfo        cacheObj = new FileCacheInfo();
                cacheObj.FolderID = folderId;
                cacheObj.LSTFiles = lstFiles;
                lstFCI.Add(cacheObj);
                CacheHelper.Add(lstFCI, "FileManagerFileList");
            }
            catch (Exception ex)
            {
                fb.ProcessException(ex);
            }
        }
        else     //if the cache list exists
        {
            int cacheIndex = lstCache.FindIndex(
                delegate(FileCacheInfo obj)
            {
                return(obj.FolderID == folderId);
            }
                );
            if (cacheIndex > -1)
            {
                lstFiles = lstCache[cacheIndex].LSTFiles;
            }
            else
            {
                try
                {
                    lstFiles = FileManagerController.GetFiles(folderId);
                    List <FileCacheInfo> lstFCI   = lstCache;
                    FileCacheInfo        cacheObj = new FileCacheInfo();
                    cacheObj.FolderID = folderId;
                    cacheObj.LSTFiles = lstFiles;
                    lstFCI.Add(cacheObj);
                    CacheHelper.Add(lstFCI, "FileManagerFileList");
                }
                catch (Exception ex)
                {
                    fb.ProcessException(ex);
                }
            }
        }


        if (IsSort == 1)
        {
            SortList(ref lstFiles);
        }
        if (lstFiles.Count > 0)
        {
            lstFiles = lstFiles.GetRange(GetStartRange(CurrentPage, PageSize), GetEndRange(CurrentPage, PageSize, lstFiles.Count));
        }

        ///Get the dictionary of images used in buttons
        Dictionary <string, string> dictImages = GetImages();

        if (lstFiles.Count > 0)
        {
            sb.Append("<div id='divFileHeader'><span class='selectAll'><input type='checkbox' id='chkSelectAll'/></span><span class='fileName'>FileName<img src=" + dictImages["Sort"].ToString() + "></span><span class='fileInfo'>FileInfo</span></div>");
        }
        sb.Append("<ul class=\"jqueryFileTree\" id=\"fileList\">\n");
        if (lstFiles.Count == 0)
        {
            sb.Append("<div class='cssClassNoFilesDiv'>No Files</div>");
        }
        string downloadPath = FileManagerHelper.ReplaceBackSlash(Path.Combine(HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority), GetRelativePath("Modules/FileManager/DownloadHandler.ashx?")));
        string test         = HttpContext.Current.Request.PhysicalApplicationPath.ToString();
        string urlPath      = GetUrlPath(filePath);
        string absolutePath = FileManagerHelper.ReplaceBackSlash(Path.Combine(HttpContext.Current.Request.PhysicalApplicationPath.ToString(), filePath));



        ///For Users with View and Write Permissions
        bool IsZip = false;
        bool IsImg = false;

        if (((lstPermissionKeys.Contains("BROWSE") && lstPermissionKeys.Contains("VIEW")) && lstPermissionKeys.Contains("EDIT")) || lstPermissionKeys.Contains("EDIT"))
        {
            foreach (ATTFile fi in lstFiles)
            {
                string ext = "";
                if (fi.Extension.Length > 1)
                {
                    ext = fi.Extension.Substring(1).ToLower();
                }
                if (ext == "zip")
                {
                    IsZip = true;
                }
                if (ext == "png" || ext == "gif" || ext == "jpg" || ext == "jpeg")
                {
                    IsImg = true;
                }
                string checkId = "chk_" + fi.FileId;
                if (fi.StorageLocation != (int)StorageLocation.SECURED_DATABASE_SYSTEM)
                {
                    switch (fi.StorageLocation)
                    {
                    case (int)StorageLocation.SECURED_FILE_SYSTEM:
                        if (File.Exists(Path.Combine(absolutePath, fi.FileName + ".resources")))
                        {
                            try
                            {
                                FileManagerHelper.ConstructHTMLString(IsZip, IsImg, fi.StorageLocation, ext, Path.Combine(urlPath, fi.FileName), Path.Combine(absolutePath, fi.FileName), downloadPath, checkId, folderId, fi, ref sb, "edit", dictImages);
                            }
                            catch (Exception ex)
                            {
                                fb.ProcessException(ex);
                            }
                        }
                        break;

                    case (int)StorageLocation.STANDARD:
                        if (File.Exists(Path.Combine(absolutePath, fi.FileName)))
                        {
                            try
                            {
                                FileManagerHelper.ConstructHTMLString(IsZip, IsImg, fi.StorageLocation, ext, Path.Combine(urlPath, fi.FileName), Path.Combine(absolutePath, fi.FileName), downloadPath, checkId, folderId, fi, ref sb, "edit", dictImages);
                            }
                            catch (Exception ex)
                            {
                                fb.ProcessException(ex);
                            }
                        }
                        break;
                    }
                }
                else if (fi.StorageLocation == (int)StorageLocation.SECURED_DATABASE_SYSTEM)
                {
                    try
                    {
                        FileManagerHelper.ConstructHTMLString(IsZip, IsImg, fi.StorageLocation, ext, Path.Combine(urlPath, fi.FileName), Path.Combine(absolutePath, fi.FileName), downloadPath, checkId, folderId, fi, ref sb, "edit", dictImages);
                    }
                    catch (Exception ex)
                    {
                        fb.ProcessException(ex);
                    }
                }
            }
        }
        ///For users with only browse permission
        else if (((lstPermissionKeys.Contains("BROWSE") && !lstPermissionKeys.Contains("VIEW")) && !lstPermissionKeys.Contains("EDIT")))
        {
            foreach (ATTFile fi in lstFiles)
            {
                string ext = "";
                if (fi.Extension.Length > 1)
                {
                    ext = fi.Extension.Substring(1).ToLower();
                }
                if (ext == "zip")
                {
                    IsZip = true;
                }
                if (ext == "png" || ext == "gif" || ext == "jpg" || ext == "jpeg")
                {
                    IsImg = true;
                }
                string checkId = "chk_" + fi.FileId;

                if (fi.StorageLocation != (int)StorageLocation.SECURED_DATABASE_SYSTEM)
                {
                    switch (fi.StorageLocation)
                    {
                    case (int)StorageLocation.SECURED_FILE_SYSTEM:
                        if (File.Exists(Path.Combine(absolutePath, fi.FileName + ".resources")))
                        {
                            try
                            {
                                FileManagerHelper.ConstructHTMLString(false, IsImg, fi.StorageLocation, ext, Path.Combine(urlPath, fi.FileName), Path.Combine(absolutePath, fi.FileName), downloadPath, checkId, folderId, fi, ref sb, "browse", dictImages);
                            }
                            catch (Exception ex)
                            {
                                fb.ProcessException(ex);
                            }
                        }
                        break;

                    case (int)StorageLocation.STANDARD:
                        if (File.Exists(Path.Combine(absolutePath, fi.FileName)))
                        {
                            try
                            {
                                FileManagerHelper.ConstructHTMLString(false, IsImg, fi.StorageLocation, ext, Path.Combine(urlPath, fi.FileName), Path.Combine(absolutePath, fi.FileName), downloadPath, checkId, folderId, fi, ref sb, "browse", dictImages);
                            }
                            catch (Exception ex)
                            {
                                fb.ProcessException(ex);
                            }
                        }
                        break;
                    }
                }
                else
                {
                    try
                    {
                        FileManagerHelper.ConstructHTMLString(false, IsImg, fi.StorageLocation, ext, Path.Combine(urlPath, fi.FileName), Path.Combine(absolutePath, fi.FileName), downloadPath, checkId, folderId, fi, ref sb, "browse", dictImages);
                    }
                    catch (Exception ex)
                    {
                        fb.ProcessException(ex);
                    }
                }
            }
        }
        else if (((lstPermissionKeys.Contains("VIEW")) && !lstPermissionKeys.Contains("EDIT")))
        {
            foreach (ATTFile fi in lstFiles)
            {
                string ext = "";
                if (fi.Extension.Length > 1)
                {
                    ext = fi.Extension.Substring(1).ToLower();
                }
                if (ext == "zip")
                {
                    IsZip = true;
                }
                if (ext == "png" || ext == "gif" || ext == "jpg" || ext == "jpeg")
                {
                    IsImg = true;
                }
                string checkId = "chk_" + fi.FileId;
                if (fi.StorageLocation != (int)StorageLocation.SECURED_DATABASE_SYSTEM)
                {
                    switch (fi.StorageLocation)
                    {
                    case (int)StorageLocation.SECURED_FILE_SYSTEM:
                        if (File.Exists(Path.Combine(absolutePath, fi.FileName + ".resources")))
                        {
                            try
                            {
                                FileManagerHelper.ConstructHTMLString(false, IsImg, fi.StorageLocation, ext, Path.Combine(urlPath, fi.FileName), Path.Combine(absolutePath, fi.FileName), downloadPath, checkId, folderId, fi, ref sb, "view", dictImages);
                            }
                            catch (Exception ex)
                            {
                                fb.ProcessException(ex);
                            }
                        }
                        break;

                    case (int)StorageLocation.STANDARD:
                        if (File.Exists(Path.Combine(absolutePath, fi.FileName)))
                        {
                            try
                            {
                                FileManagerHelper.ConstructHTMLString(false, IsImg, fi.StorageLocation, ext, Path.Combine(urlPath, fi.FileName), Path.Combine(absolutePath, fi.FileName), downloadPath, checkId, folderId, fi, ref sb, "view", dictImages);
                            }
                            catch (Exception ex)
                            {
                                fb.ProcessException(ex);
                            }
                        }
                        break;
                    }
                }
                else
                {
                    FileManagerHelper.ConstructHTMLString(false, IsImg, fi.StorageLocation, ext, Path.Combine(urlPath, fi.FileName), Path.Combine(absolutePath, fi.FileName), downloadPath, checkId, folderId, fi, ref sb, "view", dictImages);
                }
            }
        }
        sb.Append("</ul>");
        sb.Append("<div id='divBottomControl'>");
        sb.Append("</div>");
        return(sb.ToString());
    }