/// <summary> /// Lists the folder contents. /// </summary> /// <param name="relativeFolderPath">The folder path.</param> protected void ListFolderContents(string relativeFolderPath) { try { string rootFolder = GetRootFolderPath(); string physicalRootFolder = this.MapPath(rootFolder); string physicalFolder = Path.Combine(physicalRootFolder, relativeFolderPath.TrimStart('/', '\\')); bool isRestricted = false; bool isUploadRestricted = false; if (RestrictedFolders.Contains(relativeFolderPath.TrimStart('/', '\\'), StringComparer.OrdinalIgnoreCase)) { isRestricted = true; } if (UploadRestrictedFolders.Any(a => relativeFolderPath.TrimStart('/', '\\').StartsWith(a, StringComparison.OrdinalIgnoreCase))) { isUploadRestricted = true; } hfIsRestrictedFolder.Value = isRestricted.ToString(); hfIsUploadRestrictedFolder.Value = isUploadRestricted.ToString(); var sb = new StringBuilder(); sb.AppendLine("<ul class='js-rocklist rocklist'>"); string imageFileTypeWhiteList = PageParameter("imageFileTypeWhiteList"); if (string.IsNullOrWhiteSpace(imageFileTypeWhiteList)) { imageFileTypeWhiteList = "*.*"; } // Directory.GetFiles doesn't support multiple patterns, so we'll do one at a time List <string> fileFilters = imageFileTypeWhiteList.Split(new char[] { ';', ',' }, StringSplitOptions.RemoveEmptyEntries) .Select(s => s = "*." + s.TrimStart(new char[] { '*', ' ' }).TrimStart('.')) // ensure that the filter starts with '*.' .ToList(); List <string> fileList = new List <string>(); foreach (var filter in fileFilters) { fileList.AddRange(Directory.GetFiles(physicalFolder, filter).OrderBy(a => a).ToList()); } nbNoFilesInfo.Visible = !fileList.Any(); string editFilePage = PageParameter("editFilePage"); foreach (var filePath in fileList) { string ext = Path.GetExtension(filePath); string fileName = Path.GetFileName(filePath); string relativeFilePath = filePath.Replace(physicalRootFolder, string.Empty); string imagePath = rootFolder.TrimEnd('/', '\\') + "/" + relativeFilePath.TrimStart('/', '\\').Replace("\\", "/"); string imageUrl = this.ResolveUrl("~/api/FileBrowser/GetFileThumbnail?relativeFilePath=" + HttpUtility.UrlEncode(imagePath)); string editHtml = string.Empty; if (!RestrictedFileExtension.Any(a => ext.Equals(a, StringComparison.OrdinalIgnoreCase)) && !string.IsNullOrWhiteSpace(editFilePage)) { string url = editFilePage + "?RelativeFilePath=" + HttpUtility.UrlEncode(imagePath); editHtml = string.Format(@" <a data-href='{0}' title='edit' class='btn btn-xs btn-square btn-default js-edit-file action'> <i class='fa fa-pencil'></i> </a> ", url); } string nameHtmlFormat = @" <li class='js-rocklist-item rocklist-item' data-id='{0}'> <div class='rollover-container'> <div class='rollover-item actions'> <a title='delete' class='btn btn-xs btn-square btn-danger js-delete-file action'> <i class='fa fa-times'></i> </a> <a href='{3}' target='_blank' title='download' class='btn btn-xs btn-square btn-default js-download-file action'> <i class='fa fa-download'></i> </a> {4} </div> <img src='{1}' class='file-browser-image' /> <br /> <span class='file-name'>{2}</span> </div> </li> "; // put the file timestamp as part of the url to that changed files are loaded from the server instead of the browser cache var fileDateTime = File.GetLastWriteTimeUtc(filePath); imageUrl += "&timeStamp=" + fileDateTime.Ticks.ToString(); string nameHtml = string.Format( nameHtmlFormat, HttpUtility.HtmlEncode(relativeFilePath), imageUrl, fileName, HttpUtility.HtmlEncode(this.ResolveUrl(imagePath)), editHtml); sb.AppendLine(nameHtml); } sb.AppendLine("</ul>"); lblFiles.Text = sb.ToString(); } catch (Exception ex) { ShowErrorMessage(ex, string.Format("Unable to list contents of folder {0}. Contact your administrator.", relativeFolderPath)); } }
/// <summary> /// Determines whether [is restricted folder] [the specified folder name]. /// </summary> /// <param name="folderName">Name of the folder.</param> /// <returns> /// <c>true</c> if [is restricted folder] [the specified folder name]; otherwise, <c>false</c>. /// </returns> private bool IsRestrictedFolder(string folderName) { return(RestrictedFolders.Contains(hfSelectedFolder.Value.TrimStart('/', '\\'), StringComparer.OrdinalIgnoreCase)); }