Skip to content

Commit

Permalink
Update API to handle one time download
Browse files Browse the repository at this point in the history
  • Loading branch information
nakedmcse committed Apr 2, 2024
1 parent 2f09f0b commit b0b0be8
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 15 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ To Upload a file, use the `uploadFile` function. This function takes the followi
| `hideFilename` | `boolean` | If true, then the uploaded filename won't appear in the URL | false | Defaults to `false` |
| `password` | `string` | If set, then the uploaded file will be encrypted | false | |
| `ct` | `canceltoken`| An optional cancellation token that can be passed in | false | Standard cancellation token |
| `oneTimeDownload` | `boolean` | if supplied, the file will be deleted as soon as it is accessed | false | |

Using a URL:

Expand Down
16 changes: 8 additions & 8 deletions tests/waifuvaultTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ private void setupMocks() {
)
.ReturnsAsync(new HttpResponseMessage(){
StatusCode = System.Net.HttpStatusCode.OK,
Content = new StringContent("{\"url\":\"https://waifuvault.moe/f/something\", \"token\":\"test-token\", \"protected\":false, \"retentionPeriod\":100}")
Content = new StringContent("{\"url\":\"https://waifuvault.moe/f/something\", \"token\":\"test-token\", \"retentionPeriod\":100, \"options\":{\"protected\":false, \"hideFilename\":false, \"oneTimeDownload\":false}}")
})
.Verifiable();

Expand All @@ -38,7 +38,7 @@ private void setupMocks() {
)
.ReturnsAsync(new HttpResponseMessage(){
StatusCode = System.Net.HttpStatusCode.OK,
Content = new StringContent("{\"url\":\"https://waifuvault.moe/f/something\", \"token\":\"test-token\", \"protected\":true, \"retentionPeriod\":100}")
Content = new StringContent("{\"url\":\"https://waifuvault.moe/f/something\", \"token\":\"test-token\", \"retentionPeriod\":100, \"options\":{\"protected\":true, \"hideFilename\":false, \"oneTimeDownload\":false}}")
})
.Verifiable();

Expand All @@ -50,7 +50,7 @@ private void setupMocks() {
)
.ReturnsAsync(new HttpResponseMessage(){
StatusCode = System.Net.HttpStatusCode.OK,
Content = new StringContent("{\"url\":\"https://waifuvault.moe/f/something\", \"token\":\"test-token\", \"protected\":false, \"retentionPeriod\":\"10 minutes\"}")
Content = new StringContent("{\"url\":\"https://waifuvault.moe/f/something\", \"token\":\"test-token\", \"retentionPeriod\":\"10 minutes\", \"options\":{\"protected\":false, \"hideFilename\":false, \"oneTimeDownload\":false}}")
})
.Verifiable();

Expand Down Expand Up @@ -107,7 +107,7 @@ public async Task TestURLUpload() {
ItExpr.IsAny<CancellationToken>());
Assert.Equal("https://waifuvault.moe/f/something",response.url);
Assert.Equal("test-token", response.token);
Assert.Equal(false,response.fileprotected);
Assert.Equal(false,response.options.fileprotected);

Check warning on line 110 in tests/waifuvaultTests.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.

Check warning on line 110 in tests/waifuvaultTests.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.
Assert.Equal("100", response.retentionPeriod);
}

Expand All @@ -127,7 +127,7 @@ public async Task TestFileUpload() {
ItExpr.IsAny<CancellationToken>());
Assert.Equal("https://waifuvault.moe/f/something",response.url);
Assert.Equal("test-token", response.token);
Assert.Equal(false,response.fileprotected);
Assert.Equal(false,response.options.fileprotected);

Check warning on line 130 in tests/waifuvaultTests.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.

Check warning on line 130 in tests/waifuvaultTests.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.
Assert.Equal("100", response.retentionPeriod);
}

Expand Down Expand Up @@ -169,7 +169,7 @@ public async Task TestBufferUpload() {
ItExpr.IsAny<CancellationToken>());
Assert.Equal("https://waifuvault.moe/f/something",response.url);
Assert.Equal("test-token", response.token);
Assert.Equal(false,response.fileprotected);
Assert.Equal(false,response.options.fileprotected);
Assert.Equal("100", response.retentionPeriod);
}

Expand All @@ -188,7 +188,7 @@ public async Task TestFileInfo() {
ItExpr.IsAny<CancellationToken>());
Assert.Equal("https://waifuvault.moe/f/something",response.url);
Assert.Equal("test-token", response.token);
Assert.Equal(false,response.fileprotected);
Assert.Equal(false,response.options.fileprotected);
Assert.Equal("10 minutes", response.retentionPeriod);
}

Expand Down Expand Up @@ -227,7 +227,7 @@ public async Task TestUpdateInfo() {
ItExpr.IsAny<CancellationToken>());
Assert.Equal("https://waifuvault.moe/f/something",response.url);
Assert.Equal("test-token", response.token);
Assert.Equal(true,response.fileprotected);
Assert.Equal(true,response.options.fileprotected);
Assert.Equal("100", response.retentionPeriod);
}

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.2.6</Version>
<Version>1.2.8</Version>
<Authors>Walker Aldridge (walker@waifuvault.moe)</Authors>
<Company>waifuvault.moe</Company>
<PackageTags>waifuvault;temp file hosting;waifu;vault</PackageTags>
Expand Down
27 changes: 21 additions & 6 deletions waifuvault.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,9 @@ public class FileUpload
public string? expires { get; set; }
public string? password { get; set; }
public bool? hidefilename { get; set; }
public bool? oneTimeDownload { get; set; }

public FileUpload(string target, string? expires = null, string? password = null, bool? hidefilename = null) {
public FileUpload(string target, string? expires = null, string? password = null, bool? hidefilename = null, bool? oneTimeDownload = null) {
if(target.ToLower().StartsWith("http://") || target.ToLower().StartsWith("https://"))
{
this.url = target;
Expand All @@ -150,14 +151,16 @@ public FileUpload(string target, string? expires = null, string? password = null
this.expires = expires;
this.password = password;
this.hidefilename = hidefilename;
this.oneTimeDownload = oneTimeDownload;
}

public FileUpload(byte[] buffer, string filename, string? expires = null, string? password = null, bool? hidefilename = null) {
public FileUpload(byte[] buffer, string filename, string? expires = null, string? password = null, bool? hidefilename = null, bool? oneTimeDownload = null) {
this.buffer = buffer;
this.filename = filename;
this.expires = expires;
this.password = password;
this.hidefilename = hidefilename;
this.oneTimeDownload = oneTimeDownload;
}

public string buildURL(string baseURL) {
Expand All @@ -171,24 +174,36 @@ public string buildURL(string baseURL) {
if(this.hidefilename.HasValue) {
urlBuilder.Add($"hide_filename={this.hidefilename.Value.ToString().ToLower()}");
}
if(this.oneTimeDownload.HasValue) {
urlBuilder.Add($"one_time_download={this.oneTimeDownload.Value.ToString().ToLower()}");
}
return $"{baseURL}?"+String.Join('&',urlBuilder);
}
}

public class FileOptions
{
public bool? hideFilename { get; set; }
public bool? oneTimeDownload { get; set; }
[JsonPropertyName("protected")]
public bool? fileprotected { get; set; }

}

public class FileResponse
{
public string? token { get; set; }
public string? url { get; set; }
[JsonPropertyName("protected")]
public bool? fileprotected { get; set; }

[JsonConverter(typeof(StringConverter))]
public string? retentionPeriod { get; set; }
public FileOptions? options { get; set; }

public FileResponse(string? token = null, string? url = null, bool? fileprotected = null, string? retentionPeriod = null) {
public FileResponse(string? token = null, string? url = null, string? retentionPeriod = null, FileOptions? options = null) {
this.token = token;
this.url = url;
this.fileprotected = fileprotected;
this.retentionPeriod = retentionPeriod;
this.options = options;
}
}

Expand Down

0 comments on commit b0b0be8

Please sign in to comment.