/// <summary> /// Search folders and blobs in folder /// </summary> /// <param name="folderUrl">absolute or relative path</param> /// <returns></returns> public BlobSearchResult Search(string folderUrl, string keyword) { var retVal = new BlobSearchResult(); var storagePath = _storagePath; folderUrl = folderUrl ?? _basePublicUrl; var storageFolderPath = GetAbsoluteStoragePathFromUrl(folderUrl); var directories = String.IsNullOrEmpty(keyword) ? Directory.GetDirectories(storageFolderPath) : Directory.GetDirectories(storageFolderPath, "*" + keyword + "*", SearchOption.AllDirectories); foreach (var directory in directories) { var directoryInfo = new DirectoryInfo(directory); retVal.Folders.Add(new BlobFolder { Name = Path.GetFileName(directory), Url = GetAbsoluteUrlFromPath(directory), ParentUrl = GetAbsoluteUrlFromPath(directoryInfo.Parent.FullName) }); } var files = String.IsNullOrEmpty(keyword) ? Directory.GetFiles(storageFolderPath) : Directory.GetFiles(storageFolderPath, "*" + keyword + "*.*", SearchOption.AllDirectories); foreach (var file in files) { var fileInfo = new FileInfo(file); retVal.Items.Add(new BlobInfo { Url = GetAbsoluteUrlFromPath(file), ContentType = MimeTypeResolver.ResolveContentType(fileInfo.Name), Size = fileInfo.Length, FileName = fileInfo.Name, ModifiedDate = fileInfo.LastWriteTimeUtc }); } return retVal; }
public virtual BlobSearchResult Search(string folderUrl, string keyword) { var retVal = new BlobSearchResult(); if (!String.IsNullOrEmpty(folderUrl)) { var blobContainer = GetBlobContainer(GetContainerNameFromUrl(folderUrl)); if (blobContainer != null) { var directoryPath = GetDirectoryPathFromUrl(folderUrl); var blobDirectory = !String.IsNullOrEmpty(directoryPath) ? blobContainer.GetDirectoryReference(directoryPath) : null; var listBlobs = blobDirectory != null ? blobDirectory.ListBlobs() : blobContainer.ListBlobs(); if (!String.IsNullOrEmpty(keyword)) { listBlobs = blobContainer.ListBlobs(keyword, useFlatBlobListing: true); } // Loop over items within the container and output the length and URI. foreach (IListBlobItem item in listBlobs) { var block = item as CloudBlockBlob; var directory = item as CloudBlobDirectory; if (block != null) { var blobInfo = new BlobInfo { Url = Uri.EscapeUriString(block.Uri.ToString()), FileName = Path.GetFileName(Uri.UnescapeDataString(block.Uri.ToString())), ContentType = block.Properties.ContentType, Size = block.Properties.Length, ModifiedDate = block.Properties.LastModified != null ? block.Properties.LastModified.Value.DateTime : (DateTime?)null }; blobInfo.RelativeUrl = blobInfo.Url.Replace(_cloudBlobClient.BaseUri.ToString(), string.Empty); //Do not return empty blob (created with directory because azure blob not support direct directory creation) if (!String.IsNullOrEmpty(blobInfo.FileName)) { retVal.Items.Add(blobInfo); } } if (directory != null) { var folder = new BlobFolder { Name = Uri.UnescapeDataString(directory.Uri.AbsolutePath).Split(new[] { _cloudBlobClient.DefaultDelimiter }, StringSplitOptions.RemoveEmptyEntries).Last(), Url = Uri.EscapeUriString(directory.Uri.ToString()), ParentUrl = directory.Parent != null ? Uri.EscapeUriString(directory.Parent.Uri.ToString()) : null }; folder.RelativeUrl = folder.Url.Replace(_cloudBlobClient.BaseUri.ToString(), string.Empty); retVal.Folders.Add(folder); } } } } else { foreach (var container in _cloudBlobClient.ListContainers()) { var folder = new BlobFolder { Name = container.Uri.AbsolutePath.Split('/').Last(), Url = Uri.EscapeUriString(container.Uri.ToString()) }; retVal.Folders.Add(folder); } } return retVal; }