mirror of
https://github.com/ShareX/ShareX.git
synced 2024-10-01 09:47:22 +13:00
Export unencrypted settings so user can import them in another pc
This commit is contained in:
parent
ec342d7594
commit
f88ed0f7f8
3 changed files with 52 additions and 21 deletions
|
@ -30,6 +30,7 @@ You should have received a copy of the GNU General Public License
|
||||||
using System.ComponentModel;
|
using System.ComponentModel;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using System.Windows.Forms;
|
using System.Windows.Forms;
|
||||||
|
|
||||||
|
@ -109,6 +110,15 @@ public void SaveAsync()
|
||||||
SaveAsync(FilePath);
|
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)
|
private bool SaveInternal(string filePath)
|
||||||
{
|
{
|
||||||
string typeName = GetType().Name;
|
string typeName = GetType().Name;
|
||||||
|
@ -127,25 +137,8 @@ private bool SaveInternal(string filePath)
|
||||||
string tempFilePath = filePath + ".temp";
|
string tempFilePath = filePath + ".temp";
|
||||||
|
|
||||||
using (FileStream fileStream = new FileStream(tempFilePath, FileMode.Create, FileAccess.Write, FileShare.Read, 4096, FileOptions.WriteThrough))
|
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();
|
SaveToStream(fileStream, SupportDPAPIEncryption);
|
||||||
|
|
||||||
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();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!JsonHelpers.QuickVerifyJsonFile(tempFilePath))
|
if (!JsonHelpers.QuickVerifyJsonFile(tempFilePath))
|
||||||
|
@ -195,6 +188,30 @@ private bool SaveInternal(string filePath)
|
||||||
return isSuccess;
|
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)
|
public static T Load(string filePath, string backupFolder = null)
|
||||||
{
|
{
|
||||||
List<string> fallbackFilePaths = new List<string>();
|
List<string> fallbackFilePaths = new List<string>();
|
||||||
|
|
|
@ -187,6 +187,7 @@ private static ZipArchiveEntry CreateEntryFromStream(this ZipArchive archive, St
|
||||||
|
|
||||||
using (Stream entryStream = entry.Open())
|
using (Stream entryStream = entry.Open())
|
||||||
{
|
{
|
||||||
|
stream.Position = 0;
|
||||||
stream.CopyTo(entryStream);
|
stream.CopyTo(entryStream);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -346,15 +346,22 @@ public static void ResetSettings()
|
||||||
|
|
||||||
public static bool Export(string archivePath, bool settings, bool history)
|
public static bool Export(string archivePath, bool settings, bool history)
|
||||||
{
|
{
|
||||||
|
MemoryStream msApplicationConfig = null, msUploadersConfig = null, msHotkeysConfig = null;
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
List<ZipEntryInfo> entries = new List<ZipEntryInfo>();
|
List<ZipEntryInfo> entries = new List<ZipEntryInfo>();
|
||||||
|
|
||||||
if (settings)
|
if (settings)
|
||||||
{
|
{
|
||||||
entries.Add(new ZipEntryInfo(ApplicationConfigFilePath));
|
msApplicationConfig = Settings.SaveToMemoryStream(false);
|
||||||
entries.Add(new ZipEntryInfo(HotkeysConfigFilePath));
|
entries.Add(new ZipEntryInfo(msApplicationConfig, ApplicationConfigFilename));
|
||||||
entries.Add(new ZipEntryInfo(UploadersConfigFilePath));
|
|
||||||
|
msUploadersConfig = UploadersConfig.SaveToMemoryStream(false);
|
||||||
|
entries.Add(new ZipEntryInfo(msUploadersConfig, UploadersConfigFilename));
|
||||||
|
|
||||||
|
msHotkeysConfig = HotkeysConfig.SaveToMemoryStream(false);
|
||||||
|
entries.Add(new ZipEntryInfo(msHotkeysConfig, HotkeysConfigFilename));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (history)
|
if (history)
|
||||||
|
@ -370,6 +377,12 @@ public static bool Export(string archivePath, bool settings, bool history)
|
||||||
DebugHelper.WriteException(e);
|
DebugHelper.WriteException(e);
|
||||||
MessageBox.Show("Error while exporting backup:\r\n" + e, "ShareX - Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
MessageBox.Show("Error while exporting backup:\r\n" + e, "ShareX - Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||||
}
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
msApplicationConfig?.Dispose();
|
||||||
|
msUploadersConfig?.Dispose();
|
||||||
|
msHotkeysConfig?.Dispose();
|
||||||
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue