Beispiel #1
0
        public List <string> GetFilesInDirectory(string directoryPath, StorageLocation location, string blobSasUri, string searchPattern = "*", SearchOption searchOption = SearchOption.AllDirectories)
        {
            if (UseBlobStorage(location, blobSasUri))
            {
                List <string> files = new List <string>();
                var           dir   = BlobController.GetBlobDirectory(directoryPath, blobSasUri);
                foreach (
                    IListBlobItem item in
                    dir.ListBlobs(useFlatBlobListing: true))
                {
                    var relativePath = item.Uri.ToString().Replace(item.Container.Uri.ToString() + "/", "");
                    files.Add(relativePath);
                }
                return(files);
            }
            else
            {
                string rootPath    = GetRootStoragePathForWhenBlobStorageIsNotConfigured(location);
                string fullDirPath = Path.Combine(rootPath, directoryPath).ConvertForwardSlashesToBackSlashes();

                var files = new List <string>();
                if (Directory.Exists(fullDirPath))
                {
                    files = Directory.GetFiles(fullDirPath, searchPattern, searchOption).ToList();
                }
                return(files);
            }
        }
Beispiel #2
0
        public void MoveFile(string oldPath, string newPath, StorageLocation location, string blobSasUri)
        {
            if (oldPath.StartsWith("\\") || oldPath.StartsWith("/"))
            {
                oldPath = oldPath.Remove(0, 1);
            }
            if (Path.GetFullPath(oldPath).Equals(Path.GetFullPath(newPath), StringComparison.OrdinalIgnoreCase))
            {
                // Moving a file from and to the same path. Don't do anything
                return;
            }

            if (UseBlobStorage(location, blobSasUri))
            {
                var oldBlob = BlobController.GetBlobForFile(oldPath, blobSasUri);
                var newBlob = BlobController.GetBlobForFile(newPath, blobSasUri);
                newBlob.StartCopy(oldBlob);
                //newBlob.StartCopyFromBlob(oldBlob);
                oldBlob.DeleteIfExists();
            }
            else
            {
                MoveFileOnDisk(oldPath, newPath, location);
            }
        }
Beispiel #3
0
        public bool FileExists(string filePath, StorageLocation location, string blobSasUri)
        {
            if (UseBlobStorage(location, blobSasUri))
            {
                var blob = BlobController.GetBlobForFile(filePath, blobSasUri);
                return(blob.Exists());
            }

            return(FileExistsOnDisk(filePath, location));
        }
Beispiel #4
0
        public async Task <bool> FileExistsAsync(string filePath, StorageLocation location, string blobSasUri)
        {
            if (UseBlobStorage(location, blobSasUri))
            {
                var blob = BlobController.GetBlobForFile(filePath, blobSasUri);
                return(await blob.ExistsAsync());
            }

            return(FileExistsOnDisk(filePath, location));
        }
Beispiel #5
0
        public Stream ReadFile(string filePath, StorageLocation location, string blobSasUri)
        {
            if (UseBlobStorage(location, blobSasUri))
            {
                var blob   = BlobController.GetBlobForFile(filePath, blobSasUri);
                var stream = new MemoryStream();
                blob.DownloadToStream(stream);
                stream.Position = 0;
                return(stream);
            }

            string     baseDir    = GetRootStoragePathForWhenBlobStorageIsNotConfigured((StorageLocation)location);
            string     fullPath   = Path.Combine(baseDir, filePath);
            FileStream fileStream = new FileStream(fullPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);

            return(fileStream);
        }
Beispiel #6
0
        public void SaveFile(Stream fileContents, string relativeFilePath, StorageLocation location, string blobSasUri, Lease lease = null)
        {
            if (UseBlobStorage(location, blobSasUri))
            {
                var blockBlob = BlobController.GetBlobForFile(relativeFilePath, blobSasUri);
                blockBlob.UploadFromStream(fileContents, GetAccessCondition(lease), null, null);
                return;
            }

            var rootPath = GetRootStoragePathForWhenBlobStorageIsNotConfigured(location);
            var fullPath = Path.Combine(rootPath, relativeFilePath);

            Directory.CreateDirectory(Path.GetDirectoryName(fullPath));
            using (FileStream file = new FileStream(fullPath, FileMode.Create, FileAccess.Write, FileShare.ReadWrite))
            {
                fileContents.CopyTo(file);
            }
        }
Beispiel #7
0
        /// <summary>
        /// Delete the given file from all known locations
        /// </summary>
        public async Task DeleteFileAsync(string filePath, string blobSasUri, Lease lease = null)
        {
            if (!string.IsNullOrWhiteSpace(blobSasUri))
            {
                var fileBlob = BlobController.GetBlobForFile(filePath, blobSasUri);
                await fileBlob.DeleteIfExistsAsync(DeleteSnapshotsOption.None, GetAccessCondition(lease), null, null);
            }

            foreach (var baseDir in new string[] { Infrastructure.Settings.TempDir, Settings.UserSiteStorageDirectory })
            {
                string fullPath = Path.Combine(baseDir, filePath);
                if (System.IO.File.Exists(fullPath))
                {
                    RetryHelper.RetryOnException("Deleting file asynchronously...", () =>
                    {
                        System.IO.File.Delete(fullPath);
                    }, TimeSpan.FromSeconds(1));
                }
            }
        }