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

UploadObject() public method

public UploadObject ( BetterCms.Core.Services.Storage.UploadRequest request ) : void
request BetterCms.Core.Services.Storage.UploadRequest
return void
        public void UploadObject(UploadRequest request)
        {
            CheckUri(request.Uri);

            string pathRoot = Path.GetDirectoryName(request.Uri.LocalPath);
            if (pathRoot != null && !Directory.Exists(pathRoot))
            {
                Directory.CreateDirectory(pathRoot);
            }

            using (FileStream writeStream = new FileStream(request.Uri.LocalPath, FileMode.Create, FileAccess.Write))
            {
                request.InputStream.Seek(0, SeekOrigin.Begin);
                request.InputStream.CopyTo(writeStream);
                writeStream.Flush(true);
                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"));
        }
All Usage Examples Of BetterCms.Core.Services.Storage.FileSystemStorageService::UploadObject