// Copyright Epic Games, Inc. All Rights Reserved.
using HordeServer.Api;
using HordeServer.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Net.Http.Headers;
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using HordeServer.Models;
using MongoDB.Bson;
using HordeServer.Utilities;
namespace HordeServer.Controllers
{
///
/// Controller for the /api/v1/software endpoint
///
[ApiController]
[Authorize]
[Route("[controller]")]
public class SoftwareController : ControllerBase
{
///
/// Singleton instance of the ACL service
///
private readonly AclService AclService;
///
/// Singleton instance of the client service
///
private readonly SoftwareService SoftwareService;
///
/// Constructor
///
/// The ACL service
/// The client service
public SoftwareController(AclService AclService, SoftwareService SoftwareService)
{
this.AclService = AclService;
this.SoftwareService = SoftwareService;
}
///
/// Uploads a new agent zip file
///
/// Zip archive containing the new client software
/// Whether the client should immediately be made the default
/// Http result code
[HttpPost]
[Route("/api/v1/software")]
public async Task> CreateSoftwareAsync([FromForm] IFormFile File, [FromForm] bool Default = false)
{
if (!await AclService.AuthorizeAsync(AclAction.UploadSoftware, User))
{
return Forbid();
}
ObjectId Id;
using (System.IO.Stream Stream = File.OpenReadStream())
{
Id = await SoftwareService.CreateSoftwareAsync(Stream, User.Identity.Name, Default);
}
return new CreateSoftwareResponse(Id.ToString());
}
///
/// Finds all uploaded software matching the given criteria
///
/// The user that created the software
/// The user that made it the default
/// Whether the software was made the default
/// Index of the first result to return
/// Number of results to return
/// Filter for the properties to return
/// Http response
[HttpGet]
[Route("/api/v1/software")]
[ProducesResponseType(typeof(List), 200)]
public async Task>> FindSoftwareAsync([FromQuery] string? CreatedByUser = null, [FromQuery] string? MadeDefaultByUser = null, [FromQuery] bool? MadeDefault = null, [FromQuery] PropertyFilter? Filter = null, [FromQuery] int Offset = 0, [FromQuery] int Count = 200)
{
if (!await AclService.AuthorizeAsync(AclAction.DownloadSoftware, User))
{
return Forbid();
}
List Results = await SoftwareService.FindSoftwareAsync(CreatedByUser, MadeDefaultByUser, MadeDefault, Offset, Count);
List