Skip to content
This repository has been archived by the owner on May 20, 2022. It is now read-only.

Commit

Permalink
Enforce filename for multi-file uploads
Browse files Browse the repository at this point in the history
  • Loading branch information
drmathias committed Oct 5, 2020
1 parent 9322f2d commit 4257149
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/Sia.Skynet/Sia.Skynet.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
</PropertyGroup>

<PropertyGroup>
<Version>2.0.0</Version>
<Version>2.0.1</Version>
<Authors>Adam Shirt</Authors>
<Description>Library for integrating Sia Skynet into your applications</Description>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
Expand Down
11 changes: 6 additions & 5 deletions src/Sia.Skynet/SkynetWebPortal.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using Microsoft.Extensions.FileProviders;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
Expand All @@ -7,7 +8,6 @@
using System.Text.Json;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Microsoft.Extensions.FileProviders;

namespace Sia.Skynet
{
Expand Down Expand Up @@ -78,8 +78,9 @@ public async Task<Skylink> UploadFiles(IReadOnlyCollection<UploadItem> items, st
if (items is null) throw new ArgumentNullException(nameof(items));
if (items.Count == 0) throw new ArgumentException("Sequence must not be empty", nameof(items));

var fileNameIsSet = !string.IsNullOrWhiteSpace(fileName);
if (fileNameIsSet && !Regex.IsMatch(fileName, @"^[0-9a-zA-Z-._]+$"))
if (string.IsNullOrWhiteSpace(fileName))
fileName = DateTimeOffset.UtcNow.ToString("yyyy-MM-dd-HH-mm-ss");
else if (!Regex.IsMatch(fileName, @"^[0-9a-zA-Z-._]+$"))
throw new ArgumentException("File name can only contain alphanumeric characters, periods, underscores or hyphen-minus", nameof(fileName));

using var multiPartContent = new MultipartFormDataContent();
Expand All @@ -96,7 +97,7 @@ public async Task<Skylink> UploadFiles(IReadOnlyCollection<UploadItem> items, st
multiPartContent.Add(fileContent);
}

var response = await _httpClient.PostAsync($"/skynet/skyfile{(fileNameIsSet ? $"?filename={fileName}" : "")}", multiPartContent).ConfigureAwait(false);
var response = await _httpClient.PostAsync($"/skynet/skyfile?filename={fileName}", multiPartContent).ConfigureAwait(false);
response.EnsureSuccessStatusCode();

var contentStream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false);
Expand Down
5 changes: 3 additions & 2 deletions test/Sia.Skynet.Tests/UploadFilesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using System.Net;
using System.Net.Http;
using System.Text.Json;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;

Expand Down Expand Up @@ -218,15 +219,15 @@ public async Task UploadFiles_RequestUri_WithoutFileName(string fileName)
await webPortalClient.UploadFiles(new UploadItem[] { new UploadItem(fileMock.Object) }, fileName);

// Assert
var expectedUri = new Uri($"https://siasky.net/skynet/skyfile");
var expectedUriPattern = @"^https:\/\/siasky\.net\/skynet\/skyfile\?filename=\d{4}-\d{2}-\d{2}-\d{2}-\d{2}-\d{2}$";
handlerMock
.Protected()
.Verify(
"SendAsync",
Times.Exactly(1),
ItExpr.Is<HttpRequestMessage>(req =>
req.Method == HttpMethod.Post &&
req.RequestUri == expectedUri
Regex.IsMatch(req.RequestUri.OriginalString, expectedUriPattern)
),
ItExpr.IsAny<CancellationToken>()
);
Expand Down

0 comments on commit 4257149

Please sign in to comment.