glacasa / Mastonet

C# Library for Mastodon
MIT License
225 stars 37 forks source link

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

Open Guacam-Ole opened 8 months ago

Guacam-Ole commented 8 months ago

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 });
glacasa commented 8 months ago

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 commented 8 months ago

@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 commented 8 months ago

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 commented 8 months ago

@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 commented 8 months ago

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 commented 8 months ago

@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 commented 8 months ago

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 commented 8 months ago

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.