House rules: primary constructors, a cancellation token on every async call, and the rules fail the build
Build / Build (push) Successful in 30s
Build / Build (push) Successful in 30s
- Services, controllers and jobs take their dependencies through primary constructors and use them directly; where a method already named a local after a dependency, the dependency takes its type's name. - Every async method takes a CancellationToken last and passes it down to MongoDB, HTTP and the job queue. The setup claim is still given back when a request is cancelled. - The federation and media options are read through IOptionsMonitor. - The Mastodon entities keep sending "" for empty strings, and every model field where null means something says so. - The inbox's 503 with Retry-After, which other servers retry, is the one server error left, and says why. - Arasaka.HouseRules.Analyzers checks all of this in every build, here and in PrivaPub.ClientModels as decePubClient builds it. Checked against GoToSocial in the pasture: 64 passed, 0 failed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01W4YtEJrgxG12Pc39Fai8Bd
This commit is contained in:
1 parent
f0a14c737f
commit
0f73f69239
176 files changed
+2789
-3974
No files matched your search
@@ -36,7 +36,7 @@ namespace PrivaPub.Domain.Media
|
||||
if (i != 0 || j != 0)
|
||||
for (var c = 0; c < 3; c++)
|
||||
maximum = Math.Max(maximum, Math.Abs(factors[j, i, c]));
|
||||
double maximumValue;
|
||||
var maximumValue = default(double);
|
||||
if (componentsX * componentsY > 1)
|
||||
{
|
||||
var quantised = (int)Math.Max(0, Math.Min(82, Math.Floor(maximum * 166 - 0.5)));
|
||||
|
||||
@@ -44,17 +44,17 @@ namespace PrivaPub.Domain.Media
|
||||
// - what is too big to cache is remembered for an hour (it is streamed instead), what failed for five minutes;
|
||||
// - the cache's size is counted as it grows, and trimmed as soon as it passes the cap;
|
||||
// - nothing of a suspended server, or of one whose media are rejected, is proxied, and blocking one purges its files.
|
||||
public class MediaProxy : IMediaProxy
|
||||
public class MediaProxy(
|
||||
ILocalActorService localActors,
|
||||
IFederationHttp http,
|
||||
IMediaService mediaService,
|
||||
IOptionsMonitor<MediaOptions> options,
|
||||
Federation.Moderation.IDomainBlocks domainBlocks = default) : IMediaProxy
|
||||
{
|
||||
static readonly TimeSpan TooBigFor = TimeSpan.FromHours(1);
|
||||
static readonly TimeSpan FailedFor = TimeSpan.FromMinutes(5);
|
||||
const int Downloads = 8;//remote files fetched at once, however many clients ask
|
||||
|
||||
readonly ILocalActorService _localActors;
|
||||
readonly IFederationHttp _http;
|
||||
readonly IMediaService _media;
|
||||
readonly IOptionsMonitor<MediaOptions> _options;
|
||||
readonly Federation.Moderation.IDomainBlocks _domainBlocks;
|
||||
// Lazy: GetOrAdd may run its factory more than once when asked at the same time, but only one Lazy is ever evaluated
|
||||
readonly ConcurrentDictionary<string, Lazy<Task<(ProxyOutcome, string, string)>>> _inFlight = new();
|
||||
readonly ConcurrentDictionary<string, (ProxyOutcome Outcome, DateTime Until)> _refused = new();
|
||||
@@ -64,16 +64,6 @@ namespace PrivaPub.Domain.Media
|
||||
long _bytes = -1;//what the cache holds, counted once from the disk and then as it changes
|
||||
int _trimming;
|
||||
|
||||
public MediaProxy(ILocalActorService localActors, IFederationHttp http, IMediaService media, IOptionsMonitor<MediaOptions> options,
|
||||
Federation.Moderation.IDomainBlocks domainBlocks = default)
|
||||
{
|
||||
_localActors = localActors;
|
||||
_http = http;
|
||||
_media = media;
|
||||
_options = options;
|
||||
_domainBlocks = domainBlocks;
|
||||
}
|
||||
|
||||
byte[] Key
|
||||
{
|
||||
get
|
||||
@@ -87,15 +77,15 @@ namespace PrivaPub.Domain.Media
|
||||
|
||||
public string Wrap(string remoteUrl)
|
||||
{
|
||||
if (string.IsNullOrEmpty(remoteUrl) || remoteUrl.StartsWith(_localActors.BaseAddress + "/", StringComparison.OrdinalIgnoreCase))
|
||||
if (string.IsNullOrEmpty(remoteUrl) || remoteUrl.StartsWith(localActors.BaseAddress + "/", StringComparison.OrdinalIgnoreCase))
|
||||
return remoteUrl;
|
||||
var encoded = Base64Url(Encoding.UTF8.GetBytes(remoteUrl));
|
||||
return $"{_localActors.BaseAddress}/media/proxy/{Sign(remoteUrl)}/{encoded}";
|
||||
return $"{localActors.BaseAddress}/media/proxy/{Sign(remoteUrl)}/{encoded}";
|
||||
}
|
||||
|
||||
public string Verified(string signature, string encodedUrl)
|
||||
{
|
||||
string url;
|
||||
var url = default(string);
|
||||
try
|
||||
{
|
||||
url = Encoding.UTF8.GetString(FromBase64Url(encodedUrl));
|
||||
@@ -109,9 +99,9 @@ namespace PrivaPub.Domain.Media
|
||||
|
||||
public bool Refuses(string url)
|
||||
{
|
||||
if (_domainBlocks == default || !Uri.TryCreate(url, UriKind.Absolute, out var target))
|
||||
if (domainBlocks == default || !Uri.TryCreate(url, UriKind.Absolute, out var target))
|
||||
return false;
|
||||
var block = _domainBlocks.Find(target.Host);
|
||||
var block = domainBlocks.Find(target.Host);
|
||||
return block is { Severity: DomainBlockSeverity.Suspend } or { RejectMedia: true };
|
||||
}
|
||||
|
||||
@@ -132,7 +122,7 @@ namespace PrivaPub.Domain.Media
|
||||
}
|
||||
|
||||
public Task<HttpResponseMessage> Open(string url, System.Net.Http.Headers.RangeHeaderValue range, CancellationToken token) =>
|
||||
_http.OpenMedia(url, range, token);
|
||||
http.OpenMedia(url, range, token);
|
||||
|
||||
public async Task<(string Path, string ContentType)> Fetch(string signature, string encodedUrl, CancellationToken token)
|
||||
{
|
||||
@@ -166,10 +156,10 @@ namespace PrivaPub.Domain.Media
|
||||
}
|
||||
}
|
||||
|
||||
async Task<(ProxyOutcome, string, string)> DownloadOnce(string url)
|
||||
async Task<(ProxyOutcome, string, string)> DownloadOnce(string url, CancellationToken token = default)
|
||||
{
|
||||
await Task.Yield();
|
||||
if (!await _downloads.WaitAsync(TimeSpan.FromSeconds(30)))
|
||||
if (!await _downloads.WaitAsync(TimeSpan.FromSeconds(30), token))
|
||||
return (ProxyOutcome.Failed, default, default);//too busy: the client tries again
|
||||
// cached by a download that ended just before this one began
|
||||
if (Cached(url) is { Path: not null } done)
|
||||
@@ -182,16 +172,17 @@ namespace PrivaPub.Domain.Media
|
||||
try
|
||||
{
|
||||
Directory.CreateDirectory(System.IO.Path.GetDirectoryName(path)!);
|
||||
string contentType, refusal;
|
||||
var contentType = default(string);
|
||||
var refusal = default(string);
|
||||
await using (var file = new FileStream(part, FileMode.CreateNew, FileAccess.Write, FileShare.None, 64 * 1024, useAsync: true))
|
||||
(contentType, refusal) = await _http.DownloadMedia(url, _options.CurrentValue.MaxProxiedBytes, file, CancellationToken.None);
|
||||
(contentType, refusal) = await http.DownloadMedia(url, options.CurrentValue.MaxProxiedBytes, file, CancellationToken.None);
|
||||
if (contentType == default)
|
||||
{
|
||||
var outcome = refusal == "too-large" ? ProxyOutcome.TooBig : ProxyOutcome.Failed;
|
||||
_refused[url] = (outcome, DateTime.UtcNow + (outcome == ProxyOutcome.TooBig ? TooBigFor : FailedFor));
|
||||
return (outcome, default, default);
|
||||
}
|
||||
await File.WriteAllTextAsync(typePath, contentType + "\n" + new Uri(url).Host);
|
||||
await File.WriteAllTextAsync(typePath, contentType + "\n" + new Uri(url).Host, token);
|
||||
var size = new FileInfo(part).Length;
|
||||
File.Move(part, path, overwrite: true);
|
||||
Grew(size);
|
||||
@@ -209,7 +200,7 @@ namespace PrivaPub.Domain.Media
|
||||
{
|
||||
if (Interlocked.Read(ref _bytes) < 0)
|
||||
Interlocked.CompareExchange(ref _bytes, Measure(), -1);
|
||||
if (Interlocked.Add(ref _bytes, bytes) > _options.CurrentValue.ProxyCacheBytes)
|
||||
if (Interlocked.Add(ref _bytes, bytes) > options.CurrentValue.ProxyCacheBytes)
|
||||
_ = Task.Run(Trim);
|
||||
}
|
||||
|
||||
@@ -219,7 +210,7 @@ namespace PrivaPub.Domain.Media
|
||||
return;
|
||||
try
|
||||
{
|
||||
Interlocked.Exchange(ref _bytes, TrimDirectory(_media.ProxyRoot, _options.CurrentValue.ProxyCacheBytes));
|
||||
Interlocked.Exchange(ref _bytes, TrimDirectory(mediaService.ProxyRoot, options.CurrentValue.ProxyCacheBytes));
|
||||
}
|
||||
finally
|
||||
{
|
||||
@@ -229,7 +220,7 @@ namespace PrivaPub.Domain.Media
|
||||
|
||||
public int Purge(string domain)
|
||||
{
|
||||
var directory = new DirectoryInfo(_media.ProxyRoot);
|
||||
var directory = new DirectoryInfo(mediaService.ProxyRoot);
|
||||
if (string.IsNullOrEmpty(domain) || !directory.Exists)
|
||||
return 0;
|
||||
var purged = 0;
|
||||
@@ -256,7 +247,7 @@ namespace PrivaPub.Domain.Media
|
||||
|
||||
long Measure()
|
||||
{
|
||||
var directory = new DirectoryInfo(_media.ProxyRoot);
|
||||
var directory = new DirectoryInfo(mediaService.ProxyRoot);
|
||||
return directory.Exists ? directory.EnumerateFiles("*", SearchOption.AllDirectories).Where(f => f.Extension is not (".type" or ".part")).Sum(f => f.Length) : 0;
|
||||
}
|
||||
|
||||
@@ -290,7 +281,7 @@ namespace PrivaPub.Domain.Media
|
||||
(string Path, string TypePath) CachePaths(string url)
|
||||
{
|
||||
var name = Convert.ToHexStringLower(SHA256.HashData(Encoding.UTF8.GetBytes(url)));
|
||||
var path = System.IO.Path.Combine(_media.ProxyRoot, name[..2], name);
|
||||
var path = System.IO.Path.Combine(mediaService.ProxyRoot, name[..2], name);
|
||||
return (path, path + ".type");
|
||||
}
|
||||
|
||||
@@ -317,7 +308,10 @@ namespace PrivaPub.Domain.Media
|
||||
}
|
||||
}
|
||||
|
||||
public class MediaJanitor : BackgroundService
|
||||
public class MediaJanitor(
|
||||
IMediaService mediaService,
|
||||
IOptionsMonitor<MediaOptions> options,
|
||||
ILogger<MediaJanitor> logger) : BackgroundService
|
||||
{
|
||||
static readonly TimeSpan FirstPass = TimeSpan.FromMinutes(5);
|
||||
static readonly TimeSpan Interval = TimeSpan.FromHours(1);
|
||||
@@ -325,16 +319,6 @@ namespace PrivaPub.Domain.Media
|
||||
// a trashed file waits this long before it is deleted, already out of what /media/files serves
|
||||
public static readonly TimeSpan TrashGrace = TimeSpan.FromDays(1);
|
||||
|
||||
readonly IMediaService _media;
|
||||
readonly IOptionsMonitor<MediaOptions> _options;
|
||||
readonly ILogger<MediaJanitor> _logger;
|
||||
|
||||
public MediaJanitor(IMediaService media, IOptionsMonitor<MediaOptions> options, ILogger<MediaJanitor> logger)
|
||||
{
|
||||
_media = media;
|
||||
_options = options;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||
{
|
||||
@@ -353,7 +337,7 @@ namespace PrivaPub.Domain.Media
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogWarning(ex, "{Service} pass failed", nameof(MediaJanitor));
|
||||
logger.LogWarning(ex, "{Service} pass failed", nameof(MediaJanitor));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -368,7 +352,7 @@ namespace PrivaPub.Domain.Media
|
||||
public async Task Sweep(CancellationToken token)
|
||||
{
|
||||
var cutoff = DateTime.UtcNow - UnattachedLifetime;
|
||||
var unattached = await _media.Trash(m => m.PostId == null && m.ScheduledStatusId == null && m.ProfileOfAvatarId == null && m.ServerUse == null
|
||||
var unattached = await mediaService.Trash(m => m.PostId == null && m.ScheduledStatusId == null && m.ProfileOfAvatarId == null && m.ServerUse == null
|
||||
&& m.CreatedAt < cutoff, "never posted", token);
|
||||
|
||||
var trashed = await DB.Default.Find<MediaAttachment>().Match(m => m.TrashedAt != null).Limit(2000).ExecuteAsync(token);
|
||||
@@ -379,18 +363,18 @@ namespace PrivaPub.Domain.Media
|
||||
{
|
||||
if (row.TrashedAt < purgeBefore && !maintenance)
|
||||
{
|
||||
await _media.Purge(row, token);
|
||||
await mediaService.Purge(row, token);
|
||||
purged++;
|
||||
}
|
||||
else
|
||||
_media.Hide(row);
|
||||
mediaService.Hide(row);
|
||||
}
|
||||
if (unattached > 0 || purged > 0)
|
||||
_logger.LogInformation("{Service}: {Unattached} uploads never posted trashed, {Purged} trashed files deleted",
|
||||
logger.LogInformation("{Service}: {Unattached} uploads never posted trashed, {Purged} trashed files deleted",
|
||||
nameof(MediaJanitor), unattached, purged);
|
||||
TrimProxyCache();
|
||||
}
|
||||
|
||||
void TrimProxyCache() => MediaProxy.TrimDirectory(_media.ProxyRoot, _options.CurrentValue.ProxyCacheBytes);
|
||||
void TrimProxyCache() => MediaProxy.TrimDirectory(mediaService.ProxyRoot, options.CurrentValue.ProxyCacheBytes);
|
||||
}
|
||||
}
|
||||
@@ -62,8 +62,14 @@ namespace PrivaPub.Domain.Media
|
||||
Task Purge(MediaAttachment trashed, CancellationToken token);
|
||||
}
|
||||
|
||||
public class MediaService : IMediaService
|
||||
public class MediaService(
|
||||
IOptionsMonitor<MediaOptions> mediaOptions,
|
||||
ILocalActorService localActors,
|
||||
IWebHostEnvironment environment,
|
||||
ILogger<MediaService> logger) : IMediaService
|
||||
{
|
||||
readonly SemaphoreSlim _processing = new(Math.Max(1, mediaOptions.CurrentValue.Concurrency));
|
||||
|
||||
// HEIC and HEIF are not among them: the libvips bundled here decodes AVIF but has no HEVC decoder
|
||||
static readonly string[] ImageTypes = { "image/jpeg", "image/png", "image/gif", "image/webp", "image/avif" };
|
||||
// the only loaders an upload ever reaches: everything else libvips could read (SVG, PDF, TIFF, ImageMagick, ...) is
|
||||
@@ -94,22 +100,7 @@ namespace PrivaPub.Domain.Media
|
||||
Cache.Max = 0;//an upload is decoded once: nothing to keep between operations
|
||||
}
|
||||
|
||||
readonly IOptionsMonitor<MediaOptions> _options;
|
||||
readonly ILocalActorService _localActors;
|
||||
readonly SemaphoreSlim _processing;
|
||||
readonly IWebHostEnvironment _environment;
|
||||
readonly ILogger<MediaService> _logger;
|
||||
|
||||
public MediaService(IOptionsMonitor<MediaOptions> options, ILocalActorService localActors, IWebHostEnvironment environment, ILogger<MediaService> logger)
|
||||
{
|
||||
_options = options;
|
||||
_localActors = localActors;
|
||||
_processing = new SemaphoreSlim(Math.Max(1, options.CurrentValue.Concurrency));
|
||||
_environment = environment;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public string Root => Path.GetFullPath(_options.CurrentValue.Root ?? Path.Combine(_environment.ContentRootPath, "media-store"));
|
||||
public string Root => Path.GetFullPath(mediaOptions.CurrentValue.Root ?? Path.Combine(environment.ContentRootPath, "media-store"));
|
||||
|
||||
public string ProxyRoot => Root.TrimEnd(Path.DirectorySeparatorChar) + "-proxy";
|
||||
|
||||
@@ -119,13 +110,13 @@ namespace PrivaPub.Domain.Media
|
||||
|
||||
string Incoming(string id) => Path.Combine(IncomingRoot, id + ".in");
|
||||
|
||||
public string Url(string relativePath) => relativePath == default ? default : $"{_localActors.BaseAddress}/media/files/{relativePath.Replace('\\', '/')}";
|
||||
public string Url(string relativePath) => relativePath == default ? default : $"{localActors.BaseAddress}/media/files/{relativePath.Replace('\\', '/')}";
|
||||
|
||||
public async Task<MediaOutcome> Upload(LocalActor owner, IFormFile file, string description, string focus, bool later, CancellationToken token)
|
||||
{
|
||||
if (file == default || file.Length == 0)
|
||||
return MediaOutcome.Fail(StatusCodes.Status422UnprocessableEntity, "Validation failed: File can't be blank");
|
||||
var options = _options.CurrentValue;
|
||||
var options = mediaOptions.CurrentValue;
|
||||
var contentType = file.ContentType?.Split(';')[0].Trim().ToLowerInvariant();
|
||||
if (await OverQuota(owner.Id, file.Length, token) is { } full)
|
||||
return full;
|
||||
@@ -168,7 +159,7 @@ namespace PrivaPub.Domain.Media
|
||||
}
|
||||
catch (VipsException ex)
|
||||
{
|
||||
_logger.LogInformation("Refused an image upload: {Error}", ex.Message);
|
||||
logger.LogInformation("Refused an image upload: {Error}", ex.Message);
|
||||
return MediaOutcome.Fail(StatusCodes.Status422UnprocessableEntity, "Validation failed: The file is not a readable image");
|
||||
}
|
||||
finally
|
||||
@@ -210,15 +201,16 @@ namespace PrivaPub.Domain.Media
|
||||
return MediaOutcome.Fail(StatusCodes.Status422UnprocessableEntity, "Validation failed: File can't be blank");
|
||||
if (!ImageTypes.Contains(contentType))
|
||||
return MediaOutcome.Fail(StatusCodes.Status422UnprocessableEntity, "Validation failed: File type is not supported");
|
||||
if (file.Length > _options.CurrentValue.MaxImageBytes)
|
||||
if (file.Length > mediaOptions.CurrentValue.MaxImageBytes)
|
||||
return MediaOutcome.Fail(StatusCodes.Status422UnprocessableEntity, "Validation failed: File is too big");
|
||||
if (await OverQuota(avatarId, file.Length, token) is { } full)
|
||||
return full;
|
||||
var input = await Read(file, token);
|
||||
if (Refusal(input, _options.CurrentValue) is { } refused)
|
||||
if (Refusal(input, mediaOptions.CurrentValue) is { } refused)
|
||||
return refused;
|
||||
byte[] bytes;
|
||||
int outWidth, outHeight;
|
||||
var bytes = default(byte[]);
|
||||
var outWidth = default(int);
|
||||
var outHeight = default(int);
|
||||
await _processing.WaitAsync(token);
|
||||
try
|
||||
{
|
||||
@@ -229,7 +221,7 @@ namespace PrivaPub.Domain.Media
|
||||
}
|
||||
catch (VipsException ex)
|
||||
{
|
||||
_logger.LogInformation("Refused a profile picture: {Error}", ex.Message);
|
||||
logger.LogInformation("Refused a profile picture: {Error}", ex.Message);
|
||||
return MediaOutcome.Fail(StatusCodes.Status422UnprocessableEntity, "Validation failed: The file is not a readable image");
|
||||
}
|
||||
finally
|
||||
@@ -264,13 +256,14 @@ namespace PrivaPub.Domain.Media
|
||||
return MediaOutcome.Fail(StatusCodes.Status422UnprocessableEntity, "Validation failed: File can't be blank");
|
||||
if (!ImageTypes.Contains(contentType))
|
||||
return MediaOutcome.Fail(StatusCodes.Status422UnprocessableEntity, "Validation failed: File type is not supported");
|
||||
if (file.Length > _options.CurrentValue.MaxImageBytes)
|
||||
if (file.Length > mediaOptions.CurrentValue.MaxImageBytes)
|
||||
return MediaOutcome.Fail(StatusCodes.Status422UnprocessableEntity, "Validation failed: File is too big");
|
||||
var input = await Read(file, token);
|
||||
if (Refusal(input, _options.CurrentValue) is { } refused)
|
||||
if (Refusal(input, mediaOptions.CurrentValue) is { } refused)
|
||||
return refused;
|
||||
byte[] bytes;
|
||||
int outWidth, outHeight;
|
||||
var bytes = default(byte[]);
|
||||
var outWidth = default(int);
|
||||
var outHeight = default(int);
|
||||
await _processing.WaitAsync(token);
|
||||
try
|
||||
{
|
||||
@@ -281,7 +274,7 @@ namespace PrivaPub.Domain.Media
|
||||
}
|
||||
catch (VipsException ex)
|
||||
{
|
||||
_logger.LogInformation("Refused a server picture: {Error}", ex.Message);
|
||||
logger.LogInformation("Refused a server picture: {Error}", ex.Message);
|
||||
return MediaOutcome.Fail(StatusCodes.Status422UnprocessableEntity, "Validation failed: The file is not a readable image");
|
||||
}
|
||||
finally
|
||||
@@ -327,11 +320,13 @@ namespace PrivaPub.Domain.Media
|
||||
if (file.Length > EmojiMaxUploadBytes)
|
||||
return MediaOutcome.Fail(StatusCodes.Status422UnprocessableEntity, "Validation failed: An emoji's image is at most 256 KB");
|
||||
var input = await Read(file, token);
|
||||
if (Refusal(input, _options.CurrentValue) is { } refused)
|
||||
if (Refusal(input, mediaOptions.CurrentValue) is { } refused)
|
||||
return refused;
|
||||
byte[] bytes, still = default;
|
||||
int width, height;
|
||||
bool animated;
|
||||
var bytes = default(byte[]);
|
||||
var still = default(byte[]);
|
||||
var width = default(int);
|
||||
var height = default(int);
|
||||
var animated = default(bool);
|
||||
await _processing.WaitAsync(token);
|
||||
try
|
||||
{
|
||||
@@ -342,7 +337,7 @@ namespace PrivaPub.Domain.Media
|
||||
}
|
||||
catch (VipsException ex)
|
||||
{
|
||||
_logger.LogInformation("Refused an emoji's picture: {Error}", ex.Message);
|
||||
logger.LogInformation("Refused an emoji's picture: {Error}", ex.Message);
|
||||
return MediaOutcome.Fail(StatusCodes.Status422UnprocessableEntity, "Validation failed: The file is not a readable image");
|
||||
}
|
||||
finally
|
||||
@@ -408,7 +403,7 @@ namespace PrivaPub.Domain.Media
|
||||
// a login over its quota (Media:QuotaBytesPerRoot; none when 0) uploads nothing more
|
||||
async Task<MediaOutcome> OverQuota(string avatarId, long incoming, CancellationToken token)
|
||||
{
|
||||
var quota = _options.CurrentValue.QuotaBytesPerRoot;
|
||||
var quota = mediaOptions.CurrentValue.QuotaBytesPerRoot;
|
||||
if (quota <= 0)
|
||||
return default;
|
||||
var rootId = (await DB.Default.Find<Models.User.RootToAvatar>().Match(r => r.AvatarId == avatarId).ExecuteFirstAsync(token))?.RootId;
|
||||
@@ -472,7 +467,7 @@ namespace PrivaPub.Domain.Media
|
||||
// an upload refused before anything is decoded: no loader takes it, or it would decode to too many pixels or frames
|
||||
static MediaOutcome Refusal(byte[] input, MediaOptions options)
|
||||
{
|
||||
ImageHeader header;
|
||||
var header = default(ImageHeader);
|
||||
try
|
||||
{
|
||||
header = Inspect(input);
|
||||
@@ -511,7 +506,7 @@ namespace PrivaPub.Domain.Media
|
||||
// its first frame the poster
|
||||
async Task<MediaOutcome> ProcessAnimation(byte[] input, ImageHeader header, MediaAttachment attachment, CancellationToken token)
|
||||
{
|
||||
var options = _options.CurrentValue;
|
||||
var options = mediaOptions.CurrentValue;
|
||||
var temp = Path.Combine(Path.GetTempPath(), $"privapub-{Guid.NewGuid():N}");
|
||||
var gif = temp + ".gif";
|
||||
var mp4 = temp + ".mp4";
|
||||
@@ -538,7 +533,7 @@ namespace PrivaPub.Domain.Media
|
||||
}
|
||||
catch (Exception ex) when (ex is not OperationCanceledException)
|
||||
{
|
||||
_logger.LogInformation(ex, "Refused a GIF upload");
|
||||
logger.LogInformation(ex, "Refused a GIF upload");
|
||||
return MediaOutcome.Fail(StatusCodes.Status422UnprocessableEntity, "Validation failed: The file could not be processed");
|
||||
}
|
||||
finally
|
||||
@@ -598,7 +593,7 @@ namespace PrivaPub.Domain.Media
|
||||
// saved until everything succeeded, and every temporary file goes, the stored upload too.
|
||||
async Task<MediaOutcome> ProcessIncoming(MediaAttachment attachment, CancellationToken token)
|
||||
{
|
||||
var options = _options.CurrentValue;
|
||||
var options = mediaOptions.CurrentValue;
|
||||
var input = Incoming(attachment.ID);
|
||||
var temp = Path.Combine(Path.GetTempPath(), $"privapub-{Guid.NewGuid():N}");
|
||||
var frame = temp + ".png";
|
||||
@@ -615,7 +610,7 @@ namespace PrivaPub.Domain.Media
|
||||
return MediaOutcome.Fail(StatusCodes.Status422UnprocessableEntity, "Validation failed: The file could not be processed");
|
||||
var extension = AvTypes[attachment.ContentType];
|
||||
var format = probe.Format.FormatName.Split(',')[0];
|
||||
string arguments;
|
||||
var arguments = default(string);
|
||||
if (video == default)
|
||||
arguments = "-map 0:a:0 -vn -dn -sn -map_metadata -1 -map_chapters -1 -c copy" + (extension == "m4a" ? " -movflags +faststart" : string.Empty);
|
||||
else if (video.CodecName is "h264" or "vp8" or "vp9" or "av1" && (long)video.Width * video.Height <= options.MaxVideoPixels
|
||||
@@ -642,7 +637,7 @@ namespace PrivaPub.Domain.Media
|
||||
{
|
||||
attachment.Width = made.PrimaryVideoStream?.Width;
|
||||
attachment.Height = made.PrimaryVideoStream?.Height;
|
||||
await FFMpeg.SnapshotAsync(output, frame, captureTime: TimeSpan.FromSeconds(Math.Min(1, made.Duration.TotalSeconds / 2)));
|
||||
await FFMpeg.SnapshotAsync(output, frame, captureTime: TimeSpan.FromSeconds(Math.Min(1, made.Duration.TotalSeconds / 2)), cancellationToken: token);
|
||||
var still = ProcessImage(await File.ReadAllBytesAsync(frame, token), 640, options.PreviewSide);
|
||||
poster = still.Preview;
|
||||
attachment.Blurhash = still.Blurhash;
|
||||
@@ -668,7 +663,7 @@ namespace PrivaPub.Domain.Media
|
||||
}
|
||||
catch (Exception ex) when (ex is not OperationCanceledException)
|
||||
{
|
||||
_logger.LogInformation(ex, "Refused an audio or video upload");
|
||||
logger.LogInformation(ex, "Refused an audio or video upload");
|
||||
return MediaOutcome.Fail(StatusCodes.Status422UnprocessableEntity, "Validation failed: The file could not be processed");
|
||||
}
|
||||
finally
|
||||
|
||||
@@ -8,17 +8,10 @@ namespace PrivaPub.Domain.Media
|
||||
{
|
||||
// Audio and video sent to /api/v2/media, processed off the request (its lease renewed while ffmpeg runs): playable, or
|
||||
// marked failed with why. One at a time, since a transcode takes the machine's cores.
|
||||
public class ProcessMediaJob : IJobHandler
|
||||
public class ProcessMediaJob(
|
||||
IMediaService mediaService,
|
||||
ILogger<ProcessMediaJob> logger) : IJobHandler
|
||||
{
|
||||
readonly IMediaService _media;
|
||||
readonly ILogger<ProcessMediaJob> _logger;
|
||||
|
||||
public ProcessMediaJob(IMediaService media, ILogger<ProcessMediaJob> logger)
|
||||
{
|
||||
_media = media;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public JobKind Kind => JobKind.ProcessMedia;
|
||||
public int Concurrency => 1;
|
||||
public int MaxAttempts => 3;
|
||||
@@ -37,8 +30,8 @@ namespace PrivaPub.Domain.Media
|
||||
var pending = await DB.Default.Find<MediaAttachment>().Match(m => m.ID == job.Payload && m.ProcessingState == "pending" && m.TrashedAt == null).ExecuteFirstAsync(token);
|
||||
if (pending == default)
|
||||
return JobOutcome.Done;//processed already, or trashed meanwhile
|
||||
if (!await _media.ProcessPending(pending, token))
|
||||
_logger.LogInformation("Upload {Id} could not be processed", pending.ID);
|
||||
if (!await mediaService.ProcessPending(pending, token))
|
||||
logger.LogInformation("Upload {Id} could not be processed", pending.ID);
|
||||
return JobOutcome.Done;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user