Skip to content

Commit

Permalink
feat(blazorui): change upload method of BitFileUpload to accept custo…
Browse files Browse the repository at this point in the history
…m UploadUrl #6968 (#6990)
  • Loading branch information
msynk authored Feb 27, 2024
1 parent 7efd194 commit 2264fae
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ public long? ChunkSize
/// <summary>
/// Starts Uploading the file(s).
/// </summary>
public async Task Upload(BitFileInfo? fileInfo = null)
public async Task Upload(BitFileInfo? fileInfo = null, string? uploadUrl = null)
{
if (Files is null) return;

Expand All @@ -242,12 +242,12 @@ public async Task Upload(BitFileInfo? fileInfo = null)
{
foreach (var file in Files)
{
await UploadOneFile(file);
await UploadOneFile(file, uploadUrl);
}
}
else
{
await UploadOneFile(fileInfo);
await UploadOneFile(fileInfo, uploadUrl);
}
}

Expand Down Expand Up @@ -363,8 +363,6 @@ protected override async Task OnAfterRenderAsync(bool firstRender)

private async Task HandleOnChange()
{
if (UploadUrl is null) return;

var url = AddQueryString(UploadUrl, UploadRequestQueryStrings);

Files = await _js.ResetFileUpload(UniqueId, _dotnetObj, _inputRef, url, UploadRequestHttpHeaders);
Expand All @@ -379,7 +377,7 @@ private async Task HandleOnChange()
}
}

private async Task UploadOneFile(BitFileInfo fileInfo)
private async Task UploadOneFile(BitFileInfo fileInfo, string? uploadUrl = null)
{
if (Files is null || fileInfo.Status == BitFileUploadStatus.NotAllowed) return;

Expand Down Expand Up @@ -437,7 +435,7 @@ private async Task UploadOneFile(BitFileInfo fileInfo)
await OnUploading.InvokeAsync(fileInfo);
}

await _js.UploadFile(UniqueId, from, to, fileInfo.Index, fileInfo.HttpHeaders);
await _js.UploadFile(UniqueId, from, to, fileInfo.Index, uploadUrl, fileInfo.HttpHeaders);
}

private async Task PauseUploadOneFile(int index)
Expand Down Expand Up @@ -588,20 +586,22 @@ private static string AddQueryString(string uri, string name, string value)
return AddQueryString(uri, new Dictionary<string, string> { { name, value } });
}

private static string AddQueryString(string uri, IReadOnlyDictionary<string, string> queryStrings)
private static string AddQueryString(string? url, IReadOnlyDictionary<string, string> queryStrings)
{
if (url.HasNoValue()) return string.Empty;

// this method is copied from:
// https://github.com/aspnet/HttpAbstractions/blob/master/src/Microsoft.AspNetCore.WebUtilities/QueryHelpers.cs

int anchorIndex = uri.IndexOf('#', StringComparison.InvariantCultureIgnoreCase);
string uriToBeAppended = uri;
int anchorIndex = url.IndexOf('#', StringComparison.InvariantCultureIgnoreCase);
string uriToBeAppended = url;
string? anchorText = null;

// If there is an anchor, then the query string must be inserted before its first occurrence.
if (anchorIndex != -1)
{
anchorText = uri[anchorIndex..];
uriToBeAppended = uri[..anchorIndex];
anchorText = url[anchorIndex..];
uriToBeAppended = url[..anchorIndex];
}

var queryIndex = uriToBeAppended.IndexOf('?', StringComparison.InvariantCultureIgnoreCase);
Expand Down
14 changes: 7 additions & 7 deletions src/BlazorUI/Bit.BlazorUI/Components/FileUpload/BitFileUpload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@
return files;
}

public static upload(id: string, from: number, to: number, index: number, headers: Record<string, string> = {}): void {
public static upload(id: string, from: number, to: number, index: number, uploadUrl: string, headers: Record<string, string> = {}): void {
const uploaders = this.fileUploaders.filter(u => u.id === id);

if (index === -1) {
uploaders.forEach(u => u.upload(from, to, headers));
uploaders.forEach(u => u.upload(from, to, uploadUrl, headers));
} else {
const uploader = uploaders.filter(u => u.index === index)[0];
uploader.upload(from, to, headers);
uploader.upload(from, to, uploadUrl, headers);
}
}

Expand Down Expand Up @@ -103,7 +103,7 @@ class BitFileUploader {
id: string;
dotnetReference: DotNetObject;
inputElement: HTMLInputElement;
uploadEndpointUrl: string;
uploadUrl: string;
headers: Record<string, string>;
index: number;

Expand All @@ -113,7 +113,7 @@ class BitFileUploader {
this.id = id;
this.dotnetReference = dotnetReference;
this.inputElement = inputElement;
this.uploadEndpointUrl = uploadEndpointUrl;
this.uploadUrl = uploadEndpointUrl;
this.headers = headers;
this.index = index;

Expand All @@ -133,7 +133,7 @@ class BitFileUploader {
};
}

upload(from: number, to: number, headers: Record<string, string>): void {
upload(from: number, to: number, uploadUrl: string, headers: Record<string, string>): void {
const files = this.inputElement.files;
if (files === null) return;

Expand All @@ -142,7 +142,7 @@ class BitFileUploader {
const chunk = file.slice(from, to);
data.append('file', chunk, file.name);

this.xhr.open('POST', this.uploadEndpointUrl, true);
this.xhr.open('POST', uploadUrl ? uploadUrl : this.uploadUrl, true);

Object.keys(this.headers).forEach(h => {
this.xhr.setRequestHeader(h, this.headers[h]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ internal static async Task<IJSObjectReference> SetupFileUploadDropzone(this IJSR
return await jsRuntime.InvokeAsync<BitFileInfo[]>("BitFileUpload.reset", id.ToString(), dotnetObjectReference, element, uploadAddress, uploadRequestHttpHeaders);
}

internal static async Task UploadFile(this IJSRuntime jsRuntime, Guid id, long from, long to, int index = -1, IReadOnlyDictionary<string, string>? httpHeaders = null)
internal static async Task UploadFile(this IJSRuntime jsRuntime, Guid id, long from, long to, int index, string? uploadUrl, IReadOnlyDictionary<string, string>? httpHeaders)
{
await (httpHeaders is null ? jsRuntime.InvokeVoidAsync("BitFileUpload.upload", id.ToString(), from, to, index)
: jsRuntime.InvokeVoidAsync("BitFileUpload.upload", id.ToString(), from, to, index, httpHeaders));
await (httpHeaders is null ? jsRuntime.InvokeVoidAsync("BitFileUpload.upload", id.ToString(), from, to, index, uploadUrl)
: jsRuntime.InvokeVoidAsync("BitFileUpload.upload", id.ToString(), from, to, index, uploadUrl, httpHeaders));
}

internal static async Task PauseFile(this IJSRuntime jsRuntime, Guid id, int index = -1)
Expand Down

0 comments on commit 2264fae

Please sign in to comment.