Add a user api

This commit is contained in:
Zoe Roux 2024-02-03 18:02:20 +01:00
parent cee7ca2ca0
commit 530811b699
2 changed files with 83 additions and 1 deletions

View File

@ -167,6 +167,16 @@ namespace Kyoo.Abstractions.Models.Utils
: new Filter<T>.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<Func<T, bool>> _IsSameExpression<T>()
where T : IResource
{
@ -215,7 +225,7 @@ namespace Kyoo.Abstractions.Models.Utils
/// <inheritdoc />
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);
}

View File

@ -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 <https://www.gnu.org/licenses/>.
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;
/// <summary>
/// Information about one or multiple <see cref="User"/>.
/// </summary>
[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<User>(libraryManager.Users)
{
/// <summary>
/// Get profile picture
/// </summary>
/// <remarks>
/// Get the profile picture of someone
/// </remarks>
[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<ActionResult> 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);
}
}