BetterCms.Core.Services.Storage.FtpStorageService.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);

            try
            {
                TryCreateDirectory(CutLastDirectoryFromUri(destinationUri.AbsoluteUri), true);
                var response = DownloadObject(sourceUri);

                var request = new UploadRequest();
                request.Uri = destinationUri;
                request.InputStream = new MemoryStream();

                response.ResponseStream.Position = 0;
                response.ResponseStream.CopyTo(request.InputStream);

                UploadObject(request);
            }
            catch (Exception e)
            {
                throw new StorageException(string.Format("Failed to copy object. SourceUrl: {0}, DestinationUrl: {1}", sourceUri, destinationUri), e);
            }
        }

Usage Example

Exemplo n.º 1
0
        public void Should_Throw_StorageException_If_Given_Uri_Is_Not_Of_File_Scheme()
        {
            Mock<ICmsConfiguration> cmsConfigurationMock = new Mock<ICmsConfiguration>();
            Mock<ICmsStorageConfiguration> storageConfigurationMock = new Mock<ICmsStorageConfiguration>();

            cmsConfigurationMock.Setup(f => f.Storage).Returns(storageConfigurationMock.Object);
            storageConfigurationMock.Setup(f => f.ContentRoot).Returns("ftp://test.ftp.com/cms/content");
            storageConfigurationMock.Setup(f => f.ServiceType).Returns(StorageServiceType.Ftp);
            storageConfigurationMock.Setup(f => f.GetValue(It.Is<string>(s => s == "FtpRoot"))).Returns("ftp://test.ftp.com");
            storageConfigurationMock.Setup(f => f.GetValue(It.Is<string>(s => s == "FtpUserName"))).Returns("*****@*****.**");
            storageConfigurationMock.Setup(f => f.GetValue(It.Is<string>(s => s == "FtpPassword"))).Returns("psw123");
            storageConfigurationMock.Setup(f => f.GetValue(It.Is<string>(s => s == "UsePassiveMode"))).Returns("true");

            Uri httpUri = new Uri("http://www.google.com");
            FtpStorageService storageService = new FtpStorageService(cmsConfigurationMock.Object);

            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.FtpStorageService::CopyObject