[backend/drive] Improve handling of configurations with disabled media proxy

This commit is contained in:
Laura Hausmann 2025-01-10 06:23:48 +01:00
parent a549a04801
commit af750a010d
No known key found for this signature in database
GPG key ID: D044E84C5BE01605
2 changed files with 15 additions and 12 deletions

View file

@ -34,7 +34,7 @@ public class DriveController(
[EnableCors("drive")] [EnableCors("drive")]
[EnableRateLimiting("proxy")] [EnableRateLimiting("proxy")]
[HttpGet("/files/{accessKey}/{version?}")] [HttpGet("/files/{accessKey}/{version?}")]
[ProducesResults(HttpStatusCode.OK, HttpStatusCode.NoContent)] [ProducesResults(HttpStatusCode.OK, HttpStatusCode.Redirect)]
[ProducesErrors(HttpStatusCode.NotFound)] [ProducesErrors(HttpStatusCode.NotFound)]
public async Task<IActionResult> GetFileByAccessKey(string accessKey, string? version) public async Task<IActionResult> GetFileByAccessKey(string accessKey, string? version)
{ {
@ -44,7 +44,7 @@ public class DriveController(
[EnableCors("drive")] [EnableCors("drive")]
[EnableRateLimiting("proxy")] [EnableRateLimiting("proxy")]
[HttpGet("/media/emoji/{id}")] [HttpGet("/media/emoji/{id}")]
[ProducesResults(HttpStatusCode.OK, HttpStatusCode.NoContent)] [ProducesResults(HttpStatusCode.OK, HttpStatusCode.Redirect)]
[ProducesErrors(HttpStatusCode.NotFound)] [ProducesErrors(HttpStatusCode.NotFound)]
public async Task<IActionResult> GetEmojiById(string id) public async Task<IActionResult> GetEmojiById(string id)
{ {
@ -60,7 +60,7 @@ public class DriveController(
[EnableCors("drive")] [EnableCors("drive")]
[EnableRateLimiting("proxy")] [EnableRateLimiting("proxy")]
[HttpGet("/avatars/{userId}/{version}")] [HttpGet("/avatars/{userId}/{version}")]
[ProducesResults(HttpStatusCode.OK, HttpStatusCode.NoContent)] [ProducesResults(HttpStatusCode.OK, HttpStatusCode.Redirect)]
[ProducesErrors(HttpStatusCode.NotFound)] [ProducesErrors(HttpStatusCode.NotFound)]
public async Task<IActionResult> GetAvatarByUserId(string userId, string? version) public async Task<IActionResult> GetAvatarByUserId(string userId, string? version)
{ {
@ -83,7 +83,7 @@ public class DriveController(
[EnableCors("drive")] [EnableCors("drive")]
[EnableRateLimiting("proxy")] [EnableRateLimiting("proxy")]
[HttpGet("/banners/{userId}/{version}")] [HttpGet("/banners/{userId}/{version}")]
[ProducesResults(HttpStatusCode.OK, HttpStatusCode.NoContent)] [ProducesResults(HttpStatusCode.OK, HttpStatusCode.NoContent, HttpStatusCode.Redirect)]
[ProducesErrors(HttpStatusCode.NotFound)] [ProducesErrors(HttpStatusCode.NotFound)]
public async Task<IActionResult> GetBannerByUserId(string userId, string? version) public async Task<IActionResult> GetBannerByUserId(string userId, string? version)
{ {
@ -223,16 +223,16 @@ public class DriveController(
} }
if (file.IsLink) if (file.IsLink)
{
if (!options.Value.ProxyRemoteMedia)
return NoContent();
try
{ {
var fetchUrl = version is "thumbnail" var fetchUrl = version is "thumbnail"
? file.RawThumbnailAccessUrl ? file.RawThumbnailAccessUrl
: file.RawAccessUrl; : file.RawAccessUrl;
if (!options.Value.ProxyRemoteMedia)
return Redirect(fetchUrl);
try
{
var filename = file.AccessKey == accessKey || file.Name.EndsWith(".webp") var filename = file.AccessKey == accessKey || file.Name.EndsWith(".webp")
? file.Name ? file.Name
: $"{file.Name}.webp"; : $"{file.Name}.webp";

View file

@ -7,18 +7,21 @@ using Microsoft.Extensions.Options;
namespace Iceshrimp.Backend.Core.Services; namespace Iceshrimp.Backend.Core.Services;
[UsedImplicitly] [UsedImplicitly]
public class MediaProxyService(IOptions<Config.InstanceSection> instance) : ISingletonService public class MediaProxyService(
IOptions<Config.InstanceSection> instance,
IOptionsMonitor<Config.StorageSection> storage
) : ISingletonService
{ {
private string GetProxyUrl(DriveFile file, bool thumbnail) private string GetProxyUrl(DriveFile file, bool thumbnail)
{ {
var url = thumbnail ? file.RawThumbnailAccessUrl : file.RawAccessUrl; var url = thumbnail ? file.RawThumbnailAccessUrl : file.RawAccessUrl;
if (file.UserHost is null || !file.IsLink) if (!storage.CurrentValue.ProxyRemoteMedia || file.UserHost is null || !file.IsLink)
return url; return url;
return GetProxyUrlInternal($"files/{file.AccessKey}", thumbnail && file.ThumbnailUrl != null); return GetProxyUrlInternal($"files/{file.AccessKey}", thumbnail && file.ThumbnailUrl != null);
} }
public string GetProxyUrl(Emoji emoji) => emoji.Host is null public string GetProxyUrl(Emoji emoji) => !storage.CurrentValue.ProxyRemoteMedia || emoji.Host is null
? emoji.RawPublicUrl ? emoji.RawPublicUrl
: GetProxyUrlInternal($"emoji/{emoji.Id}", thumbnail: false); : GetProxyUrlInternal($"emoji/{emoji.Id}", thumbnail: false);