1
0
mirror of https://github.com/Artemis-RGB/Artemis synced 2025-12-31 01:42:02 +00:00

Remove legacy profile import

This commit is contained in:
Robert 2024-02-27 22:58:19 +01:00
parent 110dee102c
commit 1db2888aba
3 changed files with 3 additions and 122 deletions

View File

@ -1,31 +0,0 @@
using System;
using System.IO;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace Artemis.Core.JsonConverters
{
internal class StreamConverter : JsonConverter<Stream>
{
public override void Write(Utf8JsonWriter writer, Stream value, JsonSerializerOptions options)
{
using MemoryStream memoryStream = new();
value.Position = 0;
value.CopyTo(memoryStream);
writer.WriteBase64StringValue(memoryStream.ToArray());
}
public override Stream Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType != JsonTokenType.String)
throw new JsonException($"Expected a string token, but got {reader.TokenType}.");
string base64 = reader.GetString() ?? string.Empty;
if (typeToConvert == typeof(MemoryStream))
return new MemoryStream(Convert.FromBase64String(base64));
throw new InvalidOperationException("StreamConverter only supports reading to MemoryStream");
}
}
}

View File

@ -1,36 +0,0 @@
using System;
using System.IO;
using System.Text.Json.Serialization;
using Artemis.Core.JsonConverters;
using Artemis.Storage.Entities.Profile;
namespace Artemis.Core;
/// <summary>
/// A model that can be used to serialize a profile configuration, it's profile and it's icon
/// </summary>
public class ProfileConfigurationExportModel : IDisposable
{
/// <summary>
/// Gets or sets the storage entity of the profile configuration
/// </summary>
public ProfileConfigurationEntity? ProfileConfigurationEntity { get; set; }
/// <summary>
/// Gets or sets the storage entity of the profile
/// </summary>
[JsonRequired]
public ProfileEntity ProfileEntity { get; set; } = null!;
/// <summary>
/// Gets or sets a stream containing the profile image
/// </summary>
[JsonConverter(typeof(StreamConverter))]
public Stream? ProfileImage { get; set; }
/// <inheritdoc />
public void Dispose()
{
ProfileImage?.Dispose();
}
}

View File

@ -2,12 +2,10 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
using System.IO; using System.IO;
using System.IO.Compression;
using System.Linq; using System.Linq;
using System.Reactive; using System.Reactive;
using System.Reactive.Disposables; using System.Reactive.Disposables;
using System.Reactive.Linq; using System.Reactive.Linq;
using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using Artemis.Core; using Artemis.Core;
using Artemis.Core.Services; using Artemis.Core.Services;
@ -150,7 +148,7 @@ public partial class SidebarCategoryViewModel : ActivatableViewModelBase
private async Task ExecuteImportProfile() private async Task ExecuteImportProfile()
{ {
string[]? result = await _windowService.CreateOpenFileDialog() string[]? result = await _windowService.CreateOpenFileDialog()
.HavingFilter(f => f.WithExtension("zip").WithExtension("json").WithName("Artemis profile")) .HavingFilter(f => f.WithExtension("zip").WithName("Artemis profile"))
.ShowAsync(); .ShowAsync();
if (result == null) if (result == null)
@ -158,24 +156,8 @@ public partial class SidebarCategoryViewModel : ActivatableViewModelBase
try try
{ {
// Removing this at some point in the future await using FileStream fileStream = File.OpenRead(result[0]);
if (result[0].EndsWith("json")) await _profileService.ImportProfile(fileStream, ProfileCategory, true, true);
{
ProfileConfigurationExportModel? exportModel = CoreJson.Deserialize<ProfileConfigurationExportModel>(await File.ReadAllTextAsync(result[0]));
if (exportModel == null)
{
await _windowService.ShowConfirmContentDialog("Import profile", "Failed to import this profile, make sure it is a valid Artemis profile.", "Confirm", null);
return;
}
await using Stream convertedFileStream = await ConvertLegacyExport(exportModel);
await _profileService.ImportProfile(convertedFileStream, ProfileCategory, true, true);
}
else
{
await using FileStream fileStream = File.OpenRead(result[0]);
await _profileService.ImportProfile(fileStream, ProfileCategory, true, true);
}
} }
catch (Exception e) catch (Exception e)
{ {
@ -229,38 +211,4 @@ public partial class SidebarCategoryViewModel : ActivatableViewModelBase
_profileService.SaveProfileCategory(categories[i]); _profileService.SaveProfileCategory(categories[i]);
} }
} }
private async Task<Stream> ConvertLegacyExport(ProfileConfigurationExportModel exportModel)
{
MemoryStream archiveStream = new();
string configurationJson = CoreJson.Serialize(exportModel.ProfileConfigurationEntity);
string profileJson = CoreJson.Serialize(exportModel.ProfileEntity);
// Create a ZIP archive
using (ZipArchive archive = new(archiveStream, ZipArchiveMode.Create, true))
{
ZipArchiveEntry configurationEntry = archive.CreateEntry("configuration.json");
await using (Stream entryStream = configurationEntry.Open())
{
await entryStream.WriteAsync(Encoding.Default.GetBytes(configurationJson));
}
ZipArchiveEntry profileEntry = archive.CreateEntry("profile.json");
await using (Stream entryStream = profileEntry.Open())
{
await entryStream.WriteAsync(Encoding.Default.GetBytes(profileJson));
}
if (exportModel.ProfileImage != null)
{
ZipArchiveEntry iconEntry = archive.CreateEntry("icon.png");
await using Stream entryStream = iconEntry.Open();
await exportModel.ProfileImage.CopyToAsync(entryStream);
}
}
archiveStream.Seek(0, SeekOrigin.Begin);
return archiveStream;
}
} }