Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix file upload filenames not being set, allow setting MIME type #62

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

zotanmew
Copy link

This fixes uploaded files not getting the correct file_name set, and allows setting the MIME type (so that e.g. image thumbnails are generated as appropriate).

@olsh
Copy link
Owner

olsh commented Jan 29, 2025

Thank you for the contribution.

@zotanmew zotanmew requested a review from olsh January 29, 2025 12:46
@olsh
Copy link
Owner

olsh commented Feb 3, 2025

Well, after some thinking, it seems like we should refactor existing files uploading instead of implementing a new method.
We should reuse existing method PostFormAsync

public async Task<HttpResponseMessage> PostFormAsync(
string resource,
IEnumerable<KeyValuePair<string, string>> parameters,
IEnumerable<ByteArrayContent> files,
CancellationToken cancellationToken = default)
{
using (var multipartFormDataContent = new MultipartFormDataContent())
{
foreach (var keyValuePair in parameters)
{
multipartFormDataContent.Add(new StringContent(keyValuePair.Value), $"\"{keyValuePair.Key}\"");
}
foreach (var file in files)
{
multipartFormDataContent.Add(file, Guid.NewGuid().ToString(), Guid.NewGuid().ToString());
}
return await _httpClient.PostAsync(resource, multipartFormDataContent, cancellationToken)
.ConfigureAwait(false);
}

But we need to improve it a bit.
We should:

  1. Add mime detection to the existing PostFormAsync
  2. Replace IEnumerable<ByteArrayContent> files with a custom UploadFile model that contains file name and content.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants