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

Allow Async (v2) Uploads to Mastodon for bigger files (Videos); GET M… #116

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 43 additions & 10 deletions Mastonet/IMastodonClient.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using Mastonet.Entities;

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading.Tasks;

namespace Mastonet;
Expand Down Expand Up @@ -170,6 +170,42 @@ public interface IMastodonClient
/// <returns>Returns an Attachment that can be used when creating a status</returns>
Task<Attachment> UploadMedia(Stream data, string fileName = "file", string? description = null, AttachmentFocusData? focus = null);

/// <summary>
/// Wait until an attachment has been uploaded and processed
/// </summary>
/// <param name="attachment">Attachment that has just been created</param>
/// <param name="waitSeconds">Poll for Status update interval</param>
/// <param name="maxWaitSeconds">Maximum time to wait before giving opt (timeout)</param>
/// <returns>Returns an Attachment that can be used when creating a status after the processing is finished</returns>
Task<Attachment> WaitUntilMediaIsUploaded(Attachment attachment, int waitSeconds = 10, int maxWaitSeconds = 300);

/// <summary>
/// Get Information from a single Media item
/// </summary>
/// <param name="mediaId">Id from Attachment</param>
/// <returns>Returns an Attachment that can be used when creating a status after the processing is finished</returns>
Task<Attachment> GetMedia(string mediaId);

/// <summary>
/// Uploading a media attachment async (v2) You will have to wait afterwards until the attachment is processed. This is meant for bigger attachments (videos)
/// </summary>
/// <param name="data">Media stream to be uploaded</param>
/// <param name="fileName">Media file name (must contains extension ex: .png, .jpg, ...)</param>
/// <param name="description">A plain-text description of the media for accessibility (max 420 chars)</param>
/// <param name="focus">Two floating points. See <see cref="https://docs.joinmastodon.org/api/rest/media/#focal-points">focal points</see></param>
/// <returns>Returns an Attachment that can be used when creating a status after the processing is finished</returns>
Task<Attachment> UploadMediaAsync(MediaDefinition media, string? description = null, AttachmentFocusData? focus = null);

/// <summary>
/// Uploading a media attachment async (v2) You will have to wait afterwards until the attachment is processed. This is meant for bigger attachments (videos)
/// </summary>
/// <param name="media">Media to be uploaded</param>
/// <param name="description">A plain-text description of the media for accessibility (max 420 chars)</param>
/// <param name="focus">Two floating points. See <see cref="https://docs.joinmastodon.org/api/rest/media/#focal-points">focal points</see></param>
/// <returns>Returns an Attachment that can be used when creating a status after the processing is finished</returns>
Task<Attachment> UploadMediaAsync(Stream data, string fileName = "file", string? description = null, AttachmentFocusData? focus = null);


/// <summary>
/// Uploading a media attachment
/// </summary>
Expand Down Expand Up @@ -216,7 +252,6 @@ public interface IMastodonClient
/// <returns></returns>
Task DismissNotification(string notificationId);


/// <summary>
/// Fetching a user's reports
/// </summary>
Expand Down Expand Up @@ -309,7 +344,7 @@ public interface IMastodonClient
/// <returns>Returns Poll</returns>
Task<Poll> Vote(string id, IEnumerable<int> choices);

#endregion
#endregion MastodonClient

#region MastodonClient.Account

Expand All @@ -326,8 +361,6 @@ public interface IMastodonClient
/// <returns>Returns the user's own Account with Source</returns>
Task<Account> GetCurrentUser();



/// <summary>
/// Update the user's display and preferences.
/// </summary>
Expand Down Expand Up @@ -468,7 +501,7 @@ Task<Account> UpdateCredentials(bool? discoverable = null,
/// <returns></returns>
Task<IEnumerable<Tag>> GetFeaturedTagsSuggestions();

#endregion
#endregion MastodonClient.Account

#region MastodonClient.AccountActions

Expand Down Expand Up @@ -619,7 +652,7 @@ Task<Account> UpdateCredentials(bool? discoverable = null,
/// <returns></returns>
Task<MastodonList<Tag>> ViewFollowedTags(ArrayOptions? options = null);

#endregion
#endregion MastodonClient.AccountActions

#region MastodonClient.Status

Expand Down Expand Up @@ -791,7 +824,7 @@ Task<Account> UpdateCredentials(bool? discoverable = null,
/// <returns>Returns the target Status</returns>
Task<Status> Unpin(string statusId);

#endregion
#endregion MastodonClient.Status

#region MastodonClient.Timeline

Expand Down Expand Up @@ -860,5 +893,5 @@ Task<Account> UpdateCredentials(bool? discoverable = null,

TimelineStreaming GetDirectMessagesStreaming();

#endregion
}
#endregion MastodonClient.Timeline
}
73 changes: 73 additions & 0 deletions Mastonet/MastodonClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading;
using System.Threading.Tasks;

namespace Mastonet;
Expand Down Expand Up @@ -347,6 +348,20 @@ public Task<Attachment> UploadMedia(Stream data, string fileName = "file", strin
return UploadMedia(new MediaDefinition(data, fileName), description, focus);
}

/// <summary>
/// Uploading a media attachment async (v2) You will have to wait afterwards until the attachment is processed. This is meant for bigger attachments (videos)
/// </summary>
/// <param name="data">Media stream to be uploaded</param>
/// <param name="fileName">Media file name (must contains extension ex: .png, .jpg, ...)</param>
/// <param name="description">A plain-text description of the media for accessibility (max 420 chars)</param>
/// <param name="focus">Two floating points. See <see cref="https://docs.joinmastodon.org/api/rest/media/#focal-points">focal points</see></param>
/// <returns>Returns an Attachment that can be used when creating a status after the processing is finished</returns>
public Task<Attachment> UploadMediaAsync(Stream data, string fileName = "file", string? description = null,
AttachmentFocusData? focus = null)
{
return UploadMediaAsync(new MediaDefinition(data, fileName), description, focus);
}

/// <summary>
/// Uploading a media attachment
/// </summary>
Expand All @@ -373,6 +388,64 @@ public Task<Attachment> UploadMedia(MediaDefinition media, string? description =
return this.Post<Attachment>("/api/v2/media", data, list);
}

/// <summary>
/// Uploading a media attachment async (v2) You will have to wait afterwards until the attachment is processed. This is meant for bigger attachments (videos)
/// </summary>
/// <param name="media">Media to be uploaded</param>
/// <param name="description">A plain-text description of the media for accessibility (max 420 chars)</param>
/// <param name="focus">Two floating points. See <see cref="https://docs.joinmastodon.org/api/rest/media/#focal-points">focal points</see></param>
/// <returns>Returns an Attachment that can be used when creating a status after the processing is finished</returns>
public async Task<Attachment> UploadMediaAsync(MediaDefinition media, string? description = null,
AttachmentFocusData? focus = null)
{
media.ParamName = "file";
var list = new List<MediaDefinition>() { media };
var data = new Dictionary<string, string>();
if (description != null)
{
data.Add("description", description);
}

if (focus != null)
{
data.Add("focus", $"{focus.X},{focus.Y}");
}

return await this.Post<Attachment>("/api/v2/media", data, list);
}

/// <summary>
/// Wait until an attachment has been uploaded and processed
/// </summary>
/// <param name="attachment">Attachment that has just been created</param>
/// <param name="waitSeconds">Poll for Status update interval</param>
/// <param name="maxWaitSeconds">Maximum time to wait before giving opt (timeout)</param>
/// <returns>Returns an Attachment that can be used when creating a status after the processing is finished</returns>
public async Task<Attachment> WaitUntilMediaIsUploaded(Attachment attachment, int waitSeconds = 10, int maxWaitSeconds = 300)
{
int totalTimeWasted = 0;
while (attachment.Id != null && attachment.Url == null && attachment.PreviewUrl != null && totalTimeWasted < maxWaitSeconds)
{
Thread.Sleep(waitSeconds * 1000);
totalTimeWasted += waitSeconds;
attachment = await GetMedia(attachment.Id);
}
return attachment;
}

/// <summary>
/// Get Information from a single Media item
/// </summary>
/// <param name="mediaId">Id from Attachment</param>
/// <returns>Returns an Attachment that can be used when creating a status after the processing is finished</returns>
public async Task<Attachment> GetMedia(string mediaId)
{
var attachment = await this.Get<Attachment>("/api/v1/media/" + mediaId);
var prev = attachment.PreviewUrl;
var url = attachment.Url;
return attachment;
}

/// <summary>
/// Update a media attachment. Can only be done before the media is attached to a status.
/// </summary>
Expand Down