Skip to content

Commit

Permalink
Merge pull request #96 from argon-chat/fixes/cdn-do-not-use-async
Browse files Browse the repository at this point in the history
Cdn Fixes
  • Loading branch information
urumo authored Dec 3, 2024
2 parents 6bd0e97 + 89ff18e commit 775ead4
Show file tree
Hide file tree
Showing 10 changed files with 19 additions and 34 deletions.
18 changes: 5 additions & 13 deletions src/Argon.Api/Controllers/FilesController.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,11 @@
namespace Argon.Api.Controllers;
namespace Argon.Controllers;

using ActualLab.Collections;
using Features.MediaStorage.Storages;
using Contracts;
using Extensions;
using Features.MediaStorage;
using Features.MediaStorage.Storages;
using Features.Pex;
using Grains.Interfaces;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using Microsoft.AspNetCore.StaticFiles;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Argon.Features;
using Contracts.Models.ArchetypeModel;

public class FilesController(
IOptions<CdnOptions> cdnOptions,
Expand Down Expand Up @@ -61,7 +53,7 @@ public async ValueTask<IActionResult> UploadServerAvatar([FromRoute] Guid server
await grainFactory.GetGrain<IServerGrain>(serverId)
.UpdateServer(new ServerInput(null, null, assetId.ToFileId()));

return Ok(await cdn.GenerateAssetUrl(ns, assetId));
return Ok(cdn.GenerateAssetUrl(ns, assetId));
}

[HttpPost("/files/user/@me/avatar"), Authorize(JwtBearerDefaults.AuthenticationScheme)]
Expand All @@ -78,6 +70,6 @@ public async ValueTask<IActionResult> UploadUserAvatar(IFormFile file)
await grainFactory.GetGrain<IUserGrain>(userId)
.UpdateUser(new UserEditInput(null, null, assetId.ToFileId()));

return Ok(await cdn.GenerateAssetUrl(ns, assetId));
return Ok(cdn.GenerateAssetUrl(ns, assetId));
}
}
4 changes: 2 additions & 2 deletions src/Argon.Api/Features/MediaStorage/AssetId.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Argon.Api.Features.MediaStorage;
namespace Argon.Features.MediaStorage;

public readonly struct AssetId(Guid assetId, AssetScope scope, AssetKind kind, string extensions)
{
Expand Down Expand Up @@ -38,7 +38,7 @@ public Dictionary<string, string> GetTags(StorageNameSpace @namespace)

public static AssetId FromFileId(string fileId)
{
if (fileId.Length != 45)
if (fileId.Length < 46)
throw new InvalidOperationException("Bad file id");
var span = fileId.AsSpan();
var guid = Guid.Parse(span.Slice(0, 36));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Argon.Api.Features.MediaStorage;
namespace Argon.Features.MediaStorage;

using Storages;
using Genbox.SimpleS3.Core.Abstracts.Clients;
Expand Down
2 changes: 1 addition & 1 deletion src/Argon.Api/Features/MediaStorage/CdnOptions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Argon.Api.Features.MediaStorage;
namespace Argon.Features.MediaStorage;

public class CdnOptions
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
namespace Argon.Api.Features.MediaStorage;

using Contracts;
namespace Argon.Features.MediaStorage;

public class DiskContentDeliveryNetwork([FromKeyedServices(IContentStorage.DiskContentStorageKey)] IContentStorage storage,
ILogger<YandexContentDeliveryNetwork> logger) : IContentDeliveryNetwork
Expand All @@ -23,6 +21,6 @@ public async ValueTask<Maybe<UploadError>> CreateAssetAsync(StorageNameSpace ns,
public ValueTask<Maybe<UploadError>> ReplaceAssetAsync(StorageNameSpace ns, AssetId asset, Stream file)
=> throw new NotImplementedException();

public ValueTask<string> GenerateAssetUrl(StorageNameSpace ns, AssetId asset)
public string GenerateAssetUrl(StorageNameSpace ns, AssetId asset)
=> new($"/files/{ns.ToPath()}/{asset.GetFilePath()}?nocache=1");
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
namespace Argon.Api.Features.MediaStorage;

using Contracts;
namespace Argon.Features.MediaStorage;

public interface IContentDeliveryNetwork
{
Expand All @@ -20,5 +18,5 @@ ValueTask<Maybe<UploadError>> ReplaceAssetAsync(StorageNameSpace ns, AssetId ass

ValueTask<Maybe<UploadError>> CreateAssetAsync(StorageNameSpace ns, AssetId asset, Stream file);
ValueTask<Maybe<UploadError>> ReplaceAssetAsync(StorageNameSpace ns, AssetId asset, Stream file);
ValueTask<string> GenerateAssetUrl(StorageNameSpace ns, AssetId asset);
string GenerateAssetUrl(StorageNameSpace ns, AssetId asset);
}
2 changes: 1 addition & 1 deletion src/Argon.Api/Features/MediaStorage/IContentStorage.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Argon.Api.Features.MediaStorage;
namespace Argon.Features.MediaStorage;

public interface IContentStorage
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Argon.Api.Features.MediaStorage.Storages;
namespace Argon.Features.MediaStorage.Storages;

public class DiskContentStorage : IContentStorage
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
namespace Argon.Api.Features.MediaStorage.Storages;
namespace Argon.Features.MediaStorage.Storages;

using Genbox.SimpleS3.Core.Abstracts.Clients;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;

public class S3ContentStorage([FromKeyedServices("GenericS3:client")] IObjectClient s3Client, IOptions<StorageOptions> options,
ILogger<IContentStorage> logger) : IContentStorage
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
namespace Argon.Api.Features.MediaStorage;
namespace Argon.Features.MediaStorage;

using System.Web;
using Contracts;
using Microsoft.Extensions.Options;

public class YandexContentDeliveryNetwork([FromKeyedServices(IContentStorage.GenericS3StorageKey)] IContentStorage storage,
ILogger<YandexContentDeliveryNetwork> logger, IOptions<CdnOptions> options)
Expand All @@ -28,8 +26,8 @@ public async ValueTask<Maybe<UploadError>> CreateAssetAsync(StorageNameSpace ns,
public ValueTask<Maybe<UploadError>> ReplaceAssetAsync(StorageNameSpace ns, AssetId asset, Stream file)
=> throw new NotImplementedException();

public ValueTask<string> GenerateAssetUrl(StorageNameSpace ns, AssetId asset)
=> new(GenerateSignedLink(Config.BaseUrl,$"/{ns.ToPath()}/{asset.GetFilePath()}", Config.SignSecret, (int)Config.EntryExpire.TotalSeconds));
public string GenerateAssetUrl(StorageNameSpace ns, AssetId asset)
=> GenerateSignedLink(Config.BaseUrl, $"/{ns.ToPath()}/{asset.GetFilePath()}", Config.SignSecret, (int)Config.EntryExpire.TotalSeconds);

private static string GenerateSignedLink(
string hostname,
Expand Down

0 comments on commit 775ead4

Please sign in to comment.