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

Update in attachment proccess #321

Open
wants to merge 15 commits into
base: bug/318-file-uploads
Choose a base branch
from
Open
5 changes: 3 additions & 2 deletions Manatee.Trello.IntegrationTests/ClientTestsOfTheNew.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ public async Task Issue205_CopyCardWithAttachment()
var list = board.Lists.Last();
var cards = list.Cards;
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
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
Author

Choose a reason for hiding this comment

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

Alright

await sourceCard.Attachments.AddAttachment(pdf, "smallest-jpeg.jpg");


await TestEnvironment.RunClean(async () =>
{
Expand Down
3 changes: 1 addition & 2 deletions Manatee.Trello.IntegrationTests/MemberTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,7 @@ public async Task BoardBackgrounds()
await me.BoardBackgrounds.Refresh(true);

var imagePath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Files/logo.png");
var data = File.ReadAllBytes(imagePath);
var newBackground = await me.BoardBackgrounds.Add(data);
var newBackground = await me.BoardBackgrounds.Add(imagePath);

newBackground.Should().NotBeNull();
newBackground.Type.Should().Be(BoardBackgroundType.Custom);
Expand Down
21 changes: 12 additions & 9 deletions Manatee.Trello/AttachmentCollection.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -48,16 +49,18 @@ public async Task<IAttachment> Add(string url, string name = null, CancellationT
return new Attachment(newData, OwnerId, Auth);
}

/// <summary>
/// Adds an attachment to a card by uploading data.
/// </summary>
/// <param name="data">The byte data of the file to attach.</param>
/// <param name="name">A name for the attachment.</param>
/// <param name="ct">(Optional) A cancellation token for async processing.</param>
/// <returns>The <see cref="IAttachment"/> generated by Trello.</returns>
public async Task<IAttachment> Add(byte[] data, string name, CancellationToken ct = default)
/// <summary>
/// Adds an attachment to a card by uploading data.
/// </summary>
/// <param name="filePath">The path of the file to attach.</param>
/// <param name="name">A name for the attachment.</param>
/// <param name="ct">(Optional) A cancellation token for async processing.</param>
/// <returns>The <see cref="IAttachment"/> generated by Trello.</returns>
public async Task<IAttachment> AddAttachment(string filePath, string name, CancellationToken ct = default)
{
var parameters = new Dictionary<string, object> {{RestFile.ParameterKey, new RestFile {ContentBytes = data, FileName = name}}};
if (!File.Exists(filePath)) throw new Exception(filePath + " Invalid file path");

var parameters = new Dictionary<string, object> {{RestFile.ParameterKey, new RestFile {FilePath = filePath, FileName = name}}};
var endpoint = EndpointFactory.Build(EntityRequestType.Card_Write_AddAttachment, new Dictionary<string, object> {{"_id", OwnerId}});
var newData = await JsonRepository.Execute<IJsonAttachment>(Auth, endpoint, ct, parameters);

Expand Down
8 changes: 5 additions & 3 deletions Manatee.Trello/BoardBackgroundCollection.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Manatee.Trello.Internal.DataAccess;
Expand All @@ -21,12 +22,13 @@ internal BoardBackgroundCollection(Func<string> getOwnerId, TrelloAuthorization
/// <summary>
/// Adds a custom board background.
/// </summary>
/// <param name="data">The byte data of the file to attach.</param>
/// <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>
public async Task<IBoardBackground> Add(byte[] data, CancellationToken ct = default)
public async Task<IBoardBackground> Add(string filePath, CancellationToken ct = default)
{
var parameters = new Dictionary<string, object> {{RestFile.ParameterKey, new RestFile {ContentBytes = data}}};
if (!File.Exists(filePath)) throw new Exception(filePath + " Invalid file path");
var parameters = new Dictionary<string, object> {{RestFile.ParameterKey, new RestFile {FilePath = filePath , FileName = "BoardBackground"} }};
var endpoint = EndpointFactory.Build(EntityRequestType.Member_Write_AddBoardBackground, new Dictionary<string, object> {{"_id", OwnerId}});
var newData = await JsonRepository.Execute<IJsonBoardBackground>(Auth, endpoint, ct, parameters);

Expand Down
19 changes: 10 additions & 9 deletions Manatee.Trello/Contracts/IAttachmentCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@ public interface IAttachmentCollection : IReadOnlyCollection<IAttachment>
/// <returns>The <see cref="IAttachment"/> generated by Trello.</returns>
Task<IAttachment> Add(string url, string name = null, CancellationToken ct = default);

/// <summary>
/// Adds an attachment to a card by uploading data.
/// </summary>
/// <param name="data">The byte data of the file to attach.</param>
/// <param name="name">A name for the attachment.</param>
/// <param name="ct">(Optional) A cancellation token for async processing.</param>
/// <returns>The <see cref="IAttachment"/> generated by Trello.</returns>
Task<IAttachment> Add(byte[] data, string name, CancellationToken ct = default);
}
/// <summary>
/// Adds an attachment to a card by uploading data.
/// </summary>
/// <param name="filepath">The path of the file to attach.</param>
/// <param name="name">A name for the attachment.</param>
/// <param name="ct">(Optional) A cancellation token for async processing.</param>
/// <returns>The <see cref="IAttachment"/> generated by Trello.</returns>
Task<IAttachment> AddAttachment(string filepath, string name, CancellationToken ct = default);
Muhammad1Nouman marked this conversation as resolved.
Show resolved Hide resolved

}
}
14 changes: 7 additions & 7 deletions Manatee.Trello/Contracts/IBoardBackgroundCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ namespace Manatee.Trello
/// </summary>
public interface IBoardBackgroundCollection : IReadOnlyCollection<IBoardBackground>
{
/// <summary>
/// Adds a custom board background.
/// </summary>
/// <param name="data">The byte data 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(byte[] data, CancellationToken ct = default);
/// <summary>
/// Adds a custom board background.
/// </summary>
/// <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
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
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

}
}
16 changes: 8 additions & 8 deletions Manatee.Trello/Contracts/IMemberStickerCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ namespace Manatee.Trello
/// </summary>
public interface IMemberStickerCollection
{
/// <summary>
/// Adds a <see cref="ISticker"/> to a <see cref="IMember"/>'s custom sticker set by uploading data.
/// </summary>
/// <param name="data">The byte data of the file to attach.</param>
/// <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(byte[] data, string name, CancellationToken ct = default);
/// <summary>
/// Adds a <see cref="ISticker"/> to a <see cref="IMember"/>'s custom sticker set by uploading data.
/// </summary>
/// <param name="filePath">The path of the file to attach.</param>
/// <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
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.

}
}
21 changes: 12 additions & 9 deletions Manatee.Trello/MemberStickerCollection.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Manatee.Trello.Internal.DataAccess;
Expand All @@ -16,16 +17,18 @@ public class MemberStickerCollection : ReadOnlyStickerCollection, IMemberSticker
internal MemberStickerCollection(Func<string> getOwnerId, TrelloAuthorization auth)
: base(getOwnerId, auth) { }

/// <summary>
/// Adds a <see cref="ISticker"/> to a <see cref="IMember"/>'s custom sticker set by uploading data.
/// </summary>
/// <param name="data">The byte data of the file to attach.</param>
/// <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>
public async Task<ISticker> Add(byte[] data, string name, CancellationToken ct = default)
/// <summary>
/// Adds a <see cref="ISticker"/> to a <see cref="IMember"/>'s custom sticker set by uploading data.
/// </summary>
/// <param name="filePath">The path of the file to attach.</param>
/// <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>
public async Task<ISticker> Add(string filePath, string name, CancellationToken ct = default)
{
var parameters = new Dictionary<string, object> { { RestFile.ParameterKey, new RestFile { ContentBytes = data, FileName = name } } };
if (!File.Exists(filePath)) throw new Exception(filePath + " Invalid file path");

var parameters = new Dictionary<string, object> { { RestFile.ParameterKey, new RestFile { FilePath = filePath, FileName = name } } };
var endpoint = EndpointFactory.Build(EntityRequestType.Card_Write_AddAttachment, new Dictionary<string, object> { { "_id", OwnerId } });
var newData = await JsonRepository.Execute<IJsonSticker>(Auth, endpoint, ct, parameters);

Expand Down
4 changes: 2 additions & 2 deletions Manatee.Trello/Rest/RestFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ public class RestFile
/// </summary>
public string FileName { get; set; }
/// <summary>
/// The file data
/// The file path
/// </summary>
public byte[] ContentBytes { get; set; }
public string FilePath { get; set; }
Copy link
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.

}
}
13 changes: 9 additions & 4 deletions Manatee.Trello/Rest/WebApiClient.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
Expand Down Expand Up @@ -176,11 +177,15 @@ private static HttpContent GetContent(WebApiRestRequest request)
formData.Add(content, $"\"{parameter.Key}\"");
}

var byteContent = new ByteArrayContent(request.File);
formData.Add(byteContent, "\"file\"", $"\"{request.FileName}\"");
TrelloConfiguration.Log.Debug($"\tContent: {formData}");

return formData;
var fileStream = File.Open(request.File, FileMode.Open);
Copy link
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.

var fileInfo = new FileInfo(request.File);
formData.Add(new StreamContent(fileStream), "\"file\"", string.Format("\"{0}\"", "as" + fileInfo.Extension));
gregsdennis marked this conversation as resolved.
Show resolved Hide resolved
formData.Add(new StringContent("mimeType"), "image/png");
gregsdennis marked this conversation as resolved.
Show resolved Hide resolved
Copy link
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."

formData.Add(new StringContent("name"), request.FileName);
TrelloConfiguration.Log.Debug($"\tContent: {formData}");

return formData;
}

if (request.Body == null) return null;
Expand Down
2 changes: 1 addition & 1 deletion Manatee.Trello/Rest/WebApiRequestProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public IRestRequest Create(string endpoint, IDictionary<string, object> paramete
if (parameter.Key == RestFile.ParameterKey)
{
var rf = (RestFile)parameter.Value;
request.AddFile(parameter.Key, rf.ContentBytes, rf.FileName);
request.AddFile(parameter.Key, rf.FilePath, rf.FileName);
Copy link
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.

}
else
{
Expand Down
6 changes: 3 additions & 3 deletions Manatee.Trello/Rest/WebApiRestRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ internal class WebApiRestRequest : IRestRequest

internal Dictionary<string, object> Parameters { get; }
internal object Body { get; private set; }
internal byte[] File { get; private set; }
internal string File { get; private set; }
internal string FileName { get; private set; }

public WebApiRestRequest()
Expand All @@ -27,9 +27,9 @@ public void AddBody(object body)
Body = body;
}

public void AddFile(string key, byte[] contentBytes, string fileName)
public void AddFile(string key,string filePath, string fileName)
{
File = contentBytes;
File = filePath;
FileName = fileName;
}
}
Expand Down