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

Conversation

Guacam-Ole
Copy link

@Guacam-Ole Guacam-Ole commented Dec 31, 2023

Added V2-Upload

The V1 - Upload is only meant for smaller images and does not work with bigger files like Videos. The (now recommened) V2 Upload takes any attachment and does all work asynchronously in the background. While this happens the attachmentID is already returned but can not yet be used on a toot.

That is why I also added a poll mechanism to wait for the processing to be finished.

Example for usage:

var media = await client.UploadMediaAsync(videoFile, "squirrel.mp4", "possibly a squirrel");
media=await client.WaitUntilMediaIsUploaded(media);
if (media.Url == null) throw new Exception("Did not upload in time");
var status = await client.PublishStatus("There is a squirrel!!!", Visibility.Public, mediaIds: new[] { media.Id });

@Guacam-Ole Guacam-Ole closed this Dec 31, 2023
@Guacam-Ole Guacam-Ole reopened this Dec 31, 2023
@glacasa glacasa self-assigned this Jan 14, 2024
@glacasa
Copy link
Owner

glacasa commented Jan 14, 2024

What happens if we try to attach the media id on a post before the media is actually available ?
This seems to me it should be handled by the server
The media API docs doesn't seem to forbid the usage of the media id even if it is not fully procecessed

@Guacam-Ole
Copy link
Author

Guacam-Ole commented Jan 14, 2024

@glacasa trying to use the Media in a toot before it is completely processed just throws a general Error (HTML-Page, "Something went wrong on our site") (at least on the mastodon.social instance)
That's why I added the WaitUntilMediaIsUploaded() function.

Nonetheless I didn't include the waiting in the upload itself but only on the example, because there might be reasons to not wait for it. (e.g. uploading multiple files etc.)

@13xforever
Copy link

It seems there's been a change to how media uploads are handled between v2 and v3 of mastonet, now I get "Cannot attach files that have not finished processing. Try again in a moment!" and there's no corresponding media get api call implemented (it returns 206 while media is being processed), so we need that first, and maybe a wrapper like the one proposed here for convenience.

@Guacam-Ole
Copy link
Author

@13xforever Did you call the "WaitUntilMediaIsUploaded()" method before trying to create the toot?
My Bot is currently using that code and uploads videos just fine with that method:
https://mastodon.social/@squirrelbot/111702691377577652
(Just an example of a toot with a video. Created by the bot. The Code used is the one above)

@13xforever
Copy link

13xforever commented Jan 16, 2024

No, I did not. I simply upgraded the client and didn't change any code as there's no guidance or mention of any breaking changes anywhere I could see.

EDIT: also I just checked and there's no .WaitUntilMediaIsUploaded() anywhere? I thought the whole point of this PR is to add it?

@Guacam-Ole
Copy link
Author

Guacam-Ole commented Jan 16, 2024

@13xforever Sorry, but I suspect you are in the wrong issue here. What we discuss here is a change that hasn't even been merged yet. I suggest you open a different issue for your problem.

@13xforever
Copy link

What I'm saying is that there's an issue with media uploads right now because of the missing api implementation, and this particular PR is a hacky workaround for it and maybe it's worth to consider proper implementation with something similar built on top of that, and not pulling this as is.

Not only this method will be hard to discover (it's not directly under the media apis, and sits directly on the client class), it's also solving a very specific issue that's already provided by the new mastodon api, and following closer to the specification will help with discoverability (especially considering this library lacks in docs and examples at the moment).

@glacasa
Copy link
Owner

glacasa commented Jan 16, 2024

Hello, thanks to both of you.

If something was broken, could you open a new issue @13xforever ?
We probably missed something when moving from Newtonsoft to System.Text.Json

Once this fixed, I will look if we still need to handle processing like this.
It feels weird to have to wait on client side, I wouldn't be surprised if it's just a side effect of the bug, as you suggested.

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.

3 participants