Skip to content

Update in attachment proccess#321

Open
Muhammad1Nouman wants to merge 15 commits into
cdmdotnet:bug/318-file-uploadsfrom
Muhammad1Nouman:master
Open

Update in attachment proccess#321
Muhammad1Nouman wants to merge 15 commits into
cdmdotnet:bug/318-file-uploadsfrom
Muhammad1Nouman:master

Conversation

@Muhammad1Nouman

Copy link
Copy Markdown

Hi Greg,

#318 (comment) is working fine and is uploading all types of file correctly. However, the problem occurs when your tired to download the uploaded files (from trello board) specifically pdf, excel and document files etc. In addition when ones tries to view .pdf, .docx files will not be able to view it on trello board.

However, if the WebApiClient file method GetContent is modified like this

private static HttpContent GetContent(WebApiRestRequest request)
{
    if (request.File != null)
    {
	var formData = new MultipartFormDataContent();
	foreach (var parameter in request.Parameters)
	{
		var content = new StringContent(parameter.Value.ToString());
		formData.Add(content, $"\"{parameter.Key}\"");
	}
       var fileStream = File.Open(request.File, FileMode.Open);
       var fileInfo = new FileInfo(request.File);
       formData.Add(new StreamContent(fileStream), "\"file\"", string.Format("\"{0}\"", "as" + fileInfo.Extension));
       formData.Add(new StringContent("mimeType"), "image/png");
       formData.Add(new StringContent("name"), request.FileName);
       TrelloConfiguration.Log.Debug($"\tContent: {formData}");
       return formData;
  }

 if (request.Body == null) return null;

     var body = TrelloConfiguration.Serializer.Serialize(request.Body);
     var jsonContent = new StringContent(body);
     jsonContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
     TrelloConfiguration.Log.Debug($"\tContent: {body}");
     return jsonContent;
}

This code is still be able to upload all types of files, and the user will also be able to download file in original format, and can view it on the trello board as well.

@gregsdennis gregsdennis changed the base branch from master to bug/318-file-uploads December 19, 2019 17:45
Comment thread Manatee.Trello/Rest/WebApiClient.cs Outdated
Comment thread Manatee.Trello/Rest/WebApiClient.cs Outdated
/// <param name="filePath">The path of the file to attach.</param>
/// <param name="ct">(Optional) A cancellation token for async processing.</param>
/// <returns>The newly created <see cref="IBoardBackground"/>.</returns>
Task<IBoardBackground> Add(string filePath, CancellationToken ct = default);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of changing this, can you just add the new method, please? This will allow us to avoid a breaking change.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright, its seems to be a great idea

Comment thread Manatee.Trello/Rest/RestFile.cs Outdated
/// The file path
/// </summary>
public byte[] ContentBytes { get; set; }
public string FilePath { get; set; }

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similarly here: just add the property.

Comment thread Manatee.Trello/Rest/WebApiClient.cs Outdated
TrelloConfiguration.Log.Debug($"\tContent: {formData}");

return formData;
var fileStream = File.Open(request.File, FileMode.Open);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here you will need to check whether raw bytes have been uploaded or just a filename; then act appropriately.

@gregsdennis gregsdennis left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I'll move forward with this, but I don't want to lose existing functionality. Just a few suggestions on how to add this without creating breaking changes.

Comment thread Manatee.Trello/Rest/WebApiClient.cs Outdated
var fileStream = File.Open(request.File, FileMode.Open);
var fileInfo = new FileInfo(request.File);
formData.Add(new StreamContent(fileStream), "\"file\"", string.Format("\"{0}\"", "as" + fileInfo.Extension));
formData.Add(new StringContent("mimeType"), "image/png");

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like to have some simple functionality that attempts to infer the mime type from the file extension. Not looking for an extensive list; more like a "top 10."

@gregsdennis gregsdennis left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some comments. Also tabs, not spaces, please.

var sourceCard = await TestEnvironment.Current.BuildCard();
var jpeg = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Files/smallest-jpeg.jpg");
await sourceCard.Attachments.Add(File.ReadAllBytes(jpeg), "smallest-jpeg.jpg");
var pdf = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Files/smallest-jpeg.jpg");

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer not to change the client tests since they're associated with specific issues. Can you create a new test that uses the new code path, please?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright

Comment thread Manatee.Trello/Contracts/IAttachmentCollection.cs Outdated
Comment thread Manatee.Trello/Manatee.Trello.csproj Outdated
/// <param name="name">A name for the sticker.</param>
/// <param name="ct">(Optional) A cancellation token for async processing.</param>
/// <returns>The <see cref="ISticker"/> generated by Trello.</returns>
Task<ISticker> Add(string filePath, string name, CancellationToken ct = default);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add an override.

Comment thread Manatee.Trello/Rest/WebApiClient.cs Outdated
{
var rf = (RestFile)parameter.Value;
request.AddFile(parameter.Key, rf.ContentBytes, rf.FileName);
request.AddFile(parameter.Key, rf.FilePath, rf.FileName);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to check which attachment method we're using here. I don't want to completely replace the existing bytes method.

Comment thread Manatee.Trello/Rest/WebApiClient.cs Outdated
var fileStream = File.Open(request.FilePath, FileMode.Open);
var fileInfo = new FileInfo(request.FilePath);
formData.Add(new StreamContent(fileStream), "\"file\"", string.Format("\"{0}\"", "as" + fileInfo.Extension));
formData.Add(new StringContent("mimeType"), "image/png");

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd still like to have some basic mime type checking rather than always assuming png.

Comment on lines +180 to +181
if (request.FilePath != null)
{

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here I am verifying, whether the file is uploaded or byte array.

Comment on lines +27 to +29

if (rf.FilePath != null)
{

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same reason as above

Comment on lines +36 to +40
public void AddFile(string key, string filePath, string fileName)
{
FilePath = filePath;
FileName = fileName;
}

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here, overloading the of AddFile method for assigning file location as well

@gregsdennis

Copy link
Copy Markdown
Collaborator

I don't know what your editor is doing, but it keeps putting spaces in instead of tabs. The alignment is all off.

Also, for the build, the card attachment test is failing. I'd like to also see a test that uses your method to attach a file. The BasicData test has been resolved on the current master branch, so don't worry about that. I'll rebase the target branch so that it's fixed.

@gregsdennis

Copy link
Copy Markdown
Collaborator
Trello has reported an error with the request: {"message":"Must provide either url or file","error":"VALIDATOR_INVALID_VALUE"}

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