From 57cc270acb5aaabdf8e56e92b8d2be1537501be0 Mon Sep 17 00:00:00 2001 From: Yuuki Wesp Date: Wed, 4 Dec 2024 02:57:05 +0300 Subject: [PATCH 1/2] Refactor namespace declarations for MediaStorage features Changed namespace declarations in multiple files to improve organization and consistency. Updated namespaces from "Argon.Api.Features.MediaStorage" to "Argon.Features.MediaStorage". Made adjustments in AssetId, CdnFeatureExtensions, CdnOptions, DiskContentDeliveryNetwork, IContentDeliveryNetwork, IContentStorage, DiskContentStorage, S3ContentStorage, and YandexContentDeliveryNetwork files. --- src/Argon.Api/Features/MediaStorage/AssetId.cs | 4 ++-- .../Features/MediaStorage/CdnFeatureExtensions.cs | 2 +- src/Argon.Api/Features/MediaStorage/CdnOptions.cs | 2 +- .../Features/MediaStorage/DiskContentDeliveryNetwork.cs | 6 ++---- .../Features/MediaStorage/IContentDeliveryNetwork.cs | 6 ++---- src/Argon.Api/Features/MediaStorage/IContentStorage.cs | 2 +- .../Features/MediaStorage/Storages/DiskContentStorage.cs | 2 +- .../Features/MediaStorage/Storages/S3ContentStorage.cs | 3 +-- .../Features/MediaStorage/YandexContentDeliveryNetwork.cs | 8 +++----- 9 files changed, 14 insertions(+), 21 deletions(-) diff --git a/src/Argon.Api/Features/MediaStorage/AssetId.cs b/src/Argon.Api/Features/MediaStorage/AssetId.cs index 6f0e29f..dd66401 100644 --- a/src/Argon.Api/Features/MediaStorage/AssetId.cs +++ b/src/Argon.Api/Features/MediaStorage/AssetId.cs @@ -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) { @@ -38,7 +38,7 @@ public Dictionary 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)); diff --git a/src/Argon.Api/Features/MediaStorage/CdnFeatureExtensions.cs b/src/Argon.Api/Features/MediaStorage/CdnFeatureExtensions.cs index d466d66..8359fb7 100644 --- a/src/Argon.Api/Features/MediaStorage/CdnFeatureExtensions.cs +++ b/src/Argon.Api/Features/MediaStorage/CdnFeatureExtensions.cs @@ -1,4 +1,4 @@ -namespace Argon.Api.Features.MediaStorage; +namespace Argon.Features.MediaStorage; using Storages; using Genbox.SimpleS3.Core.Abstracts.Clients; diff --git a/src/Argon.Api/Features/MediaStorage/CdnOptions.cs b/src/Argon.Api/Features/MediaStorage/CdnOptions.cs index b02fcbd..ebd5b38 100644 --- a/src/Argon.Api/Features/MediaStorage/CdnOptions.cs +++ b/src/Argon.Api/Features/MediaStorage/CdnOptions.cs @@ -1,4 +1,4 @@ -namespace Argon.Api.Features.MediaStorage; +namespace Argon.Features.MediaStorage; public class CdnOptions { diff --git a/src/Argon.Api/Features/MediaStorage/DiskContentDeliveryNetwork.cs b/src/Argon.Api/Features/MediaStorage/DiskContentDeliveryNetwork.cs index 1ecf57f..648b542 100644 --- a/src/Argon.Api/Features/MediaStorage/DiskContentDeliveryNetwork.cs +++ b/src/Argon.Api/Features/MediaStorage/DiskContentDeliveryNetwork.cs @@ -1,6 +1,4 @@ -namespace Argon.Api.Features.MediaStorage; - -using Contracts; +namespace Argon.Features.MediaStorage; public class DiskContentDeliveryNetwork([FromKeyedServices(IContentStorage.DiskContentStorageKey)] IContentStorage storage, ILogger logger) : IContentDeliveryNetwork @@ -23,6 +21,6 @@ public async ValueTask> CreateAssetAsync(StorageNameSpace ns, public ValueTask> ReplaceAssetAsync(StorageNameSpace ns, AssetId asset, Stream file) => throw new NotImplementedException(); - public ValueTask GenerateAssetUrl(StorageNameSpace ns, AssetId asset) + public string GenerateAssetUrl(StorageNameSpace ns, AssetId asset) => new($"/files/{ns.ToPath()}/{asset.GetFilePath()}?nocache=1"); } \ No newline at end of file diff --git a/src/Argon.Api/Features/MediaStorage/IContentDeliveryNetwork.cs b/src/Argon.Api/Features/MediaStorage/IContentDeliveryNetwork.cs index e94e78f..7b9df6d 100644 --- a/src/Argon.Api/Features/MediaStorage/IContentDeliveryNetwork.cs +++ b/src/Argon.Api/Features/MediaStorage/IContentDeliveryNetwork.cs @@ -1,6 +1,4 @@ -namespace Argon.Api.Features.MediaStorage; - -using Contracts; +namespace Argon.Features.MediaStorage; public interface IContentDeliveryNetwork { @@ -20,5 +18,5 @@ ValueTask> ReplaceAssetAsync(StorageNameSpace ns, AssetId ass ValueTask> CreateAssetAsync(StorageNameSpace ns, AssetId asset, Stream file); ValueTask> ReplaceAssetAsync(StorageNameSpace ns, AssetId asset, Stream file); - ValueTask GenerateAssetUrl(StorageNameSpace ns, AssetId asset); + string GenerateAssetUrl(StorageNameSpace ns, AssetId asset); } \ No newline at end of file diff --git a/src/Argon.Api/Features/MediaStorage/IContentStorage.cs b/src/Argon.Api/Features/MediaStorage/IContentStorage.cs index 9274d7f..81d29f4 100644 --- a/src/Argon.Api/Features/MediaStorage/IContentStorage.cs +++ b/src/Argon.Api/Features/MediaStorage/IContentStorage.cs @@ -1,4 +1,4 @@ -namespace Argon.Api.Features.MediaStorage; +namespace Argon.Features.MediaStorage; public interface IContentStorage { diff --git a/src/Argon.Api/Features/MediaStorage/Storages/DiskContentStorage.cs b/src/Argon.Api/Features/MediaStorage/Storages/DiskContentStorage.cs index 3556da7..9ffb2ad 100644 --- a/src/Argon.Api/Features/MediaStorage/Storages/DiskContentStorage.cs +++ b/src/Argon.Api/Features/MediaStorage/Storages/DiskContentStorage.cs @@ -1,4 +1,4 @@ -namespace Argon.Api.Features.MediaStorage.Storages; +namespace Argon.Features.MediaStorage.Storages; public class DiskContentStorage : IContentStorage { diff --git a/src/Argon.Api/Features/MediaStorage/Storages/S3ContentStorage.cs b/src/Argon.Api/Features/MediaStorage/Storages/S3ContentStorage.cs index 551cb01..a0135fd 100644 --- a/src/Argon.Api/Features/MediaStorage/Storages/S3ContentStorage.cs +++ b/src/Argon.Api/Features/MediaStorage/Storages/S3ContentStorage.cs @@ -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 options, ILogger logger) : IContentStorage diff --git a/src/Argon.Api/Features/MediaStorage/YandexContentDeliveryNetwork.cs b/src/Argon.Api/Features/MediaStorage/YandexContentDeliveryNetwork.cs index 16503a3..88ba543 100644 --- a/src/Argon.Api/Features/MediaStorage/YandexContentDeliveryNetwork.cs +++ b/src/Argon.Api/Features/MediaStorage/YandexContentDeliveryNetwork.cs @@ -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 logger, IOptions options) @@ -28,8 +26,8 @@ public async ValueTask> CreateAssetAsync(StorageNameSpace ns, public ValueTask> ReplaceAssetAsync(StorageNameSpace ns, AssetId asset, Stream file) => throw new NotImplementedException(); - public ValueTask 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, From 89ff18ed15970d5545c5dbbef40a7ea595bb64f3 Mon Sep 17 00:00:00 2001 From: Yuuki Wesp Date: Wed, 4 Dec 2024 02:57:17 +0300 Subject: [PATCH 2/2] Refactor namespace and authentication in FilesController - Changed namespace from Argon.Api.Controllers to Argon.Controllers - Updated authentication method to JwtBearerDefaults.AuthenticationScheme --- src/Argon.Api/Controllers/FilesController.cs | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) diff --git a/src/Argon.Api/Controllers/FilesController.cs b/src/Argon.Api/Controllers/FilesController.cs index 341a6a9..f50359a 100644 --- a/src/Argon.Api/Controllers/FilesController.cs +++ b/src/Argon.Api/Controllers/FilesController.cs @@ -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, @@ -61,7 +53,7 @@ public async ValueTask UploadServerAvatar([FromRoute] Guid server await grainFactory.GetGrain(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)] @@ -78,6 +70,6 @@ public async ValueTask UploadUserAvatar(IFormFile file) await grainFactory.GetGrain(userId) .UpdateUser(new UserEditInput(null, null, assetId.ToFileId())); - return Ok(await cdn.GenerateAssetUrl(ns, assetId)); + return Ok(cdn.GenerateAssetUrl(ns, assetId)); } } \ No newline at end of file