Skip to content

Commit

Permalink
Merge pull request #16 from waifuvault/fix-download-album
Browse files Browse the repository at this point in the history
Fix download album, add id to file info
  • Loading branch information
nakedmcse authored Feb 3, 2025
2 parents eb3f912 + b3e60f2 commit 376922b
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 31 deletions.
62 changes: 44 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -379,30 +379,30 @@ This will respond with the new album object containing the added files.
```csharp
using Waifuvault;
var files = new List<string>() { "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<string>() { "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);
Expand Down Expand Up @@ -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<int>();
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<int>() {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<int>() { fileInfo.id };
var albumZip = await Waifuvault.Api.downloadAlbum("some-album-token", files);
Console.WriteLine(albumZip.Length);
```

Expand Down
4 changes: 3 additions & 1 deletion models.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
13 changes: 7 additions & 6 deletions tests/waifuvaultTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -608,15 +608,15 @@ public async Task TestRevokeAlbum()
}

[Fact]
public async Task TestAssociateFile()
public async Task TestAssociateFiles()
{
// Given
albumWithFilesReturn.Invocations.Clear();
Waifuvault.Api.customHttpClient = new HttpClient(albumWithFilesReturn.Object);
var files = new List<string>() { "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(),
Expand All @@ -631,15 +631,15 @@ public async Task TestAssociateFile()
}

[Fact]
public async Task TestDisassociateFile()
public async Task TestDisassociateFiles()
{
// Given
albumWithFilesReturn.Invocations.Clear();
Waifuvault.Api.customHttpClient = new HttpClient(albumWithFilesReturn.Object);
var files = new List<string>() { "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(),
Expand All @@ -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<int>();
var response = await Waifuvault.Api.downloadAlbum("test-album", files);

// Then
fileReturn.Protected().Verify("SendAsync",Times.Once(),
Expand Down
2 changes: 1 addition & 1 deletion waifuVault-csharp-api.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<PackageId>Waifuvault</PackageId>
<Version>1.4.1</Version>
<Version>1.4.2</Version>
<Authors>Walker Aldridge ([email protected])</Authors>
<Company>waifuvault.moe</Company>
<PackageTags>waifuvault;temp file hosting;waifu;vault</PackageTags>
Expand Down
9 changes: 4 additions & 5 deletions waifuvault.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public static async Task<AlbumResponse> getAlbum(string token, CancellationToken
return albumObj;
}

public static async Task<AlbumResponse> associateFile(string token, List<string> fileTokens, CancellationToken? ct = null)
public static async Task<AlbumResponse> associateFiles(string token, List<string> fileTokens, CancellationToken? ct = null)
{
var client = customHttpClient ?? new HttpClient();
var cts = new CancellationTokenSource();
Expand All @@ -133,7 +133,7 @@ public static async Task<AlbumResponse> associateFile(string token, List<string>
return JsonSerializer.Deserialize<AlbumResponse>(getResponseData) ?? new AlbumResponse();
}

public static async Task<AlbumResponse> disassociateFile(string token, List<string> fileTokens, CancellationToken? ct = null)
public static async Task<AlbumResponse> disassociateFiles(string token, List<string> fileTokens, CancellationToken? ct = null)
{
var client = customHttpClient ?? new HttpClient();
var cts = new CancellationTokenSource();
Expand Down Expand Up @@ -171,13 +171,12 @@ public static async Task<bool> revokeAlbum(string token, CancellationToken? ct =
return resp != null ? resp.success : false;
}

public static async Task<byte[]> downloadAlbum(string token, CancellationToken? ct = null)
public static async Task<byte[]> downloadAlbum(string token, List<int> files, CancellationToken? ct = null)
{
var client = customHttpClient ?? new HttpClient();
var cts = new CancellationTokenSource();
var url = $"{baseURL}/album/download/{token}";
var data = new List<int>();
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);
Expand Down

0 comments on commit 376922b

Please sign in to comment.