Skip to content

Commit

Permalink
First tests to implement downloading a file (WIP)
Browse files Browse the repository at this point in the history
  • Loading branch information
olvr-me committed Oct 26, 2024
1 parent 8c90362 commit a16423d
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 0 deletions.
14 changes: 14 additions & 0 deletions source/VMelnalksnis.PaperlessDotNet/Documents/DocumentClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Json;
Expand Down Expand Up @@ -109,6 +110,19 @@ public async IAsyncEnumerable<Document<TFields>> GetAll<TFields>(int pageSize, [
return await GetCore<Document<TFields>>(id, cancellationToken).ConfigureAwait(false);
}


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

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

return await response.Content.ReadAsStreamAsync();
}


/// <inheritdoc />
public async Task<DocumentCreationResult> Create(DocumentCreation document)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// See LICENSE file in the project root for full license information.

using System.Collections.Generic;
using System.IO;
using System.Threading;
using System.Threading.Tasks;

Expand Down Expand Up @@ -48,6 +49,13 @@ public interface IDocumentClient
/// <returns>The document with the specified id if it exists; otherwise <see langword="null"/>.</returns>
Task<Document<TFields>?> Get<TFields>(int id, CancellationToken cancellationToken = default);

/// <summary>Downloads the file of the document.</summary>
/// <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);

/// <summary>Creates a new document.</summary>
/// <param name="document">The document to create.</param>
/// <returns>Result of creating the document.</returns>
Expand Down
3 changes: 3 additions & 0 deletions source/VMelnalksnis.PaperlessDotNet/Routes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ internal static class Documents

internal static Uri IdUri(int id) => new($"{_documents}{id}/", Relative);

internal static Uri DownloadUri(int id) => new($"{_documents}{id}/download", Relative);
internal static Uri DownloadOriginalUri(int id) => new($"{_documents}{id}/download?original=true", Relative);

internal static Uri PagedUri(int pageSize) => new($"{_documents}?{_pageSize}={pageSize}", Relative);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

Expand Down Expand Up @@ -94,6 +95,19 @@ public async Task Create()

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


using (var stream = await Client.Documents.Download(id, true))
{
Assert.IsNotNull(stream);
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 a16423d

Please sign in to comment.