From b3e60f2b61ab8e9cf3099b7abe07985c95fdf9a7 Mon Sep 17 00:00:00 2001 From: Walker Aldridge Date: Sun, 2 Feb 2025 21:01:28 -0600 Subject: [PATCH] Fix download album, add id to file info --- README.md | 62 +++++++++++++++++++++++++----------- models.cs | 4 ++- tests/waifuvaultTests.cs | 13 ++++---- waifuVault-csharp-api.csproj | 2 +- waifuvault.cs | 9 +++--- 5 files changed, 59 insertions(+), 31 deletions(-) diff --git a/README.md b/README.md index e5ce442..b9e6e27 100644 --- a/README.md +++ b/README.md @@ -28,8 +28,8 @@ This API contains 19 interactions: 9. Create Album 10. Delete Album 11. Get Album -12. Associate File -13. Disassociate File +12. Associate Files +13. Disassociate Files 14. Share Album 15. Revoke Album 16. Download Album @@ -363,8 +363,8 @@ Console.WriteLine(album.name); Console.WriteLine(album.files); // Array of file objects ``` -### Associate File -To add files to an album, you use the `associateFile` function and supply the private album token and +### Associate Files +To add files to an album, you use the `associateFiles` function and supply the private album token and a list of file tokens. The function takes the following parameters: @@ -379,30 +379,30 @@ This will respond with the new album object containing the added files. ```csharp using Waifuvault; var files = new List() { "file-token-1", "file-token-2" }; -var album = await Waifuvault.Api.associateFile("some-album-token", files); +var album = await Waifuvault.Api.associateFiles("some-album-token", files); Console.WriteLine(album.token); Console.WriteLine(album.name); Console.WriteLine(album.files); // Array of file objects ``` -### Disassociate File -To remove files from an album, you use the `disassociateFile` function and supply the private album token and +### Disassociate Files +To remove files from an album, you use the `disassociateFiles` function and supply the private album token and a list of file tokens. The function takes the following parameters: -| Option | Type | Description | Required | Extra info | -|---------|----------------|-------------------------------------|----------|------------| -| `token` | `string` | The private token of the album | true | | -| `files` | `list[string]` | List of file tokens to add to album | true | | +| Option | Type | Description | Required | Extra info | +|---------|----------------|------------------------------------------|----------|------------| +| `token` | `string` | The private token of the album | true | | +| `files` | `list[string]` | List of file tokens to remove from album | true | | This will respond with the new album object with the files removed. ```csharp using Waifuvault; var files = new List() { "file-token-1", "file-token-2" }; -var album = await Waifuvault.Api.disassociateFile("some-album-token", files); +var album = await Waifuvault.Api.disassociateFiles("some-album-token", files); Console.WriteLine(album.token); Console.WriteLine(album.name); @@ -452,22 +452,48 @@ Console.WriteLine(resp); > **NOTE:** Once revoked, the URL for sharing is destroyed. If the album is later shared again, the URL issued will be different. ### Download Album -To download the contents of an album as a zip file, you use the `downloadAlbum` function and -supply a private or public token for the album. +To download the contents of an album as a zip file, you use the `downloadAlbum` function and supply a private or public +token for the album. + +You can also supply the file ids as an array to selectively download files. these ids can be found as part of the +get info response. The zip file will be returned as a buffer. The function takes the following parameters: -| Option | Type | Description | Required | Extra info | -|---------|----------------|------------------------------------------|----------|------------| -| `token` | `string` | The private or public token of the album | true | | +| Option | Type | Description | Required | Extra info | +|--------------|------------|------------------------------------------|----------|--------------------------------------------------------| +| `albumToken` | `string` | The private or public token of the album | true | | +| `files` | `number[]` | The ids of the files to download | false | the ids can be found as part of the file info response | + +download all files: + +```csharp +using Waifuvault; + +var files = new List(); +var albumZip = await Waifuvault.Api.downloadAlbum("some-album-token",files); +Console.WriteLine(albumZip.Length); +``` +selective files: ```csharp using Waifuvault; -var albumZip = await Waifuvault.Api.downloadAlbum("some-album-token"); +var files = new List() {1}; +var albumZip = await Waifuvault.Api.downloadAlbum("some-album-token", files); +Console.WriteLine(albumZip.Length); +``` + +get a file id from token: + +```csharp +using Waifuvault; +var fileInfo = await Waifuvault.Api.fileInfo("some-file-token",false); +var files = new List() { fileInfo.id }; +var albumZip = await Waifuvault.Api.downloadAlbum("some-album-token", files); Console.WriteLine(albumZip.Length); ``` diff --git a/models.cs b/models.cs index 706f7aa..ce4f451 100644 --- a/models.cs +++ b/models.cs @@ -100,13 +100,15 @@ public class FileResponse [JsonConverter(typeof(StringConverter))] public string? retentionPeriod { get; set; } public int? views { get; set; } + public int? id { get; set; } [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)] public AlbumInfoResponse? album { get; set; } public FileOptions? options { get; set; } - public FileResponse(string? token = null, string? url = null, string? bucket = null, int? views = null, string? retentionPeriod = null, AlbumInfoResponse? album = null, FileOptions? options = null) { + public FileResponse(string? token = null, int? id = null, string? url = null, string? bucket = null, int? views = null, string? retentionPeriod = null, AlbumInfoResponse? album = null, FileOptions? options = null) { this.token = token; + this.id = id; this.url = url; this.bucket = bucket; this.views = views; diff --git a/tests/waifuvaultTests.cs b/tests/waifuvaultTests.cs index 26045d6..bbf7a9d 100644 --- a/tests/waifuvaultTests.cs +++ b/tests/waifuvaultTests.cs @@ -448,7 +448,7 @@ public async Task TestDownload() { // Given fileReturn.Invocations.Clear(); Waifuvault.Api.customHttpClient = new HttpClient(fileReturn.Object); - var file = new Waifuvault.FileResponse("test-token","https://waifuvault.moe/f/something"); + var file = new Waifuvault.FileResponse("test-token",1,"https://waifuvault.moe/f/something"); // When var response = await Waifuvault.Api.getFile(file,"dangerWaifu"); @@ -608,7 +608,7 @@ public async Task TestRevokeAlbum() } [Fact] - public async Task TestAssociateFile() + public async Task TestAssociateFiles() { // Given albumWithFilesReturn.Invocations.Clear(); @@ -616,7 +616,7 @@ public async Task TestAssociateFile() var files = new List() { "file-1", "file-2" }; // When - var response = await Waifuvault.Api.associateFile("test-album", files); + var response = await Waifuvault.Api.associateFiles("test-album", files); // Then albumWithFilesReturn.Protected().Verify("SendAsync",Times.Once(), @@ -631,7 +631,7 @@ public async Task TestAssociateFile() } [Fact] - public async Task TestDisassociateFile() + public async Task TestDisassociateFiles() { // Given albumWithFilesReturn.Invocations.Clear(); @@ -639,7 +639,7 @@ public async Task TestDisassociateFile() var files = new List() { "file-1", "file-2" }; // When - var response = await Waifuvault.Api.disassociateFile("test-album", files); + var response = await Waifuvault.Api.disassociateFiles("test-album", files); // Then albumWithFilesReturn.Protected().Verify("SendAsync",Times.Once(), @@ -661,7 +661,8 @@ public async Task TestDownloadAlbum() Waifuvault.Api.customHttpClient = new HttpClient(fileReturn.Object); // When - var response = await Waifuvault.Api.downloadAlbum("test-album"); + var files = new List(); + var response = await Waifuvault.Api.downloadAlbum("test-album", files); // Then fileReturn.Protected().Verify("SendAsync",Times.Once(), diff --git a/waifuVault-csharp-api.csproj b/waifuVault-csharp-api.csproj index 04920b9..c74d3d2 100644 --- a/waifuVault-csharp-api.csproj +++ b/waifuVault-csharp-api.csproj @@ -2,7 +2,7 @@ Waifuvault - 1.4.1 + 1.4.2 Walker Aldridge (walker@waifuvault.moe) waifuvault.moe waifuvault;temp file hosting;waifu;vault diff --git a/waifuvault.cs b/waifuvault.cs index af135c5..b082b22 100644 --- a/waifuvault.cs +++ b/waifuvault.cs @@ -119,7 +119,7 @@ public static async Task getAlbum(string token, CancellationToken return albumObj; } - public static async Task associateFile(string token, List fileTokens, CancellationToken? ct = null) + public static async Task associateFiles(string token, List fileTokens, CancellationToken? ct = null) { var client = customHttpClient ?? new HttpClient(); var cts = new CancellationTokenSource(); @@ -133,7 +133,7 @@ public static async Task associateFile(string token, List return JsonSerializer.Deserialize(getResponseData) ?? new AlbumResponse(); } - public static async Task disassociateFile(string token, List fileTokens, CancellationToken? ct = null) + public static async Task disassociateFiles(string token, List fileTokens, CancellationToken? ct = null) { var client = customHttpClient ?? new HttpClient(); var cts = new CancellationTokenSource(); @@ -171,13 +171,12 @@ public static async Task revokeAlbum(string token, CancellationToken? ct = return resp != null ? resp.success : false; } - public static async Task downloadAlbum(string token, CancellationToken? ct = null) + public static async Task downloadAlbum(string token, List files, CancellationToken? ct = null) { var client = customHttpClient ?? new HttpClient(); var cts = new CancellationTokenSource(); var url = $"{baseURL}/album/download/{token}"; - var data = new List(); - var jsonData = JsonSerializer.Serialize(data); + var jsonData = JsonSerializer.Serialize(files); var content = new StringContent(jsonData, Encoding.UTF8, "application/json"); var fileResponse = await client.PostAsync(url, content, ct != null ? ct.Value : cts.Token); await checkError(fileResponse,true);