2022-02-02 10:08:15 -05:00
// Copyright Epic Games, Inc. All Rights Reserved.
using System.Net.Mime ;
using System.Threading.Tasks ;
2022-11-23 14:14:33 -05:00
using EpicGames.AspNet ;
2022-02-02 10:08:15 -05:00
using EpicGames.Horde.Storage ;
using Jupiter.Implementation ;
using Microsoft.AspNetCore.Authorization ;
using Microsoft.AspNetCore.Mvc ;
2022-08-29 04:56:16 -04:00
using ContentId = Jupiter . Implementation . ContentId ;
2022-02-02 10:08:15 -05:00
2022-10-12 06:36:30 -04:00
namespace Jupiter.Controllers
2022-02-02 10:08:15 -05:00
{
2023-07-27 11:20:47 -04:00
[ApiController]
[FormatFilter]
[Produces(MediaTypeNames.Application.Json, MediaTypeNames.Application.Octet, CustomMediaTypeNames.UnrealCompactBinary)]
[Route("api/v1/content-id")]
[Authorize]
public class ContentIdController : ControllerBase
{
private readonly IRequestHelper _requestHelper ;
private readonly IContentIdStore _contentIdStore ;
2022-02-02 10:08:15 -05:00
2023-07-27 11:20:47 -04:00
public ContentIdController ( IRequestHelper requestHelper , IContentIdStore contentIdStore )
{
_requestHelper = requestHelper ;
_contentIdStore = contentIdStore ;
}
2022-02-02 10:08:15 -05:00
2023-07-27 11:20:47 -04:00
/// <summary>
/// Returns which blobs a content id maps to
/// </summary>
/// <param name="ns">Namespace. Each namespace is completely separated from each other. Use for different types of data that is never expected to be similar (between two different games for instance). Example: `uc4.ddc`</param>
/// <param name="contentId">The content id to resolve </param>
[HttpGet("{ns}/{contentId}.{format?}", Order = 500)]
[ProducesDefaultResponseType]
[ProducesResponseType(type: typeof(ProblemDetails), 400)]
2023-08-10 09:29:07 -04:00
public async Task < IActionResult > ResolveAsync ( NamespaceId ns , ContentId contentId )
2023-07-27 11:20:47 -04:00
{
2023-08-10 23:09:40 -04:00
ActionResult ? result = await _requestHelper . HasAccessToNamespaceAsync ( User , Request , ns , new [ ] { JupiterAclAction . ReadObject } ) ;
2023-07-27 11:20:47 -04:00
if ( result ! = null )
{
return result ;
}
2023-08-10 23:09:40 -04:00
BlobId [ ] ? blobs = await _contentIdStore . ResolveAsync ( ns , contentId , mustBeContentId : true ) ;
2022-02-02 10:08:15 -05:00
2023-07-27 11:20:47 -04:00
if ( blobs = = null )
{
return NotFound ( new ProblemDetails { Title = $"Unable to resolve content id {contentId} ({ns})." } ) ;
}
2022-02-02 10:08:15 -05:00
2023-07-27 11:20:47 -04:00
return Ok ( new ResolvedContentIdResponse ( blobs ) ) ;
}
2022-02-02 10:08:15 -05:00
2023-07-27 11:20:47 -04:00
/// <summary>
/// Update a content id mapping
/// </summary>
/// <param name="ns">Namespace. Each namespace is completely separated from each other. Use for different types of data that is never expected to be similar (between two different games for instance). Example: `uc4.ddc`</param>
/// <param name="contentId">The content id to resolve </param>
/// <param name="blobIdentifier">The blob identifier to map the content id to</param>
/// <param name="contentWeight">The weight of this mapping, higher means more important</param>
[HttpPut("{ns}/{contentId}/update/{blobIdentifier}/{contentWeight}", Order = 500)]
[ProducesDefaultResponseType]
[ProducesResponseType(type: typeof(ProblemDetails), 400)]
2023-08-10 09:29:07 -04:00
public async Task < IActionResult > UpdateContentIdMappingAsync ( NamespaceId ns , ContentId contentId , BlobId blobIdentifier , int contentWeight )
2023-07-27 11:20:47 -04:00
{
2023-08-10 23:09:40 -04:00
ActionResult ? result = await _requestHelper . HasAccessToNamespaceAsync ( User , Request , ns , new [ ] { JupiterAclAction . WriteObject } ) ;
2023-07-27 11:20:47 -04:00
if ( result ! = null )
{
return result ;
}
2023-08-10 23:09:40 -04:00
await _contentIdStore . PutAsync ( ns , contentId , blobIdentifier , contentWeight ) ;
2022-02-02 10:08:15 -05:00
2023-07-27 11:20:47 -04:00
return Ok ( ) ;
}
}
2022-02-02 10:08:15 -05:00
2023-07-27 11:20:47 -04:00
public class ResolvedContentIdResponse
{
2023-08-09 13:08:12 -04:00
public BlobId [ ] Blobs { get ; set ; }
2022-02-02 10:08:15 -05:00
2023-08-09 13:08:12 -04:00
public ResolvedContentIdResponse ( BlobId [ ] blobs )
2023-07-27 11:20:47 -04:00
{
Blobs = blobs ;
}
}
2022-02-02 10:08:15 -05:00
}