// Copyright Epic Games, Inc. All Rights Reserved.
using System;
using System.Collections.Generic;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using EpicGames.Core;
#pragma warning disable CA2227
namespace EpicGames.Horde.Storage
{
///
/// Class that defines which backend to use to fetch an artifact
///
public class ArtifactBackend
{
///
/// The different types of supported backends
///
public enum BackendTypeEnum
{
///
/// The horde storage backend
///
Horde,
///
/// A zen based backend
///
Zen
}
///
/// Enum indicating which type of backend it is and thus how these options should be interpreted
///
public BackendTypeEnum Type { get; set; } = BackendTypeEnum.Horde;
}
///
/// Class which describes an artifact on Horde which can be serialized to JSON.
///
public class ArtifactDescriptor
{
///
/// Name of the artifact
///
public string? Name { get; set; }
///
/// Type of the artifact
///
public string? Type { get; set; }
///
/// Artifact description
///
public string? Description { get; set; }
///
/// Base URL for downloading from
///
public Uri BaseUrl { get; set; }
///
/// Name of the ref to download
///
public RefName RefName { get; set; }
///
/// Optional URL to the job that produced this artifact
///
public Uri? JobUrl { get; set; }
///
/// Keys associated with the artifact
///
public List? Keys { get; set; }
///
/// Metadata associated with the artifact
///
public List? Metadata { get; set; }
///
/// Filter for the files selected for download
///
public List? Filter { get; set; }
///
/// Information about which backend system to use. Defaults to Horde.
///
public ArtifactBackend Backend { get; set; } = new ArtifactBackend();
///
/// Default constructor
///
public ArtifactDescriptor()
{
BaseUrl = new Uri("http://horde");
RefName = new RefName("default");
}
///
/// Constructor
///
public ArtifactDescriptor(Uri baseUrl, RefName refName, IReadOnlyCollection? filter = null)
{
BaseUrl = baseUrl;
RefName = refName;
if (filter != null && filter.Count > 0)
{
Filter = new List(filter);
}
}
///
/// Deserialize a descriptor from utf8 bytes
///
/// Data to deserialize from
/// New descriptor instance
public static ArtifactDescriptor Deserialize(ReadOnlySpan data)
{
return JsonSerializer.Deserialize(data, HordeHttpClient.JsonSerializerOptions)
?? throw new InvalidOperationException("Cannot deserialize descriptor");
}
///
/// Writes this descriptor to storage
///
/// File to read from
/// Cancellation token for the operation
public static async Task ReadAsync(FileReference file, CancellationToken cancellationToken)
{
byte[] data = await FileReference.ReadAllBytesAsync(file, cancellationToken);
return Deserialize(data);
}
///
/// Serializes a descriptor
///
/// Data for the serialized descriptor
public byte[] Serialize()
{
JsonSerializerOptions serializerOptions = new JsonSerializerOptions(HordeHttpClient.JsonSerializerOptions);
serializerOptions.WriteIndented = true;
return JsonSerializer.SerializeToUtf8Bytes(this, serializerOptions);
}
///
/// Writes this descriptor to storage
///
/// File to write to
/// Cancellation token for the operation
public async Task WriteAsync(FileReference file, CancellationToken cancellationToken)
{
byte[] data = Serialize();
await FileReference.WriteAllBytesAsync(file, data, cancellationToken);
}
}
}