Skip to content

Commit

Permalink
Replace unmaintained TUS library with newer one
Browse files Browse the repository at this point in the history
Newer library allows passing in an HttpClient, so we don't have to copy
cookies around.
  • Loading branch information
rmunn committed Aug 22, 2024
1 parent 0af5fb2 commit b3e258d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/LfMerge.Core.Tests/LfMerge.Core.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ See full changelog at https://github.com/sillsdev/LfMerge/blob/develop/CHANGELOG
<PackageReference Include="icu.net" Version="2.9.1-beta.1" GeneratePathProperty="true" />
<PackageReference Include="SIL.ReleaseTasks" Version="2.5.0" PrivateAssets="All" />
<PackageReference Include="SIL.TestUtilities" Version="12.0.0" />
<PackageReference Include="TusDotNetClient" Version="1.2.0" /> <!-- For uploading .zip files to LexBox in S/R tests -->
<PackageReference Include="BirdMessenger" Version="3.1.3" /> <!-- For uploading .zip files to LexBox in S/R tests -->
</ItemGroup>

<ItemGroup>
Expand Down
35 changes: 25 additions & 10 deletions src/LfMerge.Core.Tests/SRTestEnvironment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
using System.Net.Http;
using System.Net.Http.Json;
using System.Threading.Tasks;
using BirdMessenger;
using BirdMessenger.Collections;
using GraphQL;
using GraphQL.Client.Http;
using GraphQL.Client.Serializer.SystemTextJson;
using LfMerge.Core.FieldWorks;
using LfMerge.Core.Logging;
using NUnit.Framework;
using SIL.TestUtilities;
using TusDotNetClient;

namespace LfMerge.Core.Tests
{
Expand Down Expand Up @@ -164,17 +165,31 @@ public async Task ResetToEmpty(string code)
await Http.PostAsync(finishResetUrl, null);
}

public async Task UploadZip(string code, string zipPath)
public async Task TusUpload(Uri tusEndpoint, string path, string mimeType)
{
var file = new FileInfo(path);
if (!file.Exists) return;
var metadata = new MetadataCollection { { "filetype", mimeType } };
var createOpts = new TusCreateRequestOption {
Endpoint = tusEndpoint,
UploadLength = file.Length,
Metadata = metadata,
};
var createResponse = await Http.TusCreateAsync(createOpts);

// That doesn't actually upload the file; TusPatchAsync does the actual upload
var fileStream = new FileStream(file.FullName, FileMode.Open, FileAccess.Read);
var patchOpts = new TusPatchRequestOption {
FileLocation = createResponse.FileLocation,
Stream = fileStream,
};
await Http.TusPatchAsync(patchOpts);
}

public Task UploadZip(string code, string zipPath)
{
var sourceUrl = new Uri(LexboxUrl, $"/api/project/upload-zip/{code}");
var file = new FileInfo(zipPath);
var client = new TusClient();
// client.AdditionalHeaders["Authorization"] = $"Bearer {Jwt}"; // Once we set up for LexBox OAuth, we'll use Bearer auth instead
var cookies = Cookies.GetCookies(LexboxUrl);
var authCookie = cookies[".LexBoxAuth"].ToString();
client.AdditionalHeaders["cookie"] = authCookie;
var fileUrl = await client.CreateAsync(sourceUrl.AbsoluteUri, file.Length, ("filetype", "application/zip"));
await client.UploadAsync(fileUrl, file);
return TusUpload(sourceUrl, zipPath, "application/zip");
}

public async Task DownloadProjectBackup(string code, string destZipPath)
Expand Down

0 comments on commit b3e258d

Please sign in to comment.