Skip to content

Commit

Permalink
Merge pull request #887 from dlcs/fix/allow_sftp_spaces
Browse files Browse the repository at this point in the history
Allow SFTP origins containing spaces
  • Loading branch information
griffri authored Jul 31, 2024
2 parents 4d22cf2 + d2e3b0b commit f6d9aeb
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,58 @@ public async Task LoadAssetFromOrigin_ReturnsExpectedResponseWithNonStandardPort
result.ContentLength.Should().Be(stream.Length);
}

[Theory]
[InlineData("sftp://www.someuri.com/public_ftp/some folder/someId")]
[InlineData("sftp://www.someuri.com/public_ftp/some%20folder/someId")]
public async Task LoadAssetFromOrigin_ReturnsExpectedResponseWithSpace_OnSuccess(string originUri)
{
// Arrange
var content = "this is a test";

var stream = content.ToMemoryStream();

var basicCredentials = new BasicCredentials()
{
User = "correctTest",
Password = "correctPassword"
};

var customerOriginStrategy = new CustomerOriginStrategy
{
Strategy = OriginStrategyType.SFTP,
Id = "correctResponse"
};

A.CallTo(() =>
credentialsRepository.GetBasicCredentialsForOriginStrategy(
A<CustomerOriginStrategy>.That.Matches(a => a.Id == "correctResponse")))
.Returns(basicCredentials);

A.CallTo(() =>
sftpReader.RetrieveFile(
A<ConnectionInfo>.That.Matches(a => a.Username == basicCredentials.User),
A<string>._,
A<CancellationToken>._))
.Returns(stream);

// Act
var result = await sut.LoadAssetFromOrigin(assetId, originUri, customerOriginStrategy);

// Assert
A.CallTo(() =>
credentialsRepository.GetBasicCredentialsForOriginStrategy(
A<CustomerOriginStrategy>.That.Matches(a => a.Id == "correctResponse")))
.MustHaveHappened();
A.CallTo(() =>
sftpReader.RetrieveFile(
A<ConnectionInfo>.That.Matches(a => a.Username == basicCredentials.User),
A<string>.That.Matches(a => a == "/public_ftp/some folder/someId"),
A<CancellationToken>._))
.MustHaveHappened();
result.Stream.Should().NotBeNull().And.Subject.Should().NotBeSameAs(Stream.Null);
result.ContentLength.Should().Be(stream.Length);
}

[Fact]
public async Task LoadAssetFromOrigin_ReturnsNull_IfCallFailsToFindCredentials()
{
Expand Down
2 changes: 1 addition & 1 deletion src/protagonist/DLCS.Repository/SFTP/SftpReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public async Task<Stream> RetrieveFile(ConnectionInfo connectionInfo,
outputStream.Close();
}

logger.LogError(ex, "Error downloading SFTP file");
logger.LogError(ex, "Error downloading SFTP file from Host: {Hostname}, Path: {Path}", connectionInfo.Host, path);
throw;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ public async Task<OriginResponse> LoadAssetFromOrigin(AssetId assetId, string or
ConnectionInfo connectionInfo = GetConnectionInfo(originUri, port, basicCredentials);

try
{
var outputStream = await sftpReader.RetrieveFile(connectionInfo, originUri.AbsolutePath, cancellationToken);
{
var outputStream = await sftpReader.RetrieveFile(connectionInfo, originUri.LocalPath, cancellationToken);
return new OriginResponse(outputStream).WithContentLength(outputStream.Length);
}
catch (Exception ex)
Expand Down

0 comments on commit f6d9aeb

Please sign in to comment.