public async Task CreateOrAppendTextWorksWithExistingFile() { // --- Arrange const string BODY = "FirstSecond"; var wfs = new AzureFileStorage(ROOT); var file = new AbstractFileDescriptor("Container", null, "TestFile.txt"); using (var textFile = await wfs.CreateTextAsync(file)) { textFile.Writer.Write("First"); } // --- Act using (var textFile = await wfs.CreateOrAppendTextAsync(file)) { textFile.Writer.Write("Second"); } // --- Assert using (var savedFile = await wfs.OpenTextAsync(file)) { var text = savedFile.Reader.ReadToEnd(); text.ShouldBe(BODY); } }
public async Task AppendTextWorksWithSpecificCulture() { // --- Arrange const string BODY = "1,25-1,25"; var wfs = new AzureFileStorage(ROOT); var file = new AbstractFileDescriptor("Container", null, "TestFile.txt"); using (var textFile = await wfs.CreateTextAsync(file, new CultureInfo("hu-hu"))) { textFile.Writer.Write(1.25); } // --- Act using (var textFile = await wfs.AppendTextAsync(file, new CultureInfo("hu-hu"))) { textFile.Writer.Write(-1.25); } // --- Assert using (var savedFile = await wfs.OpenTextAsync(file)) { var text = savedFile.Reader.ReadToEnd(); text.ShouldBe(BODY); } }
public async Task CreateOrAppendTextWorksWithNonExistingFile() { // --- Arrange const string BODY = "This is a text file"; var wfs = new WindowsFileStorage(ROOT); var file = new AbstractFileDescriptor("Container", null, "TestFile.txt"); var fileName = WindowsFileStorage.FilePathFromAbstractFile(file); if (File.Exists(fileName)) { File.Delete(fileName); } // --- Act using (var textFile = await wfs.CreateOrAppendTextAsync(file)) { textFile.Writer.Write(BODY); } // --- Assert using (var savedFile = await wfs.OpenTextAsync(file)) { var text = savedFile.Reader.ReadToEnd(); text.ShouldBe(BODY); } }
/// <summary> /// Operns an abstract text file for append operation, or creates it, provided, it does not exists. /// Returns the object to work with the file. /// </summary> /// <param name="file">Abstract file descriptor</param> /// <param name="formatProvider">Optional format provider</param> /// <param name="encoding">Optional file encoding</param> /// <param name="flushSize"> /// The flush size to set in Kbytes. If zero or less, the default storage provider flush /// size is used. /// </param> /// <returns> /// The object that provides operations to work with the text file. /// </returns> public async Task <IAbstractTextFile> CreateOrAppendTextAsync(AbstractFileDescriptor file, IFormatProvider formatProvider = null, Encoding encoding = null, int flushSize = 0) { return((await ExistsAsync(file)) ? await AppendTextAsync(file, formatProvider, encoding, flushSize) : await CreateTextAsync(file, formatProvider, encoding, flushSize)); }
/// <summary> /// Creates a Windows path from an <see cref="AbstractFileDescriptor"/> /// </summary> /// <param name="descriptor">File descriptior to create the path from</param> /// <returns>Windows file path</returns> public static string FilePathFromAbstractFile(AbstractFileDescriptor descriptor) { var folder = descriptor.RootContainer ?? ""; if (descriptor.PathSegments != null) { folder = descriptor.PathSegments.Aggregate(folder, Path.Combine); } return(Path.Combine(folder, descriptor.FileName)); }
/// <summary> /// Gets the Azure blob name from an abstract file descriptor /// </summary> /// <param name="descriptor"></param> /// <returns></returns> public static string GetBlobNameFromAbstractFile(AbstractFileDescriptor descriptor) { var folder = ""; if (descriptor.PathSegments != null) { folder = string.Join("//", descriptor.PathSegments); } return(Path.Combine(folder, descriptor.FileName)); }
/// <summary> /// Opens an abstract text file for append operation. Returns the object to work with the file. /// </summary> /// <param name="file">Abstract file descriptor</param> /// <param name="formatProvider">Optional format provider</param> /// <param name="encoding">Optional file encoding</param> /// <param name="flushSize"> /// The flush size to set in Kbytes. If zero or less, the default storage provider flush /// size is used. /// </param> /// <returns> /// The object that provides operations to work with the text file. /// </returns> public async Task <IAbstractTextFile> AppendTextAsync(AbstractFileDescriptor file, IFormatProvider formatProvider = null, Encoding encoding = null, int flushSize = 0) { await EnsureContainerAsync(file.RootContainer); var blob = await GetAppendBlobReference(file); await blob.OpenWriteAsync(false); return(new AzureTextFile(blob, formatProvider, encoding, flushSize)); }
/// <summary> /// Opens a text file for read. Returns the object to work with the file. /// </summary> /// <param name="file">Abstract file descriptor</param> /// <param name="encoding">Optional file encoding</param> /// <returns> /// The object that provides operations to work with the text file. /// </returns> public async Task <IAbstractTextFile> OpenTextAsync(AbstractFileDescriptor file, Encoding encoding = null) { return(await Task.Run(() => { var textFile = File.Open(GetFilePath(file), FileMode.Open, FileAccess.Read); var reader = encoding == null ? new StreamReader(textFile) : new StreamReader(textFile, encoding); return new WindowsTextFile(reader); })); }
/// <summary> /// Opens an abstract text file for append operation. Returns the object to work with the file. /// </summary> /// <param name="file">Abstract file descriptor</param> /// <param name="formatProvider">Optional format provider</param> /// <param name="encoding">Optional file encoding</param> /// <param name="flushSize"> /// The flush size to set in Kbytes. If zero or less, the default storage provider flush /// size is used. /// </param> /// <returns> /// The object that provides operations to work with the text file. /// </returns> public async Task <IAbstractTextFile> AppendTextAsync(AbstractFileDescriptor file, IFormatProvider formatProvider = null, Encoding encoding = null, int flushSize = 0) { return(await Task.Run(() => { var textFile = File.Open(GetFilePath(file), FileMode.Append); var writer = encoding == null ? new StreamWriter(textFile) : new StreamWriter(textFile, encoding); return new WindowsTextFile(writer, formatProvider, encoding, flushSize); })); }
/// <summary> /// Gets a reference to the block blob specified in <paramref name="file"/> /// </summary> /// <param name="file">File descriptor</param> /// <returns>Block blob, if can be obtained; null, if the container does not exist</returns> private async Task <CloudAppendBlob> GetAppendBlobReference(AbstractFileDescriptor file) { var containerName = (file.RootContainer ?? "").ToLower(); var blobClient = StorageAccount.CreateCloudBlobClient(); var container = blobClient.GetContainerReference(containerName); if (!await container.ExistsAsync()) { return(null); } return(container.GetAppendBlobReference(GetBlobNameFromAbstractFile(file))); }
/// <summary> /// Removes the specified abstract file /// </summary> /// <param name="file">File to remove</param> /// <returns>True, if the file existed before the remove operation</returns> public Task <bool> DeleteAsync(AbstractFileDescriptor file) { return(Task.Run(() => { var path = GetFilePath(file); var exists = File.Exists(path); if (exists) { File.Delete(path); } return exists; })); }
public async Task DeleteWorksWithNonExistingFile() { // --- Arrange var wfs = new AzureFileStorage(ROOT); var file = new AbstractFileDescriptor("Container", null, "TestFile.txt"); await wfs.DeleteAsync(file); // --- Act var deleted = await wfs.DeleteAsync(file); // --- Assert deleted.ShouldBeFalse(); (await wfs.ExistsAsync(file)).ShouldBeFalse(); }
/// <summary> /// Checks whether the specified file exists in the storage. /// </summary> /// <param name="file">File to check</param> /// <returns>True, if the file exists; otherwise, false.</returns> public async Task <bool> ExistsAsync(AbstractFileDescriptor file) { var containerName = (file.RootContainer ?? "").ToLower(); var blobClient = StorageAccount.CreateCloudBlobClient(); var container = blobClient.GetContainerReference(containerName); if (!await container.ExistsAsync()) { return(false); } var blob = await GetAppendBlobReference(file); return(blob != null && await blob.ExistsAsync()); }
public async Task DeleteWorksWithExistingFile() { // --- Arrange var wfs = new AzureFileStorage(ROOT); var file = new AbstractFileDescriptor("Container", null, "TestFile.txt"); using (var textFile = await wfs.CreateTextAsync(file)) { textFile.Writer.Write("Awesome"); } // --- Act var deleted = await wfs.DeleteAsync(file); // --- Assert deleted.ShouldBeTrue(); (await wfs.ExistsAsync(file)).ShouldBeFalse(); }
/// <summary> /// Creates an abstract text file. Returns the object to work with the file. /// </summary> /// <param name="file">Abstract file descriptor</param> /// <param name="formatProvider">Optional format provider</param> /// <param name="encoding">Optional file encoding</param> /// <param name="flushSize"> /// The flush size to set in Kbytes. If zero or less, the default storage provider flush /// size is used. /// </param> /// <returns> /// The object that provides operations to work with the text file. /// </returns> public async Task <IAbstractTextFile> CreateTextAsync(AbstractFileDescriptor file, IFormatProvider formatProvider = null, Encoding encoding = null, int flushSize = 0) { return(await Task.Run(() => { var fileName = GetFilePath(file); var dir = Path.GetDirectoryName(fileName); if (!Directory.Exists(dir)) { // ReSharper disable once AssignNullToNotNullAttribute Directory.CreateDirectory(dir); } var textFile = File.Open(GetFilePath(file), FileMode.Create); var writer = encoding == null ? new StreamWriter(textFile) : new StreamWriter(textFile, encoding); return new WindowsTextFile(writer, formatProvider, encoding, flushSize); })); }
public async Task CreateTextWorksWithNeutralCulture() { // --- Arrange const string BODY = "1.25"; var wfs = new WindowsFileStorage(ROOT); var file = new AbstractFileDescriptor("Container", null, "TestFile.txt"); // --- Act using (var textFile = await wfs.CreateTextAsync(file)) { textFile.Writer.Write(1.25); } // --- Assert using (var savedFile = await wfs.OpenTextAsync(file)) { var text = savedFile.Reader.ReadToEnd(); text.ShouldBe(BODY); } }
/// <summary> /// Opens a text file for read. Returns the object to work with the file. /// </summary> /// <param name="file">Abstract file descriptor</param> /// <param name="encoding">Optional file encoding</param> /// <returns> /// The object that provides operations to work with the text file. /// </returns> public async Task <IAbstractTextFile> OpenTextAsync(AbstractFileDescriptor file, Encoding encoding = null) { var blob = await GetAppendBlobReference(file); if (blob == null || !await blob.ExistsAsync()) { throw new FileNotFoundException(GetBlobNameFromAbstractFile(file)); } var memoryStream = new MemoryStream(); blob.DownloadToStream(memoryStream); if (encoding == null) { encoding = Encoding.UTF8; } memoryStream.Seek(0, SeekOrigin.Begin); var reader = new StreamReader(memoryStream, encoding); return(new AzureTextFile(reader)); }
public async Task CreateTextWorksAsExpected() { // --- Arrange const string BODY = "This is a text file"; var wfs = new AzureFileStorage(ROOT); var file = new AbstractFileDescriptor("Container", null, "TestFile.txt"); // --- Act using (var textFile = await wfs.CreateTextAsync(file)) { textFile.Writer.Write(BODY); } // --- Assert using (var savedFile = await wfs.OpenTextAsync(file)) { var text = savedFile.Reader.ReadToEnd(); text.ShouldBe(BODY); } }
/// <summary> /// Checks whether the specified file exists in the storage. /// </summary> /// <param name="file">File to check</param> /// <returns>True, if the file exists; otherwise, false.</returns> public Task <bool> ExistsAsync(AbstractFileDescriptor file) { return(Task.Run(() => File.Exists(GetFilePath(file)))); }
/// <summary> /// Gets the full file path from an abstract descriptor /// </summary> private string GetFilePath(AbstractFileDescriptor descriptor) => Path.Combine(RootFolder, FilePathFromAbstractFile(descriptor));