예제 #1
0
        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);
            }
        }
예제 #2
0
        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);
            }
        }
예제 #3
0
        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);
            }
        }
예제 #4
0
 /// <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));
 }
예제 #5
0
        /// <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));
        }
예제 #6
0
        /// <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));
        }
예제 #7
0
        /// <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));
        }
예제 #8
0
 /// <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);
     }));
 }
예제 #9
0
 /// <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);
     }));
 }
예제 #10
0
        /// <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)));
        }
예제 #11
0
 /// <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;
     }));
 }
예제 #12
0
        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();
        }
예제 #13
0
        /// <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());
        }
예제 #14
0
        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();
        }
예제 #15
0
 /// <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);
     }));
 }
예제 #16
0
        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);
            }
        }
예제 #17
0
        /// <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));
        }
예제 #18
0
        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);
            }
        }
예제 #19
0
 /// <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))));
 }
예제 #20
0
 /// <summary>
 /// Gets the full file path from an abstract descriptor
 /// </summary>
 private string GetFilePath(AbstractFileDescriptor descriptor) =>
 Path.Combine(RootFolder, FilePathFromAbstractFile(descriptor));