Skip to content

Commit

Permalink
Merge pull request #3 from CraftingDragon007/work-in-progress
Browse files Browse the repository at this point in the history
Changed to websocket based uploading functionality
  • Loading branch information
CraftingDragon007 authored Jan 24, 2024
2 parents 7808e49 + 682a888 commit 7cbe4ec
Show file tree
Hide file tree
Showing 7 changed files with 285 additions and 174 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/dotnet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
- name: Setup .NET
uses: actions/setup-dotnet@v2
with:
dotnet-version: 7.0.x
dotnet-version: 8.0.x
- name: Restore dependencies
run: dotnet restore
- name: Build
Expand Down
10 changes: 6 additions & 4 deletions KekUploadLibrary/KekUploadLibrary.csproj
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net7.0;netstandard2.1</TargetFrameworks>
<TargetFrameworks>net8.0;netstandard2.1</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<PackageId>CraftingDragon007.KekUploadLibrary</PackageId>
<PackageReadmeFile>README.md</PackageReadmeFile>
<Version>1.0.0.6</Version>
<Version>1.2.0</Version>
<Authors>CraftingDragon007</Authors>
<RepositoryUrl>https://github.com/CraftingDragon007/KekUploadLibrary</RepositoryUrl>
<RespositoryType>git</RespositoryType>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<GenerateTargetFrameworkAttribute>false</GenerateTargetFrameworkAttribute>
<PackageVersion>1.0.0.6</PackageVersion>
<PackageVersion>1.2.0</PackageVersion>
<DisableImplicitNamespaceImports>true</DisableImplicitNamespaceImports>
<ImplicitUsings>disable</ImplicitUsings>
</PropertyGroup>
Expand All @@ -25,8 +25,10 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.2" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="CraftingDragon007.SharpHash" Version="1.2.0" />
<PackageReference Include="System.Net.Http" Version="4.3.4" />

</ItemGroup>

</Project>
384 changes: 246 additions & 138 deletions KekUploadLibrary/UploadClient.cs

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions KekUploadLibrary/UploadItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ public Stream GetAsStream()
/// </summary>
/// <returns>The <see cref="byte"/> array of the to be uploaded file.</returns>
/// <exception cref="KekException">Is thrown when the <see cref="UploadType"/> is invalid.</exception>
/// <exception cref="KekException">Is thrown when trying to get byte[] of an item with <see cref="UploadType.Stream"/></exception>
public byte[] GetAsByteArray()
{
return UploadType switch
Expand Down
10 changes: 5 additions & 5 deletions KekUploadLibraryTest/KekUploadLibraryTest.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
Expand All @@ -10,10 +10,10 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.4.0" />
<PackageReference Include="NUnit" Version="3.13.3" />
<PackageReference Include="NUnit3TestAdapter" Version="4.3.1" />
<PackageReference Include="coverlet.collector" Version="3.2.0">
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="NUnit" Version="4.0.1" />
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
<PackageReference Include="coverlet.collector" Version="6.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down
50 changes: 25 additions & 25 deletions KekUploadLibraryTest/Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public void UploadTest1()
var client = new UploadClient(ApiBaseUrl, true);
if (!File.Exists("test.txt")) File.WriteAllText("test.txt", "KekUploadLibraryTest");
var result = client.Upload(new UploadItem("test.txt"));
Assert.True(result.Contains(ApiBaseUrl + "/d/"));
Assert.That(result.Contains(ApiBaseUrl + "/d/"));
}

/// <summary>
Expand All @@ -48,8 +48,8 @@ public void UploadTest1()
public void UploadTest2()
{
var client = new UploadClient(ApiBaseUrl, true);
var result = client.Upload(new UploadItem(new byte[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, "bin", "test"));
Assert.True(result.Contains(ApiBaseUrl + "/d/"));
var result = client.Upload(new UploadItem([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], "bin", "test"));
Assert.That(result.Contains(ApiBaseUrl + "/d/"));
}

/// <summary>
Expand All @@ -59,9 +59,9 @@ public void UploadTest2()
public void UploadTest3()
{
var client = new UploadClient(ApiBaseUrl, true);
var result = client.Upload(new UploadItem(new MemoryStream(new byte[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}), "bin",
var result = client.Upload(new UploadItem(new MemoryStream([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]), "bin",
"test"));
Assert.True(result.Contains(ApiBaseUrl + "/d/"));
Assert.That(result.Contains(ApiBaseUrl + "/d/"));
}

/// <summary>
Expand All @@ -71,8 +71,8 @@ public void UploadTest3()
public void UploadTestWithoutChunkHashing()
{
var client = new UploadClient(ApiBaseUrl, true, withChunkHashing: false);
var result = client.Upload(new UploadItem(new byte[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, "bin", "test"));
Assert.True(result.Contains(ApiBaseUrl + "/d/"));
var result = client.Upload(new UploadItem([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], "bin", "test"));
Assert.That(result.Contains(ApiBaseUrl + "/d/"));
}

/// <summary>
Expand Down Expand Up @@ -100,8 +100,8 @@ public void UploadTestWithCancellation()
thread.Start();
tokenSource.Cancel();
thread.Join();
Assert.True(thread.ThreadState == ThreadState.Stopped);
Assert.AreEqual("cancelled", result);
Assert.That(thread.ThreadState == ThreadState.Stopped);
Assert.That("cancelled", Is.EqualTo(result));
}

/// <summary>
Expand All @@ -113,7 +113,7 @@ public void UploadTestWithoutName()
var client = new UploadClient(ApiBaseUrl, false);
if (!File.Exists("test.txt")) File.WriteAllText("test.txt", "KekUploadLibraryTest");
var result = client.Upload(new UploadItem("test.txt"));
Assert.True(result.Contains(ApiBaseUrl + "/d/"));
Assert.That(result.Contains(ApiBaseUrl + "/d/"));
}

/// <summary>
Expand All @@ -124,7 +124,7 @@ public void DownloadTest()
{
var client = new DownloadClient();
client.Download(_downloadTestUrl, new DownloadItem("test2.txt"));
Assert.True(File.ReadAllText("test2.txt", Encoding.UTF8).Contains("KekUploadLibraryTest"));
Assert.That(File.ReadAllText("test2.txt", Encoding.UTF8).Contains("KekUploadLibraryTest"));
}

/// <summary>
Expand All @@ -139,7 +139,7 @@ public void UploadAndDownloadTest()
var result = client.Upload(new UploadItem(Encoding.UTF8.GetBytes(testString), "txt", "test"));
var client2 = new DownloadClient();
client2.Download(result, new DownloadItem("test3.txt"));
Assert.True(File.ReadAllText("test3.txt", Encoding.UTF8).Contains(testString));
Assert.That(File.ReadAllText("test3.txt", Encoding.UTF8).Contains(testString));
}

/// <summary>
Expand All @@ -154,7 +154,7 @@ public void UploadAndDownloadTestWithoutChunkHashing()
var result = client.Upload(new UploadItem(Encoding.UTF8.GetBytes(testString), "txt", "test"));
var client2 = new DownloadClient();
client2.Download(result, new DownloadItem("test4.txt"));
Assert.True(File.ReadAllText("test4.txt", Encoding.UTF8).Contains(testString));
Assert.That(File.ReadAllText("test4.txt", Encoding.UTF8).Contains(testString));
}

/// <summary>
Expand All @@ -172,11 +172,11 @@ public void UploadAndDownloadTestWithLargeFile()
Console.WriteLine($"Uploaded {args.CurrentChunkCount} Chunks of {args.TotalChunkCount}");
};
var result = client.Upload(new UploadItem(data, "bin", "test"));
Assert.True(result.Contains(ApiBaseUrl + "/d/"));
Assert.That(result.Contains(ApiBaseUrl + "/d/"));
var downloadClient = new DownloadClient();
downloadClient.Download(result, new DownloadItem("test.bin"));
var downloadedData = File.ReadAllBytes("test.bin");
Assert.AreEqual(data, downloadedData);
Assert.That(data, Is.EqualTo(downloadedData));
}

/// <summary>
Expand All @@ -191,7 +191,7 @@ public void ChunkedUploadStreamTest()
stream.Write("123456789"u8);
stream.Flush();
var url = stream.FinishUpload();
Assert.True(url.Contains(ApiBaseUrl + "/d/"));
Assert.That(url.Contains(ApiBaseUrl + "/d/"));
}

/// <summary>
Expand All @@ -208,7 +208,7 @@ public void ChunkedUploadStreamTestWithDownload()
var url = stream.FinishUpload();
var client = new DownloadClient();
client.Download(url, new DownloadItem("test1.txt"));
Assert.True(File.ReadAllText("test1.txt", Encoding.UTF8).Contains("KekUploadLibraryTest123456789"));
Assert.That(File.ReadAllText("test1.txt", Encoding.UTF8).Contains("KekUploadLibraryTest123456789"));
}

/// <summary>
Expand All @@ -223,7 +223,7 @@ public async Task ChunkedUploadStreamTestAsync()
await stream.WriteAsync("123456789"u8.ToArray());
await stream.FlushAsync();
var url = await stream.FinishUploadAsync();
Assert.True(url.Contains(ApiBaseUrl + "/d/"));
Assert.That(url.Contains(ApiBaseUrl + "/d/"));
}

/// <summary>
Expand All @@ -238,7 +238,7 @@ public async Task ChunkedUploadStreamTestWithoutChunkHashing()
await stream.WriteAsync("123456789"u8.ToArray());
await stream.FlushAsync();
var url = await stream.FinishUploadAsync();
Assert.True(url.Contains(ApiBaseUrl + "/d/"));
Assert.That(url.Contains(ApiBaseUrl + "/d/"));
}

/// <summary>
Expand All @@ -255,7 +255,7 @@ public async Task ChunkedUploadStreamTestWithDownloadAsync()
var url = await stream.FinishUploadAsync();
var client = new DownloadClient();
await client.DownloadAsync(url, new DownloadItem("test1.txt"));
Assert.True((await File.ReadAllTextAsync("test1.txt", Encoding.UTF8)).Contains("KekUploadLibraryTest123456789"));
Assert.That((await File.ReadAllTextAsync("test1.txt", Encoding.UTF8)).Contains("KekUploadLibraryTest123456789"));
}

/// <summary>
Expand All @@ -267,7 +267,7 @@ public async Task UploadTestAsync()
var client = new UploadClient(ApiBaseUrl, true);
if (!File.Exists("test.txt")) await File.WriteAllTextAsync("test.txt", "KekUploadLibraryTest");
var result = await client.UploadAsync(new UploadItem("test.txt"));
Assert.True(result.Contains(ApiBaseUrl + "/d/"));
Assert.That(result.Contains(ApiBaseUrl + "/d/"));
}

/// <summary>
Expand All @@ -278,7 +278,7 @@ public async Task DownloadTestAsync()
{
var client = new DownloadClient();
await client.DownloadAsync(_downloadTestUrl, new DownloadItem("test2.txt"));
Assert.True((await File.ReadAllTextAsync("test2.txt", Encoding.UTF8)).Contains("KekUploadLibraryTest"));
Assert.That((await File.ReadAllTextAsync("test2.txt", Encoding.UTF8)).Contains("KekUploadLibraryTest"));
}

/// <summary>
Expand All @@ -290,7 +290,7 @@ public async Task DownloadTestToStreamAsync()
var client = new DownloadClient();
await using var stream = new MemoryStream();
await client.DownloadAsync(_downloadTestUrl, new DownloadItem(stream));
Assert.True(Encoding.UTF8.GetString(stream.ToArray()).Contains("KekUploadLibraryTest"));
Assert.That(Encoding.UTF8.GetString(stream.ToArray()).Contains("KekUploadLibraryTest"));
}

/// <summary>
Expand All @@ -304,7 +304,7 @@ public async Task DownloadTestToByteArrayAsync()
var downloadItem = new DownloadItem(data);
await client.DownloadAsync(_downloadTestUrl, downloadItem);
data = downloadItem.Data;
Assert.NotNull(data);
Assert.True(Encoding.UTF8.GetString(data!).Contains("KekUploadLibraryTest"));
Assert.That(data, Is.Not.Null);
Assert.That(Encoding.UTF8.GetString(data!).Contains("KekUploadLibraryTest"));
}
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class Program
};
client.UploadStreamCreateEvent += (sender, e) => Console.WriteLine("Upload Stream created: " + e.UploadStreamId);
// Upload a file
client.Upload(new UploadItem("<Your File Path>"));
client.Upload(new UploadItem("<Your File Path>"), <Optional Cancellation Token>, <Optional wheter or not to use WebSockets for file uploading>);
// Upload a stream
using(var stream = new FileStream("<FilePath>", FileMode.Open))
{
Expand Down

0 comments on commit 7cbe4ec

Please sign in to comment.