From 530811b699d1b8b5057e3e04ad561f6ddc40b44c Mon Sep 17 00:00:00 2001 From: Zoe Roux Date: Sat, 3 Feb 2024 18:02:20 +0100 Subject: [PATCH] Add a user api --- .../Models/Utils/Identifier.cs | 12 +++- back/src/Kyoo.Core/Views/Resources/UserApi.cs | 72 +++++++++++++++++++ 2 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 back/src/Kyoo.Core/Views/Resources/UserApi.cs diff --git a/back/src/Kyoo.Abstractions/Models/Utils/Identifier.cs b/back/src/Kyoo.Abstractions/Models/Utils/Identifier.cs index 07973867..5c44b7d5 100644 --- a/back/src/Kyoo.Abstractions/Models/Utils/Identifier.cs +++ b/back/src/Kyoo.Abstractions/Models/Utils/Identifier.cs @@ -167,6 +167,16 @@ namespace Kyoo.Abstractions.Models.Utils : new Filter.Eq("Slug", _slug!); } + public bool Is(Guid uid) + { + return _id.HasValue && _id.Value == uid; + } + + public bool Is(string slug) + { + return !_id.HasValue && _slug == slug; + } + private Expression> _IsSameExpression() where T : IResource { @@ -215,7 +225,7 @@ namespace Kyoo.Abstractions.Models.Utils /// public override bool CanConvertFrom(ITypeDescriptorContext? context, Type sourceType) { - if (sourceType == typeof(int) || sourceType == typeof(string)) + if (sourceType == typeof(Guid) || sourceType == typeof(string)) return true; return base.CanConvertFrom(context, sourceType); } diff --git a/back/src/Kyoo.Core/Views/Resources/UserApi.cs b/back/src/Kyoo.Core/Views/Resources/UserApi.cs new file mode 100644 index 00000000..518a2c2e --- /dev/null +++ b/back/src/Kyoo.Core/Views/Resources/UserApi.cs @@ -0,0 +1,72 @@ +// Kyoo - A portable and vast media library solution. +// Copyright (c) Kyoo. +// +// See AUTHORS.md and LICENSE file in the project root for full license information. +// +// Kyoo is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// any later version. +// +// Kyoo is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with Kyoo. If not, see . + +using System; +using System.IO; +using System.Threading.Tasks; +using Kyoo.Abstractions.Controllers; +using Kyoo.Abstractions.Models; +using Kyoo.Abstractions.Models.Attributes; +using Kyoo.Abstractions.Models.Permissions; +using Kyoo.Abstractions.Models.Utils; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; +using static Kyoo.Abstractions.Models.Utils.Constants; + +namespace Kyoo.Core.Api; + +/// +/// Information about one or multiple . +/// +[Route("users")] +[Route("user", Order = AlternativeRoute)] +[ApiController] +[PartialPermission(nameof(User), Group = Group.Admin)] +[ApiDefinition("Users", Group = ResourcesGroup)] +public class UserApi(ILibraryManager libraryManager, IThumbnailsManager thumbs) + : CrudApi(libraryManager.Users) +{ + /// + /// Get profile picture + /// + /// + /// Get the profile picture of someone + /// + [HttpGet("{identifier:id}/logo")] + [PartialPermission(Kind.Read)] + [ProducesResponseType(StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status401Unauthorized, Type = typeof(RequestError))] + [ProducesResponseType(StatusCodes.Status403Forbidden, Type = typeof(RequestError))] + public async Task GetProfilePicture(Identifier identifier) + { + Guid gid = await identifier.Match( + id => Task.FromResult(id), + async slug => (await libraryManager.Users.Get(slug)).Id + ); + Stream img = await thumbs.GetUserImage(gid); + if (identifier.Is("random")) + Response.Headers.Add("Cache-Control", $"public, no-store"); + else + { + // Allow clients to cache the image for 6 month. + Response.Headers.Add("Cache-Control", $"public, max-age={60 * 60 * 24 * 31 * 6}"); + } + return File(img, "image/webp", true); + } +} +