Files
Libation/Source/LibationCli/Options/GetLicenseOptions.cs
MBucari ce2b81036f Add license and settings overrides to LibationCli
- Add `LIBATION_FILES_DIR` environment variable to specify LibationFiles directory instead of appsettings.json
- OptionsBase supports overriding setting
  - Added `EphemeralSettings` which are loaded from Settings.json once and can be modified with the `--override` command parameter
- Added `get-setting` command
  - Prints (editable) settings and their values. Prints specified settings, or all settings if none specified
  - `--listEnumValues` option will list all names for a speficied enum-type settings. If no setting names are specified, prints all enum values for all enum settings.
  - Prints in a text-based table or bare with `-b` switch
- Added `get-license` command which requests a content license and prints it as a json to stdout
- Improved `liberate` command
  - Added `-force` option to force liberation without validation.
  - Added support to download with a license file supplied to stdin
  - Improve startup performance when downloading explicit ASIN(s)
  - Fix long-standing bug where cover art was not being downloading
2025-11-19 23:47:41 -07:00

67 lines
1.9 KiB
C#

using ApplicationServices;
using CommandLine;
using DataLayer;
using FileLiberator;
using LibationFileManager;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using System;
using System.Threading.Tasks;
#nullable enable
namespace LibationCli.Options;
[Verb("get-license", HelpText = "Get the license information for a book.")]
internal class GetLicenseOptions : OptionsBase
{
[Value(0, MetaName = "[asin]", HelpText = "Product ID of book to request license for.", Required = true)]
public string? Asin { get; set; }
protected override async Task ProcessAsync()
{
if (string.IsNullOrWhiteSpace(Asin))
{
Console.Error.WriteLine("ASIN is required.");
return;
}
using var dbContext = DbContexts.GetContext();
if (dbContext.GetLibraryBook_Flat_NoTracking(Asin) is not LibraryBook libraryBook)
{
Console.Error.WriteLine($"Book not found with asin={Asin}");
return;
}
var api = await libraryBook.GetApiAsync();
var license = await DownloadOptions.GetDownloadLicenseAsync(api, libraryBook, Configuration.Instance, default);
var jsonSettings = new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore,
Converters = [new StringEnumConverter(), new ByteArrayHexConverter()]
};
var licenseJson = JsonConvert.SerializeObject(license, Formatting.Indented, jsonSettings);
Console.WriteLine(licenseJson);
}
}
class ByteArrayHexConverter : JsonConverter
{
public override bool CanConvert(Type objectType) => objectType == typeof(byte[]);
public override bool CanRead => false;
public override bool CanWrite => true;
public override object? ReadJson(JsonReader reader, Type objectType, object? existingValue, JsonSerializer serializer)
=> throw new NotSupportedException();
public override void WriteJson(JsonWriter writer, object? value, JsonSerializer serializer)
{
if (value is byte[] array)
{
writer.WriteValue(Convert.ToHexStringLower(array));
}
}
}