BetterCms.Core.Services.Storage.FileSystemStorageService.CopyObject C# (CSharp) Method

CopyObject() public method

public CopyObject ( Uri sourceUri, Uri destinationUri ) : void
sourceUri System.Uri
destinationUri System.Uri
return void
        public void CopyObject(Uri sourceUri, Uri destinationUri)
        {
            CheckUri(sourceUri);
            CheckUri(destinationUri);

            var cleanedDestinationPath = destinationUri.LocalPath.Remove(destinationUri.LocalPath.TrimEnd('\\', '/').LastIndexOfAny(new[] { '\\', '/' }));
            Directory.CreateDirectory(cleanedDestinationPath);
            using (FileStream readStream = new FileStream(sourceUri.LocalPath, FileMode.Open, FileAccess.Read))
            {
                using (FileStream writeStream = new FileStream(destinationUri.LocalPath, FileMode.Create, FileAccess.Write))
                {
                    readStream.CopyTo(writeStream);
                    readStream.Flush();
                    writeStream.Flush(true);
                    readStream.Close();
                    writeStream.Close();                    
                }                
            }
        }

Usage Example

        public void Should_Throw_StorageException_If_Given_Uri_Is_Not_Of_File_Scheme()
        {
            Uri httpUri = new Uri("http://www.google.com");
            FileSystemStorageService storageService = new FileSystemStorageService();

            var ex1 = Assert.Throws<StorageException>(() => storageService.ObjectExists(httpUri));
            var ex2 = Assert.Throws<StorageException>(() => storageService.CopyObject(httpUri, httpUri));
            var ex3 = Assert.Throws<StorageException>(() => storageService.DownloadObject(httpUri));
            var ex4 = Assert.Throws<StorageException>(() => storageService.UploadObject(new UploadRequest { Uri = httpUri }));

            Assert.IsTrue(ex1.Message.StartsWith("An Uri scheme") && ex1.Message.Contains("can't be processed with a"));
            Assert.IsTrue(ex2.Message.StartsWith("An Uri scheme") && ex1.Message.Contains("can't be processed with a"));
            Assert.IsTrue(ex3.Message.StartsWith("An Uri scheme") && ex1.Message.Contains("can't be processed with a"));
            Assert.IsTrue(ex4.Message.StartsWith("An Uri scheme") && ex1.Message.Contains("can't be processed with a"));
        }