Media survive failures: uploaded when attached, the server's limits, alt text asked for

The composer uploads each file as soon as it is attached (streamed from the browser file, under its own ten-minute
timeout), shows it uploading, processing (a 202 followed until ready), ready or failed with Retry, and cancels one
removed; publishing sends the ids, so a failed publish or a retried upload sends nothing twice. Size limits and types
come from /api/v2/instance, a file without a browser type gets one from its extension, and each refusal names the file
and why. Previews are 320 px thumbnails, not whole files in memory. A picture without a description is asked about once
("Publish anyway"; a setting turns it off), descriptions are sent with PUT, and an edit keeps the post's media, language
and sensitive flag.

Profile pictures are checked against the server's types and can be removed. A card shows "Media unavailable" with the
original's link when a picture fails, the blurhash behind a sensitive placeholder, and plays a GIF only on tap under
reduced motion. A failed export says so instead of downloading part of a file.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01LsXgEaXee4GCU1hwYgPJXw
This commit is contained in:
thepraandClaude Opus 5.5 committed 2026-10-07 11:56:59 +02:00
1 parent 9d662f702b
commit 70cb389537
26 files changed
+883 -132

No files matched your search

+35 -18
View File
@@ -1,3 +1,4 @@
using collAnon.Client.Services;
using System.Text;
using decePubClient.Extensions;
@@ -35,7 +36,7 @@ namespace decePubClient.Services
/// reports the rows it could not do.
/// </summary>
public class PortabilityService(AccountService accounts, ListService lists, StatusService statuses, TimelineService timelines,
InstanceService instances)
InstanceService instances, CoalescingStringLocalizer localizer)
{
/// <summary>The most rows read from an export or an import file.</summary>
public const int MaxRows = 5000;
@@ -53,23 +54,33 @@ namespace decePubClient.Services
};
/// <summary>The file Mastodon would export, as UTF-8 bytes.</summary>
public async Task<byte[]> Export(PortableData data, string personaId, CancellationToken cancellationToken = default)
// a page that could not be read stops the export, which says so, instead of a file silently missing the rest
sealed class Stop
{
var rows = data switch
{
PortableData.Follows => await Follows(personaId, cancellationToken),
PortableData.Lists => await Lists(cancellationToken),
PortableData.Blocks => [.. (await Pages(accounts.Blocks, cancellationToken)).Select(a => Csv(Full(a.Acct)))],
PortableData.Mutes => await Mutes(cancellationToken),
PortableData.ServerBlocks => [.. (await accounts.DomainBlocks(cancellationToken)).Select(d => Csv(d))],
_ => await Bookmarks(personaId, cancellationToken)
};
return Encoding.UTF8.GetBytes(string.Concat(rows.Select(r => r + "\n")));
public WebResult Failed { get; set; }
}
async Task<List<string>> Follows(string personaId, CancellationToken cancellationToken)
/// <summary>The file, as Data (bytes); invalid when a page of it could not be read.</summary>
public async Task<WebResult> Export(PortableData data, string personaId, CancellationToken cancellationToken = default)
{
var followed = await Pages((maxId, token) => accounts.Following(personaId, maxId, token), cancellationToken);
var stop = new Stop();
var rows = data switch
{
PortableData.Follows => await Follows(personaId, stop, cancellationToken),
PortableData.Lists => await Lists(cancellationToken),
PortableData.Blocks => [.. (await Pages(accounts.Blocks, stop, cancellationToken)).Select(a => Csv(Full(a.Acct)))],
PortableData.Mutes => await Mutes(stop, cancellationToken),
PortableData.ServerBlocks => [.. (await accounts.DomainBlocks(cancellationToken)).Select(d => Csv(d))],
_ => await Bookmarks(personaId, stop, cancellationToken)
};
if (stop.Failed is { } failed)
return failed;
return new WebResult { Data = Encoding.UTF8.GetBytes(string.Concat(rows.Select(r => r + "\n"))) };
}
async Task<List<string>> Follows(string personaId, Stop stop, CancellationToken cancellationToken)
{
var followed = await Pages((maxId, token) => accounts.Following(personaId, maxId, token), stop, cancellationToken);
var relationships = (await accounts.Relationships([.. followed.Select(a => a.Id)], cancellationToken)).ToDictionary(r => r.Id);
return
[
@@ -80,9 +91,9 @@ namespace decePubClient.Services
];
}
async Task<List<string>> Mutes(CancellationToken cancellationToken)
async Task<List<string>> Mutes(Stop stop, CancellationToken cancellationToken)
{
var muted = await Pages(accounts.Mutes, cancellationToken);
var muted = await Pages(accounts.Mutes, stop, cancellationToken);
var relationships = (await accounts.Relationships([.. muted.Select(a => a.Id)], cancellationToken)).ToDictionary(r => r.Id);
return
[
@@ -99,7 +110,7 @@ namespace decePubClient.Services
return rows;
}
async Task<List<string>> Bookmarks(string personaId, CancellationToken cancellationToken)
async Task<List<string>> Bookmarks(string personaId, Stop stop, CancellationToken cancellationToken)
{
var rows = new List<string>();
var maxId = default(string);
@@ -107,7 +118,10 @@ namespace decePubClient.Services
{
var result = await timelines.Load(new FeedQuery(TimelineType.Bookmarks, personaId), maxId, cancellationToken: cancellationToken);
if (result.IsInvalid || result.Data is not FeedPage page)
{
stop.Failed = result.IsInvalid ? result : new WebResult().Invalidate(localizer["The server sent an invalid response."]);
return rows;
}
rows.AddRange(page.Posts.Select(p => Csv(p.Status.Uri)));
maxId = page.Posts.Count == 0 ? null : page.MaxId;
}
@@ -116,7 +130,7 @@ namespace decePubClient.Services
}
// every page of an account list, up to MaxRows
static async Task<List<Account>> Pages(Func<string, CancellationToken, Task<WebResult>> page, CancellationToken cancellationToken)
async Task<List<Account>> Pages(Func<string, CancellationToken, Task<WebResult>> page, Stop stop, CancellationToken cancellationToken)
{
var all = new List<Account>();
var maxId = default(string);
@@ -124,7 +138,10 @@ namespace decePubClient.Services
{
var result = await page(maxId, cancellationToken);
if (result.IsInvalid || result.Data is not AccountPage accountPage)
{
stop.Failed = result.IsInvalid ? result : new WebResult().Invalidate(localizer["The server sent an invalid response."]);
return all;
}
all.AddRange(accountPage.Accounts);
maxId = accountPage.MaxId;
}