[backend/drive] Improve handling of configurations with disabled media proxy
This commit is contained in:
parent
a549a04801
commit
af750a010d
2 changed files with 15 additions and 12 deletions
|
@ -34,7 +34,7 @@ public class DriveController(
|
|||
[EnableCors("drive")]
|
||||
[EnableRateLimiting("proxy")]
|
||||
[HttpGet("/files/{accessKey}/{version?}")]
|
||||
[ProducesResults(HttpStatusCode.OK, HttpStatusCode.NoContent)]
|
||||
[ProducesResults(HttpStatusCode.OK, HttpStatusCode.Redirect)]
|
||||
[ProducesErrors(HttpStatusCode.NotFound)]
|
||||
public async Task<IActionResult> GetFileByAccessKey(string accessKey, string? version)
|
||||
{
|
||||
|
@ -44,7 +44,7 @@ public class DriveController(
|
|||
[EnableCors("drive")]
|
||||
[EnableRateLimiting("proxy")]
|
||||
[HttpGet("/media/emoji/{id}")]
|
||||
[ProducesResults(HttpStatusCode.OK, HttpStatusCode.NoContent)]
|
||||
[ProducesResults(HttpStatusCode.OK, HttpStatusCode.Redirect)]
|
||||
[ProducesErrors(HttpStatusCode.NotFound)]
|
||||
public async Task<IActionResult> GetEmojiById(string id)
|
||||
{
|
||||
|
@ -60,7 +60,7 @@ public class DriveController(
|
|||
[EnableCors("drive")]
|
||||
[EnableRateLimiting("proxy")]
|
||||
[HttpGet("/avatars/{userId}/{version}")]
|
||||
[ProducesResults(HttpStatusCode.OK, HttpStatusCode.NoContent)]
|
||||
[ProducesResults(HttpStatusCode.OK, HttpStatusCode.Redirect)]
|
||||
[ProducesErrors(HttpStatusCode.NotFound)]
|
||||
public async Task<IActionResult> GetAvatarByUserId(string userId, string? version)
|
||||
{
|
||||
|
@ -83,7 +83,7 @@ public class DriveController(
|
|||
[EnableCors("drive")]
|
||||
[EnableRateLimiting("proxy")]
|
||||
[HttpGet("/banners/{userId}/{version}")]
|
||||
[ProducesResults(HttpStatusCode.OK, HttpStatusCode.NoContent)]
|
||||
[ProducesResults(HttpStatusCode.OK, HttpStatusCode.NoContent, HttpStatusCode.Redirect)]
|
||||
[ProducesErrors(HttpStatusCode.NotFound)]
|
||||
public async Task<IActionResult> GetBannerByUserId(string userId, string? version)
|
||||
{
|
||||
|
@ -223,16 +223,16 @@ public class DriveController(
|
|||
}
|
||||
|
||||
if (file.IsLink)
|
||||
{
|
||||
if (!options.Value.ProxyRemoteMedia)
|
||||
return NoContent();
|
||||
|
||||
try
|
||||
{
|
||||
var fetchUrl = version is "thumbnail"
|
||||
? file.RawThumbnailAccessUrl
|
||||
: file.RawAccessUrl;
|
||||
|
||||
if (!options.Value.ProxyRemoteMedia)
|
||||
return Redirect(fetchUrl);
|
||||
|
||||
try
|
||||
{
|
||||
var filename = file.AccessKey == accessKey || file.Name.EndsWith(".webp")
|
||||
? file.Name
|
||||
: $"{file.Name}.webp";
|
||||
|
|
|
@ -7,18 +7,21 @@ using Microsoft.Extensions.Options;
|
|||
namespace Iceshrimp.Backend.Core.Services;
|
||||
|
||||
[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)
|
||||
{
|
||||
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 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
|
||||
: GetProxyUrlInternal($"emoji/{emoji.Id}", thumbnail: false);
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue