Skip to content

Commit

Permalink
feat: Use new class to also return fileName and ContentType of document
Browse files Browse the repository at this point in the history
  • Loading branch information
olvr-me committed Nov 5, 2024
1 parent a7d7859 commit 300fb9b
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 13 deletions.
12 changes: 6 additions & 6 deletions source/VMelnalksnis.PaperlessDotNet/Documents/DocumentClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,21 +112,21 @@ public async IAsyncEnumerable<Document<TFields>> GetAll<TFields>(int pageSize, [


/// <inheritdoc />
public async Task<Stream> Download(int id, bool original, CancellationToken cancellationToken = default)
public async Task<DocumentContent> Download(int id, bool original, CancellationToken cancellationToken = default)
{
var response = await _httpClient.GetAsync(original ? Routes.Documents.DownloadUri(id).ToString() :
Routes.Documents.DownloadOriginalUri(id).ToString());

await response.EnsureSuccessStatusCodeAsync().ConfigureAwait(false);

var content = response.Content;
var headers = response.Content.Headers;

var contentString = await content.ReadAsStringAsync().ConfigureAwait(false);

return await content.ReadAsStreamAsync().ConfigureAwait(false);
return new DocumentContent(
content: await response.Content.ReadAsStreamAsync().ConfigureAwait(false),
fileName: headers.ContentDisposition?.FileName ?? string.Empty,
contentType: headers.ContentType?.MediaType ?? string.Empty);
}


/// <inheritdoc />
public async Task<DocumentCreationResult> Create(DocumentCreation document)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ public interface IDocumentClient
/// <param name="id">The id of the document to download.</param>
/// <param name="original">Download the original document when an archived document is available.</param>
/// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
/// <returns>Downloaded file as stream.</returns>
Task<Stream> Download(int id, bool original, CancellationToken cancellationToken = default);
/// <returns>The content of the document.</returns>
Task<DocumentContent> Download(int id, bool original, CancellationToken cancellationToken = default);

/// <summary>Creates a new document.</summary>
/// <param name="document">The document to create.</param>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,20 +95,21 @@ public async Task Create()

updatedDocument.Title.Should().Be(update.Title);

using (var stream = await Client.Documents.Download(id, true))
var documentDownload = await Client.Documents.Download(id, true);

documentDownload.ContentType.Should().Be("text/plain");
documentDownload.FileName.Should().Contain("Lorem Ipsum");

using (var stream = documentDownload.Content)
{
Assert.IsNotNull(stream);

if (stream.CanSeek)
stream.Seek(0, SeekOrigin.Begin);

StreamReader reader = new StreamReader(stream);
string text = reader.ReadToEnd();

Assert.IsTrue(text.Contains("Lorem ipsum"));
}


await Client.Correspondents.Delete(correspondent.Id);
foreach (var tag in tags)
{
Expand Down

0 comments on commit 300fb9b

Please sign in to comment.