Export unencrypted settings so user can import them in another pc

This commit is contained in:
Jaex 2020-08-14 12:47:52 +03:00
parent ec342d7594
commit f88ed0f7f8
3 changed files with 52 additions and 21 deletions

View file

@ -30,6 +30,7 @@
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
@ -109,6 +110,15 @@ public void SaveAsync()
SaveAsync(FilePath);
}
public MemoryStream SaveToMemoryStream(bool supportDPAPIEncryption = false)
{
ApplicationVersion = Application.ProductVersion;
MemoryStream ms = new MemoryStream();
SaveToStream(ms, supportDPAPIEncryption, true);
return ms;
}
private bool SaveInternal(string filePath)
{
string typeName = GetType().Name;
@ -127,25 +137,8 @@ private bool SaveInternal(string filePath)
string tempFilePath = filePath + ".temp";
using (FileStream fileStream = new FileStream(tempFilePath, FileMode.Create, FileAccess.Write, FileShare.Read, 4096, FileOptions.WriteThrough))
using (StreamWriter streamWriter = new StreamWriter(fileStream))
using (JsonTextWriter jsonWriter = new JsonTextWriter(streamWriter))
{
JsonSerializer serializer = new JsonSerializer();
if (SupportDPAPIEncryption)
{
serializer.ContractResolver = new DPAPIEncryptedStringPropertyResolver();
}
else
{
serializer.ContractResolver = new WritablePropertiesOnlyResolver();
}
serializer.Converters.Add(new StringEnumConverter());
serializer.DateTimeZoneHandling = DateTimeZoneHandling.Utc;
serializer.Formatting = Formatting.Indented;
serializer.Serialize(jsonWriter, this);
jsonWriter.Flush();
SaveToStream(fileStream, SupportDPAPIEncryption);
}
if (!JsonHelpers.QuickVerifyJsonFile(tempFilePath))
@ -195,6 +188,30 @@ private bool SaveInternal(string filePath)
return isSuccess;
}
private void SaveToStream(Stream stream, bool supportDPAPIEncryption = false, bool leaveOpen = false)
{
using (StreamWriter streamWriter = new StreamWriter(stream, new UTF8Encoding(false, true), 1024, leaveOpen))
using (JsonTextWriter jsonWriter = new JsonTextWriter(streamWriter))
{
JsonSerializer serializer = new JsonSerializer();
if (supportDPAPIEncryption)
{
serializer.ContractResolver = new DPAPIEncryptedStringPropertyResolver();
}
else
{
serializer.ContractResolver = new WritablePropertiesOnlyResolver();
}
serializer.Converters.Add(new StringEnumConverter());
serializer.DateTimeZoneHandling = DateTimeZoneHandling.Utc;
serializer.Formatting = Formatting.Indented;
serializer.Serialize(jsonWriter, this);
jsonWriter.Flush();
}
}
public static T Load(string filePath, string backupFolder = null)
{
List<string> fallbackFilePaths = new List<string>();

View file

@ -187,6 +187,7 @@ private static ZipArchiveEntry CreateEntryFromStream(this ZipArchive archive, St
using (Stream entryStream = entry.Open())
{
stream.Position = 0;
stream.CopyTo(entryStream);
}

View file

@ -346,15 +346,22 @@ public static void ResetSettings()
public static bool Export(string archivePath, bool settings, bool history)
{
MemoryStream msApplicationConfig = null, msUploadersConfig = null, msHotkeysConfig = null;
try
{
List<ZipEntryInfo> entries = new List<ZipEntryInfo>();
if (settings)
{
entries.Add(new ZipEntryInfo(ApplicationConfigFilePath));
entries.Add(new ZipEntryInfo(HotkeysConfigFilePath));
entries.Add(new ZipEntryInfo(UploadersConfigFilePath));
msApplicationConfig = Settings.SaveToMemoryStream(false);
entries.Add(new ZipEntryInfo(msApplicationConfig, ApplicationConfigFilename));
msUploadersConfig = UploadersConfig.SaveToMemoryStream(false);
entries.Add(new ZipEntryInfo(msUploadersConfig, UploadersConfigFilename));
msHotkeysConfig = HotkeysConfig.SaveToMemoryStream(false);
entries.Add(new ZipEntryInfo(msHotkeysConfig, HotkeysConfigFilename));
}
if (history)
@ -370,6 +377,12 @@ public static bool Export(string archivePath, bool settings, bool history)
DebugHelper.WriteException(e);
MessageBox.Show("Error while exporting backup:\r\n" + e, "ShareX - Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
msApplicationConfig?.Dispose();
msUploadersConfig?.Dispose();
msHotkeysConfig?.Dispose();
}
return false;
}